如何更改此项以删除班次/减少冲突?
var_part
:
| VAR var_declaration SEMIC var_part_multi
;
var_part_multi
: var_declaration SEMIC var_part_multi
|
;
var_declaration
: ID id_list COLON ID
;
id_list
: COMMA ID id_list
|
;
我有两个冲突,y.output给了我这个:
State 19 conflicts: 1 shift/reduce
State 59 conflicts: 1 shift/reduce
state 19
4 var_part: VAR var_declaration SEMIC . var_part_multi
5 var_part_multi: . var_declaration SEMIC var_part_multi
6
7 var_declaration: . ID id_list COLON ID
ID shift, and go to state 12
ID [reduce using rule 6 (var_part_multi)]
$default reduce using rule 6 (var_part_multi)
var_part_multi go to state 33
var_declaration go to state 34
state 59
5 var_part_multi: . var_declaration SEMIC var_part_multi
5 | var_declaration SEMIC . var_part_multi
6 | . [ID, BEGIN, DOT, IF, FUNCTION, REPEAT, SEMIC, VAL, WHILE, WRITELN]
7 var_declaration: . ID id_list COLON ID
ID shift, and go to state 12
ID [reduce using rule 6 (var_part_multi)]
$default reduce using rule 6 (var_part_multi)
var_part_multi go to state 95
var_declaration go to state 34
我知道问题在于ID,它有两条可能的路线,但我一直在尝试最后一小时更改规则,添加优先权等等,并且无法消除冲突。你们能帮忙吗?
答案 0 :(得分:1)
你还没有足够的语法来回答这个问题,但这几乎肯定与var_part_multi
可以为空的事实有关。
问题是使用var_part
的背景是什么;具体而言,var_part
如何可以跟随ID
开头的内容。
在这种情况下,由于var_part_multi
可以为空,因此解析器必须在使用var_part_multi
启动非空ID
或减少空var_part_multi
之间进行选择。 (然后减少var_part
),这将允许ID
启动可以跟随var_part
的非终端。
顺便说一下,在y.output文件的粘贴中,State 19
下的第三行(以6
开头的那一行)已被截断。它应该类似于State 59
下的第三行。
如果您无法通过检查语法ID
如何跟随var_part
来解决问题,那么从两个冲突状态之一向后追踪状态机可能会有所帮助。
答案 1 :(得分:0)
我尝试了多个独立的yacc实施,并且所有人都同意您发布的语法没有冲突。例如:
$ cat so.y
%token VAR ID COLON SEMIC COMMA
%%
var_part
:
| VAR var_declaration SEMIC var_part_multi
;
var_part_multi
: var_declaration SEMIC var_part_multi
|
;
var_declaration
: ID id_list COLON ID
;
id_list
: COMMA ID id_list
|
;
$ cat y.output
state 0 //
0 $accept: . var_part
1 var_part: . [$end]
$end reduce using rule 1 (var_part)
VAR shift, and goto state 2
var_part goto state 1
state 1 // [$end]
0 $accept: var_part . [$end]
$end accept
state 2 // VAR
2 var_part: VAR . var_declaration SEMIC var_part_multi
ID shift, and goto state 4
var_declaration goto state 3
state 3 // VAR ID COLON ID [SEMIC]
2 var_part: VAR var_declaration . SEMIC var_part_multi
SEMIC shift, and goto state 11
state 4 // VAR ID
5 var_declaration: ID . id_list COLON ID
7 id_list: . [COLON]
COLON reduce using rule 7 (id_list)
COMMA shift, and goto state 6
id_list goto state 5
state 5 // VAR ID [COLON]
5 var_declaration: ID id_list . COLON ID
COLON shift, and goto state 9
state 6 // VAR ID COMMA
6 id_list: COMMA . ID id_list
ID shift, and goto state 7
state 7 // VAR ID COMMA ID
6 id_list: COMMA ID . id_list
7 id_list: . [COLON]
COLON reduce using rule 7 (id_list)
COMMA shift, and goto state 6
id_list goto state 8
state 8 // VAR ID COMMA ID [COLON]
6 id_list: COMMA ID id_list . [COLON]
COLON reduce using rule 6 (id_list)
state 9 // VAR ID COLON
5 var_declaration: ID id_list COLON . ID
ID shift, and goto state 10
state 10 // VAR ID COLON ID
5 var_declaration: ID id_list COLON ID . [SEMIC]
SEMIC reduce using rule 5 (var_declaration)
state 11 // VAR ID COLON ID SEMIC
2 var_part: VAR var_declaration SEMIC . var_part_multi
4 var_part_multi: . [$end]
$end reduce using rule 4 (var_part_multi)
ID shift, and goto state 4
var_declaration goto state 13
var_part_multi goto state 12
state 12 // VAR ID COLON ID SEMIC [$end]
2 var_part: VAR var_declaration SEMIC var_part_multi . [$end]
$end reduce using rule 2 (var_part)
state 13 // VAR ID COLON ID SEMIC ID COLON ID [SEMIC]
3 var_part_multi: var_declaration . SEMIC var_part_multi
SEMIC shift, and goto state 14
state 14 // VAR ID COLON ID SEMIC ID COLON ID SEMIC
3 var_part_multi: var_declaration SEMIC . var_part_multi
4 var_part_multi: . [$end]
$end reduce using rule 4 (var_part_multi)
ID shift, and goto state 4
var_declaration goto state 13
var_part_multi goto state 15
state 15 // VAR ID COLON ID SEMIC ID COLON ID SEMIC [$end]
3 var_part_multi: var_declaration SEMIC var_part_multi . [$end]
$end reduce using rule 3 (var_part_multi)
$