fork(2) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef void effector(int);
  5. typedef struct transition transition;
  6. struct transition {
  7. int state;
  8. effector *effect;
  9. };
  10.  
  11. enum sigil { Letter, Slash, Star, EndOfFile, Squote, Dquote, Bslash };
  12. typedef enum sigil sigil;
  13.  
  14. sigil sigilify(int c) {
  15. switch (c) {
  16. case '/': return Slash;
  17. case '*': return Star;
  18. case EOF: return EndOfFile;
  19. case '\'': return Squote;
  20. case '"': return Dquote;
  21. case '\\': return Bslash;
  22. default: return Letter;
  23. }
  24. }
  25.  
  26. void emit (int c) { putchar(c); }
  27. void slash (int c) { putchar('/'); putchar(c); }
  28. void nothing (int c) { }
  29. void halt (int c) { exit(EXIT_SUCCESS); }
  30.  
  31. #define T(state, sigil) (((state) << 4) | sigil)
  32.  
  33. int main() {
  34. transition state = { 0, emit };
  35. transition table[] = {
  36.  
  37. [T(0, Slash)] = { 1, nothing },
  38. [T(0, Letter)] = { 0, emit },
  39. [T(0, Star)] = { 0, emit },
  40. [T(0, EndOfFile)] = { 0, halt },
  41. [T(0, Squote)] = { 6, emit },
  42. [T(0, Dquote)] = { 4, emit },
  43. [T(0, Bslash)] = { 0, emit },
  44.  
  45. [T(1, Slash)] = { 0, slash },
  46. [T(1, Letter)] = { 0, slash },
  47. [T(1, Star)] = { 2, nothing },
  48. [T(1, EndOfFile)] = { 0, halt },
  49. [T(1, Squote)] = { 0, slash },
  50. [T(1, Dquote)] = { 0, slash },
  51. [T(1, Bslash)] = { 0, slash },
  52.  
  53. [T(2, Slash)] = { 2, nothing },
  54. [T(2, Letter)] = { 2, nothing},
  55. [T(2, Star)] = { 3, nothing },
  56. [T(2, EndOfFile)] = { 0, halt },
  57. [T(2, Squote)] = { 2, nothing },
  58. [T(2, Dquote)] = { 2, nothing },
  59. [T(2, Bslash)] = { 2, nothing },
  60.  
  61. [T(3, Slash)] = { 0, nothing },
  62. [T(3, Letter)] = { 2, nothing },
  63. [T(3, Star)] = { 2, nothing },
  64. [T(3, EndOfFile)] = { 0, halt },
  65. [T(3, Squote)] = { 2, nothing },
  66. [T(3, Dquote)] = { 2, nothing },
  67. [T(3, Bslash)] = { 2, nothing },
  68.  
  69. [T(4, Slash)] = { 4, emit },
  70. [T(4, Letter)] = { 4, emit },
  71. [T(4, Star)] = { 4, emit },
  72. [T(4, EndOfFile)] = { 0, halt },
  73. [T(4, Squote)] = { 4, emit },
  74. [T(4, Dquote)] = { 0, emit },
  75. [T(4, Bslash)] = { 5, emit },
  76.  
  77. [T(5, Slash)] = { 4, emit },
  78. [T(5, Letter)] = { 4, emit },
  79. [T(5, Star)] = { 4, emit },
  80. [T(5, EndOfFile)] = { 0, halt },
  81. [T(5, Squote)] = { 4, emit },
  82. [T(5, Dquote)] = { 4, emit },
  83. [T(5, Bslash)] = { 4, emit},
  84.  
  85. [T(6, Slash)] = { 8, emit },
  86. [T(6, Letter)] = { 8, emit },
  87. [T(6, Star)] = { 8, emit },
  88. [T(6, EndOfFile)] = { 0, halt },
  89. [T(6, Squote)] = { 8, emit },
  90. [T(6, Dquote)] = { 8, emit },
  91. [T(6, Bslash)] = { 7, emit },
  92.  
  93. [T(7, Slash)] = { 8, emit },
  94. [T(7, Letter)] = { 8, emit },
  95. [T(7, Star)] = { 8, emit },
  96. [T(7, EndOfFile)] = { 8, emit },
  97. [T(7, Squote)] = { 8, emit },
  98. [T(7, Dquote)] = { 8, emit },
  99. [T(7, Bslash)] = { 8, emit },
  100.  
  101. [T(8, Slash)] = { 0, emit },
  102. [T(8, Letter)] = { 0, emit },
  103. [T(8, Star)] = { 0, emit },
  104. [T(8, EndOfFile)] = { 0, halt },
  105. [T(8, Squote)] = { 0, emit },
  106. [T(8, Dquote)] = { 0, emit },
  107. [T(8, Bslash)] = { 0, emit },
  108. };
  109.  
  110. for (;;) {
  111. int c = getchar();
  112. (state = table[T(state.state, sigilify(c))]).effect(c);
  113. }
  114. }
  115.  
Success #stdin #stdout 0s 9432KB
stdin
abc/*comment/*still comment*/def
" /*not a comment "
'/* broken syntax, but not a comment
stdout
abcdef
" /*not a comment "
'/* broken syntax, but not a comment