Conflicts & Precedence
List the shift/reduce and reduce/reduce conflicts of an LR(1) table, settle them with bison’s %left, %right and %nonassoc, compare the parse trees, and read the Parser.output bison writes.
- Lecture reference: Bottom-up Parsing III
- Lecture reference: Bottom-up Parsing III
Grammar and bison’s precedence declarations
One per line, lowest precedence first: a later line binds tighter. %left, %right or %nonassoc, then the terminals: '+' or then.
Import from bison
Declarations, %%, the rules. Actions and C code are left out; %left, %right and %nonassoc lines become the precedence
declarations, and %start the start symbol.
Open in LL(1) Parsing Table, Shift-Reduce Parsing or LR(1) Parser Builder
The same grammar with bison’s declarations. A later line binds tighter, and the precedence of a rule is that of its last terminal. All four conflicts are resolved by precedence, and int + int * int parses as int + (int * int).
Will choose to ?
Show answer
reduce because precedence of rule E → E * E is higher than that of terminal + (the conflict in state 6 on +).
Lecture reference: Bottom-up Parsing III
Now we also have a shift/reduce on input +. Which is chosen?
Show answer
Reduce (state 5 on +): the rule E → E + E has the precedence of +, the same as the terminal +, and + is declared %left. None of the three conditions for a shift holds, so + groups to the left.
Lecture reference: Bottom-up Parsing III
Conflicts of the LR(1) table, 7 states
4 conflicts: 4 settled by precedence.
State 5 on +
shift/reduce reduce, by precedence[E → E • + E, +] and [E → E + E •, +]
Stack E + E divider + …
- shift leads to E + E + divider …
- red. E → E + E leads to E divider + … taken
- Rule E → E + E
- last terminal +: line 1, %left
- Terminal +
- line 1, %left
Resolve shift/reduce conflict with a shift if
- does not hold: no precedence declared for either rule or terminal
- does not hold: input terminal has higher precedence than the rule
- does not hold: the precedences are the same and right associative
reduce because the precedences of rule E → E + E and terminal + are the same and left associative.
State 5 on *
shift/reduce shift, by precedence[E → E • * E, *] and [E → E + E •, *]
Stack E + E divider * …
- shift leads to E + E * divider … taken
- red. E → E + E leads to E divider * …
- Rule E → E + E
- last terminal +: line 1, %left
- Terminal *
- line 2, %left
Resolve shift/reduce conflict with a shift if
- does not hold: no precedence declared for either rule or terminal
- holds: input terminal has higher precedence than the rule
- does not hold: the precedences are the same and right associative
shift because terminal * has higher precedence than rule E → E + E.
State 6 on +
shift/reduce reduce, by precedence[E → E • + E, +] and [E → E * E •, +]
Stack E * E divider + …
- shift leads to E * E + divider …
- red. E → E * E leads to E divider + … taken
- Rule E → E * E
- last terminal *: line 2, %left
- Terminal +
- line 1, %left
Resolve shift/reduce conflict with a shift if
- does not hold: no precedence declared for either rule or terminal
- does not hold: input terminal has higher precedence than the rule
- does not hold: the precedences are the same and right associative
reduce because precedence of rule E → E * E is higher than that of terminal +.
State 6 on *
shift/reduce reduce, by precedence[E → E • * E, *] and [E → E * E •, *]
Stack E * E divider * …
- shift leads to E * E * divider …
- red. E → E * E leads to E divider * … taken
- Rule E → E * E
- last terminal *: line 2, %left
- Terminal *
- line 2, %left
Resolve shift/reduce conflict with a shift if
- does not hold: no precedence declared for either rule or terminal
- does not hold: input terminal has higher precedence than the rule
- does not hold: the precedences are the same and right associative
reduce because the precedences of rule E → E * E and terminal * are the same and left associative.
Parse the token string with the table as resolved
As resolved
AcceptedMarked: the node where the parse used a conflict’s entry: action[5, *] = shift.
11 steps of the LR parser
Choose the other action of a conflict above to parse the token string with it here, beside the parse as resolved.
Parser.output bison –v Parser.yy -o Parser.cc
bison builds the LALR(1) automaton, not the canonical LR(1) automaton of the slides: it merges
the LR(1) states whose items differ only in their lookaheads. The LR(1) automaton here has 7
states, the LALR(1) automaton 7, and bison adds one more, which accepts
after shifting $end. State numbers are bison’s for a Parser.yy that declares the terminals other than single characters
with %token, then the precedence lines, then the rules in the order of the grammar.
bison –v Parser.yy -o Parser.cc Grammar
0 $accept: E $end
1 E: E '+' E
2 | E '*' E
3 | int
Terminals, with rules where they appear
$end (0) 0
'*' (42) 2
'+' (43) 1
error (256)
int (258) 3
Nonterminals, with rules where they appear
$accept (6)
on left: 0
E (7)
on left: 1 2 3
on right: 0 1 2
State 0
LR(1) state 0 0 $accept: • E $end
int shift, and go to state 1
E go to state 2
State 1
LR(1) state 1 3 E: int •
$default reduce using rule 3 (E)
State 2
LR(1) state 20 $accept: E • $end 1 E: E • '+' E 2 | E • '*' E $end shift, and go to state 3 '+' shift, and go to state 4 '*' shift, and go to state 5
State 3
added by bison: accepts after$end 0 $accept: E $end •
$default accept
State 4
LR(1) state 3 1 E: E '+' • E
int shift, and go to state 1
E go to state 6
State 5
LR(1) state 4 2 E: E '*' • E
int shift, and go to state 1
E go to state 7
State 6
LR(1) state 51 E: E • '+' E 1 | E '+' E • 2 | E • '*' E '*' shift, and go to state 5 $default reduce using rule 1 (E)
State 7
LR(1) state 61 E: E • '+' E 2 | E • '*' E 2 | E '*' E • $default reduce using rule 2 (E)
Grammar classes LL(1), LR(0), SLR(1), LALR(1), LR(1)
| Class | Grammar in it | Conflicts of its table |
|---|---|---|
| LL(1) | no |
|
| LR(0) 7 states | no |
|
| SLR(1) 7 states | no |
|
| LALR(1) 7 states | no |
|
| LR(1) 7 states | no |
|
int + int * int has at least two parse trees: the grammar is ambiguous.