Advertisement
tdulik

FSM - comment remover

Nov 3rd, 2020
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. #include <stdio.h>
  2. double x=1/3;
  3. /* this is multiline comment,
  4.  * continuing here...
  5.  * and here...
  6.  */
  7.  
  8. // this is a single line comment
  9.  
  10. int main()
  11. {
  12.     printf("Comment remover: insert C code, end=CTRL+Z\n");
  13.     enum { START, SLASH, MULTILINE,
  14.            END_OF_MULTILINE, SINGLELINE} state=START;
  15.     int c;
  16.     while ((c=getchar())!=EOF) {
  17.         switch (state) {
  18.         case START:
  19.             if (c=='/') {
  20.                 state=SLASH;
  21.             } else {
  22.                 putchar(c);
  23.             }
  24.             break;
  25.         case SLASH:
  26.             if (c=='*') {
  27.                 state=MULTILINE;
  28.             } else if (c=='/') {
  29.                 state=SINGLELINE;
  30.             } else {
  31.                 putchar('/');
  32.                 putchar(c);
  33.                 state=START;
  34.             }
  35.             break;
  36.         case MULTILINE:
  37.             if (c=='*') {
  38.                 state=END_OF_MULTILINE;
  39.             }
  40.             break;
  41.         case END_OF_MULTILINE:
  42.             if (c=='/') {
  43.                 state=START;
  44.             } else {
  45.                 state=MULTILINE;
  46.             }
  47.             break;
  48.         case SINGLELINE:
  49.             if (c=='\n') {
  50.                 putchar(c);
  51.                 state=START;
  52.             }
  53.             break;
  54.         }
  55.     }
  56.     return 0;
  57. }
  58.  
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement