You are on page 1of 120

BCSE307L

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:

It checks the syntactical structure of the given input.

(i.e. whether the given input is in the correct syntax or not).

Syntax analysis checks the syntactical structure and builds a

data structure called parse tree or syntax tree.


The parse tree is constructed by using the pre-defined

grammar of the language and the input string.

If the given input string can be produced with the help of

syntax tree then the string is in the correct syntax (accepted)

if not error will be reported.


Parse Tree:
A graphical representation for a derivation showing
how to derive the string of a language from
grammar starting from start symbol.

E  E + E / E * E / – E / ( E ) / id

Input string: – (id + id)


Consider the CFG:
SSS+/SS*/a
and the string a a + a *
i) Give a leftmost derivation for the string.
ii) Give a rightmost derivation for the string.
iii) Give a parse tree for the string.
iv) Is the grammar ambiguous or unambiguous?
Justify.
Ambiguity:

A grammar that produces more than one parse tree


for some sentence is said to be ambiguous.

Put another way, an ambiguous grammar is one that


produces more than one leftmost derivation or more than
one rightmost derivation for the same sentence.
Ambiguity can be eliminated by:
 Rewriting the grammar (unambiguous grammar) or

 Use ambiguous grammar with additional rules to resolve ambiguity.

Dangling Else:

stmt  if expr then stmt

| if expr then stmt else stmt

| other
stmt  if expr then stmt

| if expr then stmt else stmt

| other
Note: “other” represents any other program statements.

The compound conditional statement of above grammar is

if E1 then S1 else if E2 then S2 else S3


The compound conditional statement of above grammar is

if E1 then S1 else if E2 then S2 else S3


Consider the input string:

if E1 then if E2 then S1 else S2

PARSE TREE - 1
Consider the input string:

if E1 then if E2 then S1 else S2  (Dangling else)

PARSE TREE - 2
if E1 then if E2 then S1 else S2  (Dangling else)

Rewrite the dangling else grammar as unambiguous grammar.

Then the grammar:


if E1 then if E2 then S1 else S2
Example 2:

S  S0S1S / 01

After Eliminating of Left Recursion:

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’………….

A’  α1A’/ α2A’ / α3A’ ……… / ℇ


Example 4:
expr  expr + expr / expr * expr / id
After Eliminating of Left Recursion:

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

After Eliminating of Left Recursion:

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

After Eliminating of Left Recursion:

A  bdA’ / fA’
A’ cA’ / adA’ / ℇ
SAa / b
Elimination of LEFT FACTOR:
helps to convert non-deterministic grammar
into deterministic grammar.

A  αβ1 / αβ2 / αβ3……. / γ1 / γ2 / γ3….

Replace by

A  αA’ / γ1 / γ2 / γ3….

A’  β1 / β2 / β3…….
Example 1: A  αβ1 / αβ2 / αβ3……. / γ1 / γ2 / γ3….

Replace by
SiEtS / iEtSeS / a
A  αA’ / γ1 / γ2 / γ3….

Eb A’  β1 / β2 / β3…….

Consider the 1st production: Consider the 2nd production:

S iEtS / iEtSeS / a E b

After eliminating Left Factor There is no left factor.

S  iEtSS’ / a

S’  ℇ / eS
Example 2: A  αβ1 / αβ2 / αβ3……. / γ1 / γ2 / γ3….

Replace by
AaAB / aA / a
A  αA’ / γ1 / γ2 / γ3….

B bB / b A’  β1 / β2 / β3…….

Consider the 1st production: Consider the 2nd production:

AaAB / aA / a B bB / b

After eliminating Left Factor After eliminating Left Factor

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…….
D1/2/3

Example 4:

ET+E/T

T  int / int*T / (E)


A  αβ1 / αβ2 / αβ3……. / γ1 / γ2 / γ3….
Example 5:
Replace by
A  aB / abc A  αA’ / γ1 / γ2 / γ3….

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…….

After eliminating Left Factor


S  aS’ / b

S’  SSbS / SaSb / bb

S’  SS” / bb

S”  SbS / aSb
FIRST and FOLLOW:
Example 2:
S  ABCDE
A a / ℇ
Bb/ℇ
Cc First: Follow:
Dd/ℇ First (S) = { a, b, c} Follow (S) = { $ }
Ee/ℇ
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
Bg/ℇ
Ch/ℇ

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/ℇ
Bd/ℇ

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
LL,S/S

and trace the string “ ( a ) ”


Example 3:
Construct the predictive parse for the given
grammar.

S  aAB / bA / ℇ
A  aAb / ℇ
B  bB / ℇ

and trace the string “ aabb”


Example 4:
Construct the predictive parse for the given
grammar.

S  iEtS / iEtSeS / a

Eb

Check whether the given grammar is LL(1)


parser or not.
Steps to solve operator precedence:

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:

Construct the operator precedence table for the following


grammar.

E  E + E / E * E / id

and trace the string “id + id * id”.

Step 1:

Check the give grammar is operator grammar or not.


Step 2: Construct the operator precedence relation table.

E  E + E / E * E / id

+ * id $

id

$
Step 2: Construct the operator precedence relation table.

E  E + E / E * E / id

+ * id $

+ > < < <

* > > < >

id > > >

ACCEPTED
$ < < <
Example 2:
Construct the operator precedence relation table
for the given grammar.

EE+T/T

TT*F/F

Fa/b/c/d

Parse the input string “a + b * c + d”.


Example 3:
Construct the operator precedence relation table
for the given grammar.

Sa/↑/(T)

TT,S/S

Parse the input string “ ( ( ( a , a ) , a ) , a) ”.


Hint:
(=) (<( )>) (<, ,<↑ (<↑ $<↑ ,< a ,<(
Example 4:
Construct the operator precedence relation table
for the given grammar.

EE+T/T

TT*F/F

FG^F/G

G  id

Parse the input string “id + id * id ^ id”.


Example 5:
Construct the operator precedence relation table
for the given grammar.

ABC/B

CbBC/bB

BDbB/D

DE*D/E

E  id
Parse the input string “id * id * id”.
EE+T/T

TT*F/F

F  ( E ) / id

Leading (E)={+, Lead (T)} Leading (E)={+,*, (, id}


Leading ( T)={*, Lead(F)} Leading ( T)={*, (, id}
Leading ( F )= {(, id} Leading ( F )= {(, id}
EE+T/T

TT*F/F

F  ( E ) / id

Trailing (E)={+, Trail (T)} Trailing (E)={+,*, ), id}


Trailing ( T)={*, Trail (F)} Trailing ( T)={*, ), id}
Trailing ( F )= { ), id} Trailing ( F )= { ), id}
Sa/↑/(T)

TT,S/S

Leading (S) = { a, ↑, ( }
Leading (T) = {, , Lead(s)}

Trailing (S) = { a, ↑, ) }
Trailing (T) = {, , Trail (s)}
Sa/↑/(T)

TT,S/S
Sa/↑/(T)

TT,S/S
Sa/↑/(T)

TT,S/S
Sa/↑/(T)

TT,S/S
a ↑ ( ) , $

a > > >

↑ > > >

( < < < = < >

) > >

, < < < > >

$ < < <

You might also like