>我有以下yacc语法:
OPTIONS:OPTIONS OPTION {printf("%sn", "Options enabled");}
| OPTION {printf("%sn", "First option");}
|
;
OPTION: DEBUG {printf("%sn", "debug enabled");}
| NESTING {printf("%sn", "nesting enabled");}
| '(' STACK '=' NAME ')' {printf("%sn", "stack size given");}
| NOLIST {printf("%sn", "nolist enabled");}
| VIEW EQ NAME {printf("%sn", "this is a view, first name is view name");}
;
对于空规则,它给了我移位/减少错误。Y.错误输出文件如下所示:
17 OPTIONS: . OPTIONS OPTION
18 | . OPTION
19 | . [SEMICOLON, VIEW, DEBUG, NESTING, NOLIST, '(']
20 OPTION: . DEBUG
21 | . NESTING
22 | . '(' STACK '=' NAME ')'
23 | . NOLIST
24 | . VIEW EQ NAME
NAME shift, and go to state 4
VIEW shift, and go to state 11
DEBUG shift, and go to state 12
NESTING shift, and go to state 13
NOLIST shift, and go to state 14
'(' shift, and go to state 15
VIEW [reduce using rule 19 (OPTIONS)]
DEBUG [reduce using rule 19 (OPTIONS)]
NESTING [reduce using rule 19 (OPTIONS)]
NOLIST [reduce using rule 19 (OPTIONS)]
'(' [reduce using rule 19 (OPTIONS)]
$default reduce using rule 19 (OPTIONS)
有人可以建议,如何解决问题吗?
你在规则选项上有一个递归。conflit之所以存在,是因为有两种方法可以阻止这种递归。例如,如果你只有一个选项,则有两种不同的解析树。
OPTIONS or OPTIONS
| |
OPTION OPTIONS OPTION
| |
... "empty rule"
因此,删除空规则或OPTIONS : OPTION
(维护空规则),问题应该消失了。