0% found this document useful (0 votes)
6 views40 pages

CD Parser

The document provides an overview of syntax analysis in compiler design, focusing on parsers, their classifications, and methods for generating parse trees. It discusses top-down and bottom-up parsing approaches, including recursive descent and LL(1) parsers, along with the concepts of FIRST and FOLLOW sets. Additionally, it includes examples and rules for constructing LL(1) parsing tables.

Uploaded by

sahusurabhi02
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)
6 views40 pages

CD Parser

The document provides an overview of syntax analysis in compiler design, focusing on parsers, their classifications, and methods for generating parse trees. It discusses top-down and bottom-up parsing approaches, including recursive descent and LL(1) parsers, along with the concepts of FIRST and FOLLOW sets. Additionally, it includes examples and rules for constructing LL(1) parsing tables.

Uploaded by

sahusurabhi02
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/ 40

Compiler Design

• Course Code: CS105101CS


• Unit 2: Syntax Analysis
• Lecture :Parser
Outcome
§ Definition of Parser
§ Ways of generating Parse trees
§ Classification of Parsers
Parser
§ A parser is a program that generates a parse tree for the given string, if
the string is generated from the underlying grammar.

S
X = a + b * c;
id = E ;
id = id + id * id ; Parser
E + T
S à id = E ; T T * F
EàE+T|T
TàT*F|F F F
F à id
id id
Generation of Parse Tree S à aABe, A à Abc | a, B à d

§ Top-down approach § Bottom-up approach aabcde

S S S à aABe
Decision: à aAde
a A B e Which à aAbcde
production to use. A B à aabcde
A b c d A (Right Most Derivation)
In reverse
S à aABe
a a a b c d e
à aAbcBe
à aabcBe Decision:
à aabcde When to reduce
(Left Most Derivation)
Classification of Parsers
Parsers

Top-Down Parsers Bottom-Up Parsers


(Shift-Reduce Parsers)

With Without
Backtracking Backtracking Operator
LR Parsers
Precedence Parsers

Brute Forcing
Predictive
Recursive
Parsers
Descent LR(0) SLR(1) LALR(1) CLR(1)
LL(1),
Parsers
LL(K)
Top-down Parser
§ Top-down approach
§ In order to construct top-down parser the
context free grammars should not have,
1. Left Recursion S
2. Non-determinism Decision:
a A B e Which
AàAα|β A è βα* production to us.
A b c d
A à βA’
A’ à α A’ | ε S à aABe
a
à aAbcBe
A à α β1 | α β2 | α β3 à aabcBe
à aabcde
A à αA’
(Left Most Derivation)
A’ à β1 | β2 | β3
Recursive Descent Parser
§ A recursive descent parser is a top-down parser built from a set of mutually
recursive procedures (or a non-recursive equivalent) where each such procedure
implements one of the non-terminals of the grammar. Thus, the structure of the
resulting program closely mirrors that of the grammar it recognizes.

Consider the following grammar having the rules, E à iE’


E’ à + iE’ | ε
E à iE’
Recursive Descent Parser E’ à + iE’ | ε

i E’

+ i E’

Input: i + i $
E à iE’
Recursive Descent Parser E’ à + iE’ | ε

Input: i + i $
E à iE’
Recursive Descent Parser E’ à + iE’ | ε

match(i)

E()
main()
Input: i + i $
E à iE’
Recursive Descent Parser E’ à + iE’ | ε

E’(i)

E()
main()
Input: i + i $
Predictive Parser (LL(1))
Input Buffer:
$

LL(1) Parser
$
Stack
LL(1)
Parsing Table
LL(1) Parser
“1” Look-ahead symbol.
Use Left most Derivation.
Scan from Left to Right.
LL(1) Parser
1. FIRST( ):
Given any non-terminal of a CFG, if we derive all the possible strings from it, the
first terminal(s) is the FIRST() of the non-terminal.
S A
E.g (1): S à aABC FIRST(S): FIRST(A):
Aàb
Bàc a A B C
b
Càd
FIRST(B): FIRST(C):
B C

c d
LL(1) Parser
1. FIRST( ):
Given any non-terminal of a CFG, if we derive all the possible strings from it, the
first terminal(s) is the FIRST() of the non-terminal.
S
E.g (2): S à ABC FIRST(S): a
Aàa|ε
Bàb A B C
Càc
a
LL(1) Parser
1. FIRST( ):
Given any non-terminal of a CFG, if we derive all the possible strings from it, the
first terminal(s) is the FIRST() of the non-terminal.
S
E.g (2): S à ABC FIRST(S): {a, b}
Aàa|ε
Bàb A B C
Càc
ε b
LL(1) Parser
2. FOLLOW( ):
During the process of derivation, the terminal(s) that could follow the non-
terminal are to be considered as FOLLOW( ) of the non-terminal.

E.g (1): S à ABC FOLLOW(S): {$}


Aàa S $
Bàb|ε FOLLOW(A):
Càc A B C

b
LL(1) Parser
2. FOLLOW( ):
During the process of derivation, the terminal(s) that could follow the non-
terminal are to be considered as FOLLOW( ) of the non-terminal.

E.g (1): S à ABC FOLLOW(S): {$}


Aàa S $
Bàb|ε FOLLOW(A): {b, c}
Càc A B C $ FOLLOW(B): {c}

ε c FOLLOW(C): {$}
Derivation of FIRST:
FIRST (F): { id, ( }
E à TE’
E’ à +TE’ | ε FIRST (T’): { *, ε }
T à FT’ FIRST (T): FIRST(F): { id, ( }
T’ à *FT’ | ε
F à id | (E) FIRST (E’): { +, ε }

FIRST (E): FIRST (T): { id, ( }


FIRST
E à TE’ { id, ( }
E’ à +TE’ | ε { +, ε }
T à FT’ { id, ( }
T’ à *FT’ | ε { *, ε }
F à id | (E) { id, ( }
Derivation of FOLLOW:
FIRST FOLLOW S à abC
E à TE’ { id, ( } { $, ) }
S $
E’ à +TE’ | ε { +, ε } { $, ) }
T à FT’ { id, ( } {+, $, ) }
T’ à *FT’ | ε { *, ε } a b C $
F à id | (E) { id, ( }

FOLLOW(T) = FIRST(E’) = {+, ε }


FOLLOW(E’) = FOLLOW(E’) 1) The following terminal symbol will be selected as
FOLLOW.
2) The FIRST of the following Non-terminal will be
R.H.S L.H.S selected as FOLLOW.
3) If it is the right most in the RHS, the FOLLOW of
LHS will be selected.
Derivation of FOLLOW:
FIRST FOLLOW
E à TE’ { id, ( } { $, ) }
E’ à +TE’ | ε { +, ε } { $, ) }
T à FT’ { id, ( } {+, $, ) }
T’ à *FT’ | ε { *, ε } {+, $, ) }
F à id | (E) { id, ( } { *, +, $, ) }

FOLLOW(T’) = FOLLOW(T) = {+, $, ) }

FOLLOW(F) = FIRST(T’) = {*, ε}


= {*} U FOLLOW(T) U FOLLOW(T’)
= { *, +, $, ) }
Solved Examples:
Q1. Consider the following grammar:
P à xQRS
Q à yz | z
Ràw|ε
Sày
What is FOLLOW(Q)?
FOLLOW(Q) = FIRST(R) U FIRST(S)
A. {R}
B. {w} FIRST(R) = {w, ε}
C. {w, y}
D. {w, ε}
{w, y}
Solved Examples:
Q2. Find the FIRST and FOLLOW of all the non-terminals:

S à ABCDE
FIRST FOLLOW
Aàa|ε
Bàb|ε S à ABCDE {a, b, c} {$}
Càc
Dàd|ε Aàa|ε {a, ε} {b, c}
Eàe|ε Bàb|ε {b, ε} {c}
Càc {c} {d, e, $}
Dàd|ε {d, ε} {e, $}
Eàe|ε {e, ε} {$}
Solved Examples:
Q3. Find the FIRST and FOLLOW of all the non-terminals:

S à Bb | Cd
B à aB | ε FIRST FOLLOW
C à cC | ε
S à Bb | Cd {a, b, c, d} {$}
B à aB | ε {a, ε} {b}
C à cC | ε {c, ε} {d}
Solved Examples:
Q4. Find the FIRST and FOLLOW of all the non-terminals:

S à aBDh
FIRST FOLLOW
B à cC
C à bC | ε S à aBDh {a} {$}
D à EF
Eàg|ε B à cC {c} {g, f, h}
Fàf|ε C à bC | ε {b, ε} {g, f, h}
D à EF {g, f, ε} {h}
Eàg|ε {g, ε} {f, h}
Fàf|ε {f, ε} {h}
Solved Examples:
Q5. Find the FIRST and FOLLOW of all the non-terminals:

S à ACB | CbB | Ba
A à da | BC
Bàg|ε
Càh|ε FIRST FOLLOW
S à ACB | CbB | Ba {d, g, h, b, a, ε} {$}
A à da | BC {d, g, h, ε} {h, g, $}
Bàg|ε {g, ε} {$, a, h, g}
Càh|ε {h, ε} {g, $, b, h}
LL(1) Parser
“1” Look-ahead symbol.
Use Left most Derivation.
Scan from Left to Right.
Construction of LL(1) Parsing Table
E à TE’ id ( ) + * $
E’ à +TE’ | ε
T à FT’ E E à TE’ E à TE’

T’ à *FT’ | ε E’ E’ à ε E’ à +TE’ E’ à ε
F à id | (E)
T T à FT’ T à FT’

T’ T’ à ε T’ à ε T’ à *FT’ T’ à ε

F F à id F à (E)

Rules:
1. All the ε-productions are placed under FOLLOW sets.
2. Remaining productions are placed under the FIRSTs.
LL(1) Parsing FIRST FOLLOW

S à aABb {a} {$}


S à aABb Input Buffer:
Aàc|ε Aàc|ε {c, ε} {d, b}
Bàd|ε Bàd|ε {d, ε} {b} a d b $

S
LL(1) Parser
$
Stack
a b c d $ LL(1)
S S à aABb Parsing Table
A Aàε Aàc Aàε
B Bàε Bàd
FIRST FOLLOW
LL(1) Parsing
S à aABb {a} {$}
S à aABb Input Buffer:
Aàc|ε Aàc|ε {c, ε} {d, b}
Bàd|ε Bàd|ε {d, ε} {b} a d b $

S a
A
a A B d
B
b
LL(1) Parser
$
Stack
a b c d $ LL(1)
S S à aABb Parsing Table
A Aàε Aàc Aàε
B Bàε Bàd
FIRST FOLLOW
LL(1) Parsing
S à aABb {a} {$}
S à aABb Input Buffer:
Aàc|ε Aàc|ε {c, ε} {d, b}
Bàd|ε Bàd|ε {d, ε} {b} a d b $

S
A
a A B d
B
ε b
LL(1) Parser
$
Stack
a b c d $ LL(1)
S S à aABb Parsing Table
A Aàε Aàc Aàε
B Bàε Bàd
FIRST FOLLOW
LL(1) Parsing
S à aABb {a} {$}
S à aABb Input Buffer:
Aàc|ε Aàc|ε {c, ε} {d, b}
Bàd|ε Bàd|ε {d, ε} {b} a d b $

S
a A B d
B
ε b
LL(1) Parser
$
Stack
a b c d $ LL(1)
S S à aABb Parsing Table
A Aàε Aàc Aàε
B Bàε Bàd
FIRST FOLLOW
LL(1) Parsing
S à aABb {a} {$}
S à aABb Input Buffer:
Aàc|ε Aàc|ε {c, ε} {d, b}
Bàd|ε Bàd|ε {d, ε} {b} a d b $

S
a A B d
d
ε d b
LL(1) Parser
$
Stack
a b c d $ LL(1)
S S à aABb Parsing Table
A Aàε Aàc Aàε
B Bàε Bàd
FIRST FOLLOW
LL(1) Parsing
S à aABb {a} {$}
S à aABb Input Buffer:
Aàc|ε Aàc|ε {c, ε} {d, b}
Bàd|ε Bàd|ε {d, ε} {b} a d b $

S
a A B d

ε d b
LL(1) Parser
$
Stack
a b c d $ LL(1)
S S à aABb Parsing Table
A Aàε Aàc Aàε
B Bàε Bàd
FIRST FOLLOW
LL(1) Parsing
S à aABb {a} {$}
S à aABb Input Buffer:
Aàc|ε Aàc|ε {c, ε} {d, b}
Bàd|ε Bàd|ε {d, ε} {b} a d b $

S
a A B d

ε d
LL(1) Parser
$
Stack
a b c d $ LL(1)
S S à aABb Parsing Table
A Aàε Aàc Aàε
B Bàε Bàd
Problem Solving
1. Find out whether the following grammar is LL(1)?
S à aSbS | bSaS | ε

Sol: FIRST(S): {a, b, ε}


FOLLOW(S): {$, b, a} Not LL(1) Grammar

a b $
S à aSbS S à bSaS
S
Sàε Sàε Sàε
Problem Solving
2. Find out whether the following grammar is LL(1)?
S à (S) | ε

Sol: FIRST(S): {(, ε}


FOLLOW(S): {$, )} LL(1) Grammar

( ) $
S S à (S) Sàε Sàε
Problem Solving
3. Find out whether the following grammar is LL(1)?
S à AaAb | BbBa
Aàε
Bàε

Sol: FIRST(S): {a, b} FOLLOW(S): {$}


FIRST(A): {ε} FOLLOW(A): {a, b}
FIRST(B): {ε} FOLLOW(B): {b, a}

a b $
S S à AaAb S à BbBa
A Aàε Aàε
B Bàε Bàε
Problem Solving
4. Find out whether the following grammar is LL(1)?
SàA|a
Aàa
5. Find out whether the following grammar is LL(1)?
S à aB | ε
B à bC | ε
C à cS | ε
6. Find out whether the following grammar is LL(1)?
S à AB
Aàa|ε
Bàb|ε
7. Find out whether the following grammar is LL(1)?
S à aSA | ε
Aàc|ε
Problem Solving
8. Find out whether the following grammar is LL(1)?
SàA
A à Bb | Cd
B à aB | ε
C à cC | ε
9. Find out whether the following grammar is LL(1)?
S à aAa | ε
A à abS | ε

You might also like