I am writing a c-like language, but for grammar shown below:
module: module declORFunc
| declORFunc
;
declORFunc: decl
| func
decl: CONST_opt btype varDef_list SEMICOLON ;
CONST_opt: /* empty */ | CONST ;
btype: INT | FLOAT ;
...
func: functype ID LPAREN funcFParams_opt RPAREN blockStmt ;
functype: INT | FLOAT | VOID ;
Because yacc is LALR(1), when processing token INT, it exhibits a shift-reduce conflict during grammar resolution for both sytanx start with INT ID. For more detail:
Sy.y: warning: shift/reduce conflict on token INT [-Wcounterexamples]
First example: • INT ID LPAREN funcFParams_opt RPAREN blockStmt $end
Shift derivation
$accept
↳ 0: module $end
↳ 2: declORFunc
↳ 4: func
↳ 20: functype ID LPAREN funcFParams_opt RPAREN blockStmt
↳ 21: • INT
Second example: • INT varDef_list SEMICOLON $end
Reduce derivation
$accept
↳ 0: module $end
↳ 2: declORFunc
↳ 3: decl
↳ 5: CONST_opt btype varDef_list SEMICOLON
↳ 6: ε • ↳ 8: INT
I trid:
- replace btype and functype with type: INT | FLOAT | VOID;
- add %proc to force yacc to shift into decl before reduce into func.
How can I resolve this conflict?