I get 2 shift/reduce errors when trying to compile my grammar:
program : declaration_list ;
declaration_list : declaration_list declaration | declaration ;
declaration : var_declaration | fun_declaration ;
var_declaration: type_specifier var_decl_list ;
scoped_var_declaration: scoped_type_specifier var_decl_list;
var_decl_list : var_decl_list "," var_decl_initialize | var_decl_initialize ;
var_decl_initialize : var_decl_id ;
var_decl_id : ID | ID "[" INT_NUM "]" ;
scoped_type_specifier : type_specifier;
type_specifier: INT | CHAR ;
fun_declaration: type_specifier ID "(" params ")" statement | ID "(" params ")" statement ;
params : | param_list ;
param_list: param_list ";" param_type_list | param_type_list ;
param_type_list : type_specifier param_id_list ;
param_id_list : param_id_list "," param_id | param_id ;
param_id : ID | ID "[" "]" ;
statement: expression_stmt | compound_stmt | selection_stmt | iteration_stmt | return_stmt | break_stmt ;
compound_stmt : "{" local_declaration statement_list "}" ;
local_declaration : | local_declaration scoped_var_declaration ;
statement_list : | statement_list statement ;
expression_stmt : expression ";" | ";" ;
selection_stmt : "if" "(" simple_expression ")" statement | "if" "(" simple_expression ")" statement "else" statement ;
iteration_stmt: "while" "(" simple_expression ")" statement ;
return_stmt : "return" ";" | "return" expression ;
break_stmt : "break" ;
expression : mutable ASSIGN expression | mutable EINC expression | mutable EDEC expression | mutable INC | mutable DEC | simple_expression ;
simple_expression: simple_expression OR and_expression | and_expression ;
and_expression: and_expression AND unary_rel_expression | unary_rel_expression ;
unary_rel_expression: "!" unary_rel_expression | rel_expression ;
rel_expression : sum_expression relop sum_expression | sum_expression ;
relop: "<" | ">" | LE | GE | COMP_OP | NE ;
sum_expression : sum_expression sumop term | term ;
sumop : ADD | SUB ;
term: term mulop unary_expression | unary_expression ;
mulop: MULT | DIV | REM ;
unary_expression : unaryop unary_expression | factor ;
unaryop : "-" | "*" ;
factor : immutable | mutable ;
mutable : ID | ID "[" expression "]" ;
immutable : "(" expression ")" | call | constant ;
call: ID "(" args ")" ;
args: | arg_list;
arg_list : arg_list","expression | expression ;
constant : INT_NUM | STRINGCONST | CHARCONST | 'true' | 'false' ;
I get 2 shift/reduce at
State 38 conflicts: 1 shift/reduce State 139 conflicts: 1 shift/reduce
The specific states are :
state 38
81 mutable: ID . [$end, ADD, SUB, DIV, MULT, COMP_OP, INT, CHAR, CHARCONST, STRINGCONST, GE, LE, NE, EINC, EDEC, INC, DEC, AND, OR, REM, ASSIGN, INT_NUM, ID, ",", "]", "(", ")", ";", "{", "}", "if", "else", "while", "return", "break", "!", "<", ">", "-", "*", 't', 'f']
82 | ID . "[" expression "]"
86 call: ID . "(" args ")"
"[" shift, and go to state 77
"(" shift, and go to state 78
"(" [reduce using rule 81 (mutable)]
$default reduce using rule 81 (mutable)
state 139
40 selection_stmt: "if" "(" simple_expression ")" statement . [$end, INT, CHAR, CHARCONST, STRINGCONST, INT_NUM, ID, "(", ";", "{", "}", "if", "else", "while", "return", "break", "!", "-", "*", 't', 'f']
41 | "if" "(" simple_expression ")" statement . "else" statement
"else" shift, and go to state 141
"else" [reduce using rule 40 (selection_stmt)]
$default reduce using rule 40 (selection_stmt)
Could someone please explain how to fix this error, I know it has something to do with precedence.