calc.y
%union
{
int ival;
const char *sval;
}
%token <ival> NUM
%nterm <ival> exp
%token <sval> STR
%nterm <sval> useless
%left '+' '-'
%left '*'
%%
exp:
exp '+' exp
| exp '-' exp
| exp '*' exp
| exp '/' exp
| NUM
;
useless: STR;
%%
编译
$ bison --report=state calc.y
calc.y: warning: 1 nonterminal useless in grammar [-Wother]
calc.y: warning: 1 rule useless in grammar [-Wother]
calc.y:20.1-7: warning: nonterminal useless in grammar: useless [-Wother]
20 | useless: STR;
| ^~~~~~~
calc.y: warning: 7 shift/reduce conflicts [-Wconflicts-sr]
calc.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
$ cat calc.output
Nonterminals useless in grammar
useless
Terminals unused in grammar
STR
Rules useless in grammar
6 useless: STR
State 8 conflicts: 1 shift/reduce
State 9 conflicts: 1 shift/reduce
State 10 conflicts: 1 shift/reduce
State 11 conflicts: 4 shift/reduce
Grammar
0 $accept: exp $end
1 exp: exp '+' exp
2 | exp '-' exp
3 | exp '*' exp
4 | exp '/' exp
5 | NUM
POSIX只允许%type表示终结符,但是bison也允许%nterm标记终结符。
含义: 终结符符号
|(ASCII码)
| 在几号语法规则中用到了
Terminals, with rules where they appear
$end (0) 0
-- 实例: '*' (42) 3
-- 含义: 终结符乘号 (ASCII42) 在3号语法规则中用到了
'+' (43) 1
'-' (45) 2
'/' (47) 4
error (256)
NUM <ival> (258) 5
STR <sval> (259)
Nonterminals, with rules where they appear
$accept (9)
on left: 0
exp <ival> (10)
on left: 1 2 3 4 5
on right: 0 1 2 3 4
状态0:
State 0
0 $accept: • exp $end
NUM shift, and go to state 1
exp go to state 2
状态1:
5 exp: NUM •
已经匹配上了。不管lookahead是什么,解析器都会开始规约。$default表示任何向前看lookahead的字符(•位置表示的字符)是什么都能匹配上后面的规则。
State 1
5 exp: NUM •
$default reduce using rule 5 (exp)
状态2:注意dot的位置。
exp • '+' exp
dot向前看来了一个'+'
,则移进解析堆栈,转移到状态4。syntax error
。State 2
0 $accept: exp • $end
1 exp: exp • '+' exp
2 | exp • '-' exp
3 | exp • '*' exp
4 | exp • '/' exp
$end shift, and go to state 3
'+' shift, and go to state 4
'-' shift, and go to state 5
'*' shift, and go to state 6
'/' shift, and go to state 7
状态3:
State 3
0 $accept: exp $end •
$default accept
状态4:
State 4
1 exp: exp '+' • exp
NUM shift, and go to state 1
exp go to state 8
State 5
2 exp: exp '-' • exp
NUM shift, and go to state 1
exp go to state 9
State 6
3 exp: exp '*' • exp
NUM shift, and go to state 1
exp go to state 10
State 7
4 exp: exp '/' • exp
NUM shift, and go to state 1
exp go to state 11
回忆下所有rules:
Grammar
0 $accept: exp $end
1 exp: exp '+' exp
2 | exp '-' exp
3 | exp '*' exp
4 | exp '/' exp
5 | NUM
状态8
'\'
'\'
的第一个选择:走rule4,把’'移进解析堆栈。'\'
的第二个选择:走rule1把前面的exp'+'exp
recude掉,再处理后面的’'。'\'
与'+'
之间没有优先级,可能出现: NUM + (NUM / NUM)
(NUM + NUM) / NUM
State 8
1 exp: exp • '+' exp
1 | exp '+' exp • [$end, '+', '-', '/'] (使用--report=lookahead能看到末尾dot代表的字符)
2 | exp • '-' exp
3 | exp • '*' exp
4 | exp • '/' exp
'*' shift, and go to state 6
'/' shift, and go to state 7
'/' [reduce using rule 1 (exp)]
$default reduce using rule 1 (exp)
State 9
1 exp: exp • '+' exp
2 | exp • '-' exp
2 | exp '-' exp •
3 | exp • '*' exp
4 | exp • '/' exp
'*' shift, and go to state 6
'/' shift, and go to state 7
'/' [reduce using rule 2 (exp)]
$default reduce using rule 2 (exp)
State 10
1 exp: exp • '+' exp
2 | exp • '-' exp
3 | exp • '*' exp
3 | exp '*' exp •
4 | exp • '/' exp
'/' shift, and go to state 7
'/' [reduce using rule 3 (exp)]
$default reduce using rule 3 (exp)
状态11:
(1/2)/3
和1/(2/3)
无法区分。 '/' shift, and go to state 7
'/' [reduce using rule 4 (exp)]
State 11
1 exp: exp • '+' exp
2 | exp • '-' exp
3 | exp • '*' exp
4 | exp • '/' exp
4 | exp '/' exp •
'+' shift, and go to state 4
'-' shift, and go to state 5
'*' shift, and go to state 6
'/' shift, and go to state 7
'+' [reduce using rule 4 (exp)]
'-' [reduce using rule 4 (exp)]
'*' [reduce using rule 4 (exp)]
'/' [reduce using rule 4 (exp)]
$default reduce using rule 4 (exp)
state
和lookahead
的区别lookahead会补充最后的dot,列出可能性。
state
和itemset
的区别itemset会展开item项目,展开非终结符,补充非终结符的内容。
state
和solved
的区别solved会尝试给出解决方法。
state
和counterexamples
的区别counterexamples会给出用例。