0

I'm trying to run a source code from "Programming Languages : Principles and Practice by Kenneth C. Louden" in xCode and for some reason the while loop only stops when the user input a number. For every other case, the while loops keeps running. Is there a reason why it's doing this?

typedef enum {TT_PLUS, TT_TIMES, TT_LPAREN, TT_RPAREN, TT_EOL, TT_NUMBER, TT_ERROR} TokenType;

int numVal; /*computed numeric value of a NUMBER token */
int currChar; /*current character*/

TokenType getToken(){
    while (currChar == ' '){ //Skips blanks
        currChar = getchar();
    }
    if (isdigit(currChar)){
        numVal = 0;
        while (isdigit(currChar)){//Compute numeric value
            numVal = 10 * numVal + currChar - '0';
            currChar = getchar();
        }
        return  TT_NUMBER;
    }
    else{ //recognize a special symbol
        switch (currChar){
            case '(': return TT_LPAREN; break;
            case ')': return TT_RPAREN; break;
            case '+': return TT_PLUS; break;
            case '*': return TT_TIMES; break;
            case '\n': return TT_EOL; break;
            default: return TT_ERROR; break;
        }

    }
}

int main() {
    //printf("Print tokens to scan:\n");
    TokenType token;
    currChar = getchar();

    do {
        token = getToken();
        switch(token){
            case TT_PLUS: printf("TT_PLUS\n"); break;
            case TT_TIMES: printf("TT_TIMES\n"); break;
            case TT_LPAREN: printf("TT_LPAREN\n");break;
            case TT_RPAREN: printf("TT_RPAREN\n");break;
            case TT_EOL: printf("TT_EOL\n"); break;
            case TT_NUMBER : printf("TT_NUMBER: %d\n",numVal);break;
            case TT_ERROR : printf("TT_ERROR: %c\n", currChar); break;
        }
    }while (token != TT_EOL);
    return 0;
}
5
  • 1
    Are your new lines definitely represented by \n, rather than say \r\n? Commented Nov 2, 2015 at 16:23
  • break after return is superfluous; execution of the function ends at return. Commented Nov 2, 2015 at 16:25
  • What about using a debugger to inspect the value of the variable that control the loop? BTW: There are two loops. What is kept running? Commented Nov 2, 2015 at 16:29
  • @harper How do i check that? Do I leave break points before each loop? Commented Nov 2, 2015 at 19:45
  • This depends on your system. In case of GNU try 'man gdb'. Commented Nov 2, 2015 at 20:09

1 Answer 1

1

Simply replacing your while loop at the begging of getToken with:

do {
    currChar = getchar();
} while (currChar == ' ');

Seems to do the trick for me ...

So the problem seems to be caused by your lexer getting stuck reading the same token over and over again.

Sign up to request clarification or add additional context in comments.

3 Comments

It works now but it seems to be skipping the first character every run.
Keep the loop as it was. But make 'CurChar' local to the function and initialize it with anything different than a space.
@RonRamirez Just get rid of the getchar call in main. That's causing the first character skip.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.