Exercise: construct the SLR parsing table for grammar: S->L=R, S->R L->*R L->id R->L The grammar can have

# Exercise: construct the SLR parsing table for grammar: S->L=R, S->R L->*R L->id R->L The grammar can have

## Exercise: construct the SLR parsing table for grammar: S->L=R, S->R L->*R L->id R->L The grammar can have

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Exercise: construct the SLR parsing table for grammar: S->L=R, S->R L->*R L->id R->L • The grammar can have shift/reduce conflict or reduce/reduce conflict. • What about shift/shift conflict?

2. SLR does not always remember the right context -- using the follow set to determine when to reduce is sometimes too general. • LR(1) parsing table has more context information in its items. • An LR(1) item is of the form • terminal a has not effect when is not . • When is , an item of the form means to reduce only when the next input symbol is a.

3. Computing sets of LR(1) items. • Closure(I) is now slightly different • Start the construction of the set of LR(1) items by computing the closure of

4. Example 1: first page • Example 2: S’ -> S S -> CC C ->cC C ->d

5. Constructing the LR(1) parsing table

6. The number of states in LR(1) parsing table is much more than that in SLR parsing table. • LALR reduces the number of states in LR(1) parsing table. • LALR (LookAhead LR) is less powerful than LR(1) • reducing states may introduce reduce-reduce conflict, but not shift-reduce conflict. • LALR has the same number of states as SLR, but more powerful. • Constructing LALR parsing table. • Combine LR(1) sets with the same sets of first parts (ignore lookahead). • Algorithms exist that skip constructing the LR(1) sets.

7. Using ambiguous grammars • ambiguous grammars will results in conflicts • Can use precedence and assocativity to resolve the conflicts • May result in a smaller parsing table in comparison to using un-ambiguous grammars. • Example: E->E+E E->E*E E->(E) E->id