LR 0 Notes
LR 0 Notes
Automata Theory
and
Compiler Design
Shridhar Venkatanarasimhan
2. LR(0) Parsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.1 LR(0) Automaton with Sets of Items (States) and the GOTO
Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
and a form of the parse tree is constructed from the bottom to the top.
Abstract 5
each step, the current input symbol is either shifted on to the stack, or
to A.
An example of shift-reduce parsing is given for the string id ∗ id$ for the
1. E → E + T
2. E → T
3. T → T ∗ F
4. T → F
5. F → (E)
6. F → id
1. Introduction to Shift-Reduce Parsing 7
Note that this parser makes a left-to-right scan of the input and
process of seeing this production, the parser has recognized α thus far.
Let us begin with a set of items which represent a state of what the
• E′ → E
• E →E+T
• E→T
2. LR(0) Parsers 9
• T →T ∗F
• T →F
• F → (E)
• F → id
• E ′ → .E
• E → .E + T
• E → .T
• T → .T ∗ F
• T → .F
• F → .(E)
• F → .id
Refer to figure 2.1 for the LR(0) automaton which has these states and
Fig. 2.1: LR(0) Automaton with Sets of Items (States) and the GOTO Function
2. LR(0) Parsers 11
An LR(0) Parsing Table has a row for every state. It has a column for
Refer to figure 2.2 for the LR(0) parsing table for the expression
grammar.
stand for shift. If GOT O(Ii , A) = Ij dfor a non-terminal A then the entry
A → α., then for all in FOLLOW(A), the entry for that row is rk where k
Refer to figure 2.3 for the parsing algorithm which makes use of the
Refer to figure 2.4 for the example of parsing the input id ∗ id.
2. LR(0) Parsers 13
[1] Alfred V. Aho, Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman;
son.