1
votes

for an if and if-else statement grammar I have some grammar.

The following is a simplified excerpt from my code to show how I have the if and if-else statement grammar, so if there are errors unrelated to that grammar then don't worry about it. I assure you there are no compilation errors in the code I am using:

%token IF ELSE VOID ID VOID_PARAMS

%nonassoc shift_else
%nonassoc ElSE

%%

Func: VOID ID VOID_PARAMS '{' Stmt '}'
;
If_Stmt: IF '(' L_expr ')' Stmt
;
Stmt: If_Stmt shift_else
;
| If_Stmt ELSE Stmt
;
| ';'
| ...
;

L_expr: ...
;

It has been working just fine for a while, but now it is finding errors when it reaches the end of a function. For example:

void foo(void) {
    if (1 > 5)
        ;
}

gives this output ( using yyerror() ):

Found unexpected token: '}' on line 4

Any suggestions as to why this could be happening? And what can I do to fix this?

1
While it's great that you've given us a reduced piece of the code, we can't independently test this out. If you post a minimal, self-contained example that lets us reproduce the issue, we can try things out on our own systems.templatetypedef
@templatetypedef I can attempt to do this, though before I do, can any of the code associated with each grammar rule effect this? I have some type checking and other stuff going on fore these rules, which would make it basically impossible to give complete code. If I only need the grammar part then it is more reasonable.user3672051
What's a shift_else?user253751
@immibis Im not sure what you mean, its a placeholder for the empty string I guess, used to resolve the trailing else shift/reduce conflictuser3672051
The rules Stmt: If_Stmt shift_else ; | If_Stmt ELSE Stmt ; | ';' | ... ; are not legal. It is impossible to help you if you don't post a legal, usable extract of your grammar.user207421

1 Answers

0
votes

After reading if(1 > 5) ; the parser is expecting an ELSE or a shift_else.

Presumably there's not an ELSE there, since it doesn't appear in the source code.

Unless your lexer conjures a shift_else out of thin air, the next token will be a }, which is not an ELSE or shift_else, hence the error.