Professional Documents
Culture Documents
COMPILER DESIGN
Module - 2
SYNTAX ANALYSIS
Module:2 SYNTAX ANALYSIS 8 hours
Role of Parser- Parse Tree - Elimination of Ambiguity – Top Down Parsing - Recursive
Descent Parsing - LL (1) Grammars – Shift Reduce Parsers- Operator Precedence
Parsing - LR Parsers, Construction of SLR Parser Tables and Parsing- CLR Parsing-
LALR Parsing.
Role of Parser
or
Role of syntax analysis
SCANNER
Syntax analysis or Parsing:
E E + E / E * E / – E / ( E ) / id
Dangling Else:
| other
stmt if expr then stmt
| other
Note: “other” represents any other program statements.
PARSE TREE - 1
Consider the input string:
PARSE TREE - 2
if E1 then if E2 then S1 else S2 (Dangling else)
S S0S1S / 01
S 01S’
S’ 0S1S’ / ℇ
Example 3:
S (L) / x
L L,S/S
After Eliminating of Left Recursion:
S (L) / x
L SL’
L’ , S L’ / ℇ
A A α1 / A α2 / A α3 ……… / β1 / β2 / β3……….
Replace by
A β1 A’/ β2 A’ / β3 A’………….
expr id expr’
expr’ + expr expr’ / * expr expr’ / ℇ
A A α1 / A α2 / A α3 ……… / β1 / β2 / β3………
Replace by
A β1 A’/ β2 A’ / β3 A’………….
Example 5: A’ α1A’/ α2A’ / α3A’ ……… / ℇ
S Sx / SSb / xS / a
S xSS’ / aS’
S xS’ / SbS’ / ℇ
A A α1 / A α2 / A α3 ……… / β1 / β2 / β3………
Example 6: Replace by
A β1 A’/ β2 A’ / β3 A’………….
A Ac / Sd / f A’ α1A’/ α2A’ / α3A’ ……… / ℇ
S Aa / b
Note: Indirect left recursion and immediate left recursion
A Ac / Aad / bd / f
A bdA’ / fA’
A’ cA’ / adA’ / ℇ
SAa / b
Elimination of LEFT FACTOR:
helps to convert non-deterministic grammar
into deterministic grammar.
Replace by
A αA’ / γ1 / γ2 / γ3….
A’ β1 / β2 / β3…….
Example 1: A αβ1 / αβ2 / αβ3……. / γ1 / γ2 / γ3….
Replace by
SiEtS / iEtSeS / a
A αA’ / γ1 / γ2 / γ3….
Eb A’ β1 / β2 / β3…….
S iEtS / iEtSeS / a E b
S iEtSS’ / a
S’ ℇ / eS
Example 2: A αβ1 / αβ2 / αβ3……. / γ1 / γ2 / γ3….
Replace by
AaAB / aA / a
A αA’ / γ1 / γ2 / γ3….
B bB / b A’ β1 / β2 / β3…….
AaAB / aA / a B bB / b
A aAA’ / a B bB’
A’ AB / A / ℇ B’ B / ℇ
A αβ1 / αβ2 / αβ3……. / γ1 / γ2 / γ3….
Example 3:
Replace by
X X+X / X * X / D A αA’ / γ1 / γ2 / γ3….
A’ β1 / β2 / β3…….
D1/2/3
Example 4:
ET+E/T
A’ β1 / β2 / β3…….
A αβ1 / αβ2 / αβ3……. / γ1 / γ2 / γ3….
Example 6: Replace by
A αA’ / γ1 / γ2 / γ3….
S aSSbS / aSaSb / abb / b
A’ β1 / β2 / β3…….
S’ SSbS / SaSb / bb
S’ SS” / bb
S” SbS / aSb
FIRST and FOLLOW:
Example 2:
S ABCDE
A a / ℇ
Bb/ℇ
Cc First: Follow:
Dd/ℇ First (S) = { a, b, c} Follow (S) = { $ }
Ee/ℇ
First (A) = { a, ℇ} Follow (A) = { b, c}
First (B) = { b, ℇ} Follow (B) = { c}
First (C) = { c} Follow (C) = { d, e, $}
First (D) = { d, ℇ} Follow (D) = { e, $}
First (E) = { e, ℇ} Follow (E) = { $}
Example 3:
S Bb / Cd
B aB / ℇ
C cC / ℇ
First: Follow:
First (S) = { a, b, c, d} Follow (S) = { $ }
First (B) = { a, ℇ} Follow (B) = { b }
First (C) = { c, ℇ} Follow (C) = { d }
Example 4:
S ACB / CbB / Ba
A da / BC
Bg/ℇ
Ch/ℇ
First: Follow:
First (S) = { d, g, h, b, a} Follow (S) = { $ }
First (A) = { d, g, h, ℇ} Follow (A) = { h, g, $ }
First (B) = { g, ℇ} Follow (B) = { $, a, h, g }
First (C) = { h, ℇ} Follow (C) = { g, $, b, h }
Example 5:
S aABb
A c/ℇ
Bd/ℇ
First: Follow:
First (S) = {a} Follow (S) = { $ }
First (A) = { c, ℇ} Follow (A) = { d, b }
First (B) = { d, ℇ} Follow (B) = { b }
PARSER
Top down Bottom up parser
parser Shift Reduce Parser
With
Without Operator
backtracking
backtracking Precedence LR parser
Parser
Brute Recursive Predictive
Forcing Descent parser
parser LL (1),
LL (K)
LR (0) SLR (1) LALR (1) CLR (1)
Example 2:
Construct the predictive parse for the following
grammar.
S (L)/a
LL,S/S
S aAB / bA / ℇ
A aAb / ℇ
B bB / ℇ
S iEtS / iEtSeS / a
Eb
Step 1:
Check operator grammar or not.
Step 2:
Construct operator precedence relation table.
Step 3:
Parse the input string.
Step 4:
Generate the parse tree.
Example 1:
E E + E / E * E / id
Step 1:
E E + E / E * E / id
+ * id $
id
$
Step 2: Construct the operator precedence relation table.
E E + E / E * E / id
+ * id $
ACCEPTED
$ < < <
Example 2:
Construct the operator precedence relation table
for the given grammar.
EE+T/T
TT*F/F
Fa/b/c/d
Sa/↑/(T)
TT,S/S
EE+T/T
TT*F/F
FG^F/G
G id
ABC/B
CbBC/bB
BDbB/D
DE*D/E
E id
Parse the input string “id * id * id”.
EE+T/T
TT*F/F
F ( E ) / id
TT*F/F
F ( E ) / id
TT,S/S
Leading (S) = { a, ↑, ( }
Leading (T) = {, , Lead(s)}
Trailing (S) = { a, ↑, ) }
Trailing (T) = {, , Trail (s)}
Sa/↑/(T)
TT,S/S
Sa/↑/(T)
TT,S/S
Sa/↑/(T)
TT,S/S
Sa/↑/(T)
TT,S/S
a ↑ ( ) , $
) > >