LR 1 Notes
LR 1 Notes
Automata Theory
and
Compiler Design
Parsers
Shridhar Venkatanarasimhan
1. LR(0) Parsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2. LALR Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
LIST OF FIGURES
1.1 LR(1) Automaton with Sets of Items (States) and the GOTO
Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
reductions possible.
conflict?
LR(1) parser is strictly more powerful than LR(0) parser, that is, if
have grammars with LR(1) parsing tables without conflicts, for which
lookahead symbol.
Add B → .γ, F IRST (βa) to I. Keep doing these, till no more items can
be added to I.
1. S → CC
2. C → cC
3. C → d
• S′ → S
• S → CC
• C → cC
1. LR(0) Parsers 7
• C→d
• S ′ → .S, $
• S → .CC, $
• C → .cC, c|d
• C → .d, c|d
Note how the lookaheads are c and d in the last two items.
αX.β, a. Thus from a set of items I we GOTO a set J(J = CLOSU RE(J))
on X.
Refer to figure 1.1 for the LR(1) automaton which has these states and
An LR(1) Parsing Table has a row for every state. It has a column for
Refer to figure 1.2 for the LR(1) parsing table for the sample gram-
mar.
Fig. 1.1: LR(1) Automaton with Sets of Items (States) and the GOTO Function
1. LR(0) Parsers 9
item A → α., a|b|c..., then for the lookaheads a, b, c, ..., the entry for that
The parsing algorithm for LR(1) is just like that of the LR(0), except that
it makes use of the LR(1) parsing table instead. We have covered the
between LR(0) and LR(1). For any grammar, the LR(0)and LALR parser
always have the same number of states which is much less than an
are the same except that the lookaheads are different. In LALR, we
merge these states to form a single state I47 which has the items with a
union of lookaheads {c, d, $}. Similarly, I36 and I89 are formed as given
in figure 2.1.
The parsing algorithm is just like that for LR(0) and LR(1).
2. LALR Parser 12
[1] Alfred V. Aho, Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman;
son.