Programming
Languages and
Compilers
Lecture #05
Professor Chung Yung
Spring 2024, Dept. CSIE, NDHU
Bottom-Up
Parsing 1. LL(k) Grammars
2. Recursive-Descent vs.
Table-Driven Parsers
3. Shift-Reduce Parsers
4. LR(0) Table Construction
3/26/2024 © All rights reserved by Chung Yung. 2
1
LL(k) Grammars
5.1 – 5.2
Top-Down Parsers
Parsers begin with grammar’s start symbol and grow a parse
tree from its root to its leaves.
• Predictive parsing: must predict at each step in the derivation.
• LL(k): Left-to-right scanning, produce a Leftmost derivation, and use k
look-ahead symbols.
• Recursive descent parsers
3/26/2024 © All rights reserved by Chung Yung. 4
Computing Predict Sets
3/26/2024 © All rights reserved by Chung Yung. 5
An Example of CFG
First Follow
S a, b, c, q, $
C c $, d
A a, b, q c, $
B b c, d, q, $
Q q c, $
3/26/2024 © All rights reserved by Chung Yung. 6
Predict Set
3/26/2024 © All rights reserved by Chung Yung. 7
Determining LL(1)
3/26/2024 © All rights reserved by Chung Yung. 8
2
Recursive-Descent vs. Table-Driven
5.3 – 5.4
Deciding Rule to Use
3/26/2024 © All rights reserved by Chung Yung. 10
An Example
3/26/2024 © All rights reserved by Chung Yung. 11
Construct LL(1) Parse Table
3/26/2024 © All rights reserved by Chung Yung. 12
Trace of Parsing
3/26/2024 © All rights reserved by Chung Yung. 13
Self-Study Exercise: Prove Non-LL(1)
• Prove that the following grammar is NOT LL(1).
1. S ® Stmt $
2. Stmt ® if expr then Stmt V
3. | other
4. V ® else Stmt
5. | !
3/26/2024 © All rights reserved by Chung Yung. 14
3
Shift-Reduce Parsers
6.1 – 6.2
Bottom-Up Parsers
• A bottom-up parser begins with the parse tree’s leaves and moves
toward its root.
• A bottom-up parser traces a rightmost derivation in reverse order.
• A bottom-up parser uses a grammar rule to replace the RHS with its
LHS.
3/26/2024 © All rights reserved by Chung Yung. 16
An Example
3/26/2024 © All rights reserved by Chung Yung. 17
Resembles Knitting
3/26/2024 © All rights reserved by Chung Yung. 18
Complete Steps
3/26/2024 © All rights reserved by Chung Yung. 19
Shift-Reduce Operations
• A shift operation transfers a symbol from the right needle to the left
needle.
• When a reduction by the rule A → γ is to performed, reduction by A
→ γ, removes the symbol in γ and prepends the LHS symbol A to the
unprocessed input of the right needle.
3/26/2024 © All rights reserved by Chung Yung. 20
Driver for Bottom-Up Parsing
3/26/2024 © All rights reserved by Chung Yung. 21
An Example
3/26/2024 © All rights reserved by Chung Yung. 22
Parse Table
3/26/2024 © All rights reserved by Chung Yung. 23
Parsing a b b d c $
3/26/2024 © All rights reserved by Chung Yung. 24
4
LR(0) Parsing
6.3
LR(0) Items
• An LR(0) item is a grammar production with a bookmark (a big dot)
that indicates the current progress through the production’s RHS.
LR(0) Item Meaning Next Remark
E → ● plus E E Beginning of rule plus beginning item
E → plus ● E E Processed a plus E
E → plus E ● E Processed a plus and a E E
E → plus E E ● Ready to reduce ⍊ reduce item
3/26/2024 © All rights reserved by Chung Yung. 26
LR(0) Expansion
• If there are k symbols on the Steps:
RHS, the production has k+1 • The initial item
LR(0) items. • Need to expand?
• If the next symbol is a • Make it a state and give it a
nonterminal A, it needs to number
expand: (Single circle or double circle?)
The beginning item of each A’s • Number of outgoing edges?
production is copied.
3/26/2024 © All rights reserved by Chung Yung. 27
LR(0) Computation
3/26/2024 © All rights reserved by Chung Yung. 28
An LR(0) Parse Table
3/26/2024 © All rights reserved by Chung Yung. 29
Parsing plus plus num num num $
0 Shift 1 plus plus num num num $
0 plus 1 Shift 1 plus num num num $
0 plus 1 plus 1 Shift 2 num num num $
0 plus 1 plus 1 num 2 Reduce 3 num num $
0 plus 1 plus 1 Shift 5 E num num $
0 plus 1 plus 1 E 5 Shift 2 num num $
0 plus 1 plus 1 E 5 num 2 Reduce 3 num $
0 plus 1 plus 1 E 5 Shift 6 E num $
0 plus 1 plus 1 E 5 E 6 Reduce 2 num $
0 plus 1 Shift 5 E num $
0 plus 1 E 5 Shift 2 num $
0 plus 1 E 5 num 2 Reduce 3 $
0 plus 1 E 5 Shift 6 E$
0 plus 1 E 5 E 6 Reduce 2 $
0 Shift 3 E$
0E3 Shift 4 $
0E3$4 Reduce 1 $
0 Accept Start $
3/26/2024 © All rights reserved by Chung Yung. 30
LR(0) Construction And Closure
3/26/2024 © All rights reserved by Chung Yung. 31
CompleteTable And TryRuleInState
3/26/2024 © All rights reserved by Chung Yung. 32
Conflicts
A parse table conflict arises when the table construction
method cannot decide between multiple alternatives for
some table-cell entry.
• Shift/reduce conflicts
• Reduce/reduce conflicts
• An ambiguous grammar (textbook pp. 232)
• Not LR(k) (textbook pp. 234)
3/26/2024 © All rights reserved by Chung Yung. 33
Non-LR(0)
3/26/2024 © All rights reserved by Chung Yung. 34
Parsing num plus num times num $
3/26/2024 © All rights reserved by Chung Yung. 35
5
Quiz #4
Exercise
Quiz #4 Exercise
• Show the process of bottom-up
parsing for
a b c d $
(Reference Slide 13)
3/26/2024 © All rights reserved by Chung Yung. 37
Quiz #4 Parse Table
3/26/2024 © All rights reserved by Chung Yung. 38