fork 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, Newline };
  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. case '\n': return Newline;
  23. default: return Letter;
  24. }
  25. }
  26.  
  27. void emit (int c) { putchar(c); }
  28. void slash (int c) { putchar('/'); putchar(c); }
  29. void nothing (int c) { }
  30. void halt (int c) { exit(EXIT_SUCCESS); }
  31.  
  32. #define T(state, sigil) (((state) << 4) | sigil)
  33.  
  34. int main() {
  35. transition state = { 0, emit };
  36. transition table[] = {
  37.  
  38. [T(0, Slash)] = { 1, nothing },
  39. [T(0, Letter)] = { 0, emit },
  40. [T(0, Star)] = { 0, emit },
  41. [T(0, EndOfFile)] = { 0, halt },
  42. [T(0, Squote)] = { 6, emit },
  43. [T(0, Dquote)] = { 4, emit },
  44. [T(0, Bslash)] = { 0, emit },
  45. [T(0, Newline)] = { 0, emit }, // Handle newline in state 0
  46.  
  47. [T(1, Slash)] = { 9, nothing }, // Transition to line comment state
  48. [T(1, Star)] = { 2, nothing }, // Start of block comment
  49. [T(1, Letter)] = { 0, slash }, // Not a comment, emit '/'
  50. [T(1, EndOfFile)] = { 0, halt },
  51. [T(1, Squote)] = { 0, slash },
  52. [T(1, Dquote)] = { 0, slash },
  53. [T(1, Bslash)] = { 0, slash },
  54. [T(1, Newline)] = { 0, slash }, // Not a comment, emit '/'
  55.  
  56. [T(2, Slash)] = { 2, nothing },
  57. [T(2, Letter)] = { 2, nothing },
  58. [T(2, Star)] = { 3, nothing },
  59. [T(2, EndOfFile)] = { 0, halt },
  60. [T(2, Squote)] = { 2, nothing },
  61. [T(2, Dquote)] = { 2, nothing },
  62. [T(2, Bslash)] = { 2, nothing },
  63. [T(2, Newline)] = { 2, nothing }, // Newlines inside block comments are ignored
  64.  
  65. [T(3, Slash)] = { 0, nothing },
  66. [T(3, Letter)] = { 2, nothing },
  67. [T(3, Star)] = { 2, nothing },
  68. [T(3, EndOfFile)] = { 0, halt },
  69. [T(3, Squote)] = { 2, nothing },
  70. [T(3, Dquote)] = { 2, nothing },
  71. [T(3, Bslash)] = { 2, nothing },
  72. [T(3, Newline)] = { 2, nothing },
  73.  
  74. [T(4, Slash)] = { 4, emit },
  75. [T(4, Letter)] = { 4, emit },
  76. [T(4, Star)] = { 4, emit },
  77. [T(4, EndOfFile)] = { 0, halt },
  78. [T(4, Squote)] = { 4, emit },
  79. [T(4, Dquote)] = { 0, emit },
  80. [T(4, Bslash)] = { 5, emit },
  81. [T(4, Newline)] = { 4, emit },
  82.  
  83. [T(5, Slash)] = { 4, emit },
  84. [T(5, Letter)] = { 4, emit },
  85. [T(5, Star)] = { 4, emit },
  86. [T(5, EndOfFile)] = { 0, halt },
  87. [T(5, Squote)] = { 4, emit },
  88. [T(5, Dquote)] = { 4, emit },
  89. [T(5, Bslash)] = { 4, emit },
  90. [T(5, Newline)] = { 4, emit },
  91.  
  92. [T(6, Slash)] = { 8, emit },
  93. [T(6, Letter)] = { 8, emit },
  94. [T(6, Star)] = { 8, emit },
  95. [T(6, EndOfFile)] = { 0, halt },
  96. [T(6, Squote)] = { 8, emit },
  97. [T(6, Dquote)] = { 8, emit },
  98. [T(6, Bslash)] = { 7, emit },
  99. [T(6, Newline)] = { 8, emit },
  100.  
  101. [T(7, Slash)] = { 8, emit },
  102. [T(7, Letter)] = { 8, emit },
  103. [T(7, Star)] = { 8, emit },
  104. [T(7, EndOfFile)] = { 8, emit },
  105. [T(7, Squote)] = { 8, emit },
  106. [T(7, Dquote)] = { 8, emit },
  107. [T(7, Bslash)] = { 8, emit },
  108. [T(7, Newline)] = { 8, emit },
  109.  
  110. [T(8, Slash)] = { 0, emit },
  111. [T(8, Letter)] = { 0, emit },
  112. [T(8, Star)] = { 0, emit },
  113. [T(8, EndOfFile)] = { 0, halt },
  114. [T(8, Squote)] = { 0, emit },
  115. [T(8, Dquote)] = { 0, emit },
  116. [T(8, Bslash)] = { 0, emit },
  117. [T(8, Newline)] = { 0, emit },
  118.  
  119. // New State 9: Inside Line Comment
  120. [T(9, Slash)] = { 9, nothing },
  121. [T(9, Star)] = { 9, nothing },
  122. [T(9, Letter)] = { 9, nothing },
  123. [T(9, Squote)] = { 9, nothing },
  124. [T(9, Dquote)] = { 9, nothing },
  125. [T(9, Bslash)] = { 9, nothing },
  126. [T(9, Newline)] = { 0, emit }, // End of line comment
  127. [T(9, EndOfFile)] = { 0, halt }, // End of file while in line comment
  128. };
  129.  
  130. for (;;) {
  131. int c = getchar();
  132. state = table[T(state.state, sigilify(c))];
  133. state.effect(c);
  134. }
  135. }
Success #stdin #stdout 0s 5284KB
stdin
abc/*comment/*still comment*/def
" /*not a comment "
'/* broken syntax, but not a comment
abcd //test comment
//test comment
stdout
abcdef
" /*not a comment "
'/* broken syntax, but not a comment
abcd