I'm having trouble fixing shift/reduce conflicts in my grammar. This is the grammar:
body: variable_decl function_list
| variable_decl
;
variable_decl: variable variable_decl
| /* no variables declaration */
variable: KW_STATIC basictypes varhelp2 SEMICOLON
| basictypes varhelp2 SEMICOLON
;
varhelp2: varhelp2 COMA id varhelp1
| id varhelp1
;
varhelp1: dimarray
| ASSIGN constants
;
dimarray: /* no array */
| LBRACKET id RBRACKET dimarray
;
constants: pint
| real
| bool
;
basictypes: KW_INTEGER
| KW_BOOLEAN
| KW_CHARACTER
| KW_REAL
| KW_STRING
;
function_list: function_list function
| function
;
function: KW_VOID id LEFT_PARENTHESIS function_help1 RIGHT_PARENTHESIS
| basictypes id LEFT_PARENTHESIS function_help1 RIGHT_PARENTHESIS
;
function_help1: /* empty */
| function_help2
;
function_help2: function_help2 COMA basictypes id
| basictypes id
;
I think the problem is caused by function_list and variable_decl. Both functions and variables can start with basictypes and id. How should I make function_list in order to resolve this problem?
I 've tried a lot of things, I've changed my grammar several times but the shift/reduce conflicts remain. I 've just started with bison and I lack of experience, so any help would be greatly appreciated.
Here is the bison debug output:
State 0
0 $accept: . program $end
1 program: . body
7 body: . variable_decl function_list
8 | . variable_decl
9 variable_decl: . variable variable_decl
10 | . %empty [$end, KW_BOOLEAN, KW_INTEGER, KW_CHARACTER, KW_REAL, KW_STRING, KW_VOID]
11 variable: . KW_STATIC basictypes varhelp2 SEMICOLON
12 | . basictypes varhelp2 SEMICOLON
27 basictypes: . KW_INTEGER
28 | . KW_BOOLEAN
29 | . KW_CHARACTER
30 | . KW_REAL
31 | . KW_STRING
KW_STATIC shift, and go to state 1
KW_BOOLEAN shift, and go to state 2
KW_INTEGER shift, and go to state 3
KW_CHARACTER shift, and go to state 4
KW_REAL shift, and go to state 5
KW_STRING shift, and go to state 6
KW_BOOLEAN [reduce using rule 10 (variable_decl)]
KW_INTEGER [reduce using rule 10 (variable_decl)]
KW_CHARACTER [reduce using rule 10 (variable_decl)]
KW_REAL [reduce using rule 10 (variable_decl)]
KW_STRING [reduce using rule 10 (variable_decl)]
$default reduce using rule 10 (variable_decl)
program go to state 7
body go to state 8
variable_decl go to state 9
variable go to state 10
basictypes go to state 11
State 10
9 variable_decl: . variable variable_decl
9 | variable . variable_decl
10 | . %empty [$end, KW_BOOLEAN, KW_INTEGER, KW_CHARACTER, KW_REAL, KW_STRING, KW_VOID]
11 variable: . KW_STATIC basictypes varhelp2 SEMICOLON
12 | . basictypes varhelp2 SEMICOLON
27 basictypes: . KW_INTEGER
28 | . KW_BOOLEAN
29 | . KW_CHARACTER
30 | . KW_REAL
31 | . KW_STRING
KW_STATIC shift, and go to state 1
KW_BOOLEAN shift, and go to state 2
KW_INTEGER shift, and go to state 3
KW_CHARACTER shift, and go to state 4
KW_REAL shift, and go to state 5
KW_STRING shift, and go to state 6
KW_BOOLEAN [reduce using rule 10 (variable_decl)]
KW_INTEGER [reduce using rule 10 (variable_decl)]
KW_CHARACTER [reduce using rule 10 (variable_decl)]
KW_REAL [reduce using rule 10 (variable_decl)]
KW_STRING [reduce using rule 10 (variable_decl)]
$default reduce using rule 10 (variable_decl)
variable_decl go to state 18
variable go to state 10
basictypes go to state 11