0% found this document useful (0 votes)
2 views54 pages

19CSE401 CD 3.2 TopDownParsers

The document discusses top-down parsing techniques, focusing on LL(1) grammars and predictive parsing. It covers concepts such as eliminating left recursion, left factoring, and computing first and follow sets, along with examples and rules for constructing a recursive descent parser. Additionally, it outlines steps for implementing a table-driven LL(1) parser and provides parsing examples using specific grammars.

Uploaded by

vsreevanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views54 pages

19CSE401 CD 3.2 TopDownParsers

The document discusses top-down parsing techniques, focusing on LL(1) grammars and predictive parsing. It covers concepts such as eliminating left recursion, left factoring, and computing first and follow sets, along with examples and rules for constructing a recursive descent parser. Additionally, it outlines steps for implementing a table-driven LL(1) parser and provides parsing examples using specific grammars.

Uploaded by

vsreevanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

19CSE401 – Compiler Design

Top Down Parsers


Chapter 3

Dept. of Computer Science and Engineering

Amrita School of Computing, Bengaluru


Amrita Vishwa Vidyapeetham
The Classic Expression Grammar
Top-down Parsing

29-08-2025 3
Top-down Parsing

29-08-2025 4
Eliminating Left Recursion

29-08-2025 5
Predictive Parsing
• If a top-down parser picks the “wrong” production, it may need to
backtrack.

• The alternative is to look ahead in the input & use context to pick
the correct production.

• How much lookahead is needed for a context-free grammar?


– In general, an arbitrarily large amount

• Fortunately,
– Large subclasses of CFGs can be parsed with limited lookahead
– Most programming language constructs fall in those subclasses
– Among the interesting subclasses are LL(1) grammars
29-08-2025
• We will focus, for now, on LL(1) grammars & predictive parsing. 6
Predictive Parsing

29-08-2025 7
Predictive Parsing

29-08-2025 8
Predictive Parsing

29-08-2025 9
Left Factoring

Left Factoring is required to remove backtracking


29-08-2025 10
Left Factoring Example

29-08-2025 11
Left Factoring Example

29-08-2025 12
Perform left factoring for the following
grammar:
E→E+T|E–T|T
T→T*F|T/F|F
F→F^2 |X
X → (E) | num

E→EY|T
Y→ +T|-T

T→TZ|F
Z→ *F|/F
F→F^2 |X
X → (E) | num

29-08-2025 13
Perform left factoring for the following grammar:
S → if C then E | if C then E else E
C → true | false
E → num | id

S → if C then E Q
Q → else E | ϵ
C → true | false
E → num | id

29-08-2025 14
First Set

EOF is the End Of File

29-08-2025 15
Lets Learn the Rules with the help of Examples
Lets find out the First sets for SheepNoise Grammar:

29-08-2025 16
SheepNoise Example

An iterative process

29-08-2025 17
Another Example
(Parentheses Grammar)
$

29-08-2025 18
Another Example
(Parentheses Grammar)
$

EOF is $

29-08-2025 19
Rules for First Sets
1.If X is a terminal then First(X) is just X
2.If there is a Production X → ε then add ε to first(X)
3.If there is a Production X → Y1Y2..Yk then add first(Y1Y2..Yk) to first(X)
4.First(Y1Y2..Yk) is either
1.First(Y1) (if First(Y1) doesn't contain ε)
2.OR (if First(Y1) contains ε) then First (Y1Y2..Yk) is everything in First(Y1)
<except for ε > as well as everything in First(Y2..Yk)
3.If First(Y1) First(Y2)..First(Yk) all contain ε then add ε to First(Y1Y2..Yk) as well.

29-08-2025 20
Lets find the First Set for Classic Expression Grammar
$

EOF is $

29-08-2025 First Set 21


Follow Set

29-08-2025 22
Follow Set

29-08-2025 23
Rules for Follow Sets

1.First put $ (the end of input marker) in Follow(S) (S is the start


symbol)
2.If there is a production A → αBβ, (where α can be a whole
string) then everything in FIRST(β) except for ε is placed in FOLLOW(B).
3.If there is a production A → αB, then everything in FOLLOW(A) is in
FOLLOW(B)
4.If there is a production A → αBβ, where FIRST(β) contains ϵ,
then everything in FOLLOW(A) is in FOLLOW(B)

29-08-2025 24
Follow Set
(Parentheses Grammar)
$

EOF is $

29-08-2025 25
Classic Expression Grammar
$

EOF is $

29-08-2025 26
Find the First and Follow sets for the given grammar.
S → aABbCD |ϵ
A → ASd |ϵ | y
B → SbC | Ch |ϵ
C → SDf|e g |ϵ
D → pBD |q
Non Nullable First Follow
Terminal

S Yes ϵa $dbpq
A Yes ϵyad abpqehd
B Yes ϵabpqeh bpq
C Yes ϵapqe pqbh
D pq $dbpqf

29-08-2025 Note: Can Remove left recursion and then find First and Follow sets 27
Find the First and Follow sets for the given grammar.

Non Nullable First Follow


Terminal

S ac $ac
A yes ϵa c
B yes ϵb $ac

29-08-2025 28
Implementing a Recursive Descent Parser
• A recursive-descent parser is structured as a set of
mutually recursive procedures, one for each nonterminal
in the grammar.
• The procedure corresponding to nonterminal A
recognizes an instance of A in the input stream.

• To recognize a nonterminal B on some right-hand side for


A, the parser invokes the procedure corresponding to B.

• Thus, the grammar itself serves as a guide to the parser’s


implementation.
29-08-2025 29
Lets create a recursive Descent Parser for the classic
Expression Grammar
The grammar is: $

29-08-2025 30
$
Recursive Descent Parser for the
classic Expression Grammar

29-08-2025 31
Recursive Descent Parser for the classic
Expression Grammar

29-08-2025 32
$
Recursive Descent Parser for the
classic Expression Grammar

When first symbol is ϵ


then use Follow set
symbols to process
further

29-08-2025 33
Recursive Descent Parser for the classic Expression
Grammar

29-08-2025 34
Recursive Descent Parser for the classic
When first symbol is ϵ
Expression Grammar then use Follow set
symbols to process
further

29-08-2025 35
Recursive Descent Parser for the
classic Expression Grammar

name is id here

29-08-2025 36
Find the First and Follow sets of the given grammars

1) S → A
A → aB | Ad
B→b
C→g

2) S → aBDh
B → xC
C → bC | Є
D → EF
E → g | Є
F → f | Є

29-08-2025 37
Solutions

1) The given grammar is left recursive.


So, we first remove left recursion from the given grammar.
After eliminating left recursion, we get the following grammar- S→A
S→A A → aB | Ad
A → aBA’ B→b
A’ → dA’ | ∈ C→g
B→b
C→g

•First(S) = First(A) = { a } •Follow(S) = { $ }


•First(A) = { a } •Follow(A) = Follow(S) = { $ }
•First(A’) = { d , ∈ } •Follow(A’) = Follow(A) = { $ }
•First(B) = { b } •Follow(B) = { First(A’) – ∈ } ∪ Follow(A) = { d , $ }
•First(C) = { g } •Follow(C) = NA

29-08-2025 38
Solutions
2) S → aBDh
FIRST set B → xC
FIRST(S) = { a }
C → bC | Є
FIRST(B) = { x }
FIRST(C) = { b , Є }
D → EF
FIRST(D) = FIRST(E) U FIRST(F) = { g, f, Є } E → g | Є
FIRST(E) = { g , Є } F → f | Є
FIRST(F) = { f , Є }

FOLLOW Set
FOLLOW(S) = { $ }
FOLLOW(B) = { FIRST(D) – Є } U FIRST(h) = { g , f , h }
FOLLOW(C) = FOLLOW(B) = { g , f , h }
FOLLOW(D) = FIRST(h) = { h }
FOLLOW(E) = { FIRST(F) – Є } U FOLLOW(D) = { f , h }
FOLLOW(F) = FOLLOW(D) = { h }

39
29-08-2025
Table Driven Top down Parser
LL(1)

• The first L denotes scanning from Left to right for Parsing


• The second L denotes Left Most Derivation
• No. of Lookahead symbols are 1

29-08-2025 40
Steps of LL(1) Parsing:

1. Augment the given grammar


2. Remove left recursion
3. Apply Left factoring
4. Compute First and Follow sets
5. Construct the parsing table
6. Parse the string using stack

29-08-2025 41
Construct the LL(1) Parsing table for the given grammar and parse the string id + id * id.
Trace using stack.
E → TE1
E1 → + T E1 |- T E1 | ϵ
T → F T1
T1 → * F T1 | / F T1 | ϵ
F → (E) | id | num
Step1: Augmentation Step 2: Left recursion removal
0 G→E$ No left recursion
1 E → TE1
2 E1 → + T E1
3 |- T E1
Step 3: Left Factoring
4 |ϵ No left factoring required
5 T → F T1
6 T1 → * F T 1
7 | / F T1
8 |ϵ
9 F → (E)
10 | id
11 | num
42
29-08-2025
Step 4: Compute First and Follow sets
Non Null First Follow
0 G→E$
Terminal able
1 E → TE1
2 E1 → + T E1 G ( id num $
3 |- T E1
4 |ϵ E ( id num $ )
5 T → F T1 E1 Yes ϵ+- $ )
6 T1 → * F T 1
7 | / F T1 T ( id num +-$ )
8 |ϵ
9 F → (E) T1 Yes ϵ*/ +-$ )
10 | id F ( id num */+-$ )
11 | num All the blank
entries in
Step 5: Construct the LL(1) parsing table
LL(1) Parsing
NT T + - * / ( ) id num $ table are error
G 0 0 0
E 1 1 1
E1 2 3 4 4
T 5 5 5
T1 8 8 6 7 8 8
29-08-2025 F 9 10 11 43
Stack Input Action Step 6: Parse id + id * id $
G id + id * id $ G → E $ Push using LL(1) table and trace using stack
$E id + id * id $ E → TE1 Push
$ E1 T id + id * id $ T → F T1 Push 0 G→E$
$ E1 T1 F id + id * id $ F → id Push 1 E → TE1
2 E1 → + T E1
$ E1 T1 id id + id * id $ Match Pop
3 |- T E1
$ E1 T1 + id * id $ T1 → ϵ 4 |ϵ
$ E1 + id * id $ E1 → + T E1 5 T → F T1
+ id * id $ 6 T1 → * F T 1
$ E1 T + Match Pop 7 | / F T1
$ E1 T id * id $ T → F T1 Push 8 |ϵ
$ E1 T1 F id * id $ F → id Push 9 F → (E)
10 | id
$ E1 T1 id id * id $ Match Pop
11 | num
$ E1 T1 * id $ T1 → * F T1
$ E1 T1 F * * id $ Match Pop
$ E1 T1 F id $ F → id Push
When the input $ and Stack also contains $, then
$ E1 T1 id id $ Match Pop parsing is complete and successful
$ E1 T1 $ T1 → ϵ
$ E1 $ E1 → ϵ
29-08-2025 44
$ $ Accept
Step 6: Parse (id +* id) $
using LL(1) table and trace using stack
Stack Input Action
G (id +* id) $ G → E $ Push 0 G→E$
$E (id +* id) $ E → TE1 Push 1 E → TE1
$ E1 T (id + *id) $ T → F T1 Push 2 E1 → + T E1
3 |- T E1
$ E1 T1 F (id +* id) $ F → (E) Push 4 |ϵ
$ E1 T1 ) E ( (id +* id) $ Match Pop 5 T → F T1
$ E1 T1 ) E id +* id) $ E → TE1 Push 6 T1 → * F T 1
7 | / F T1
$ E1 T1 ) E1 T id +* id) $ T → F T1 8 |ϵ
$ E1 T1 ) E1 T1 F id +* id) $ F → id 9 F → (E)
$ E1 T1 ) E1 T1 id +* id) $ Match Pop 10 | id
11 | num
$ E1 T1 ) E1 T1 +* id) $ T1 → ϵ
$ E1 T1 ) E1 +* id) $ E1 → + T E1
$ E1 T1 ) E1 T + * id $ Match Pop
$ E1 T1 ) E1 T * id $ Error Parsing is unsuccessful and the given sentence
is not accepted

29-08-2025 45
LL(1) Grammars
A grammar is said to be LL(1) if there exists atmost one entry in the
LL(1) parsing table.

If there are more than one entry in any cell, there exists conflict and
the grammar is not suitable for LL(1) parsing.

29-08-2025 46
Check whether the given grammar is suitable for LL(1) parsing. Using Parsing table for the given grammar parse the string
ahfbf and show the tracing using stack.

S → aABbCD |ϵ
A → ASd |ϵ
B → SaC | hC |ϵ
C → Sf|Cg
D → aBD | ϵ
Step1: Augmentation Step 2: Left recursion removal Step 3: Left Factoring
0 G→S$ 0 G→S$ No left factoring required
S → aABbCD 1 S → aABbCD
S→ϵ 2 S→ϵ
A → Asd 3 A → A1
A→ϵ 4 A1 → Sd A1
B → SaC 5 A1 → ϵ
B → hC 6 B → SaC
B→ϵ 7 B → hC
C → Sf 8 B→ϵ
C → Cg 9 C → S f C1
D → aBD 10 C1 → g C1
D→ϵ 11 C1 → ϵ
12 D → aBD
29-08-2025 13 D → ϵ 47
Non Nulla First Follow Step 4: Compute First and Follow sets 0 G→S$
Terminal ble
1 S → aABbCD
G a$ $ 2 S→ϵ
S Yes aϵ $daf 3 A → A1
4 A1 → Sd A1
A Yes ϵad ahb
5 A1 → ϵ
A1 Yes ϵad ahb 6 B → SaC
B Yes ϵah ba$df 7 B → hC
8 B→ϵ
C afϵ $dafb
9 C → S f C1
C1 Yes ϵg $dafb 10 C1 → g C1
D Yes ϵa $daf 11 C1 → ϵ
12 D → aBD
NT T a b d f g h $ 13 D → ϵ
Step 5: Construct the
LL(1) parsing table G 0 0
S 1, 2 2 2 2
This grammar is not LL(1)
A 3 3 3 3
parsing
A1 4,5 5 4 5
B 6, 8 8 8 8 7 8
C 9 9 9 9 9
C1 11 11 11 11 10 11
29-08-2025 48
D 12,13 13 13 13
Step 6: Parse ahfbf$ using parsing table and trace using stack 0 G→S$
1 S → aABbCD
Stack Input Action 2 S→ϵ
G ahfbf $ G → S $ Push 3 A → A1
$S ahfbf $ Conflict between 1 or 2 4 A1 → sd A1
Select rule 1 5 A1 → ϵ
6 B → SaC
$DCbBAa ahfbf $ Match pop 7 B → hC
$DCbBA hfbf$ 8 B→ϵ
9 C → S f C1
10 C1 → g C1
11 C1 → ϵ
12 D → aBD
NT T a b d f g h $
13 D → ϵ
G 0 0
S 1, 2 2 2 2
A 3 3 3 3
A1 4,5 5 4 5
B 6, 8 8 8 8 7 8
C 9 9 9 9 9
C1 11 11 11 11 10 11
29-08-2025 49
D 12,13 13 13 13
Check whether the given grammar is LL(1) grammar. Using Parsing table for the given grammar parse the string (a, a)
and trace using stack.
S → a | ^ | (T)
T → T , S |S
Step1: Augmentation
0 G→S$
S→a
S→^
S → (T)
T→T,S
T→S

Step 2: Left recursion removal Step 3: Left Factoring


0 G→S$ No left factoring required
1 S→a
2 S→^
3 S → (T)
4 T → S T1
5 T1 → , S T1
6 T1 → ϵ

29-08-2025 50
Step 4: Compute First and Follow sets
Non Null First Follow
0 G→S$ Terminal able
1 S→a
2 S→^
G a^( $
3 S → (T) S a^( $,)
4 T → S T1
5 T1 → , S T1 T a^( )
6 T1 → ϵ
T1 Yes ϵ , )
Step 5: Construct the LL(1) parsing table

NT T a ^ , ( ) $
G 0 0 0 This grammar is suitable for LL(1) parsing
S 1 2 3
T 4 4 4
T1 5 6

29-08-2025 51
Step 6: Parse (a, a) $ using LL(1) table and trace using stack 0 G→S$
1 S→a
Stack Input Action 2 S→^
G (a, a) $ G → S $ Push 3 S → (T)
4 T → S T1
$S (a, a) $ S → (T) 5 T1 → , S T1
$) T ( (a, a) $ Match pop 6 T1 → ϵ
$) T a, a) $ T → S T1
$) T1 S a, a) $ S→a
$) T1 a a, a) $ Match pop
$) T1 , a) $ T1 → , S T1 NT a ^ , ( ) $
$) T1 S , , a) $ Match pop T

$) T1 S a) $ S→a G 0 0 0
$) T1 a a) $ Match pop S 1 2 3
$) T1 )$ T1 → ϵ T 4 4 4
$) )$ Match pop T1 5 6
$ $ Accept

The string is parsed successfully.


29-08-2025 52
Strength of Parsing technique:
LL(1) < LL(2) < LL(3) …… LL(k)

29-08-2025 53
Exercises: Check whether the following grammars are suitable for LL(1) parsing.

1)

2) E -> T + E
E -> T
T -> x

29-08-2025 54

You might also like