Syntax analysis

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 · slides 16–25
  • Lecture reference: Bottom-up Parsing III · slides 26–28

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
Tab inserts a tab. Press Escape, then Tab, to move focus out of the editor.

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

Lecture reference: Bottom-up Parsing III · slides 19–21

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 · slide 20

  • 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 · slide 21

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 •, +]

State 3
State 5
[E → E + • E, +]
moves on E to
[E → E + E •, +]
[E → • E + E, +]
[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.

action[5, +] in the other parse

The parse of the token string does not look up action[5, +].

State 5 on *

shift/reduce shift, by precedence

[E → E • * E, *] and [E → E + E •, *]

State 3
State 5
[E → E + • E, *]
moves on E to
[E → E + E •, *]
[E → • E * E, *]
[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.

action[5, *] in the other parse

The parse of the token string looks up action[5, *].

State 6 on +

shift/reduce reduce, by precedence

[E → E • + E, +] and [E → E * E •, +]

State 4
State 6
[E → E * • E, +]
moves on E to
[E → E * E •, +]
[E → • E + E, +]
[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 +.

action[6, +] in the other parse

The parse of the token string does not look up action[6, +].

State 6 on *

shift/reduce reduce, by precedence

[E → E • * E, *] and [E → E * E •, *]

State 4
State 6
[E → E * • E, *]
moves on E to
[E → E * E •, *]
[E → • E * E, *]
[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.

action[6, *] in the other parse

The parse of the token string does not look up action[6, *].

Parse the token string with the table as resolved

As resolved

Accepted

Marked: 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 2
    0 $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 5
    1 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 6
    1 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)

Grammar classes and their conflicts
ClassGrammar in itConflicts of its table
LL(1) no
  • [E, int]: E → E + E and E → E * E and E → int
LR(0) 7 statesno
  • state 5 on +, shift/reduce: [E → E • + E] and [E → E + E •]
  • state 5 on *, shift/reduce: [E → E • * E] and [E → E + E •]
  • state 6 on +, shift/reduce: [E → E • + E] and [E → E * E •]
  • and 1 more
SLR(1) 7 statesno
  • state 5 on +, shift/reduce: [E → E • + E] and [E → E + E •]
  • state 5 on *, shift/reduce: [E → E • * E] and [E → E + E •]
  • state 6 on +, shift/reduce: [E → E • + E] and [E → E * E •]
  • and 1 more
LALR(1) 7 statesno
  • state 5 on +, shift/reduce: [E → E • + E, +] and [E → E + E •, +]
  • state 5 on *, shift/reduce: [E → E • * E, *] and [E → E + E •, *]
  • state 6 on +, shift/reduce: [E → E • + E, +] and [E → E * E •, +]
  • and 1 more
LR(1) 7 statesno
  • state 5 on +, shift/reduce: [E → E • + E, +] and [E → E + E •, +]
  • state 5 on *, shift/reduce: [E → E • * E, *] and [E → E + E •, *]
  • state 6 on +, shift/reduce: [E → E • + E, +] and [E → E * E •, +]
  • and 1 more

int + int * int has at least two parse trees: the grammar is ambiguous.

Unambiguous GrammarsAmbiguousGrammarsLR(k)LR(1)LALR(1)SLRLR(0)LL(k)LL(1)LL(0)
The dot marks an ambiguous grammar. Simple LR (SLR); Look-ahead LR (LALR). The slides only name LR(0), SLR and LALR(1); their tables are built here the standard way. Lecture reference: Bottom-up Parsing III · slide 26