So I have these grammar productions in my source code.
function_defination : FUNC WHITESPACE ID LPAREN optional_parameters RPAREN WHITESPACE optional_return_type WHITESPACE LBRACE ENTER statements RBRACE
optional_parameters : has_parameter
| empty
has_parameter : has_parameter COMMA has_parameter
| ID COL WHITESPACE TYPE
Upon running, I realise it gives me 1 shift/reduce conflict. Upon closer examination of the parser.out
file,
(27) has_parameter -> has_parameter COMMA has_parameter .
(27) has_parameter -> has_parameter . COMMA has_parameter
! shift/reduce conflict for COMMA resolved as shift
I realise the conflict comes because of the production
has_parameter : has_parameter COMMA has_parameter
| ID COL WHITESPACE TYPE
This shift/reduce conflict arises due to the fact that if has_parameter COMMA has_parameter were currently on the stack, the parser (if on encountering comma as next symbol ) wouldn't know if it is to reduce it to has_parameter or shift COMMA onto the stack.
I have tried many different ways like having many different productions, etc but I'm not sure of an effective way to eliminate this shift/reduce conflict.
Any help would be appreciated.
COMMA
with%left COMMA
? – rodrigo