Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Can anyone tell me whats wrong with the following code that's suppose to remove comments and strings from an input (but not comments that's why it recognizes comments)? This is related to a prior question of mine: Removing comments with a sliding window without nested while loops

#include <stdio.h>

int main()
{
    int c, c1 = 0, c2 = 0 ,state = 0, next = 0;
    while(1)
    {
        switch(state)
        {
           case 0: next = ((c2 == '*' && c1 == '/') ? 1 : (c2 == '"') ? 2 : (c2 == '/' && c1 == '/') ? 3 : (c2 == ''') ? 4: 0); break; 
           case 1: next = ((c2 == '/' && c1 == '*') ? 0 : 1); break; 
           case 2: next = ((c2 == '"' && c1 != '\') ? 0 : 2); break;
           case 3: next = ((c2 == '
') ? 0 : 3); break;
           case 4: next = ((c2 == ''' && c1 != '\') ? 0 : 4); break;
           default: next = state; 
        }
        c = getchar(); if( c < 0) break;
        c1 = c2; c2 = c; // slide window
        if(state == 1)
        {
            if(c2 == '*')
            {
                c = getchar();
                c1 = c2; c2 = c;
                if(c2 != '/')
                   putchar(c1);
            }
            else
                putchar(c2);
        }
        else if(state == 2)
        {
            if(c2 != '"' || (c2 == '"' && c1 != '\'))
                putchar(c2);
        }
        else if(state == 3)
        {
                putchar(c2);
        }
        else
        state = next;
        // c2 is the current input byte and c1 is the previous input byte
    }
    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
165 views
Welcome To Ask or Share your Answers For Others

1 Answer

I don't think you actually need a sliding window for your task of removing C and C++ comments. You can expand your state machine to include a few additions states for tracking escapes, etc... With more states the code gets a bit bigger, but it might make it conceptually simpler since you only have one state to track. So converting the spirit of your code to the new state machine formula I'd suggest, you get the code below (and I also agree with Basile's suggestion of using enums and included it).

#include <stdio.h>

int main()
{
    enum {
        START, SLASH,
        STRING, CHAR, STRING_ESCAPE, CHAR_ESCAPE,
        SINGLE_LINE_COMMENT, MULTI_LINE_COMMENT, MULTI_LINE_END,
    } state = START;
    int c;

    while ((c = getchar()) != EOF) {
        switch (state) {
        case START:
        state_START:
            if (c == '/') { state = SLASH; break; }
            putchar(c);
            if (c == '"') state = STRING;
            else if (c == ''') state = CHAR;
            break;
        case SLASH:
            if (c == '/') state = SINGLE_LINE_COMMENT;
            else if (c == '*') state = MULTI_LINE_COMMENT;
            else { state = START; goto state_START; }
            break;
        case STRING:
            putchar(c);
            if (c == '"') state = START;
            else if (c == '\') state = STRING_ESCAPE;
            break;
        case CHAR:
            putchar(c);
            if (c == ''') state = START;
            else if (c == '\') state = CHAR_ESCAPE;
            break;
        case SINGLE_LINE_COMMENT:
            if (c == '
') state = START;
            break;
        case MULTI_LINE_COMMENT:
        state_MULTI_LINE_COMMENT:
            if (c == '*') state = MULTI_LINE_END;
            break;
        case STRING_ESCAPE:
            putchar(c);
            state = STRING;
            break;
        case CHAR_ESCAPE:
            putchar(c);
            state = CHAR;
            break;
        case MULTI_LINE_END:
            if (c == '/') state = START;
            else { state = MULTI_LINE_COMMENT; goto state_MULTI_LINE_COMMENT; }
            break;
        }
    }
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...