You are on page 1of 14

Compiler Construction

(CS-460)

1
Outline

1. Bottom-Up Parsing
2. LR(0) Items
3. Finite Automata of LR(0) Items
4. LR(0) Parsing
5. LR(0) Parsing Table
6. Summary

2
Bottom-Up Parsing

Lecture: 15-16

3
Bottom-Up Parsing

 A bottom-up parser uses an explicit stack to perform


a parse, similar to LL(1) parser
 The parsing stack will contain both tokens and
nonterminals and also some extra state information
 The stack is empty at the beginning of a bottom-up
parse and will contain the start symbol at the end of
a successful parse
 A bottom-up parse has two possible actions;
1. Shift a terminal from front of the input to top of the stack
2. Reduce a string α at the top of the stack to a nonterminal
A, given the BNF choice A  α
4
Bottom-Up Parsing (Continue…)

 A bottom-up parser is sometimes called a shift-


reduce parser
 Shift actions are indicated by writing the word shift
 Reduce actions are indicated by writing the word
reduce and giving the BNF choice used in reduction
 One further feature of bottom-up parsing is that
grammars are always augmented with a new start
symbol
 Suppose S was the start symbol of the grammar;
S/  S
5
Bottom-Up Parsing (Continue…)

 Grammar: S’  S
S(S)S|
 Input: ()
No Parsing Stack Input Action
1 $ ()$ Shift
2 $( )$ reduce S  
3 $(S )$ shift
4 $(S) $ reduce S  
5 $(S)S $ reduce S  ( S ) S
6 $S $ reduce S’  S
7 $ S’ $ accept

6
Bottom-Up Parsing (Continue…)

 A bottom-up parser can shift input symbols onto the


stack until it determines what action to perform
 The parser may need to look deeper into the stack then just
the top in order to determine what action to perform
 Thus bottom-up parsing requires arbitrary ‘stack
lookahead’, which is not as serious as input
lookahead because parser builds the stack and can
arrange for the appropriate information to be
available
 The mechanism that will do this is a deterministic
finite automaton of “items”
7
LR(0) Items

 The LR(0) item (or just item) of a CFG is a


production choice with a distinguished position on its
right hand side
 We use period “.” symbol to indicate distinguished position
 If A  α is a production choice, and if β and γ are
any two strings of symbols (including ε) such that βγ
= α, then A  β.γ is an LR(0) item
 Example:
 Write down LR(0) items for the following grammar:
S’  S
S(S)S|ε
8
Finite Automata of LR(0) Items

 In first pass a NFA is generated for LR(0) items


 In second pass NFA is converted into DFA of LR(0)
items
 States in the DFA are numbered
 The transitions in symbols can be terminals,
nonterminals, and ε (only in NFA)
 There is an initial state
 There is one or more accepting states depending
upon the grammar

9
Construction of NFA & DFA
of LR(0) Items

S’  S
S(S)S|ε

10
LR(0) Parsing

 LR(0) is a bottom-up parsing algorithm


 The “L” indicates the input will be processed from left to
right
 The “R” indicates rightmost derivation is produced
 The “0” means no lookahead token is used
 LR(0) uses DFA of items to perform parsing
 The basic actions remain the same including shift
and reduce

11
LR(0) Parsing (Continue…)

 Grammar: S’  S
S(S)S|
 Input: ()

No Parsing Stack Input Action


1 $0 ()$ shift
2 $0(2 )$ reduce S  
3 $0(2S3 )$ shift
4 $0(2S3)4 $ reduce S  
5 $0(2S3)4S5 $ reduce S  ( S ) S
6 $0S1 $ accept

12
LR(0) Parsing Table

 A LR(0) parsing table can be constructed against


the DFA of items of a given grammar with columns
 State
 Action
 Rule
 Input
 Goto

13
Summary

Any Questions?

14

You might also like