我在修复语法中的转换/减少冲突时遇到了问题。这是语法:
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
;
我认为问题是由function_list和variable_decl引起的。 功能和变量都可以从基本类型和 id 开始。我应该如何制作function_list以解决这个问题?
我尝试了很多东西,我已经多次改变了我的语法,但仍然存在转换/减少冲突。我刚从野牛开始,而且我缺乏经验,所以任何帮助都会非常感激。
以下是野牛调试输出:
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