0% found this document useful (0 votes)
13 views69 pages

2-Role of Parser and Parse Tree-02!08!2024

Module 2 covers syntax analysis, also known as parsing, which involves generating a parse tree from tokens produced by a lexical analyzer. It discusses context-free grammar (CFG), including its components such as non-terminals, terminals, start symbols, and productions, as well as derivation methods and ambiguity in grammar. The module also explores various parsing techniques, including top-down and bottom-up parsing, and introduces LL(1) parsing as a predictive parsing method.

Uploaded by

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

2-Role of Parser and Parse Tree-02!08!2024

Module 2 covers syntax analysis, also known as parsing, which involves generating a parse tree from tokens produced by a lexical analyzer. It discusses context-free grammar (CFG), including its components such as non-terminals, terminals, start symbols, and productions, as well as derivation methods and ambiguity in grammar. The module also explores various parsing techniques, including top-down and bottom-up parsing, and introduces LL(1) parsing as a predictive parsing method.

Uploaded by

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

Module 2 – Syntax Analysis

Syntax analysis
• Syntax Analysis is also called Parsing or Position = initial + rate*60
Hierarchical Analysis.
• It takes token produced by lexical analyzer Lexical analysis

as Input & generates the parse tree. id1 = id2 + id3 *


60
• Matching of parenthesis.
Syntax analysis
• The syntax analyzer checks each line of the
code and spots every tiny mistake. =
• If code is error free then syntax analyzer +
id1
generates the tree.
id2 *
• Two types of Parser :
1. Top-down parser id3 60

2. Bottom-up parser
Context free grammar
• A context free grammar (CFG) is a 4-tuple where,
is finite set of non terminals,
is disjoint finite set of terminals,
is an element of and it’s a start symbol,
is a finite set formulas of the form where and

 Nonterminal symbol:
 The name of syntax category of a language, e.g., noun, verb, etc.
 The It is written as a single capital letter, or as a name enclosed between < … >, e.g., A
or <Noun> <Noun Phrase> → <Article><Noun>
<Article> → a | an | the
<Noun> → boy | apple | doctor
Context free grammar
• A context free grammar (CFG) is a 4-tuple where,
is finite set of non terminals,
is disjoint finite set of terminals,
is an element of and it’s a start symbol,
is a finite set formulas of the form where and

 Terminal symbol:
 A symbol in the alphabet.
 It is denoted by lower case letter and punctuation marks used in language.
<Noun Phrase> → <Article><Noun>
<Article> → a | an | the
<Noun> → boy | apple | doctor
Context free grammar
• A context free grammar (CFG) is a 4-tuple where,
is finite set of non terminals,
is disjoint finite set of terminals,
is an element of and it’s a start symbol,
is a finite set formulas of the form where and

 Start symbol:
 First nonterminal symbol of the grammar is called start symbol.

<Noun Phrase> → <Article><Noun>


<Article> → a | an | the
<Noun> → boy | apple | doctor
Context free grammar
• A context free grammar (CFG) is a 4-tuple where,
is finite set of non terminals,
is disjoint finite set of terminals,
is an element of and it’s a start symbol,
is a finite set formulas of the form where and

 Production:
 A production, also called a rewriting rule, is a rule of grammar. It has the form of
A nonterminal symbol → String of terminal and nonterminal symbols
<Noun Phrase> → <Article><Noun>
<Article> → a | an | the
<Noun> → boy | apple | doctor
Example: Grammar
Write terminals, non terminals, start symbol, and productions
for following grammar.
E  E O E| (E) | -E | id
O+|-|*|/ |↑
Terminals: id + - * / ↑ ( )
Non terminals: E, O

Start symbol: E

Productions: E  E O E| (E) | -E | id
O+|-|*|/ |↑
Derivation &
Ambiguity
Derivation
• Apply the productions of CFG to find the set of strings that can
be formed from that grammar
• Derivation is used to find whether the string belongs to a given
grammar or not.
• Types of derivations are:
1. Leftmost derivation
2. Rightmost derivation
Leftmost derivation
• A derivation of a string ‘w’ in a grammar is a left most
derivation if at every step the left most non terminal is
replaced.
• Grammar: SS+S | S-S | S*S | S/S | a Output string: a*a-a

S
Parse tree represents the
S structure of derivation S - S
S-S
S * S a
S*S-S

a*S-S a a
a*a-S Parse tree

a*a-a
Leftmost Derivation
Rightmost derivation
• A derivation of a string ‘w’ in a grammar is a right most
derivation if at every step the right most non terminal is
replaced.
• It is all called canonical derivation.
• Grammar: SS+S | S-S | S*S | S/S | a Output string: a*a-a
S

S S * S

S*S
a S - S
S*S-S

S*S-a a a
Parse Tree
S*a-a
Rightmost Derivation
Exercise: Derivation
1. Perform leftmost derivation and draw parse tree.

A0A | 𝜖
SA1B

B0B | 1B | 𝜖
Output string: 1001
2. Perform leftmost derivation and draw parse tree.
S0S1 | 01 Output string: 000111
3. Perform rightmost derivation and draw parse tree.
EE+E | E*E | id | (E) | -E
Output string: id + id * id
Ambiguity
• Ambiguity, is a word, phrase, or statement which contains
more than one meaning.
• In formal language grammar, ambiguity would arise if identical
string can occur on the RHS of two or more productions.
• Grammar:
𝑵𝟏 𝑵𝟐 Replaced by
or ?

• can be derived from either N1 or N2


𝜶
Ambiguous grammar
• Ambiguous grammar is one that produces more than one
leftmost or more then one rightmost derivation for the same
sentence.
• Grammar: SS+S | S*S | (S) | a Output string: a+a*a
S S

S S * S S S + S
S*S S+S
S + S a a S * S
S+S*S a+S
a+S*S a a a+S*S a a
a+a*S a+a*S
a+a*a a+a*a
• Here, Two leftmost derivation for string a+a*a is possible
Exercise: Ambiguous Grammar
Check Ambiguity in following grammars:
1. S aS | Sa | 𝜖 (output string: aaaa)
2. S aSbS | bSaS | 𝜖 (output string: abab)
3. S SS+ | SS* | a (output string: aa+a*)
4. <exp> → <exp> + <term> | <term>
<term> → <term> * <letter> | <letter>
<letter> → a|b|c|…|z (output string: a+b*c)
5. Prove that the CFG with productions: S  a | Sa | bSS | SSb |
SbS is ambiguous (Hint: consider output string yourself)
Left recursion & Left
factoring
Left recursion
• A grammar is said to be left recursive if it has a non terminal
such that there is a derivation for some string

𝐴→𝐴𝛼∨β 
β 𝐴’

𝜖
𝛼 𝐴’
Examples: Left recursion elimination
EE+T | T
ETE’
E’+TE’ | ε
TT*F | F
TFT’
T’*FT’ | ε
XX%Y | Z
XZX’
X’%YX’ | ε
Exercise: Left recursion
1. AAbd | Aa | a
BBe | b
2. AAB | AC | a | b
3. SA | B
AABC | Acd | a | aa
BBee | b
4. ExpExp+term | Exp-term | term
Left factoring
Left factoring is a grammar transformation that is useful for
producing a grammar suitable for predictive parsing.
SaAB | aCD
SaS’
S’AB | CD
A xByA | xByAzA | a

A xByAA’ | a
A’ Є | zA
A aAB | aA |a

A’AB | A | 𝜖
AaA’

A’AA’’ | 𝜖
A’’B | 𝜖
Exercise
1. SiEtS | iEtSeS | a
2. A ad | a | ab | abc | x
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.
Classification of parsing methods
Parsin
g

Top down Bottom up parsing (Shift


parsing reduce)
Back tracking Operator
precedence
Parsing without
backtracking LR parsing
(predictive
parsing) SLR
LL(1)
CLR
Recursi
ve LALR

descen
t
Backtracking
• In backtracking, expansion of nonterminal symbol we choose
one rule and if any mismatch occurs then we try another
alternative.
• Grammar: S cAd Input string: cad
A ab | a
S S S

c A d c A d c A d
Make prediction Make prediction

a b Backtrack a Parsing done


Exercise
1. E 5+T | 3-T
T V | V*V | V+V
V a | b
String: 3-a+b
Parsing Methods
Parsin
g

Top down Bottom up parsing (Shift


parsing reduce)
Back tracking Operator
precedence
Parsing without
backtracking LR parsing
(predictive
parsing) SLR
LL(1)
CLR
Recursi
ve LALR

descen
t
LL(1) parser (predictive parser)
• LL(1) is non recursive top down parser.
1. First L indicates input is scanned from left to right.
2. The second L means it uses leftmost derivation for input
string
3. 1 means it uses only input symbol to predict the parsing
process. a + b $ INPU
T
X
Y Predictiv
e parsing OUTPU
Z
Stack program T
$

Parsing table
M
LL(1) parsing (predictive parsing)
Steps to construct LL(1) parser
1. Remove left recursion / Perform left factoring (if any).
2. Compute FIRST and FOLLOW of non terminals.
3. Construct predictive parsing table.
4. Parse the input string using parsing table.
Example-1: LL(1) parsing
SaBa
NT First
BbB | ϵ S {a}
Step 1: Not required
B {b,𝜖}
Step 2: Compute FIRST
First(S) S  a B a
Rule 1
SaBa A  add to
FIRST(S)={
a}
First(B)
BbB B𝜖
B  b B B  𝜖
A  Rule 1
A 
add to Rule 2
,𝜖}
FIRST(B)={ add to
b
Example-1: LL(1) parsing
SaBa
NT First Follow

BbB | ϵ S {a} {$}

Step 2: Compute FOLLOW B {b,𝜖} {a}

Follow(S)
Rule 1: Place $ in FOLLOW(S)
Follow(S)={ $ }

Follow(B)
SaBa BbB
S  a B a Rule 2 B  b B
Rule 3
A  B First( A  B Follow(A)=follow(B)

Follow(B)={ a }
Example-1: LL(1) parsing
SaBa
NT First Follow
S {a} {$}
BbB | ϵ
B {b,𝜖} {a}
Step 3: Prepare predictive parsing table
NT Input Symbol
a b $
S SaBa
B

Rule: 2
SaBa A
a = first()
a=FIRST(aBa)={ a } M[A,a] = A

M[S,a]=SaBa
Example-1: LL(1) parsing
SaBa
NT First Follow
S {a} {$}
BbB | ϵ
B {b,𝜖} {a}
Step 3: Prepare predictive parsing table
NT Input Symbol
a b $
S SaBa
B BbB

Rule: 2
BbB A
a = first()
a=FIRST(bB)={ b } M[A,a] = A

M[B,b]=BbB
Example-1: LL(1) parsing
SaBa
NT First Follow
S {a} {$}
BbB | ϵ
B {b,𝜖} {a}
Step 3: Prepare predictive parsing table
NT Input Symbol
a b $
S SaBa Error Error
B Bϵ BbB Error

Rule: 3
Bϵ A
b = follow(A)

M[B,a]=B𝜖
b=FOLLOW(B)={ a } M[A,b] = A
Example-2: LL(1) parsing
SaB | ϵ

BbC | ϵ
CcS | ϵ
Step 1: Not required NT First
S { a, 𝜖 }
Step 2: Compute FIRST B {b,𝜖}
First(S) C {c,𝜖}

SaB S𝜖
S  a B S  𝜖
Rule 1 Rule 2
A  A 
add to add to

FIRST(S)={ a , 𝜖 }
Example-2: LL(1) parsing
SaB | ϵ

BbC | ϵ
CcS | ϵ
Step 1: Not required NT First
S { a, 𝜖 }
Step 2: Compute FIRST B {b,𝜖}
First(B) C {c,𝜖}

BbC B𝜖
B  b C B  𝜖
Rule 1 Rule 2
A  A 
add to add to

FIRST(B)={ b , 𝜖 }
Example-2: LL(1) parsing
SaB | ϵ

BbC | ϵ
CcS | ϵ
Step 1: Not required NT First

Step 2: Compute FIRST S { a, 𝜖 }


B {b,𝜖}
First(C)
C {c,𝜖}
CcS C𝜖
C  c S C  𝜖
Rule 1 Rule 2
A  A 
add to add to

FIRST(B)={ c , 𝜖 }
Example-2: LL(1) parsing
Step 2: Compute FOLLOW
Follow(S) Rule 1: Place $ in FOLLOW(S)
Follow(S)={ $ }

CcS SaB | ϵ

C  c S BbC | ϵ
Rule 3
A  B Follow(A)=follow(B) CcS | ϵ
Follow(S)=Follow(C) ={$}
NT First Follow
S {a,𝜖} {$}
BbC SaB B {b,𝜖} {$}
C {c,𝜖} {$}
B  b C S  a B
Rule 3 Rule 3
A  B Follow(A)=follow(B) A  B Follow(A)=follow(B)
Follow(C)=Follow(B) ={$} Follow(B)=Follow(S)={$}
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a,𝜖} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $

S SaB
B
C

Rule: 2
A
SaB a = first()
M[A,a] = A
a=FIRST(aB)={ a }
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $

S SaB S𝜖
B
C

Rule: 3
A
S𝜖 b = follow(A)
M[A,b] = A
b=FOLLOW(S)={ $ }
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $

S SaB S𝜖
B BbC
C

Rule: 2
A
BbC a = first()
M[A,a] = A
a=FIRST(bC)={ b }
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $

S SaB S𝜖
B BbC B𝜖
C

Rule: 3
A
B𝜖 b = follow(A)
M[A,b] = A
b=FOLLOW(B)={ $ }
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $

S SaB S𝜖
B BbC B𝜖
C CcS
Rule: 2
A
CcS a = first()
M[A,a] = A
a=FIRST(cS)={ c }
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $

S SaB Error Error S𝜖


B Error BbB Error B𝜖
C Error Error CcS C𝜖
Rule: 3
A
C𝜖 b = follow(A)
M[A,b] = A
b=FOLLOW(C)={ $ }
Example-3: LL(1) parsing
EE+T | T
TT*F | F
F(E) | id
Step 1: Remove left recursion
ETE’
E’+TE’ | ϵ
TFT’
T’*FT’ | ϵ
F(E) | id
Example-3: LL(1) parsing
Step 2: Compute FIRST ETE’
First(E) E  T E’ Rule 3
E’+TE’ | ϵ
First(A)=First(Y
A Y Y
ETE’  1 2
1)
TFT’
= {(,
FIRST(E)=FIRST(T)
id }
T’*FT’ | ϵ
NT First
EF(E){ (,id
| id}
First(T) T  F T’ Rule 3
First(A)=First(Y
E’
A  Y1 Y2 T { (,id }
TFT’ 1)
= {(,
FIRST(T)=FIRST(F) T’
id }
F { (,id }
First(F)
F  ( E ) F  id
F(E) A  Rule 1 Fid
A  Rule 1
add to add to
FIRST(F)={ ,
( id }
Example-3: LL(1) parsing
Step 2: Compute FIRST ETE’

First(E’) E’+TE’ | ϵ
E’+TE’ TFT’
E’  + T E’ Rule 1
add to T’*FT’ | ϵ
A  NT First
EF(E){ (,id }
{ +, 𝜖 }
| id
E’
T { (,id }
E’𝜖
T’
E’  Rule 2 F { (,id }
A  add to

FIRST(E’)={ + , 𝜖 }
Example-3: LL(1) parsing
Step 2: Compute FIRST ETE’

First(T’) E’+TE’ | ϵ
T’*FT’ TFT’
T’  * F T’ Rule 1
add to T’*FT’ | ϵ
A  NT First
EF(E){ (,id }
{ +, 𝜖 }
| id
E’
T { (,id }
T’𝜖
T’ { *, 𝜖 }
T’  Rule 2 F { (,id }
A  add to

FIRST(T’)={ *, 𝜖 }
Example-3: LL(1) parsing
Step 2: Compute FOLLOW ETE’

FOLLOW(E) E’+TE’ | ϵ
Rule 1: Place $ in FOLLOW(E) TFT’

F(E) NT First T’*FT’ |ϵ


Follow
F(E)
E { (,id }| id { $,) }
E’ { +, 𝜖 }
F  ( E ) Rule 2 T { (,id }
{ *, 𝜖 }
A  B
T’
F { (,id }

FOLLOW(E)={ $, ) }
Example-3: LL(1) parsing
ETE’
Step 2: Compute FOLLOW
E’+TE’ | ϵ
FOLLOW(E’) TFT’
ETE’
NT First T’*FT’ |ϵ
Follow
E  T E’ F(E)
Rule 3 E { (,id }| id { $,) }
{ +, 𝜖 } { $,) }
A  B
E’
T { (,id }
T’ { *, 𝜖 }
E’+TE’ F { (,id }
E’  +T E’ Rule 3
A  B

FOLLOW(E’)={ $,) }
Example-3: LL(1) parsing
Step 2: Compute FOLLOW ETE’

FOLLOW(T) E’+TE’ | ϵ
TFT’
ETE’
NT First T’*FT’ |ϵ
Follow
E  T E’ F(E)
Rule 2 E { (,id }| id { $,) }
{ +, 𝜖 } { $,) }
A  B
E’
T { (,id }
T’ { *, 𝜖 }
F { (,id }
E  T E’ Rule 3
A  B

FOLLOW(T)={ +, $, )
Example-3: LL(1) parsing
Step 2: Compute FOLLOW ETE’

FOLLOW(T) E’+TE’ | ϵ
TFT’
E’+TE’
NT First T’*FT’ |ϵ
Follow
E’  + T E’ F(E)
Rule 2 E { (,id }| id { $,) }
{ +, 𝜖 } { $,) }
A  B
E’
T { (,id } { +,$,) }
T’ { *, 𝜖 }
F { (,id }
E’  + T E’ Rule 3
A  B

FOLLOW(T)={ +, $, ) }
Example-3: LL(1) parsing
Step 2: Compute FOLLOW ETE’

FOLLOW(T’) E’+TE’ | ϵ
TFT’
TFT’
NT First T’*FT’ |ϵ
Follow
T  F T’ F(E)
Rule 3 E { (,id }| id { $,) }
{ +, 𝜖 } { $,) }
A  B
E’
T { (,id } { +,$,) }
T’*FT’ T’ { *, 𝜖 } { +,$,) }
F { (,id }
T’  *F T’ Rule 3
A  B

FOLLOW(T’)={+ $,) }
Example-3: LL(1) parsing
Step 2: Compute FOLLOW ETE’

FOLLOW(F) E’+TE’ | ϵ
TFT’
TFT’
NT First T’*FT’ |ϵ
Follow
T  F T’ F(E)
Rule 2 E { (,id }| id { $,) }
{ +, 𝜖 } { $,) }
A  B
E’
T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id }
T  F T’ Rule 3
A  B

FOLLOW(F)={ *, + ,$ , )
Example-3: LL(1) parsing
Step 2: Compute FOLLOW ETE’

FOLLOW(F) E’+TE’ | ϵ
TFT’
T’*FT’
NT First T’*FT’ |ϵ
Follow
T’  * F T’ F(E)
Rule 2 E { (,id }| id { $,) }
{ +, 𝜖 } { $,) }
A  B
E’
T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id } {*,+,$,)}
T’  * F T’ Rule 3
A  B

FOLLOW(F)={ *,+,$, ) }
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’

NT Input Symbol E’+TE’ | ϵ


TFT’
id + * ( ) $
E ETE’ ETE’ T’*FT’ | ϵ
E’ NT First F(E)
Follow
| id
T E { (,id } { $,) }
T’ E’ { +, 𝜖 } { $,) }
F T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id } {*,+,$,)}
Rule: 2
ETE’ A
a = first()
a=FIRST(TE’)={ (,id } M[A,a] = A

M[E,(]=ETE’
M[E,id]=ETE’
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’

NT Input Symbol E’+TE’ | ϵ


TFT’
id + * ( ) $
E ETE’ ETE’ T’*FT’ | ϵ
E’ NT First F(E)
Follow
E’+TE’
| id
T E { (,id } { $,) }
T’ E’ { +, 𝜖 } { $,) }
F T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id } {*,+,$,)}
Rule: 2
E’+TE’ A
a = first()
a=FIRST(+TE’)={ + } M[A,a] = A

M[E’,+]=E’+TE’
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’

NT Input Symbol E’+TE’ | ϵ


TFT’
id + * ( ) $
E ETE’ ETE’ T’*FT’ | ϵ
E’ NT First F(E)
Follow
E’+TE’ E’𝜖 E’𝜖
| id
T E { (,id } { $,) }
T’ E’ { +, 𝜖 } { $,) }
F T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id } {*,+,$,)}
Rule: 3
E’𝜖 A
b = follow(A)
b=FOLLOW(E’)={ $,) } M[A,b] = A

M[E’,$]=E’𝜖
M[E’,)]=E’𝜖
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’

NT Input Symbol E’+TE’ | ϵ


TFT’
id + * ( ) $
E ETE’ ETE’ T’*FT’ | ϵ
E’ NT First F(E)
Follow
E’+TE’ E’𝜖 E’𝜖
| id
T TFT’ TFT’ E { (,id } { $,) }
T’ E’ { +, 𝜖 } { $,) }
F T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id } {*,+,$,)}
Rule: 2
TFT’ A
a = first()
a=FIRST(FT’)={ (,id } M[A,a] = A

M[T,(]=TFT’
M[T,id]=TFT’
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’

NT Input Symbol E’+TE’ | ϵ


TFT’
id + * ( ) $
E ETE’ ETE’ T’*FT’ | ϵ
E’ NT First F(E)
Follow
E’+TE’ E’𝜖 E’𝜖
| id
T TFT’ TFT’ E { (,id } { $,) }
T’ T’*FT’ E’ { +, 𝜖 } { $,) }
F T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id } {*,+,$,)}
Rule: 2
T’*FT’ A
a = first()
a=FIRST(*FT’)={ * } M[A,a] = A

M[T’,*]=T’*FT’
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
NT Input Symbol
E’+TE’ | ϵ
id + * ( ) $ TFT’
E ETE’ ETE’
T’*FT’ | ϵ
E’ E’+TE’ E’𝜖 E’𝜖
NT First F(E)
Follow
T TFT’ TFT’ | id
E { (,id } { $,) }
{ +, 𝜖 }
T’ T’𝜖 T’*FT’ T’𝜖 T’𝜖
E’ { $,) }
F
T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
T’𝜖 F { (,id } {*,+,$,)}
Rule: 3
b=FOLLOW(T’)={ +,$,) } A
b = follow(A)
M[T’,+]=T’𝜖 M[A,b] = A

M[T’,$]=T’𝜖
M[T’,)]=T’𝜖
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’

NT Input Symbol E’+TE’ | ϵ


TFT’
id + * ( ) $
E ETE’ ETE’ T’*FT’ | ϵ
E’ NT First F(E)
Follow
E’+TE’ E’𝜖 E’𝜖
| id
T TFT’ TFT’ E { (,id } { $,) }
T’ T’𝜖 T’*FT’ T’𝜖 T’𝜖 E’ { +, 𝜖 } { $,) }
F F(E) T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id } {*,+,$,)}
Rule: 2
A
a = first()
F(E) M[A,a] = A

a=FIRST((E))={ ( }
M[F,(]=F(E)
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’

NT Input Symbol E’+TE’ | ϵ


TFT’
id + * ( ) $
E ETE’ ETE’ T’*FT’ | ϵ
E’ NT First F(E)
Follow
E’+TE’ E’𝜖 E’𝜖
| id
T TFT’ TFT’ E { (,id } { $,) }
T’ T’𝜖 T’*FT’ T’𝜖 T’𝜖 E’ { +, 𝜖 } { $,) }
F Fid F(E) T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id } {*,+,$,)}
Rule: 2
A
a = first()
Fid M[A,a] = A

a=FIRST(id)={ id }
M[F,id]=Fid
Example-3: LL(1) parsing
• Step 4: Make each undefined entry of table be Error
NT Input Symbol
id + * ( ) $
E ETE’ Error Error ETE’ Error Error
E’ Error E’+TE’ Error Error E’𝜖 E’𝜖
T TFT’ Error Error TFT’ Error Error
T’ Error T’𝜖 T’*FT’ Error T’𝜖 T’𝜖
F Fid Error Error F(E) Error Error
Example-3: LL(1) parsing
Step 4: Parse the string : id + id * id $ NT Input Symbol
id + * ( ) $
STACK INPUT OUTPUT
E ETE’ Error Error ETE’ Error Error
E$ id+id*id$
E’ Error E’+TE’ Error Error E’𝜖 E’𝜖
TE’$ id+id*id$ ETE’
T TFT’ Error Error TFT’ Error Error
FT’E’$ id+id*id$ TFT’
T’ Error T’𝜖 T’*FT’ Error T’𝜖 T’𝜖
idT’E’$ id+id*id$ Fid
F Fid Error Error F(E) Error Error
T’E’$ +id*id$
E’$ +id*id$ T’𝜖
+TE’$ +id*id$ E’+TE’
TE’$ id*id$ FT’E’$ id$
FT’E’$ id*id$ TFT’ idT’E’$ id$ Fid
idT’E’$ id*id$ Fid T’E’$ $
T’E’$ *id$ E’$ $ T’𝜖
*FT’E’$ *id$ T*FT’ $ $ E’𝜖
Parsing methods
Parsin
g

Top down Bottom up parsing (Shift


parsing reduce)
Back tracking Operator
precedence
Parsing without
backtracking LR parsing
(predictive
parsing) SLR
LL(1)
CLR
Recursi
ve LALR

descen
t
Recursive descent parsing
• A top down parsing that executes a set of recursive procedure
to process the input without backtracking is called recursive
descent parser.
• There is a procedure for each non terminal in the grammar.
• Consider RHS of any production rule as definition of the
procedure.
• As it reads expected input symbol, it advances input pointer to
next position.
Cont.,
Example: Recursive
Procedure T descent parsing
Proceduce
Match(token t)
Procedure E {
{ If lookahead=’*’ {
If { If
lookahead=num lookahead=t
{ Match(‘*’);
If lookahead=next_toke
Match(num); lookahead=num n;
T(); { Else
}
Else Match(num); Error();
Error(); Procedure Error
}
If lookahead=$ T(); {
{ } Print(“Error”);
Declare Else }
success;
} Error();

T* numT| 𝜖
Else
Error(); }
Enum T
} Else
3 * 4 $ Succes NULL
} s
Example: Recursive
Procedure T descent
Proceduce parsing
Procedure E { Match(token t)
{ If lookahead=’*’ {
If { If
lookahead=num lookahead=t
{ Match(‘*’);
If lookahead=next_toke
Match(num); lookahead=num n;
T(); { Else
}
Else Match(num); Error();
Error(); Procedure Error
}
If lookahead=$ T(); {
{ } Print(“Error”);
Declare Else }
success;
} Error();

T* numT| 𝜖
Else
Error(); }
Enum T
} Else
3 * 4 $ Succes NULL
3 4 * $
} s Error

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy