Removing comments with a sliding window without nested while loops

前端 未结 3 1725
囚心锁ツ
囚心锁ツ 2021-01-06 10:50

I\'m trying to remove comments and strings from a c file with c code. I\'ll just stick to comments for the examples. I have a sliding window so I only have character n

3条回答
  •  走了就别回头了
    2021-01-06 10:57

    The algorithm written with one while loop could look like this:

    while ((c = getchar()) != EOF)
    {
        ... // looking at the byte that was just read
    
        if (...) // the symbol is not inside a comment
        {
            putchar(c);
        }
    }
    

    To decide whether the input char belongs to a comment, you can use a state machine. In the following example, it has 4 states; there are also rules for traversing to next state.

    int state = 0;
    int next_state;
    while ((c = getchar()) != EOF)
    {
        switch (state)
        {
            case 0: next_state = (c == '/' ? 1 : 0); break;
            case 1: next_state = (c == '*' ? 2 : c == '/' ? 1 : 0); break;
            case 2: next_state = (c == '*' ? 3 : 2); break;
            case 3: next_state = (c == '/' ? 0 : c == '*' ? 3 : 2); break;
            default: next_state = state; // will never happen
        }
    
        if (state == 1 && next_state == 0)
        {
            putchar('/'); // for correct output when a slash is not followed by a star
        }
        if (state == 0 && next_state == 0)
        {
            putchar(c);
        }
        state = next_state;
    }
    

    The example above is very simple: it doesn't work correctly for /* in non-comment contexts like in C strings; it doesn't support // comments, etc.

提交回复
热议问题