Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ambiguity in the error message on Part 3 #27

Open
tobby168 opened this issue Jul 30, 2020 · 0 comments
Open

Ambiguity in the error message on Part 3 #27

tobby168 opened this issue Jul 30, 2020 · 0 comments

Comments

@tobby168
Copy link

Hi, thanks for the excellent content of the book! I have really learned a lot from it.

However, when I was reading Part 3: Operator Precedence, the error message came from input03 confused me.

syntax error on line 1, token 5          # input03 result

12 34 + -56 * / - - 8 + * 2

According to the code in expr.c, token 5 should stand for token type = T_INTLIT (in this case, which is 5 in enum). The error message is a little misleading for me since it's unaligned to other common error messages.
Will it be better if set the error message to syntax error on line 1 token 2, token type 5 by adding a global variable to track the pointed token?

// Convert a binary operator token into an AST operation.
int arithop(int tokentype) {
switch (tokentype) {
case T_PLUS:
return (A_ADD);
case T_MINUS:
return (A_SUBTRACT);
case T_STAR:
return (A_MULTIPLY);
case T_SLASH:
return (A_DIVIDE);
default:
fprintf(stderr, "syntax error on line %d, token %d\n", Line, tokentype);
exit(1);
}
}

Same issue also on

static struct ASTnode *primary(void) {
struct ASTnode *n;
// For an INTLIT token, make a leaf AST node for it
// and scan in the next token. Otherwise, a syntax error
// for any other token type.
switch (Token.token) {
case T_INTLIT:
n = mkastleaf(A_INTLIT, Token.intvalue);
scan(&Token);
return (n);
default:
fprintf(stderr, "syntax error on line %d, token %d\n", Line, Token.token);
exit(1);
}
}

Thanks again😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant