Unit-5 Top Down Parsing
Unit-5 Top Down Parsing
Unit – 5
Top Down Parsing
Topics to be covered
Looping
• Classification of parsing
• Backtracking
• LL(1) parsing
• Recursive Descent Paring
1
2/2/2024
Parsing
Parsing is a technique that takes input string and produces output either a parse tree if string is
valid sentence of grammar, or an error message indicating that string is not a valid.
Types of parsing are:
1. Top down parsing: In top down parsing parser build parse tree from top to bottom.
2. Bottom up parsing: Bottom up parser starts from leaves and work up to the root.
Parsing without
backtracking (predictive LR parsing
parsing)
LR (0)
LL(1) SLR
Recursive CLR
descent LALR
2
2/2/2024
Backtracking
If one production of a non-terminal fails in deriving the input string. The parser has to go back
to the position where it has chosen the production. And start deriving the string again using
another production of the same nonterminal. This process may need repeated scans over the
input string and we refer to it as backtracking.
Backtracking parsers are a type of top-down parser that can handle non-deterministic grammar.
When a parsing decision leads to a dead end, the parser can backtrack and try another
alternative. Backtracking parsers are not as efficient as other top-down parsers because they
can potentially explore many parsing paths.
Backtracking
In backtracking, expansion of nonterminal symbol we choose one alternative and if any
mismatch occurs then we try another alternative.
S S S
c A d c A d c A d
Make prediction Make prediction
3
2/2/2024
Backtracking
Key Steps for Backtracking in a Top-Down Parser:
1. Choose a non-terminal: At each step, the parser chooses a non-terminal from the current production
rule to expand.
2. Apply a production: The parser selects a production rule for the chosen non-terminal that matches
the current input. If multiple choices are available, it may need to try each alternative.
3. Recursive expansion: The chosen production is recursively expanded by applying the rules to its
non-terminals. This process continues until a terminal symbol is reached or until further expansion is
not possible.
4. Backtrack on failure: If a selected production fails to match the input, the parser backtracks to the
previous decision point, undoing the previous expansion and selecting an alternative choice if
available.
5. Repeat until success or failure: The parser repeats the above steps, trying different alternatives
and backtracking as necessary until it either successfully matches the entire input or exhausts all
possible alternatives, resulting in a parsing error.
Backtracking
Advantages of Backtracking:
Disadvantages of Backtracking
1. Performance Impact: Backtracking can lead to inefficient parsing, particularly in cases where there
are numerous backtracking points or long ambiguous sequences in the input. In such scenarios,
alternative parsing techniques may be more efficient.
2. Complexity: Managing backtracking points and tracking alternative choices can introduce additional
complexity to the parsing algorithm, requiring careful implementation and optimization.
4
2/2/2024
Backtracking
then A-->y is chosen which is false So backtrack to A and verify other options (1st backtrack).
then A-->z is chosen which is false So backtrack to A and verify other options (2nd backtrack).
Since all options of A are verified now backtrack to S and choose S-->rB. (3rd backtrack )
then B-->zzd is chosen which is false So backtrack to B and verify other options (4th backtrack).
So total 4 backtracks
Parsing Methods
Parsing
Parsing without
backtracking (predictive LR parsing
parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
5
2/2/2024
a + b $ INPUT
X
Predictive
Y
Stack parsing OUTPUT
Z program
$
Parsing table M
6
2/2/2024
7
2/2/2024
A → 𝛼𝐵𝛽
𝛽 𝑖𝑠 𝑎𝑏𝑠𝑒𝑛𝑡 𝛽 𝑖𝑠 𝑝𝑟𝑒𝑠𝑒𝑛𝑡
8
2/2/2024
First(S) S a B a Rule 1
SaBa A 𝛼 add 𝛼 to 𝐹𝐼𝑅𝑆𝑇(𝐴)
FIRST(S)={ a }
First(B)
BbB B𝜖
B b B B 𝜖
Rule 1
A 𝛼 A 𝜖
add 𝛼 to 𝐹𝐼𝑅𝑆𝑇(𝐴) Rule 2
add 𝜖 to 𝐹𝐼𝑅𝑆𝑇(𝐴)
FIRST(B)={ b , 𝜖 }
9
2/2/2024
Follow(B)
SaBa BbB
S a B a Rule 2 B b B Rule 3
A 𝛂 B 𝛃 First(β) − 𝜖 A 𝛂 B Follow(A)=follow(B)
Follow(B)={ a }
NT Input Symbol
a b $
S SaBa
B
SaBa
Rule: 2
a=FIRST(aBa)={ a } A 𝛼
a = first(𝛼)
M[S,a]=SaBa M[A,a] = A 𝛼
10
2/2/2024
NT Input Symbol
a b $
S SaBa
B BbB
BbB
Rule: 2
a=FIRST(bB)={ b } A 𝛼
a = first(𝛼)
M[B,b]=BbB M[A,a] = A 𝛼
NT Input Symbol
a b $
S SaBa Error Error
B Bϵ BbB Error
Bϵ
Rule: 3
b=FOLLOW(B)={ a } A 𝛼
b = follow(A)
M[B,a]=B𝜖 M[A,b] = A 𝛼
11
2/2/2024
S a B S 𝜖
Rule 1 Rule 2
A 𝛼 add 𝛼 to 𝐹𝐼𝑅𝑆𝑇(𝐴) A 𝜖 add 𝜖 to 𝐹𝐼𝑅𝑆𝑇(𝐴)
FIRST(S)={ a , 𝜖 }
B b C B 𝜖
Rule 1 Rule 2
A 𝛼 add 𝛼 to 𝐹𝐼𝑅𝑆𝑇(𝐴) A 𝜖 add 𝜖 to 𝐹𝐼𝑅𝑆𝑇(𝐴)
FIRST(B)={ b , 𝜖 }
12
2/2/2024
C c S C 𝜖
Rule 1 Rule 2
A 𝛼 add 𝛼 to 𝐹𝐼𝑅𝑆𝑇(𝐴) A 𝜖 add 𝜖 to 𝐹𝐼𝑅𝑆𝑇(𝐴)
FIRST(B)={ c , 𝜖 }
13
2/2/2024
N Input Symbol
T a b c $
S SaB
B
C
SaB Rule: 2
A 𝛼
a=FIRST(aB)={ a } a = first(𝛼)
M[S,a]=SaB M[A,a] = A 𝛼
N Input Symbol
T a b c $
S SaB S 𝜖
B
C
S𝜖 Rule: 3
A 𝛼
b=FOLLOW(S)={ $ } b = follow(A)
M[S,$]=S𝜖 M[A,b] = A 𝛼
14
2/2/2024
N Input Symbol
T a b c $
S SaB S 𝜖
B BbC
C
BbC Rule: 2
A 𝛼
a=FIRST(bC)={ b } a = first(𝛼)
M[B,b]=BbC M[A,a] = A 𝛼
N Input Symbol
T a b c $
S SaB S 𝜖
B BbC B𝜖
C
B𝜖 Rule: 3
A 𝛼
b=FOLLOW(B)={ $ } b = follow(A)
M[B,$]=B𝜖 M[A,b] = A 𝛼
15
2/2/2024
N Input Symbol
T a b c $
S SaB S 𝜖
B BbC B𝜖
C CcS
CcS Rule: 2
A 𝛼
a=FIRST(cS)={ c } a = first(𝛼)
M[C,c]=CcS M[A,a] = A 𝛼
N Input Symbol
T a b c $
S SaB Error Error S 𝜖
B Error BbB Error B𝜖
C Error Error CcS C𝜖
C𝜖 Rule: 3
A 𝛼
b=FOLLOW(C)={ $ } b = follow(A)
M[C,$]=C𝜖 M[A,b] = A 𝛼
16
2/2/2024
NT First
First(T) E { (,id }
T F T’ Rule 3
TFT’ E’
A Y1 Y2 First(A)=First(Y1)
T { (,id }
FIRST(T)=FIRST(F)= {(, id } T’
First(F) F { (,id }
F(E) Fid
F ( E ) F id
A 𝛼 Rule 1 A 𝛼 Rule 1
add 𝛼 to 𝐹𝐼𝑅𝑆𝑇(𝐴) add 𝛼 to 𝐹𝐼𝑅𝑆𝑇(𝐴)
FIRST(F)={ ( , id }
17
2/2/2024
E’𝜖 E’ { +, 𝜖 }
T { (,id }
T’
E’ 𝜖 Rule 2
F { (,id }
A 𝜖 add 𝜖 to 𝐹𝐼𝑅𝑆𝑇(𝐴)
FIRST(E’)={ + , 𝜖 }
T’𝜖 E’ { +, 𝜖 }
T { (,id }
T’ { *, 𝜖 }
T’ 𝜖 Rule 2
F { (,id }
A 𝜖 add 𝜖 to 𝐹𝐼𝑅𝑆𝑇(𝐴)
FIRST(T’)={ * , 𝜖 }
18
2/2/2024
FOLLOW(E)={ $, ) }
FOLLOW(E’)={ $,) }
19
2/2/2024
FOLLOW(T)={ +, $, )
FOLLOW(T)={ +, $, ) }
20
2/2/2024
FOLLOW(T’)={+ $,) }
FOLLOW(F)={ *, + ,$ , )
21
2/2/2024
FOLLOW(F)={ *,+, $, ) }
22
2/2/2024
23
2/2/2024
24
2/2/2024
25
2/2/2024
26
2/2/2024
27
2/2/2024
28
1
2
3
4
5
6
7