CD Unit - 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 148

IUNIT II I

2 Syntax Analysis

Syllabus
Roll· c?( Parser - Grammars - Context:fi'ee gramrnars - Writing a grammar Top Down Parsing -
Gt'neral Srrategies - Recursive Descent Parser Predictive Parser-LL(!) - Parser-Shift Reduce
Parser-LR Parser- LR (0)/tem Construction of SLR Parsing Table - Introduction to LALR Parser -
Error Handling and Recove,:v in Syntax Analyzer-YACC tool - Design of a syntax Analyzer f or a
Sample language.

Contents
2. 1 Introduction to Syntax Analysis
2.2 Role of Parser ..... . . .... . . . . ... Dec.-06, 07, · . · · · · · · · · · · · Marks 6

2.3 Errors Handling


2.4 Context-free Grammars .. . . . . .. .. . .. ... . . Dec.-05, May-16, 18, · · · · · Marks 10
2.5 Ambiguous Grammar .... . . . . . . . .... . .. Dec-09, May-14, 18, · · · · · · · Marks 8

2. 6 Parsing Techniques . . .. . . . . . ... .. .. . . Dec.-10, · · · · · · · · · · · · · · · Marks 2

2. 7 Top-down Parsing .. . . .. ... .. .. . ... . May-04,07,08, 15,

. . . . .. .. . .. . ...... Dec.-05,07, 16, · · · · · · · · · Marks 10

2.8 Recursive Descent Parsing


2.9 Predictive Parser LL(1) Parser- . . . . .. . . .. . . May-04,07, 11, 12, 13, 16, 17, 18,
. . .... . .. .... .. . . . Dec.-05,09, 12, 16,22, · · · · · Marks 16

2.10 Bottom Up Parsing ... . . . . . . .... . .. . . May-04,05, 06,0 7, 10, 11, 16,
. . . .. . .. . ... . . .. . . Dec.-04,06, · · · · · · · · · · · · · Marks 8

2. 11 Shift Reduce Parser .. . ........ .. . . .. . Dec.-04,06, May-16, · · · · · · Marks 8

2. 12 LR Parser . .... .. ...... . .. . . Dec. -07, · · · · · · · · · · · · · · · Marks 4

2. 13 SLR Parsing .. . . . ............. May-04, 11, 13, 14, 15, 17, 19, Dec.-05,

........... .. ..... 06,09, 10, 11, 12, 16, 17, · · · · Marks 16

(2 - 1)
r
Syn tax Ana/ys,s
Com pile r Des ign
2-2 -- - - - - - - ----
2. 14 Canonical LR Parsing -06 07 10, 18,
. . . . . . ... . . . May ' ' . . . . . . Ma rks 13
oec.-10, . . ... .. .
..........··· May-19, Ma rks 16
2. 15 LALR Parsing oec.-06,07, 13, 18,
••· · · · · · · · · · · · · · · ·
... . •·M ark s 2
2. 16 Comparison of LR Parsers . . .. oec.-10, · · · · · · · · ·
.. • · · · · · · · · · . . . . . . . . Ma rks 15
2. 17 Handlin g Ambiguous Gra mm ar
. . . . . . . . . . - · May-12'18' · · ..
2. 18 Err or Handling and Recovery
in _ . _ . . . . . . . Ma rks 8
Syntax Analyz'3r . . ... ... . ... . ... .. May-06,07, 11,
·
2. 19 YA CC

2.2 0 Two Ma rks Questions with Ans


wer s
compihtr Design 2-3 Syntax Analysis

- Introduction to Syntax Analysis


The syntax analysts is the second phase in compilation. The syntax analyzer (Parser)
basically checks for the syntax of the language. A syntax analyzer takes the tokens from
the lexical analyzer and groups them in such a way that some pTogramming structure
(syntax) can be recognized. After grouping the tokens if at all, any syntax cannot be
f'l'('t1gni zl'd then syntactic error will be generated. This overaU process is called syntax
checking of the language.

Definition of parser : A parsing or syntax analysis is a process which takes the input
string w and produces either a parse tree (syntactic structure) or generates the syntactic
errors.

For example : a = b+10;


The above programming statement is first given
to lexical analyzer. The lexical analyzer will divide
it into group of tokens. The syntax analyzer takes
the tokens as input, and generates a tree like b 10
structure called parse tree. = b + 10
Fig. 2.1.1 Parse tree for a
The parse tree as shown in Fig. 2.1.1 is for some programming statement. It shows
how the statement gets parsed according to their syntactic specification.

fill·Basic Issues in Parsing


• There are two important issues in parsing :
i) Specification of syntax
ii) Representation of input after parsing.
• A very important issue in parsing is specification of syntax in programming
language. Specification of syntax means how to write any programming statement.
There are certain characteristic of specification of syntax -
i) This specification should be precise and unambiguous.
ii) This specification should be in detail, i.e. it should cover all the details of
the programming language.
iii) This specification should be complete.
Such a specification is called "Context Free Grammar" .
• Another important issue in parsing is representation of the input after parsing.
This is important because all the subsequent phases of compiler take the
information from the parse tree being generated. This is important because the
information suggested by any input programming statement sh ould not be
differed after building the syntax tree for it.

®
TECHNICAL PUBL/CA TIONS - an up-thrust for knowledge
~ omp1fe1 Das,gn ? 4

, ti • 1,nrf'ln~ n1>4°
rllhrn hmH•d nn wh k h w 1, )'•·t
• l .a s lly the mo~I crud L,1 i:4~ \lt' 1~
, 11)1,
ll . litf,•r<'nl ,,ppro,h'l1t•~1 t u p ,r,
W will d,~cl1Kt4 l , 1 1, , ► • .
p;)t'SC' tri. .'t' hw thl' ~ivcn input. e . , nr1-1 in~ nlgnri lhirw c orwt •r "
1

1 1 111 1
Tl,p-dl"IWn and hoth1m-up . /\nd we w ill ~ l\l L Y I 11 It>
these a ppronch cs.

UI Role of Parser AU : Dec .-06, 07. Mnrl&, '•

ln the proce ss of compila tion the parser an


d lex ica l onn ly:t.t•r work togNhc.•r. ·1h
. , t1 t
n1e ans, when parser requires stri n g of tokens .,t • okcs lcx 1ca 1 nnn 1yzc r . 1n turn tf
inv · ' )(•
le xical analyzer s upplies tokens to syntax an aly zer (pArscr).

_ ____.____ Demand for tokens


Source Lexical Parse Rest of Output
Parser compiler
program analyzer tree code
...__ _ _ _____. Supply for tokens L - - - ~

Symbol table

Fig. 2.2.1 Role of parser


The parser collects sufficient number of tokens and builds a parse tree. Thus by
building the parse tree, parser smartly finds the syntactical errors if any. It is also
necessary that the parser should recover from commonly occurring errors so that
remaining task of process the input can be continued.

Review Question

1. Explain the role of parser in detail .


AU : Dec.-06, Marks 6, Dec.-07. Marks 4

f ..fl Errors Handling


During various phases of compiler, all po~sible errors made b y the programmer Mt'
d e tecte d and they are re_ported to t~e user m the form of m essages. T his process c1f
locating e rro rs and reporting to user 1s ca lled error handling process.
FoJl o wing are examp les of varios errors that can be detected by . h .;e'- r t
various p "· ·
compiler -
1. Lexica I phase errors
These type of e rrors can b e d e tected during lexical ana lys is phase. Typical lexical
phase e rrors a re -
• Spelling errors . H e n ce get incorrect to kens.
Compiler Design 2-5 Syntax Analysis

• Exceeding length of identifie r or numeric constants.


• Appeara nce of illegal characte rs.
Normally due to typing mistakes
~
tl,e w rong spe 11 ·mgs may appea r in the program .
This causes a lexical error.
For example
swtich (choice)
{
·····················
·····················
}

In above code 'switch' cannot be recognized as a misspel ling of keyword switch.


Rather lexical analyze r will understan'ci that it is an identifier •and will return it as valid
identifier. Thus misspel ling causes errors in token formation. This is can be a possibility
of an error which occurs due to cascadin g of characters.
For example
switch (choice) .
{
easel: get.,..dafa ();
break; ,
case2: · display (); · "
break;
}
In above given 'C' code case 1 will not be identified because there is no space
between 'case' and '1'. But the compile r does not report any error message as it identifies
'easel' as valid identifie r. This type of error is lexical phase error.
Conside r
printf("\n Hello IndiaHt l
This is also a lexical error as an illegal characte r '$' appears at the end of the
statement.
If Jength of identifie rs get exceede d the error occurs. For example, if in FORTRA N
there is an identifie r wh~se length is of 10 ch~racters then lexical error occurs.

2. Syntax analysis phase errors


These are the errors which are popular ly known as syntax errors. They appear during
syntax analysis phase of compile r.
Typical phase errors are :
• Errors in structur e
• Missing operator s
• Unbalanced parenthe sis.
®
TECHNICAL PUBL/CA TIONS • an up-thrust for knowledge
Compiler Design 2-6

Syntax Ana/ .
Ys,8

The parser demands for tokens from lexical analyzer and if t~e tokens do not satisfy
the grammatic al rules of program.m ing language then the syntactica l errors get raised.

3. Semantic e.m>rs
· Semantic errors are those errors which get detected during semantic analysis phase.
Typical errors in this phase are :
• Incompati ble types of operands.
• Undeclare d variables.
• Not matching of actual argument s with formal argument s.
For example
int a(10), b ;

a= b;
It generates a semantic error.
Similarly
Another example
if(f== l)
{
return 1; .
printf("%d" ,f);
}
This will generate an error unreachab le code. This is because after return statement
the control goes back to main and hence after return whatever code we give that
becomes unreachab le. Thus the code printf is beyond the scope of the compiler.

DI Context-free Grammars AU : Dec.-05, May-16,18 . Marks 10

Definitio n : The context free grammar G is a collection of following things.


1. V is a set of non-termi nals.
2. T is a set of terminals.
3. S is a start symbol.
4. P is a set of productio n rules.
Thus G can be represente d as G = (V, T, S, P)
The productio n rules are given in following form -
Non-term inal ~ (V UT)*

TECHNICAL PUBLICA TIONs® - an up-thrust for knowledge


. ,oesign._ _ _ _ _ _~ 2-7 SyntflK An1Jly• l1J
~£iii Let the language L = a"b" w hen• n ~
[{ti wr~---
G _ (V, T, S, P )
1
Lei - .
Where V = (SJ,
r::: (a,bl
And S is a start symbol then, give the production rules.
solution :
. p :::: {
s~aSb
s ~ ab
}
The production rules actually defines the language a"b".
The non-terminal symbol occurs at the left hand side . These · are the symbo ls which
need to be expanded. The terminal symbols are nothing but the tokens used in the
language. Thus any language construct can be defined by the context free grammar.
Example 2.4.2 Define the declarative sentence using context free grammar.

Solution :
State ➔ Type List Terminator
Type ➔ int I float
List ➔ List,id
List ➔ id
Terminator ➔

Using above rules we can derive,

Type List Terminator

I
int
~
Li~d

List ' id

I
id
Fig. 2.4.1 Parse tree for derivation of int Id, Id, Id;
--------~~------= --- - - - - - - - -
TECHNICAL PUBLICA TIONs® - an up-thrust for knowledge
~~~ ~1!! ___ __ __ __ __ __2_-_a__ __ __
__ __ __ __s_y_nt_ax Anat .
>_!!)Pife
1 r Design
~
. "d "d "d• can be defin ed by mean s of abov e Cont ext
Hence int 1 , 1 , 1 , Free Gram mar
·
Following rules to be followed whil e writi ng a CFG .
1) A single non-terminal shou ld be at L.H.S.
Z) The rule shou ld be alwa ys in the form of LH.S. ➔ R.H.
S. whe re R.H.S. may be the
combination of non- term inal and term inal symb ols.
3) The NULL deriv ation can be speci fied as NT ➔ E.
4) One of the _non- term inals shou ld be start symb
ol and conv entio nally we should
write the rules for this non- term inal.
xample 2.4.3 Consider the following CFG grammar,
S
aABe ➔

nA➔ Abe I b
B ➔ d

where a, b, c, d, e are terminals, 'S' (start symbol), A and B are


non-terminals.
a) Parse the sentence "abbcde" ·using right-most derivations.
b) Parse the sentence "abbcde" using left-most derivations.
c) Draw the parse tree.
AU : May- 18, Marks 7
oluti on:
a) s b) s
aABe
aABe
aAde
aAbc Be
aAbcd e
abbcB e
abbcd e
abbcd e
c)

s
a/ /\e
A;1\B\
I b C d

b
Revie w Ques tions

1 - Defin e CFC.
2· Explain contex t free ~ram
- - - .
mers w ith examples.
AU : May- 16, Marks 10

TECH NICAL PUBLICA Tln/\i c-®


Syntn" AnolytJIB

fJI Ambiguous Grammar


r 111111\,1r l~ i~ ~ 1id "' b,• ambiglln\ls , p,ww tr(•Cfl for
,\ \l • , • II it ~l•m•ml(.•R mnrl' thnn 111w
. ''" ,it t.1n~1111~,· I ((:) .
•-
.:i,•tlll'lll ,

. for "'""'r'" :
F ~ F + E I E ,.. E I id

E
E

/ ~ ff i
E

id
·~
E • E E
~

+

E
E

Id

id id id Id

(a) Parse tree 1 (b) Parse tree 2

Fig. 2.5.1 Ambiguous grammar

j!fj.jj.jiJJ• Show that following grammar is ambiguous


S ➔ aSbS
S ➔ bSaS
s ➔ E.
Solution : Consider a string '~bab'. We can construct parse trees for deriving 'abab'

a
/I~ S b S
/I~
a S b S

I~ j I~
a S b S
b S a S

I I £ E
I I f f

Parse tree 2
Parse tree 1

®
TECHNICAL PUBLICA T/ONS - an 11p-thn1st for knowledge
~C~om~p1~ 2 - 10 Synta x Ana/y .
·1e~r~De~si~gn~ - - - -- - - - - - -= -~-- - - - -- - - - - -- - ~
The right most deriv ation for abab is -

s
s
aSb S
/ 1 ~S
a S b
I a
/1
S b
~S - aSb aSb s
I
E
I
E
a Sb a Sb

a Sb ab
aba b
This is a lang uage cont ainin g all the strin gs with equa
l num ber of a's and b's.
Exam ple 2.5.2 Create a parse trees for the following string : string · id
+ id -:- id. Check
whether the string is ambiguous or not.
AU : May- 18 . Marks 7
Solu tion : The gram mar for deriv ing the strin gs will
be
E➔E +E
E ➔ E-E
E ➔ id
The pars e trees will be

id id id id

Fig. 2.5.2
Revi ew Ques tions

1. What is an ambiguous grammar ? Give ah ~ mple~ -


-
All : Dec .-09. Marks 2
2. What is an ambig uous grammar
· ls th.e fiotiowin
·
7
g grammar ambi·guous 7 prvz•r
E ➔ E + EI £ • £ I ( £ )1 id. The grammar should be moved to
the next line, centered.
All : May- 14 . Marks 8
TECH NICA L PUBL ICATI ONS® - an up-thrust for
knowledge
► 2 - 11 Syntax Analysis
1er [)esign
~
fJI parsing Techniques !\I l ""' - 10. M,uk11 2

th on the
As we know, ere are two parsing techniques, these parsing techniques work
ing principle.
folJoW
th e input string from left to right and identifi.es that the
l. The parser scans
derivation is leftmost or rightmost.

z. The parser makes use of production rules for choosing the approp riate derivation .
g the
The different parsing techniques use different approaches in selectin
appropriate rules for derivation. And finally a parse tree is constructed.
then such
When the parse tree can be constructed from root and expand ed to leaves
tree can
type of parser is called Top-do wn parser. The name itself tells us that the parse
be built from top to bottom .
of parser
When the parse tree can be constructed from leaves to root, then such type
is called as bottom -up parser. Thus the parse tree is built in bottom
up manne r.

Types of parser

To~ow n parser Bottom-up parser

Predictive parser Shift reduce parser LR parser


Backtracking

SLR LALR LR
parser parser parser

Fig. 2.6.1 Parsing techniques

Let us discuss the -parsing techniques in detail.

Comparison between top down and bottom up parser

Sr. Top down parser Bottom up parser


No.
-
1.

2.
Parse tree can be built from root to leaves.

This is simple to implement.


Parse tree is built from leaves to root.

This is complex to implement.


-- -
®
TECHNICAL PUBL/CA TIONS · an up-thrust for knowledge
Gom{JIIOI O<ls,gn 2 - 12
- - -=-=-=-=-=-=-----_-_-_~_~_-=-_-_-_=-=-=-=-=-=- Sy nta x Ana1y

,.
- - =-=-=-=-=-=-=-~=-=-=-==---:
Wh en the bo tto m up pa
f h il, I$ ki--" dfk icn t po rsi rse r h an
ng
t: h':'
iqu:~ am big uo us gra mm ar con dle s \
\',1rim1-. pn ,bl cm s th " t ~ flic ts occ ur in
do wn tt'Chnlquc J~ om btg ur_ urm g pa rse table.
u,ty left
n._"C'u~ion .
- - - - - -- - - - - -
11 i~ npplicahlc to ~mall cla ss of lan gu ages. It is app lic abl e to a bro ad
cla ss of
lan gu age s.
- - -- - - ----
V,n ious p.,rsi.ng t1..'Ch niq ues Va rio us pa rsi ng tec hn iqu
are es are
\ ) R1._~ urs ive desL-ent par ser 1) Sh ift red uc e 2) Op era tor pre ced ence
2) Pret.iictive par ser . 3) LR par ser .

Review Question

I. Di{ft•r mtint i> top dow n appronc/r


from bottom up approach parsing with an example.
.. to

M•M•UMi'MMtitu
I ll Top-down Parsing AU : Ma y-0 4, 07 , 08 , 15
, De c.- 05 , 07 , 16 , Marks
10
• 1n top -d ov.rn pa rsi ng the
pa rse tre e is ge ne rat ed fro
to lea ve s). The de riv ati on m top to bo tto m (i.e. fro
ter mi na m root
tes wh en the req uir ed in pu
t str ing ter mi na tes.
• Th e lef tm ost d eri va tio n ma
tch es thi s req uir em en t.
',' • The ma in tas k in top -d
ow n pa rsi ng is to fin d the
or de r to pr od uc e the co ap pr op ria te pr od uc tio n
rre ct inp ut str ing . W e wi rule in
top-do,..,rn pa rsi ng wi th the ll un de rst an d the pr ocess of
he lp of so me ex am ple .
• Con sid er a gra mm ar,
S ➔ xPz
p ➔ yw ly
• Cons ide r the inp ut str ing
xy z is as sh ow n be low .

\ x \ y \ z j
i
Inp ut bu ffe r
• N~w we wi ll co ns ~ruct
t~e ~a rse tre e for ab ov e
stn ng. And for this de n va gr am ma r de riv ing the giv
ho n we wi ll ma ke us e en input
of top do wn ap pr oa ch . .
Step 1 : (Refe r Fig. 2.7.1 (a))
The firs t leftm ost lea f of the
pa rse tre e ma tches wi th
th~ fi rst inp ut sy mb ol. He
nc e we wi ll ad va nc e the
po int er. The next lea f no de inp ut
is P. W e ha ve to ex pand
no de P. After ex pa ns ion the
we ge t the no de y wh
ma tch es w ith the inp ut sy ich
mb ol y .
F.1g.
2.7.1 (a )

TECHNICAL PUBLI CA TIO


Ns® - an up-thrust for kno
wle dge
compiler Design 2 - 13 Syntax Analysis

Step 2 : (Refer Fig. 2. 7.1 (b))


Now the next node is w which is not matching with
the input symbol. Hence we go back to see whether
there is another alternativ e of P. The another alternativ e
for p is y which matches w~th current input symbol.
And thus we could produce a successfu l parse tree for
given input string.
Step 3 : (Refer Fig. 2.7.1 (c))
We halt and declare that the parsing is complete d
successfully.
• In top-down parsing selection of proper rule is . Fig. 2.7.1 (b) -
very importan t task. And this selection is based
on trial and error technique .
• That means w~ have to select a particular rule
and if it is not producin g the correct input string
then we need to backtrack and then we have to
try another productio n.
• This process has to be repeated until we get the
correct input string. After trying all the
productio ns if we found every productio n
unsuitabl e for the string match then in that case
the parse tree cannot be built. Fig. 2.7.1 (c)

fD■ Problems with Top-down Parsing


There are certain problems in top-down parsing. In order to implemen t the parsing
we need to eliminate these problems . Let us discuss these problems and how to remove
them.

1) Backtracking

Backtracking is a technique in which for expansion of non-term inal symbol we choose


one alternative and if some mismatch occurs then we try another alternativ e if any.
For .ex.ample :

S ~ xPz
P ~ ywly

I'

)

c~~~
~ompile ~~?.__ __ _ _ _ _ _ _ _ _ _2_-_1_4_ _ _ _ _ _ _ _ _ __ __s_yn
r Design _ t_ax
_ Ana/ys· ~

Then

Fig. 2.7.2
lf for a non-terminal there are multip le produc tion rules beginn ing with the
same
input symbol then to get the correct derivat ion we need to try all these alterna
tives.
Secondly, in backtracking we need to move some levels upwar d in order to check
the
possibilities. This increases lot of overhe ad in implem entatio n •of parsing . And hence
it
become s necessa ry to e~at e the backtra cking by modify ing the gramm ar.
Exampl e 2.7.1 Construct parse tree for the input string w = cad using top down parser.
S ➔ cAd

A ➔ ab/a
AU : Dec.-16 . Marks 6
Solutio n :

Step 1 : Apply rule 1. i.e. S ➔ cAd

Step 2 :

But this selection


does not yield the
required string
hence backtrack

Fig. 2.7.3
Apply rule A , ab
Step 3 : Apply ru le A , a
Thus the required string LS
parsed .

27
- - - - - - - -- -T~E~C;H~N~IC
~A~L~PU
~B: L~IC
:A: .T
: :l=
O~N~
s ®®- - -~ F~lg!.~ -~~-•~ - - - - - - - - - -
- an up-thrust for knowledge
cm,p1er Design
2- 15
Syntax Analysis
2) Left recursion

The left recursi ve gramm ar is a gramm ar which is as given below.


A~ Aa

Here ~ means derivin g the input in one or more steps. The A can be non-terminal
and a denotes some input string. If left recursio n is present in the gramm
ar then it
creates serious problem s. Because of left recursio n the top-dow n parser can enter
in
infinite loop. This is as shown in the Fig. 2.7.5.
of
Thus expans ion of A causes further expans ion of A only and due to generat ion
ma1or
A, Aa, Aa.a, Aaaa, ... , the input pointer will not be advanc ed. This causes
problem in top-do wn parsing .and therefo re elimina tion of left recursion is a must.

Fig. 2.7.5 Left recursion

• • te 1e ft recursi· on we need to modify the gramm ar. Let, G be a context free


To elmuna
gramm ar hav~g a produc tion rule with left recursion.
A ➔ Aa

A ➔~ } ... (2.7.1)

Then we elimina te left recursi on by re-writi ng the produc tion rule as :

... (2.7.2)
A' ➔ E

Thus a new symbol A , · 5


1 introdu ced. We can also verify, whethe r the modified
gramma r is equival ent to origina l or not.

TECHNICAL PUBL/CA TIONs® - an U,>-thrust for knowledge


_
c _o_
m..:._
p_ile
_ r_D
_e_s....:ig 2 _- _1_6 _ _ _ _ _ __ _ _ _ _ _ _s..:.y...:.
:..n_ _ _ _ _ _ _ _ _ _ _ _ _ _ n:.:tax Ana,
Ys,~
For example : Conside r the gramn1ar
E ➔ E+T IT
We can map this gramn,ar with the ru1e A ➔ A a I f3 . Then using equation (2
.7,J)
we can say,
A = E
a = +T
f3 = T
Then the rule becon,es,
E ➔ TE'
E' ➔ +TE' I £

Sinularly for the rule,


T ➔ T*F IF

a a a

Input string

Fig. 2.7.6
Compiler Design 2 - 17 Syntax Analysis

We ca n elimina te left recursion as


T ➔ FT '
T' ➔ * FT' I £

The grammar for arithmetic expression can be equivalently written as -


E ➔ E+ TI T E ➔ TE'
E' ➔ +TE' I E

T ➔ FT'

T ➔ T*F I F ⇒ T' ➔ * FT' I E

F ➔ (E) I id ⇒ F ➔ (E) I id
Example 2.7.2 Consider the following grammar
A ➔ ABdlAal a
B ➔ Be l b
remove left recursion.
Solution : Consider the rule,

A ➔ ABdlAala

We map this grammar with the rule A ➔ A a I~


This can be eliminated by re-writing the production rule as :

A--A ~la
♦ ♦ ♦ ♦
A A a 13

A ➔ ~A' A ➔ aA'
A' ➔ aA' ⇒ A' ➔ BdA'
A' ➔ E A' ➔ E

For A ➔ Aala
l lll
A A a~
This left recursion ca n be e1iminated as -
A ➔ ~A' A ➔ aA'
A' ➔ aA ' ⇒ A' ➔ aA'
AI ➔ £ A' ➔ E
r Compiler Design

To su m m ar iz e
.:::.yntax Ana1yais

~ aA'
A -4 ABd I Aa Ia :::::> A B dA '\a A '
A' ~
A' ~ £

B -4 S ei b
-4 A a
\A
W e m ap th is gram
mar w1'th th e ru le A 1-1·
B -4 B b e\
t i i i
A A a ~
use the rule A ➔ ~A', a.A' A '~ £ Then· w et
A➔ ' ·
eg
B ➔ b B'

B ➔ eB '

B' ➔ E
To summarize, the gr
ammar withou·t left · will be
recursio n
A ➔ a A'
A' ➔ B d A 'l aA
'
A' ➔ E

B ➔ b B'
B' ➔ e B'
B' ➔ E

3) Left factoring
If .the grammar is
left factored then it
factoring is used w becomes su it ab le
hen it is _not clear for th e us e. Basic
th at w hi ally lett
expand the non-term ch of th e tw o al te rn
inal. By left factorin at iv es is used ~o
which the decision g w e m ay be ab le
can be deferred until to re -w ri te th e prod
en ou gh of th e in pu uction ll\
choice. t is se en to make
the right
In general if

A ➔ a~ 1l a~ 2

is a production th
en it is not possible
rule or second . In su for us to ta ke a d
. .
ch a situation the above
gr am m ar ec1s1on w h e ther to choose fi~1
A ➔ a A' b ft f
can e 1e actored as

- - - --::====~- - -_____/ TECHNICAL PUBL


ICATIONS®
- an up-thrust fo r
knowledge
:'JynlaK AnalyB/8
Co mp iler Design

I lw follc lw l ng M., 0 111111111.,


Fo r ex a mp le : C nn ~ld t•r
S ➔ iE tS I iE ISt 'S I n
E ➔ b
h<•t·o m t•~I
Th e le ft fac tm -ed g rn n,m nr
s ➔ i EtSS' I A

S' ➔ eS If
E ➔ b

the left rec ursion from the following g r1u11111flf


Example 2.7 .3 EUmina te
Al l . M,ni-0 7 . M,trl.., 10
A ➔ Ac lA ad l bd l c. e then
ion we ha ve o ru le : W hen ;\ ➔ A u / (:S is rul
le ft re cu rs
So lu tio n : To e lim in ate
con ve rt it to
A ➔ f3A'
A' ➔ aA '

rs.
be ca us e of fo llo wi ng ru les the left rec ursio n occu
In th e gi ve n gr am m ar
A ➔ Ac an d A ➔ Aa d
lef t re cu rsi on we wi ll sli gh tly ch an ge the ru le :
To eli mi na te th is
A . ➔ f31A' If32A'
A ➔ A a I I A a2 I '31 I f32
⇒ . A' ➔ a1 A' la 2A '
A' ➔
A' ➔ E
-
th e re m ov al of lef t re cu rsi ve gr am ma r we ge t
Af ter
A ➔ bd A 'lc A '
A' ➔ cA ' I ad A '

El im ina te lef t recursion for the grammar. Al l : Ma y-0 8. Ma rk.. 2


Ex am ple 2. 7.4
F, F ➔ ( E) I id
E ➔ E +T IT, T ➔ T .. FI
is -
m in ate th e Jeft rec urs io n
So lu tio n : The ru )e to eli
th en conv er t it to
A ➔ Aa l '3
A ➔ f3A'
A ' ➔ aA '
A' ➔ E
uy111a,11 11na1ysis

~ ~~~ i!1_ !}_ __ __ 0


~
__ __ __ _2~ -2~- ---- --- - - -- -----
ler Design
~ ompi

Rule 1 : E ~ E + TI T

lf we ma p the above rule as T


E --t E+ T

.J,
u.J,
.J,
A A a

Then ·w e get -
E ➔ TE'

E' ➔ +TE '


E' ➔ E
Rule 2 : T ➔ T*F /F
If we map the above rule as

Then we get,
T ➔ FT'

T' ➔ *FT'
T' ➔ E
mar ize, we get follo wing set of
Rule 3 : F ➔ (E) I id is not a left recursive . To sum
rules after eliminating left recursion.
E ➔ TE'
E' ➔ +TE 'IE
T ➔ FT'
T' ➔ *FT 'I E

F ➔ (E) I id
Example 2.7.5 Do left factoring in the following grammar -
A➔_ aAB laAl a

B ➔ bBlb
Solution : If the rule is A ➔ a ~1la~2I ... is
a prod ucti on then the gram mar need s to be
left factored ..
Con side r
A ➔ aAB!aAla
LJ LJ
j, j, j, j, ,l, j,
a ~1 a ~2 a ~3

to A A'
The rule is if A ➔ a~1 l a~2I ... then convert it ➔a and A' ➔ t3 1It3 2 I
Hen ce we get
A ➔ a A'

A' ➔ AB/ Aj E
___ _
TE=C
- -- - - - - - -~ :L~P
~H:N:IC~A IC:A~Tl~O=N:s®®_-_n _ _ __ _ _ _ _ __
:B~L~
;U ___
8
up-thrust for knowledge
compiler Design 2 - 21 Syntax Analysis

Similarly
B ~ b Bib
gives B ~ bB'
j, j, j, j,
out B' 4 Blt::
A a 131 a 132
To summarize, the grammar with left factor operation will be -
A ➔ a A'
A' ➔ ABIAI E

B ➔ b B'
B' ➔ B IE
Example 2.7.6 Write down the algorithm to eliminate left-recursion and left-factoring and
apply both to the following grammar.
E ➔ E+T I E-T IT
T ➔ a l b KE) AU : May-15, Marks 8
Solution : Rule to eliminate left recursion is

~'
can be written as -
A ➔ J31A'j ... jJ3mA'
A' ➔ a 1A'j ... janA'IE
Rule to do left factoring is ·
If A ➔ a J3 1 la J3 21 ... is a production then
A ➔ aA'

A ' ➔ J31 I J32I · · ·

Rule 1 : E ➔ E+TJ E-TJ T


We will map the rule as -

Then we get
E ➔ TE'
E' ➔ +TE'I-TE' IE
Rule 2 : T ➔ a I b I (E) does not contain left recursion

4) Ambiguity
The ambiguous grammar is not d esirable in top-down parsing. Hence we need to
remove the ambiguity from the g ra mma r if it is present.
®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
2 - 22
~
S11n,_
Compiler Des,gn - : _ _ _ - - - - - - - - - - - . : ' ' '1.6;, A,.~
----------- •141~

(a) Parse tree 1 (b) Parse tree 2

Fig. 2.7.7 Ambigous grammar

For example :
E ➔ E + E r E * E I id
is an ambiguous grammar. We will design the parse tree for id+ id* id as follows.

Removal of ambiguity
For removing the ambiguity we will apply one rule : If the grammar has left
associative operator (such as +, -, *, /) then induce the left recursion and if the
grammar has right associative operator (exponentia l operator) then induce the right
recursion.
The unambiguo us grammar is
E ➔ E+T
E ➔ T

T ➔ T*F

T ➔ F

F ➔ id
Note on e thing that the grammar is unambiguo us but it is le ft recursive al".
elimination o f su ch left recursion is again a must .
Example 2 .7 .7 Explain ambiguous grammar G : E ➔ E + E I E * E I (E) I - E I id fat ,-
sentence id + id* id. IJ•Mrl4Wf#d111
SoJution :
• Ambiguous g rammar is a gramma_r using whkh we can build more thaJl cP

_ ___---- ---~ ---- ---- ----


P
__:_
arse trees for d e riving the sam e string.

TECHNICAL PUBLICA TIONs® - an up-thrust for knowledge


__,.,/
compiler Design 2 - 23 :Jyntox AnaJy~ttS

• Cosider string id + id * id
E E

E
/I\
+ E
/1\E
E *

I /I\
Id E • E
/1\ I
E + E Id

I IdI
id
I idI
id

Parse tree 1 ParH tree 2

Fig. 2.7.8
• From above ·construct ion, we get two different parse trees clearly. Hence given
grammar is ambiguou s grammer.
There are two types by which the top-down parsing can be performed .
1. Backtracking 2. Predictive parsing
Top-down parsers

Backtracking Predictive parsers

Recursive LL( 1) parser


descent

Fig. 2. 7.9 Types of top-down parsers


A backtrack ing parser will try different productio n rules to find the match for the
input string by backtrack ing each time. The backtrack ing is powerful than predictive
parsing. But this technique is slower and it requires exponent ial time in general. Hence
backtrack ing is not preferred for practical compilers .
As the name suggests the predictiv e parser tries to predict the next construct ion using
one or more lookahea d symbols from input string. There are two types of predictive
parsers :
1. Recursive descent 2. LL(l) parser.
Let us discuss these types along with some examples .

Review Question s

1. What are the necessary conditions to be carried out before construction u

AlJ : Dec . -05. 07 . Marks 2


2. What is an ambiguous grammar ? Give an example.

TECHNICAL PUBLICATIONS® - an up-thrust for knowledge


Compile r Design

IIJ Recursive Descent Parsing


2 - 24
-
Syntax Analysts

. dur es for parsin g the giv en inp


•A par ser tha t uses coll ection of recu rsiv e proce ut
stn ng is call ed Rec urs ive Des cen I th' type of parser t h c CF·C 1.s use
· t (RD) Parser. n is d t0
bui ld the recu rsiv e routines. The R.H f h d f1on rule 1.s dir .
ect ly con ve rted to
. .S. o t e pro uc ·
pro gra m. For each non-termm al a sep ara te pro ce ~dur e is wri tten and bod y of thi'I
proced ure (cod e) is R.H.S. of the cor
resp ond ing non -ter min al.
e

Basic steps for construction of


RD parser
The R.H.S. of the rule is directly con
verted into pro gra m cod e sym bol
by sym bol .
1. lf the inp ut symbol is non-ter
minal then a call to the pro ced ure
cor res pon din g to
the non -ter min al is made.
2. lf the inp ut symbol is terminal the
n it is matche d wit h the loo kah ead
The loo kah ead poi nte r has to be adv from input.
anc ed on ma tch ing of the inp ut sym
bo l.
3. If the pro duc tion rule has ma
ny alternates the n all the se alte
com bin ed into a single bod y of pro rna tbs has to be
ced ure .
4. The par ser sho uld be activated
by a pro ced ure cor resp ond ing to
the sta rt symbol.
Let us tak e one example to und erst
and the con stru ctio n of RD par ser
gra mm ar hav ing star t symbol E. . C ons ide r tht!

E ~ num T
T ~ * num T I E
pro ced ure E
{
ti loo kah ead = num the n
{
ma tch (nu m) ;
T;
/* call to procedure T • /
}
else
error;
ti loo kah ead = $
{
declare success; /* Return on succu1:11:1 ·,. /
}
else
error;
} /"' end of procedure E• /
pro ced ure T
{
if look ahe ad ""' '•'
{
ma tch ('*') ;
if lookah oud • 'num '

TEC I IN/CAI /JUI-J LICA I ICJN s'Jti


fl ll '''' ll1111 ti/ ftn 1,,, 11 ,w/11, tu,,
Compiler Design
2 - 25 Syntax Analysis

{
match(num);
T;
} /* inner if closed*/
else
error;
} /* outer if closed*/
else NULL; /*indicates E Here the other alternate is
combined into same procedure T */
} /* end of procedure T* /
procedure match( token t )
{
if lookahead =t
lookahead = next token- - I /*lookahead pointer is advanced*/.
else
error;
} /* end of procedure match*/
procedure error
{
piint("Error!!!");
} /* end of procedure error*/

Fig. 2.8.1 Pseudo code for recursive descent parser


The parser constructed above is recursive descent parser for the given grammar. We
can see froin above code that the procedures for the non-terminals are written and the
procedure body is simply mapping of R.H.S. of the corresponding rule.
Consider that the input string is 3 * 4. We

~
E➔ num T
will parse this string using above . given
procedures.
The parser will be activated by calling
T➔ * num T
procedure E. Since the first input character 3
is matching with num the procedure match
(num) will be invoked and then the
T➔ * num T
lookahead will point to next token. And a
call to procedure T is given.

A match with '*' is found hence lookahead 13 1 * 14 I $ I Declare Success !

= next_token. t
Now '4' is matching with num hence again the procedure fo r match (num) ts
fulfilled. Then procedure for T is invoked. And T is replaced by c.
As lookahead pointer points to $ we quit by reporting success. Thus the input string
can be parsed using recursive descent parser.

®
. TECHNICAL PUBL/CA TIONS - an up-thrust for knowledge
2_-_26_ _ _ _ _ _ _ _ _ _ _SYntax An~
==s,~·g~n_ _ _ _ _ _ _ _ _ _ _
......:p
C_om
_ e~r De
::...:.i:..=l
~
. How ever e prog ramm ing langu
th
Construction of recursive descent parser is easy
recu rsion . The inter nal details are age
that we choose for RD parse r should supp ort 11
<lt
in this pars er we cann ot acce ss the
accessible in this type of parser. For insta nce; current
nt, we cann ot access the stack containin
leftmost sente ntial form. Secondly at any insta
g
recu rsive calls .
(A -Ht~1la~2) is crucial. And to ll\ake
Writing procedur es for left recursive gram mar
factor the gram mar. We cannot wnte .
such procedur e simpler first we have to left ,
· d ext free gram mar.
recursive esce nt parsers for all types cont

Example 2.8.1 Consider the following grammar


E ➔ T +El T
r ➔ v•r iv
V ➔ id
mar to make a recursive descent
Write down procedures for nonterminals of the gram
parser.
Solu tion :
Procedure E ( )
{
T ();
if ( look ahea d = '+ ')
{
matc h('+' );
E ( );
}
else
error ( );
} if (lookahead = '$') { decl are SUCCESS ;}
Proc edur e T { )
{
V ( );
if (lookahead = '*')
{
matc h ('*');
T ( );
}
else
error ( );
}
Proc edur e V ( )
{
if (lookahea d = 'id')
match ('id');
else
erro r ( );

TECHNICA L PUBLICATIONs® · an up th 111


• st for knowledge
Compiler Design 2 - 27 Syntax Analysis

PrOD8Cl'U18 match (token t)

if (lookabead = t)
lookahead = next -
token· I

el8e
error;

PIOaclme error { )

~ Print (" Error I 11


);

~-------
Example 2.8.2 What is recursive . descent parser ? C.onstruct a recursive descent parser for
the following grammar,
E➔ E+TIT
T ➔ TFIF
F ➔ F*lalb.

Solution : Recursive .descent parser : Refer section 2.8.


The given grammar is
E ➔ E+T IT
T ➔ TF IF
F ➔ F*la lb.
Which is a left recursive grammar. We will eliminate this left recursion before
converting it to recursive descent parser. The rule to eliminate left recursion is
.if A ➔ A a I~ then A ,:-~ ~A'
Convert to A' ➔ aA'lc

Consider
1) E ➔ E + T IT E ➔ TE'
J, J, ~ J,
A A a ~
E' ➔ + TE' I£
T ➔ T F IF
2) J, J, J, J,
A A a ~

F ➔ F * lalb F ➔ aF' lbF'


3) J, J, J, J, J,
A Aa ~ B . F' ➔ *F' I £

To summarize,
E ➔ TE'
E' ➔ +TE' le
T ➔ Ff'
-
T' ➔ Ff' I E
F ➔ aF' I bF'
F' ➔ ""F' IE
Now the recursive descent parser is .
Procedure E ( )
{
T( );
Edash ( );
}
Procedure Edash ( )
{ .

if (lookhead = '+') ·
{
match('+');
T( );
· Edash ( );
}
else null;
}
Procedure T( )
{
F( );
Tdash ( );
}
Procedure Tdash ( )
{
if (true) ·
{ F ( );
Tdash ( );
return;
}
else null;
}
Procedure F ()
{
if (lookahead = 'a' 11 lookahead = 'b')
Fdash ( );
}
Procedure Fdash ()
{
if (lookahead = '*')
Fdash ( );
else null;
}

®
TECHNICAL PUBLICA TIONS - an up-thrust for lcnowledge
eompiler Design 2- 29 Syntax Analysis

Example 2.8.3 Write a recursive descent parser for the' grammar.


bexpr ➔ bexpr or bterm I btenn
bterm ➔ bten,z and bfactor I bfactor
bfactor ➔ notbfactor I (bexpr) I true lfalse
Where or, and, not, (,), true, false are terminals of the grammar.
Solution : For simp lification we will replace the given non terminals by another non
temunal symbols.
bex-pr = E
bterm = T
bfactpr = F

or = +
and = *
Now the grammar becomes
E ➔ E+T I T

T ➔ T*F I F

F ➔ ! F I (E) I true I false


But the grammar IS left recursive. We will eliminate the left .recursion using
following rule.
if A ➔ Aal~ then ·A ➔ ~A'

A' ➔ aA'IE
l)E ➔ E+TIT E ➔ TE'
j, j,~ j,
A A a ~
. E' ➔ +TE' I€

2) T ➔ T *FI F T ➔ FT'
j, j, ~ j,
T' ➔ *FT' IE
A A a ~

3) F ➔ ! FI (E) I true I false does not contain left recursion.


To summarize
E ➔ TE'

. E' ➔ +TE' IE
T ➔ FT'
T' ➔ *FT'I E

®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
c0mpi/et 00s1g11
r 1!1·1(1() 11n1l' lf,il~c
Th•· l'""'•dun· w recursiv•• Jesccnl parsing are _

J'l1oc-odu11' E ( )
{
T( ):
Edash ( );

Procedure Ectash ( )
{
if (lookhead == '+ ')
{
match('+');
T( );
Edash ( );

else null·'

Procedure T( )
{
F( );
} Tdash ( );

Procedure
{ Tdash ( )

{if (lookahead = '*')

match ('*')·
F ( ); I

} Tdash ( );

} else null·
Proc d '
{ e ure F ( )

if Oookaheact = '!')
{

} match ('!');

else .f F( ) .
{ i Oookah .' = '(' )
eact

match('('
E ( ); );

'f
l Oookaheaa == ')' )
}
match ( ')');
compiler Design 2 - 31 Syntax Analysis

else if (true)
return true
else
return false;
}
Advantages of recursive descent parser
]. Recursive desce nt parse rs are simpl e to build.
tree.
2. Recursive desce nt parse rs can be const ructed with the help of parse

Limitations of recursive descent parser


1. Recursive desce nt parse rs are not very efficient as comp
ared to other parsin g
techniques.
an infinite loop for
2. There are chanc es that the progr am for RD parse r may enter in
some input.
3. Recursive desce nt parse r can not provi de good error messaging
long.
4. It is difficult to parse the string if looka head symbol is arbitrarily

flJ Predictive Parser LL(1) Parser - 3,16,17,18, Dec.-05,09,12,16,22, Marks 16


Lt . . AU : May-0 4,07,1 1,12,1

type of parsin g a
This top-d own parsin g algor ithm is of non-recursive type. In this
from left to right. The
table is built. For LL(l) - the first L mean s the input is scann ed
the numb er 1 in the
second L mean s it uses leftm ost deriv ation for input string. And
to predi ct the parsin g
input symb ol mean s it uses only one input symbol (lookahead)
process.
parsi_ng table. The
The data struct ures used by LL(l) are i) input buffe r ii) stack iii)
is used to hold the
LL(l) parse r uses input buffe r to store the input tokens. The stack
the stack in reverse
left sentential form.The symb ols in R.H.S. of rule are pushe d into
ithm non-recursive. The
order i.e. from right to left. Thus use of stack make s this algor
for non-t ermin al and
table is basically a two. dimen siona l array. The table has row
A is a non-t ermin al
column for termi nals. The table can be repre sente d as M[A, a] where
and a is curre nt input symb ol. The parse r works as follows -
The simple block diagr am for LL(l) parse r is as given below

®
TECHNICAL PUBL/CA TIONS - an up-thrust for knowledge
~C-Omp,ler~~Oe.~cSlg/)~
~
- - - - -- -
----
Input
token
a
2-32

+ I J$ I
J
b
J
u.(1) Par98'
:,._-._. Output

Top
-- s
s
-Staci< --
-
Parsing table
/

b $
-
~

a +
~

A -
B -
C ~

current input symbol. With the


of the stack an d a
els t0
The parsing program re.a ~ . . d rmined. The parsing actions can be
help of these two symbols the parsing action IS ete
,,_
Parsing action
Top Input token
- -- - -
s s Parsing successful, halt. _
- a A Pop a and advance lookahead to next token.
--

- -
A
A
a
-- B
a

a
Error. - ---
Ref.er table M[A, a] if entry at M[A, aJ is e-rror repart Error.
Refer table M{A, aj if entry at MfA, a] is A ➔ PQR then pop
A then push R, then push Q then push P.

TitE parser con5ults the table M[A, a] each time while taking the parsing actions
h(..-nc:e this type of parsing method is called table driven parsing algorithm. The
configuration of LUl ) parser can be defined by top of the stack and a lookahead token.
One by one configuration u; performed and the input is successfully parsed if the parser
r ~ the halting configuration. When the stack is empty and next token is $ then it
"--'"~ ro 5 ~ful parse.

DI■ Construction of Predictive LL(1) Parser

~f'hf; a~roctkm <n


predktive LUl) parser is based on two very important functiOOS
f.1110 ~~ ;)fl; FIRST and FOLWW.

1'ur (:f,n+,trod,im, c,f pred ictive LUJJ parser we ruive to follow the following steps ·
J, 0,rrtpv.tatvJn m fHl'tf amJ FOLLOW function.
zr n~JnJ.t:t tw.: prt:.dktive parsing table w;ing FIRST
11 d FOLLO .
an W functions.
,, Vi,r-~- tlv· mp ut $tring with the help of predictive . b
J parsing ta le.

TE(;HI l/<;ftL PU8LJCA TIONS<'f.l • an u,n..J ...


,,-.,,rust for know1edge
compiler Design
2 - 33 Syntax Analysis

FIRST function

FTRST(a) is a set of terminal symbols that are first symbols appea ring at R.H.S. in
derivation of a. If a ⇒ E then E is also in FIRST (a ).
Follov,ring are the rules used to compute the FIRST functi ons.
1. If the terminal symbol a then FIRST(a) = ·lal.
2. lf there is a rule X ➔E then FlRST(X)=l£1.
3. For the rule A ➔ X1 X2 X3 ... Xk FIRST(A) = (FIRST(X ) u FIRST(X ) u FIRST(X ) ...
1 2 3
u FIRST(Xk))
,,..,here k X j ~ n such that 1 ~ j ~ k- 1.

FOLLOW function .

FOLLOW (A) is defined as the ·set of terminal symbols that appear immediately to
the right o_f A. In other words
'*
FOLLOW(A) = { a I S ⇒ a Aa J3 where a and J3 are some grammar symbols may be
terminal or non-terminal!.
The rules for computing FOLLOW function are as given below -
1. For the start symbol S place $ in FOLLOW(S).

2. If there is a production A ➔a B J3 then everything in FIRST( J3) without £ is to be


placed in FOLLOW(B).
3. If there is a production A ➔ a B J3 or A ➔ a B and FIRST( J3 )= {£} then
FOLLOW(A) =
FOLLOW(B) or FOLLOW(B)=FOLLOW(A). That means everything
in FOLLOW(A) is in FOLLOW(B).
Let us take some examples to compute FIRST and FOLLOW functions using above
rules.
Example 2.9.1 Consider grammar
E ➔ TE'
E' ➔ +TE' I £

I .T ➔ FT
1 T' ➔ *FT 1£
F ➔ ([)I id
Find the Frn ST nnd [OLLOW f 11nctions for flu· a/Jove grnm 11111r. AU : Dec.-05. Marks 12
Solution : E ➔ TE' is a rule in which the first sy mbo l at R.l-1 .S. is T. Now T ➔ FT' in
which the first symbol at R.H.S. is P anJ there is a rult> for Fas F ➔ (E) I id.
t~

®
TECHNICAL PUBLICATIONS - an up-thmst for knowledge
Com piler Des ign 2 - 34 Synt ax Ana/YSis

:. FlR ST( E) = FIRST( T) = FlR ST( F)


T ~ Ff' - - rep lace F by R.H.S. rule
As
f ~ (E)
F ~
id
I
I

\
/ j
T ~ (E)T ' I idT .

Hen ce FlR ST( E) = FlRST(T) = FIRST(F) = I ('1.d l :: Thi s can


be add ed in
: FIRST (T)
FIRST(E') = (+, E)
As E' ~ +TE'
E' ~ by refe rrin g com put atio n rule 2
E

The firs t sym bol app eari ng at R.H .S. f


o prod uc tion rule for E' is add ed in the
FIRST fun ctio n.
FIRST(T') = (*, E)
As T' ~* TE'
E' ~ E

The firs t term inal sym bol app ear ing at


R.H .S. of pro duc tion rule for T' is add
the FIRST fun ctio n. Now we wil l com ed in
put e FOL LO W fun ctio n.
FOL LOW (E) -
i) As the re is a rule F ~ (E) the sym bol
')' app ear s imm edi ate ly to the righ t
of E.
Hen ce ')' wil l be in FOLLOW(E).
ii) The com put atio n rule is A ~ aBl3
we can ma p this rule wit h F ➔ (E)
a = (, B = E, 13 = ). then A= F,

FOLLOW(B) = FIR ST$ ) = FIRST( ) ) = \ ) }


FOL LOW (E) = I )l
Sin ce E is a star t sym bol , add $ to foll
ow of E.
Hen ce FOL LO W(£ ) = I ),$}

FOLLOW(E') -
i) E ~ TE' the com put atio nal rule
is A~ a Bl3 ..
A = E', a = T, B = E'' 13 =E the n
by com put atio nal rule 3 eve rythin~
FOL LO W(A ) is in FOLLOW(B) i.e. eve iJ1
ryth ing in FOL LO W(E ) is in FOLLOW
(E').
· FOL LQ W(E ') = I ), $ }
ii) E' ~ +TE' the com put atio nal rule is
A ~ a B 13_
A -- E'' a = +T ' B = E' A =c th
' .., en b Y. com put atio nal rule 3 eve ryt h. I.! i1'
FO LLO W( A) is in FOL LOW (B) i.e. eve ino
ryth ing in FOL LO W(E ') is in FOLLOW
(E').
TEC HNIC AL PUB LICA TION s® - an
up.t hrus t for know ledg e
compiler Design 2 - 35

FOLLOW(E') = I ),$ I
We can observe in the given gramm ar that ) is really foll owing E.

FOLLOW(T) •
We have to observe two rules
E ➔ TE'
E' ➔ +TE'
i) Consider
E➔ TE' we will map it with A ➔ a B f3
A = E, a= E, B = T, f3 = E' by comput ational rule 2 FOLLOW(B) = IFlRST (ts) - ti,
That is FOLLOW(T) ={FIRST(E') - E)

= {{+,E}-E}
= l+l
ii) Consid er E' ➔ +TE' we will map it with A ➔a B f3
A = E', a = +, B' = T, f3 = E' by comput ational rule 3
FOLLOW(A) = FOLLOW(B) i.e. FOLLOW(E') = FOLLOW(T)
FOLLOW(T) = {),$)
Finally FOLLOW(T) = l+l u 1),$1
= I+ ,), $I
We can observe in the given gramm ar that + an<;i ) are really followi ng T.

FOLLOW(T') -

T ➔ Ff' -

We will map this rule with A ➔ a Bf3 then A = T, a = F, B = T', f3 = E then


FOLLOW(T') = FOLLOW(T) = l+,),$1
T ➔ *Ff'
We will map this rule with A ➔ a Bf3 then A = T, a = *F, B = T', f3 = E' the,>J)
FOLLOW(T') = FOLLOW(T) = I+,),$}
Hence FOLLOW(T') = l+,),$1

FOLLOW(F).

Consider T ➔ FT' or T' ➔ *Ff' then by comput ational rul e 2,

TECHNIC AL PUBL/CA TIONS


®
- an u1rthrust for knowledge
2 - 36 Syntax Ana1Yfit
Compiler Design
-- - ~::__-
T' ~ •FT'
----- ~

T ➔ FT'
A - HX Bil
A~ a BP
A = T, et = r , U = F, II = T'
A = T', a = •, B = F, p = T'
FOLLOW(B} = IFiRST(f:3) - El
FOLLOW(U) = IFll{ST (P) - i:I
FOLLOW(F) = IFIRST(T') - El FOLLOW(F) = IFIRST(T') - El

FOLLOW(F) = 1•1 FOLLOW(F) = 1•1

Consider T' ➔ •FT' by computational rule 3

T' ➔ •FT'
A ➔ a Bp

A = T', a = *, B = F, p = T'
FOLLOW(A) = FOLLOW(B)
FOLLOW(T') = FOLLOW(F)
Hence FOLLOW(F) = l+,),$1

Finally FOLLOW(F) = l*l u l+,),$1

FOLLOW(F) = I+,*,),$)

To -summarize above computation

Symbols FIRST FOLLOW

E l(,idl 1),$1

E' {+,E} 1),$1

T l(,idl ) $I
(+,,

T' {\ 1:} (+,),$1

P l(,idl (+/,),$1

Algorithm for predictive parsing table -


d pt\'I.f '-ti\t
The construction of pre icti ve parsing table i~ 1:1 n irn portant dctivity in 1
parsing method. This algorithm requires PlRST and FOLLOW functil)n~.
Input : n,c context: free gramrnar C.
Output •. Predictive paroing table M.

®
Tf;CIINICAI rtJnt,IGAT!ONS • 011 lll)•th111s1 fot k11ow1octgt1
compiler Design 2 • 37 Syntax Anolysw

Algorithm :

For the rule A - Hx, of gra mnu,r G


1. For each a in FIRST(cx. ) create entry MIA, a] = /\ -► <X where a iB terminal symbol.

2. For E in HRST(cx.) create entry Ml A, b] = A - Hl

Where b is the symbols from FOLLOW(A).


3. If -E is in FIRST(.a) and $ is in FOLLOW(A) then create entry in the table
M[A, $] = A .➔ ex..

4. All the remaining entries in the table M are marked as SYNTAX ERROR.
Now we will make use of the above algorithm' to create the parsing table for the
grammar-
E ➔ TE'
E' ➔ +TE'lc
T ➔ Ff'
T' ➔ *Ff' 1£
F ➔ (E) I id

First create the table as follows :

id + .. ( ) $

E
I

E' , I

I
T
I I I l
r f
T'
! I
I '

F
I r'

Now we will fill up the entries in the table using the above given algorithm. For that
consider each rule one by one.
E ➔ TE'
A ➔a
A = E, a = TE'
FLRST(TE') if E' = E then FI RST(T) = I(,id I
M[E, (] = E ➔ TE'
MIE, id] = E ➔ TE'

TECHNICAL PUBLICA TIONs ® - an up-thrust for knowledge


~Desig.,.
2 - 35

E' ~ +TI·

A ---4 a
A== E', a == +TE·

FlRST( +TE') == \+ }

Hence M[E, +] == E' ➔ +TE'

E' ➔ 1:
A ➔a

A = E', a = 1: then
FOLLOW(E') = {),S}
M[E', )] = E' ➔ 1:
M[E, $] = E' ➔ 1:

T ➔ Fr'
A ➔a

A = E', a = Fr'

FIRST(Ff') = FIRST(F) = l(,id}


Hence M[F,(] = T ➔ Fr'
And M[F, id] = T ➔ IT'
T ➔ *Fr'
A ➔a

A= T, a = *Fr'
FIRST(*FT') = l*l

Hence M[T, *] = T ➔ *Fr'


T' ➔E

A ➔a

A = T', a =E

I
FOLLOW(T') = l+,),$1
Hence M[T', +] = T' ➔ E
MfT',)] = T'➔ E

TECHNICAL PUBLICA T/ONs® - an u~thn.,s' '~r


compiler Design 2- 39 Syntax Analysis

M[T', $] = T'➔ E

F ➔ (E)

A ➔ <X
A= F, a = (E)
FJRST((E)) = l ( I
Hence M[F,(]= F ➔ (E)
F ➔ id
A ➔a

A= F, a = id
FIRST(id) = { id I
Hence M[F, id] = F ➔ id

The complete table can be as shown below :

id ,. ( ) $
+
·,. Error
E E ➔ TE' Error Error E ➔ TE' Error

Error E ➔ +TE' Error Error E➔ E E➔ E


E' ' ;

E ➔ .Ff I Error Error T' ➔ FT' Error Error


T
Error T' ➔ E TI ➔ *FT' Error T '➔E T' ➔ E
T'

F ➔ id Error Error F ➔ (E) Error Error


F

Now the input .string id + id * id $ can be parsed using above table. At the initial
is
configuration the stack will contain start symbol E, in the input buffer input string
placed.

Stack Input Action

$E id + id * id $

Now symbol E is at top of the stack and input pointer is at first id, hence M[E, id] is
referred. This entry tells us E ➔ TE', so we will push E' first then T.
Stack Input Action

$E'T id +id* id$ E ➔ TE'

$E' T' F id+ id* id$ E ➔ FT'

$E'T' id id+ id * id $ F ➔ id

TECHNICAL PUBLICATIONS® - an up-thrust for knowledge


Compiler D esign 2 . 40
--
$E' T I id • id 'f,
+ iJ ♦ iJ $ r' ) I
$E'
I~- ~ I I I•'
$E' T+ t id -+- id !l>

$E'T id • id $
id "' iJ $ T > Fr'
$E' T' F
$E' T' id id * id $ F > id

$E' T' * iJ $
T > .. FT'
$E' T ' F* * id $
$E'T F id $
_J;E' T' id id $ F -> id
$E' T' $
$E' $ T - > t:
$ $ E' ➔ €

Thus it is observed tha t the input is scanned fro m le ft to ri g ht a nd w e ..ilwa ys f1 1llnw


left m ost d erivation while parsing the input string . Also a t a tim e o nl y o n e in put sy n1hnl
is referred to taking the p ars ing actio n. H en ce the na m e o f this p a rsL'r is LL( I). ·1he
LL(l) parser is a table driven predictive p a rser. T he le ft rec urs io n a n d a m big u()us
grammar is n o t a llowed for LL(l) pa rser.
Example 2.9.2 Construct parsing tnble for t/Je grnm111ar a11rl find !he 1110VI!!- 111od,• li11
p~edictive parser on input id + id * id and find FIR ST anrl FO LLO W .
E ➔ E +T
E ➔ T

T ➔ T*F
T ➔ F

F ➔ (E) / id
AU : Dec.-16. Marks 10
Solution : The given gram.mer is left recursive W e ca n t
. . • n o p a rse the s trin g if the
g rammar 1s left recursive. Hence we can elimina te left recu rs ion .
For elimination of left recursion - Re fer example 2.7.4.
W e get
E ➔ TE '

E' ➔ +TE' IE
T ➔ FT'
T'➔ * FT' IE
F ➔ (E) I id
For findin g FIRST and FOLLOW of above gr ~
p arsing ta ble refer example 2.9.1. · ammar and fo r bui ldi n g pred i<.i JV

:.._____:~-- - --=-- ~~=~~;--------- -----


TECHNICAL PUBLICA TIONs® - an up-thru t , k
s ,or now/edge
Compile, Desfr,n
- - - -- -- -- c Nynl,; ,, A111Jly~/ti

Examplos with Solution

Examplo 2.9.J ,%ori 1 tll,11 fv ll11w l11,1.t' j~r,111111m1· ,·


S -► A11 Af, I lll1H11
A ➔ l'
, 13 -) l'

is Lt (1).

Solution : (.\m t-idc r the gmm innr :


S ➔ AnAb
S -► BbBn
A ➔ t:
B -) E

fun ction~.
Now w~ will cmn put~ FrnST ond FOLLOW
FlRST(S) = la, bl
Beca use 1) S -+ AaA b
Suppose if A ➔ r:: then
S ➔ aAb lab

Similarly 2) S ➔ BbBa

S ➔ bBa Whe n B ➔ r::

FIRST(A) = FIRST(B) = lr::I


FOLLOW(S) = 1$1
FOL LOW (A) = FOLLOW(B) = la, bl
The LL(l ) parsing table is
a b $

s S 4 AaAb ·S 4 BbUo

A A 4e A-H

B B ➔ t: B -+ e

-
Now cons ider the strin g "ba" . For pars .i ng

Input Action
Stack
ba$ S - ► BbB.,
$S
B ➔ r
$a8bB
bo$

$a8b bo$

- - - - - --
- - - - - -- - - - - - - -- -s® - an up-thrust for knowledge
TECHNICAL PUBLICA TION
( f .1/l f/)/ft1 / / )1jtj/f} rl
;> 47

a1l
n!\,

s $
-- -- ~<< <'pl

·1 Il l~, 11h1lWH 1h,,1 1lw >\ivl' ll grnm mar iH


I.L( l)
l IU\ llll)I O 2.9.4 f'11r 11w fnllmoin~ .~n11111n11r find f /RST all£/ l'Ol
.LO W sets for each of n~
/1 •111111111/ ,

"' ► t1AHll,AI,,
A > nAIJ Ir
H -> '7131 r
Solu tion :

PIRST(S) ;:,; The first te rmin a l sym bol app


ea ring on R.H .S.
Pl RST(S) = {a, b, e}
FTRS T(A ) = Th e first te rmin al sym bol
app eari ng on R.H .S
FlR ST( A) = {a,e}
Pl RST(B) = The first term inal sym bol app
eari ng on R.H .S. of prod ucti on
rule for B
Pl RST(B) = {b, £}
No w, w e will com pute FOL LOW func tion
as follo ws -
POL LOW (S) = {$} ·: S is a star t sym bol
FOL LOW (A) = {b} beca use
Con s ide r the rule ,
A ➔ a B13 whi ch can be map ped with
A ➔ a A B The n the FIRST(l3) = FIRST(B) = {b, £}
j, j, j,
a B 13
The n with o ut c rem ains b. Hen ce b e FOL
LOW (A).
No w con s ide r the rule A ➔ a B then " eve
ryth ing in
FOL LOW (A) = FOL LOW (B). This rule
can be map ped with
S ➔ b A
j, j, j, :. ever ythi ng in FOL LOW (S) = FOL LOW
(A)
A a B
: . FOL LOW (A) = { $}
To s umm a rize FOL LOW (A) = { b, $}

TEC HNIC AL PUB LICA TION S® - an up-th


rust for know ledge
compiler Design

Now consider the rule,


2 - 43 Syntax Analysis l
S ➔ aA B
if we map .j, .j, .j,
· Then according to this rule,
A ➔ a B
everything in FOLLOW(S) = FOLLOW(B)
FOLLOW(B) = {$}

BRST(A) :; {a, i::} FOLLOW(A) = {b, $}


FIRST(B) = {b, i::} FOLLOW(B) = {$}
FIRST(S) = {a, b, i::} FOLLOvy(s) = {$}

Example 2.9.5 Con.sider the grammar


textp ➔ atom I list

atom ➔ number I identifier


list ➔ ( textp-seg)

textp-seg ➔ textp, textp-seg I textp


i) Left factor this grammar
ii) Construction FIRST and FOLLOW sets for the nonterminals
iii) Show that resulting grammar is LL(l)
iv) Construct LL(l) parsing table for the resulting grammar.
Solution : For the given grammar let us list out the set of terminals and nonterminals
first. Set of te_rrninals i.e. T = {number, identifier, (, ), ,}
Set of nonterminals i.e. V = {textp, atom, list, textp-seg}
i) Consider the rule and map it against A ➔ a~1la~2-
textp-seg ➔ textp, textp-seg I textp
j, J,' :j; 'J, j,
A a ~1 a ~2 (i.e. i::)

The grammar after left factorization will be -


A ➔ aA'

A' ➔ ~1 I ~2. 1--·


textp-seg ➔. textp textp-seg'
textp-seg' ➔ , textp-seg IE
ii) After left factoring, all the production rules can be enlisted as below -

textp ➔ atom I list


atom ➔ number I identifier

®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
Comp,ler Design 2 - 44 ◄
li~t ~ (l c ,t p -~c g)

tc, p -~c ~ ➔ h __,,t1r1 tl', tp -~c g'


t-t~, tv-~q1,, -4 , tc, I p -8c g
It::
Nt"m · fl1r c,,l-h tllm -tc
rmin,-ll ~y m b o l the FfR ST a nd FOLLOW co uld b e cor
np l ' t
Ccl , ,
FIR ST(tc ,tp ) = Fir~t te nrn.
na l sy m b o 1 c1ppea 1.·m g o n. R H S fo r the rul
· · · · e
textp.
textp ➔ atOIT\ ➔ nu mb er Iide
nti fie r
:. nu mber an d ide ntifie r is
ad de d in FI RS T( tex tp) .
textp ➔ lis t ➔ (te xtp -se g)
: . ( Wi ll be in FlRST(textp
))
FIRST(textp ) = I (, nu mber,
identifier}
FIRST(atom ) = First ter mi
na l sy mb ol ap pearing at R.H
.S. of ru le for
ato m.
ato m ➔ nu mb er I ide nti fie r
FlRST(atom ) = !nu mb er, ide
ntifier}
FIRST(lis t) = I ( I
FIRST(te xtp -seg)= Th e firs t
sy mb ol ap pe ari ng af R.H.S.
of rul e for tex tp-se g.
textp-seg ➔ textp textp-seg'
~
➔ atom I list
·L - 1 L.....J

number~ id en ti~

FIRST(textp -seg) = !nu mbe ( tex tp- seg )


r, ide nti fie r, ( }.
FIRST(textp -se g') = {, ,£} .
No w w e wi ll co mp ute FO
LL OW .
FO LL OW (textp ) = i) As tex
tp is a sta rt sy mb ol ad d $
to it.
tex tp-seg ➔ tex tp tex tp- seg
'
tex tp , tex tp- seg
i

FO LL OW (tex tp) = , is ad de d in FO LLO W .


{$, ,}
FO LLO W(a tom ) =
FO LL OW (tc xt p) be ca us e
.-. FO L LO W(a torn) tex tp ➔ a to m .
= {$, , }
FO LLO WS(liHl)

- - -- --=-- -~~~~~~~---------
=
FO LL OW (te xt p ) be ca u se

TEC HNICA L PU BL ICA TIO


tex tp ➔ lis t .

NS® - an up-thrt1st for kno


wle dge
-
com piler Design 2 45 Synt r,x Anfll y.'1 /'l

fOL LOW ( list) = {$, , }

FOL LOW (tc\ tp-scg) = i) list ➔ (tcx tp-s cg)


i
textp-seg --4 textp textp -seg'
l l l
A ex 8

i.e. ) fo ll ows tcxt p-sc g


) is add ed in FOLLOW
FOLLOW (text-seg) = I ) f
a rule if A ➔ aB
FOLLOW (tex tp-s eg') = i) As ther e exis ts
LOW(B).
then eve ryth ing in FOLLOW(A) is in FOL

:. FOLLOW (tex tp-s eg) = FOLLOW(textp-seg') = I ) f.


FIRST(textp) = I (, num ber, iden tifie r ) FOLLOW(textp) = {$, '}
·-- - ~- -
FIRST(atom) = /num ber, iden tifie r I FOLLOW(atom) = {$, 1 }
- ---- - ·-
FIRST(list) =I ( I FOLLOW(list) = {$, 1}

--
-
- = I (, num ber, identifier!· FOLLOW(textp-seg) = {)}
FIRST(textp-seg) --
=.{' , e} FOLLOW(textp-seg') = {)}
FIRST(textp- seg')

stru cted as follows -


iii) The pre dict ive par sing tabl e can be con
) $
number identifier
x-
te_ tp_➔ atom
t~xtp ➔ (list)
te_
__ ...,___➔
xtp o_
_a_t_ m __
tex
atom atom ➔ num ber atom ➔
iden tifie r
list ➔ (textp -seg)
list
textp-seg ➔
text p-se g ➔ text p-se g ➔
textp-seg textp textp-seg'
text p text p-se g' text p
tex_!p-seg'
te,tp-scg'
tcxtp -seg' ➔
textp-seg' ➔ €
, tex tp -seg

uniq ue entry,. the g iven g rammar is LL(


l).
As each cell in the abo ve tabl e con ta ins
.:::
C_: om
:=
P':·:..::
' e:.:...
r ~De
-= ::...n_ _ _ _ _ _ _ _ _ _ _ _ _2_ -_ 4_6_ _ _ _ _
sig

_ __ _ _ _ __s..:__ ntax Ana,,:.-
~_:_:

Examp le 2.9.6 i) Construct the predictive parser for the following grammar.
S ➔ (U la
L ➔ L, SIS
Construct the behavior of the parser on the sentence (a, a) using the gramm
ii)
ar spe ii'.:
c1, ,ed
above ¥\IN ~
. . . . -·- - - . •»I
-
FM k■J tfl
Soluti on : 1) As the given gram. mar 1s left recur sive becau se - - ---..:. . if!UiJ
of
L ➔ L , S I S.
We will first elimin ate left recurs ion. As
<.f} A ➔ A a I~ can be conve rted as
A ➔ ~A'
A' ➔ aA' IE
We can write L ➔ L, SIS as
.L ➔ SL'
L' ➔ ,SL' IE
Now the gram mar taken for predic tive parsin g is -
S ➔ (L) I a
L ➔ SL'
L' ➔, SL' IE
Now we will comp ute FIRST and FOLL OW of non termi nals
FIRST(S) = I(, a}
FIRST(L) = IC al
FIRST(L') = I', EI
FOLL OW(S ) = I', ), $J
FOLL OW(l ) = FOLL OW(L
') = I )}
The predi ctive parsin g table can be const ructed as

s s ➔a
S ➔ (L)
$

L L ➔ SL' L ➔ SL'
I

L
L' ➔ f
L' ➔ , S L'
ii) As we have co ,
( . nstru cted a predi cti ve . . .
a, a) us ing that tabl ·h pars ing _. _.
table tn (1) we will pdrse the :;tnt1r
e as s o wn below -

- -- - - - - -- ~T;E;C~H~N~
IC:A~L~P
=U
:B~L:IC=-A~T:-IO
:-~N
- ;®
;R___ ___ ______ ____.--/
s - an Up-thru st for kn nLU/orl~ -

---
compiler D_e_s..:...ig_n_ __ _ _ __ _ _ _ ____:2: :. . -_~4:..:_7_ _ _

- $S
State
- - - lnput
(a, a) $
-
'
_ _ _ _ _ _ ____:_S~y~nt~

Action s
-
s
n a~ly~s~i
a~x~A~

-
$_) L (
$J !:_ -....
(a, a) $
a, a2.JL
_S ➔ (L)
1..---+ g_' -
--
._lli '_S a, a) $ S➔ a
_ ~ ~ - - ~- -a,-a)
~$
$) L'
---
, a) $ L' ➔ , g_'
$ ) L' S, I a} $
$) L'S a)$ S ➔ a
$) L' a a) $
$) l ' )$ L' ➔ E
$ ) )$
$ .$ Accept

Example 2.9. 7 · Constr uct the predictive parser for the followi ng gramm ar
S ➔ a Ii I (T)
T ➔ T,S IS

Show the behavior of the pars.er in the sentence~;


i) (a, (a, a)) ii) (((a, a), i , (a,), a) iii)(a, a) .

Solution : i) We W:ill first elimin ate left recurs ion from given gramm
ar.
S ➔ a I i I (T)
T ➔ ST'
T' ➔ I ST' IE
Now let us compu te FIRST and FOLL OW
FIRST (S) = {a, i, (}
FIRST (T) = {a, i, (}
FIRST (T') = { ' , E}
FOLL OW (S) = { ' , ), $}
FOLL OW (T) = FOLL OW (T') = { ) }
Now the parsin g table can be constr ucted as

Input Symb ols


~

:
i ( _) , $
t---
s
a
- -
i
- - S ➔
-
(T)
~

..... T
S➔a
- - - TS ➔- ---
T ➔ ST'
I
·,
- -- ➔ ST'
T ➔ ST'
T' ➔ , ST
- T' T' ➔ r

®
TECHN ICAL PUBLICA TION S - an up -thmst for knowled ge
Cn1111 >lint Oo!fl(Jn _ _ _2_ - 4_B_ _ _ __ __ _ _ _ _s..:__
yntax An

W!' wi ll nnw
~
,.ir~ (• tlw st rings wi th th e h elp of predicti ve parsing table.
1

State Input Actions

i,~ ( a, (n, n) ) $ S ➔ (T)

,;, ) r < ( o, (a, a ) ) $

~ ) T'S a, (a, a) ) $ S ➔ a

~ )T ' n a, (a, a) ) $
$) T' , (a, a))$ T ➔ ,ST'
$) T'S, , (a, a) ) $
:i,) T'S (a, a) ) $ S ➔ (T)
$)T')T( _(~) )J, -
$) T ' ) T a, a) ) $ T ➔ ST'
---
$) T'J T'S ___ a, a)) _$
--·- --- --- -
$) T' ) T'a a, a) ) $ S ➔ a

$) T ') T ' . , a) ) $ T' ➔ ST'


-- --- ·-•· I
l $ ) T') T'S, , a) ) $
-·- -----·
$ ) T ') T 'S a) ) $ S ➔ a

$) T ' )T'a a) ) $
$) T')T' ))$ . T' ➔ E

$ ) T ') ))$
$ ) T') ))$
-----
$ ) T' )$ T' ➔ C
-· ----·. -·-
$ ) )$
$ $
ii)

State Input Actions


$S ( ( (a, a), i, (a), a ) S ➔ (T)
$ ) T ( ( ( (a, a}, i, (a), a )
$)T ( ( (a, a), i, (a), a ) T ➔ ST '
$) T'S ( ( (a, n), i, (a), a ) S ➔ (T)
' ~)T' )T( ( ( (a, a), i, (n), ,l )
S> ) T') T (,1 , a), i, (a), .i ) T > ST '
Si ) T') T'S
(a, n), i, (a), ,.1 ) S _; (T)
S, ) T') T') T (
(a , a), i, (a), ,l )
Si) T') T') T
a, a), i, (a), a ) T ➔ ST '
~
2 - 49 Syntax Analysis

$) T ') T ') T S a, a), i, (a ), a ) S-> a


$ ) T' ) T ' ) T 'a a, a ), i, (a), a ) $
$ )T ' )T ') T ' , .,), i , (a ), a ) $
$) T ') T ' ) T 'S, , a}, i , (a), a J_ $·
$) T ') T ' ) T ' S ~), i, (a), a )_$ S➔ a

$ ) T ' )_T ' ) T 'a a), i, (a), a ) $


$) T '_) T ' )T' ), i, (a ), ~ ) f
$ ) T ') T ' ) ), i, (at a )
$ ) T ') T ' , i, (a), a ) T' ➔ ,ST '
$ ) T') T ' S,_. , i, (a ),_?_)
'
$ ) T' ) T'S i, (a), a )
-~~~-
S➔ i
$) T ') T ') T ' _ , ~ i, (a), a ) $_ _
$ ) T' ) T ' ) T 'S, , a ), i, (!3), a _L$_
$ ) T ') T' )_T ' S _ _aLL_(a), a ) $ S➔ a
- - 4-- -
$ )_T ' ) T ' )_ T '.~ _ , i
_a),_, (~).L.
, a=- 3...
$ _ _ _ _ __
)
.L.

$ ) T ' )_I ' )_J'_ -


), i , (a), a ) $
--------'--'--'--=-'-' - ~ -
T' ➔ e

J) !_' l I' ) ), i, (a), a )


$ ) r )-T, -- - , i,(a),a)~ - T ' ➔ ,ST '
--~----'-~~~
$J _T ' ) T ' S,_ ; i , (a), a )
$) T ~) T'S__ i, (a), ,a )
$) T ') T ' i _. i, (a), a) $

_ .!J a), a ) $ T' ➔_,__ST I


.$)!')_.I'_
$) T ' ) I'S, , (a), a ) $

$) T ' ) T ' S (a), a )_j__ __ S ➔ (T)

$) T ' ) T ' ) T ( (a)__01_ ) $


-
$) T ' ) T ' ) T a), a ) $ T ➔ ST '
S➔ a
$ ) T ' ) T ' ) T 'S - -- - a1_,0_ $ -
$) T ' ) T ' ) T 'a a), a ) $
$) T') T ' ) T ' ), a ) $ T' ➔ e

$. ) T '.) T' ) )! a ) $

$) T' ) T ' ,a )$ T' ➔ ,ST '

$ ) T' ) T 'S, , a] _$
$) T') T 'S a )$ S ➔ a

$) T ' ) T 'a a )_j


-
$ ) T' ) T' )$ T '-> f

$ ) T' ) ) $

$) T' $

®
TECHNICAL PUBL/CA TIONS - an up-thrust for knowledge
2 - 50 Syntax A
~~~~~
~omp,ler ~ - - - - - - - - - - ~::_:= ------~ ~~~~~ ~a/i,,s,.
Design ---.........._ ·

The in rut <,Iring i~ rcaJ com p c c_ y


1 t I but there are .some s tates in stack. Hen ce th1,
. an Jn\ a Iiu. 1 ... 1ri1w for g i ven gn1mma1.
,.., n

iii)
.
Input Actions
State -
S ➔ (T)
$.5 (a,a)$ -- - - --
$)T( {a,a)$ --
a,a)$ T ➔ ST'
$)T -
~,a)j, S➔ a
$)T'S --
$)Ta a,a)$
-
$)T' ,a)$ T' ➔ ST'
$)T'S, ,a)$
--
a)$ - s ➔ a
$)T'S
- - -
$)T'a a)$
-
S)T' )$ T' ➔ €
- -
$) )$ -
$ $ ACCEPT .

Example 2.9.8 Check whether the following grammar is LL(l) grammar.


S ➔ iEtS/iEtSeS/a
E~b
Also define FIRST and FOLLOW procedures. AU : May-16, Marks 8
Solution : Let,

S ~ iEtS/iEtSeS /a
As the production rule for S needs to be left factored, we will rewrite the grammar as·
S ➔ iEtSS' /a
S' --; eS/£
E ~ b

~ ow we will compute FIRST and FOLLOW for S, S' and E.


; ' - FIRST (S) = /i, a)
FIR.ST (S') = /e, c)
FIRST (E) = /b)
FOLLOW (S) = le, $)

FOLLOW (S') = le, $)

FOLLOW (E) = ltl

TECHNICAL PUBLICATION S® - an up-thrust for knowledge


eomp•!er LJe~,YI I

The predictive parsing table can be constructed as :


a b e j t s
s -+iEtSS'
s S ➔ a

S' ➔ e S' ➔ i
-
s
S' ➔ eS

E E ➔b

As we ha ve got multiple entries in M[S', e] this grammar is not LL(l) grammar

Example 2.9.9 Consider the following Pascal-like grammar where terminals are in boldface :
< < var decl > ➔ var < decl list >
< decl list > ➔ < decl > ; < decl list > I < decl >
< decl > ➔ <id list > : id
< id list > ➔ < id list >' id I id
i) Eliminate left-recursion and left-factor the grammar.
ii) Find the FIRST and FOLLOW sets for all the non-terminal symbols of the new
grammar of part (a).
iii) Construct the predictive parsing table
iv) Show the nwves of the parser on
a) var id, id : id b) var id, id AU : May-I 1, Marks 16

Solution : Consider the rule


i) •
<id list> ➔ <id list> ,id I id
This rule has left recursion. It can be eliminated by the following rule -
If A➔ Aa If3 which can be converted as A ➔ f3A'
and A ➔ aA' IE
<id list> ➔ id <id list'>
<id list'> ➔ ,id <id list'> IE
For removing the left factoring consider the grammar
<decl list> ➔ <decl> ; <decl list> I <decl>
If the rule is A ➔a f3 1ja f3 2j... the grammar needs to be left factored.
<decl list> --t< decl> ;< decl list> I< decl>
i i J, J,
a f31
We have to convert it to
A ➔ a A' < decl list > ➔ < decl >< decl list' >
⇒ .
A' ➔ f3 1 1f3 2 <decllist'> ➔ ;< decllist > IE

®
TECHNICAL PUBUCA TIONS - an up-thrust for knowledge
Com pile r Design 2 • 52 Syntax An:i,
Y~,~
ii) After rem oving left rec
urs ion and le ft fa cto rin g the gra mm ar the ne w ~ ram
' " rl'\;i r
bec om es -
< var dee} >_ , var < dec l list >
< dee l list >_ , < dec l > < dec l list ' >
< dee I list ' > -, ; < dec l list > Ie
< dee l > _, < id list > : id
< id list > _, id< id list ' >
< id list ' > _, ,id < id list ' > \c
FIRST( <var dec1 > ) = {va r}
FIRST( < dec1 list > ): {id}
FIRST{ < dee1 list >)= { ;,E}
FIRST( < dec l > )= {id}
FIRST( <id list >) = {id }
FIRST( <id list >) = {,,E}
FOLLOW( < var dec l > ) = {$}
FO LL OW ( <d ecl list >) = { }
FO LL OW ( <d ecl list'>) = { }
FO LL OW ( <d ecl >) = {;}
FO LL OW (<i d lis t>) ={ :}
FO LL OW {<i d lis t'> )= {:}
iii) The prectictive par sin g tab le
is as giv en bel ow -

id

<dee! list>
<dee) list> ->
<de cl> <de cl
<ded list'> list' >
<de cl list'>->;<d ecl list>
<d ec1 list'> ·>
<decl> £
<de cl>-> <id
<id list> list> :id
<id list> ->id
<id list> <id list' >
<id list' >·>E
<id list'>->
iv) a) M id< id list';
ove s for v ar id id . 'd
, • I

Stack
S <va r decl> Input
Rules applied
va r id, id : id S
~e cl lt.'it>var
<\'a r ded > -+ var <de d lc.t>

- - -- - -
v a r id , id : id S

TECHNICAL PUBU~C;A~T~IO
~~® ; - - - - - - -
- - - - -- - - -
NS • an t1p-thrust for kno
wledge
- compiler Design 2 - 53 Syntax Analysis

r$
$ <dl'CI list>
<d'--cl list' > <d<~l>
id, id : id $

id, id : id $
<dee! li1,t>

<decb ➔
➔ <dcd ><dccl list' >

<id list> : id
$ <d<'CI Hst' >id:<id Hst> id, id : id$ <id list> ➔ id<id lis I'>
$ <dccl Li.st' >.id:<idlis t'>id id, id : id $

$ <decl list' >id:<idlist' > , id : id$ <id list' > ➔, id <id list' >

S <dccl Hst' >id :<idlis t' >id, , id : id$

$ <decl list' >id:<idlis t'>id id : id $

$ <decl list' >id:<idJist'> : id$ <id list' > ➔ E

$ <ded list' >id: : id$


l--------
$ <ded list' >id id$ .

$ <decl list' > $ <dee! list' > ➔ E


f- -
$ $ Accept

b) Moves for var id, id.

Stack Input · Rules applied


$ <Var decl> var id, id$ <var decb ➔ var .<decl list>

$ <decl list>var var id, id$ · <decl list> ➔ <decl> <decl list'>

$ <decllist><decl list'> <ded> id, id $ <decl> ➔ .<id list> : id

$ <decl list> <decl lis t'> <id list> : id id, id -


..
$ <ded list><decl list'><id list> : , id can not be further processed.

Example 2.9.10 Construct non recursive predictive parsing table for the following grammar.
E ➔ E or E I E and E I not E I (E) I O I l. AU : Dec.-12, Marks 16

Solution : As the given grammar needs to be left factored. Also the ambiguity of this
grammar needs to be removed. The modified grammar will be· as follows.

• Eliminating ambiguity
E ➔ E or T
E ➔ T

T ➔ T and F
T ➔ F

F ➔ not G
F ➔ G

G ➔ (E)IO I 1

®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
~~ ~~~ - - - - - - - -~ 2~-~5:_ Syntax An
4_ _- - - - - - -- -~
~om piler Design
s
• Eliminating left recursion

E ➔ TE'

£' ➔ orT E'IE

T ➔ Ff'

T' ➔ and FI 'IE


F➔ notG
F➔G

G ➔ (E)IO\ 1
The FIRST and FOLLOW for abo ve gra
mm ar is -
FIRST (E) = { not, (,0,1}
FIRST (E') = {or, E}
FIRST (T) = { not, (, 0, 1}
FIRST (T') = {an d, E}
FIRST (F) = {no t,(, 0, 1}
FIRST (G) = {(, 0, 1}
FOLLOW( E) = { ), $}
FOLLOW( E') = { ), $}
FOLLOW( T) = {or,},$}
FOLLOW( T') = {or,),$ }
FOLLOW( F) = { and , or,) ,$}
FOLLOW( G) = { or, and ,),$ }
The pred ictiv e pars ing tabl e will be

or and not ( ) 0 1 $
\ £
· E->TE' E->TE'
£'
- -- - - - - - E->TE' E->TE'
I
E'->orTE'
E'->E E'->E
I
T ----
-----· T-> FT' T-> FT' T-> FT' T-> FT'
T'
T'->andFT'
T'->E T'->E
F ---
F->n ot G F->G
G F->G F->G
G->(E)

TECHNICAL PUBLICA TIONs®


G->0 G->1
-----
- an up-thrust for knowledg e
compiler Design 2 - 55 Syntax Analysis

Example 2.9.11 Construct a predictive parsing table for the grammar S ➔ (L)/a, L➔ L, S IS
and show whether the following string will be accepted or not. (a, (a, (a,a))).
AU : May-17 , Marks 16

Solution : For predictive parsing table - Refer example 2.9.6.


~imulation of (a, (a, (a,a)))
.
State Input A.ction

$S (a, (a, (a, a) ) ) $ S ➔ (L)

$)Lf f a, (a, (a, a) ) ) $


.., .
$)L a, (a, (a, a) ) ) $ L➔ srt .. •

$)L'S a, (a, (a, a) ) ) $ S ➔ a ;;<_·


;

$) L';;, 11, (a, (a, a) ) ) $

$) L' , (a, (a, a) ) ) $ L' ➔ SL'

$) L' L'S, , (a, (a, a) ) ) $

$) L' L'S (a, (a, a) ) ) $ S ➔ (L)

$) L' L' ) L f f a, (a, a) ) ) $

$) L' L') L a, (a, a) ) ) $ L ➔- SL'

$) L' L' ) L'S a, (a~ a) ) ) $ S ➔ a


-
$) L' L') L' 11 11, (a, a) ) ) $ "'

$) L' L') L' , (a, a) ) ) $ L' ➔,SL'


- ---
, (a, a) ) ) $
- $) L ' L' ) L' S,

$) L' L') L 'S (a, a) ) ) $


--
S ➔ (L)
--- - -- - - -- - -
$ ) L' L' ) L' ) L ( (a, a) ) ) $
- - -
$) L' L' ) L' ) L a, a) ) ) $ L ➔ SL'
- --
$) L' L' ) L' ) L'S a, a) ) ) $ S ➔ a

$) L' L' ) L' ) L',, i\ a) ) ) $


-
$ ) L' L' ) L' ) L' , a) ) ) $ L' ➔,SL'
Com piler Design 2 - 56

-- Syntax Ana/
Y,~1·1

$ ) L' L ' ) L' ) L'S, , a) ) ) 'i,

$ )L' L' ) !,' )L'S a)) )~ S➔ a

$ ) L' L' ) L' ) L '~ ~ )) )$

$) L' L" ) L' ) L' )))$ t· ➔ e

$) L' L' ) L" h h))$


I $) L' L' ) L'

I
))$ L' ➔. e

$) L' L' ~ h)$

$) L' L' )$ L' ➔ e

$) L'
-)$- - - - - - - ·~·-·,., -L' ➔ e _____,,_-..,
,,,,,.,. '

h$
$ ACCEPT

Exa mpl e 2.9.12 Con sider the following CFC grammar over the non
-terminals {X, Y, Z} and
terminal s {a, c, d} with the productions below and
start symbol Z.
X ~ a

x~ Y
Z -, d
Z- , XY Z
Y~ c
y ~£
Compute the Ff RST and FOL LOW sets of every
non-terminal and the set of non-terminals
that are nullable.
AU : May -18, Marks 6
Sol utio n : FIRST set

Sym bol

X
Set
la, c, el
----i
y
le, ti
z la, c, d,cl I

~ :..5. .::..ig_n _ _ _ _ _ __ _ _ ____,:2::___-~5~7- - - - - - - - - -~S~y~n~ta~x~A~na~ly~s~is
FOLLOW set

Symbo l Set
X le, di
y
ldl
z I$.

example 2.9.13 Construct LL(l) parsing table for the following grammar, G and check
whether the word "ia<btd" is a string of the grammar .or not
S➔iEtS I iEtSeS I d
£➔id OP id
OP➔<l>l<=l>=l==I! = AU : Dec.-22, Marks 13

Solution : Let, the given gramm ar is -

S ➔ iEtS I iEtSeS I d
E ➔ id OP id

OP ➔ <I> I<= I>= I== I !=

We need to do left factor ing on S, we will rewrit e the gramm ar as -


S ➔ iEtSS' I d

S' ➔ eSIE

E ➔ id OP id

OP ➔ < I > I <= I >= I == I !=

We will compu te FIRST and FOLLOW for S, S', E

FIRST (S) = Ii,d/


FIRST (S') = le, E}
FIRST (E) = lid/
FIRST (OP) = /<, >, =, !j
FOLLO W (S) = le, $/
FOLLO W (E) = Itl
FOLLO W (S') = le, $/
FOLLOW (OP) = lid/

TECHN ICAL PUBLIC A T/ONS® - an up-thrus t for knowledge


2 - 58
- -- - - - - - - -
Compiler Design

The LL(l) p ar.-ing tc1bk w ill be as fol lows -

id e
------------
= < > $
d

s S~ d S' ➔ iEtSS'

s· S' ➔ r

5' 4 eS

I E

OP
E ➔ id
OP id

OP ➔ < OP ➔ > OP ➔ = OP-t !


s, --) E I

J
1___ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ o_
_P ➔_<_ o_
=_ _ _
P_➔ >=
_ _ _ _ __ _ .

ln above parsing table we have got m ultiple entries at M [S'~ e] and also at M[OP, <I
and M[OP, >] the above grammar is not LL(l ) grammar.

Examples for Practice

Example 2.9.14 : Discuss LL(l) parsing method for the following grammar
E➔TE'
E'➔+ TE' I£
T➔IT'
T'➔>t< FT'I £
F➔(E) /id.

Example 2.9.15 : Construct LL(l) parsing for


S ➔ A

A ➔ aB I aC I Ad I Ac
B ➔ b Be IF
C ➔ a.

Review Questions

7. Give an algorithm fo r finding the FIRST and - - .~ - . . .


FOLLOW posit10ns for a given non-tem11nt1l~.
AU : Dec.-05, Marks 4
7 Write down the necessary algorithms fo r finding FIRST and FOLLOW.

AU : Dec.-09, Marks 8
3. Write an nlxurith fi
m or non recursive predictive parsing.
AU : May-16, Marks 6

-;::;;;;:-:~ - --;;---------~
TECHNICAL PUBLICA TIONs®
- an up-thrust for knowledge
_~~~~ ~~
-=
_ __ _ __:2::__ 59:____ __ __ __ __ ax Ana lysis
Synt
1er oesig:__n_ _ _ _ _ _ _ _
~
Bottom Up Pa rsi ng Ei•Mufi(IW.v.a,vw1 1 11 •-tN•U&•4MtfN~M1d: ■
1 -- rm PB ll9 ■
---·••=
• .
th ·s sect ion , we .wil l disc uss "Ho w an inp ut stri ng get s parsed effi cien tly ? I.n
- , 1.n 1
compi1ers, the tas k ts don e by a
pro gra m ca lJed parsers .
. . .
par ser is two'Jfold · eith ·t h eck s the mp ut strm g com plet ely fo r its
·h task of . ' · er 1 c
, T e on syn tact ical ly inp ut stri ngs .
syntax or it gen era tes e rror m essa ges
to red uce
tn bot tom -u~ pars ing me tho d, the
inp ut stri ng is take n first and we try
,
th th e hel p of gra mm ar and try to obt ain the star t sym bol.
this stri ng wi

ly as soo n as we reac h to star t sym bol
, TI1e pro cess of par sin g hal ts suc ces sful
tom to up tha t ,is from leav es to roo t.
, The parse tree is con stru cted from bot
ful
plac ed at the leaf nod es afte r suc cess
, In this pro ces s, the inp ut sym bol s are
pars ing.
tog ethe r
star ting from leav es, the leaf nod es
, The bot tom -up par se tree is cre ated d and
thes e inte rna l nod es are furt her red uce
are red uce d furt her to inte rna l nod es,
eve ntu ally a roo t nod e is obt ain ed.
sym bol s.
the list of term inal and non ~ter min al
, The inte rna l nod es are cre ated from
• This inv olv es -
-ter min al U term inal
Non -ter min al for inte rna l nod e = Non and
this pro ces s, bas ical ly par ser trie s to iden tify R.H.S. of pro duc tion rule
• In
s acti vity is call ed red uct ion .
replace it by cor res pon din g L.H.S. Thi
tion s
the cru cial but prim e task in bot tom -up par sing is to find the pro duc
• Thus
that can be use d for red uct ion .
. of
tom -up par se tree con stru ctio n pro ces s ind icat es tha t the trac ing
• The bot
ord er.
der ivat ion s are to be don e in rev erse
For exa mp le
stat eme nt,
Consider the gra mm ar for dec lara tive
S ➔ TL;
T ➔ int I floa t
L ➔ L,id I id
The inp ut stri ng is floa t id,i d,id ;
Parse Tree
Step 1 : We will star t from leaf nod e

G)
Step 2 :

0 an up-th rust for know le dge


TEC HNIC AL PUB LICA TION S® -
1..,C~
o~m ~~ile~r~D~e=s~ig~n~ _ _
______ 2 6
_ ___,__ _
- _- _ _o_ _ ________
_ __ s----=-
yntax An
St ep 3 : Re du cing float to T.T
➔ flo at ~

flo at

St ep 4 : Re ad ne xt str in g fro m
in pu t.

0
St ep 5 Re du ci ng id to L. L ➔
id .

St ep 6 Re ad ne xt st rin g fro m
in pu t.

St ep 7 Re ad ne xt st rin g fro m
in pu t.
flo at
6
flo at

St ep 8 id , id ge ts re du ce d.
'--- ---v --"
L

flo at
6
TE CH NI CA L PU BL IC
AT IO NS® - ~n
Comp1/er Design 2 - 61 Syntax Analysis

Step 9 : TL ; reduced to.


--------
~

float

Fig. 2.10.1 Bottom-up parsing

Step 10 : The sentential form produced while constructing this parse tree is ·
float id,id;
T id,id;
T L,id;
TL;
s
Step 11 : Thus looking at .sentential form we can say that the rightmost derivation in
reverse order is performed.
• Thus basic steps in bottom- up parsing are
1) Reduction of input string to start symbol.
2) The sententia l forms that are produce d in the reductio n process should trace out
rightmos t derivatio n in reverse.

Handle pruning
As said earlier, the crucial task in bottom-u p parsing is to find the substring that
could be reduced by appropri ate non-term inal. Such a substring is called handle.
In other words handle is a string of substrin g that matches the right side of
production and we can reduce such string by a non-term inal on left hand side
producti on. Such redu ction represen ts one step along the reverse of rightmos t derivatio n.
Formally we can defi ne handle as follow :

Definitio n : "Handle of right sentential form y is a producti on A ➔ ~ and a


position of y where the string -~ may be found and replaced by A to produc;e the
previous right sentential form in rightmos t deriv,a tion of y".

®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
Com pile r Design

For exa mp le
Consider the gra mm ar
E -) E+E
E -) id
. . . + id and the rig htm ost der iva · is
No w cons1·d et• the stn ng id + id tio n

E ⇒ E+E
nn
E ⇒ E+ E+ E
rm
E ⇒ E + E+ id
rm
E ⇒ E +i d+ id
nn
E ⇒ id+ id+ id
nn
The bol d stri ngs are called han dle
s.

Right sentential form dl


H an___ Pro du cti on
-·-· --·- ---·
· ~--- - - - - - - - - --1
-··· - - - - - · - · ·
id

---E---
+id + 1.d
--- - -- - - - -id
E + E + id id
r- - - - -- --- - -.. - - -=-- -- - --E..,
-) id
..- -------;:_7
E+ E+ E E+ E E ➔ E+E ·

E+ E ·· E4 E + 'E
E

Th us bot tom par ser is ess ent iall


y a pro ces s of det ect ing han dle
red uct ion . Th is pro ces s is cal led s an d usi ng them in
han dle pru nin g.

Review Questions

1. What do you mea n by handle prunin


g ?
AU : May-0 4, 11, De c.-0 6, Mar
2. Defin e handle. ks 2

AU : De c.-0 4, May-05, Marks


3. Give two example for each of 2
top _down.parser and bottom up parser.
- - -- ---
AU : May-10, Marks 2
f Il l Shift Reduce Parser
AU : De c.- 04, 06, May-16, Mar 8
Shift red uce parser att em pts to ks
con str uct par se
tree from lea ves to roo t. Th us it
wo rks on the sam e
principle of bottom up parser. Stack Inp ut buffer

I w$ J
Fig. 2.11.1 Initial con figu rati on
TEC HNICAL PUBL ®
ICA TIONS - an up- thru st for kno wle dge
p
2 - 63 Syntax Analys is
compiler Design
--- -- ----- ------
• A sruft reduce parse r requi res fo llowing d a ta struc tu res.
1. The input buffe r sto ring the in p ut string .
of ru les.
z.,A stack fo r storin g and accessing the L. 1-1.S. and RJ-1.S.
J11e ini tia l config ura tion of Shift reduce parse r is as show
n in Fig. 2.1J.1.

n,e parser perfo rms follow ing basic opera tions.


1. Shift : Movi ng of the symb ols from inpu t buffe
r onto the stack, this action is called
shift.
2. Reduce : If the hand le appe ars on the top of
the stack then reduc tion of it by
ed of and L.H.S . is push ed
appropriate rule is done . That mean s R.H.S. of rule is popp
in. This action is called Redu ce .action.
3. Accept : If the stack conta ins start symb ol only
and input buffe r is empt y at the
state is obtai ned in the process
same time then that actio n is called accept. When accept
of parsi ng then it mean s a successful parsi ng is done.
e the symb ols, it
4. Error : A situa tion in whic h parse r canno t eithe r shift or reduc
cannot even perfo rm the accep t actio n is called as error.

Concept of viabl e prefi x :


s that can appe ar on the
Viable prefix es are the set of prefixes of right sente ntial forin
stack of shift/ reduc e parse r are called viabl e prefi xes.
of a viabl e prefix to
It is alwa ys possi ble to add termi nal symb ols to the end
obtain a right sente ntial form.
Let us take some exam ples -to learn the shift-reduc e parse r.
Example 2.11.1 Consider the grammar
E ➔ E+E

E➔ E* E
E ➔ id
AU : May-16. Marks 8
Perform Shift-Reduce parsing of the input string "idl + idb id3 ".

Solution :
-- -
Stack Inpu t buffe r Parsing action

$ id 1 , id 2+ic.1 3$ Shill

'l,idl +ic.1 2• id3$ l<edt11L' b, I: ) 1d

S,E I id~, id JS, Sh,fl

$E- ic.1 2• id1$ ~hifl

®
L/CAT IONS - an up-thrust for knowle dge
2 - 64
Compiler Design Syntax A '

Reduce by E ➔ id
~
$E+id2 • id3$

$E+E • id3$ Shift

$E+ E• id3$ Shift

$E+E•id3 $ Reduce by .E ➔ i.d


-
$E+E• E $ Reduce ?Y ~ ➔ E * E

$E+E $ Reduce by E ➔ E + E

$E $ Accept

Here we have followed two rules.


1. If the incoming operator has more priority than in stack operator then · perform
shift.
2. If in stack operator has same or less priority than the priority of incoming operator
then perform reduce.
Example 2.11.2 Consider the following grammar
S ➔ TL;
T ➔ int I float
L ➔ L,id I id
Parse the input string int id,id; using shift-reduce parser. ·
Solution :

Stack Input buffer Parsing action


---- - - -
int id,id;$ Shift
- -
$int id,id;$ Reduce by T ➔ int
- -
$T · id,id;$ Shift
--- - --
$Tid id;$ Reduce by L ➔ id
--- - - - -
$TL id;$ Shift
--- -- - -
$TL, id;$ Shift
...
- - -- - - - - - -- - - - - ----
$TL,id ;$ Reduce by L ➔ L,
id
----------- -
$TL ;$ Shift
--·--------- -
$TL; $ Reduce by S ➔ TL;
$5 $ _A ccept

TECHNICAL PUBLICA TIONs® - an up-thrust for knowledge


- Syntax Analysis
c ompiler Design 2 - 65

Example 2.11 .3 Consider the following grammar


S --) (L) I a
L --) L, SI S
Parse the input string (a, (a, a)) using shift-reduce parser.
Solution :

Stack Inpu t buffer Parsing action


- ---- -- - - __.._ -~-
$ (a, . (a, a)) $ Shift
-
$( a, (a, a)) $ Shift

$(a , (a, a)) $ Reduce. S ➔ a

$(5 , (a, a)) $ Reduc e L ➔ S

$(L , (a, a)) $ Shift


- ·-·

$ ( L, (a, a)) $ Shift

$ ( L, ( a, a)) $ Shift ·

.$ ( L, (a I a)) $ Reduc e S ➔ a

$ ( L, ( S , a)) $ Reduc e L ➔ S

$ ( L, ( L , a)) $ Shift

$ (L, (L, a))$ Shift

$ (L, (L, a )) $ Reduc e S ➔ a


---
$ (L, (L, S )) $ Reduc e L ➔ L, S
--- ---·-
$ (L, (L )) $ Shift
- - -
$ (L, (L) )$ Reduc e S ➔ (L)
--
$ (L, S )$ Reduc e L ➔ L, S
- ---- - ··-
--
$ (L )$ Shift
....._.. -- - - - --- -

$ (L) $ Reduce S ➔ (L)


--
-- $S
-
$ Accep t

Review Questions

I l . What do you mean by viable prefixes ?


AU : Dec .-04. Marks 2

AU : Dec.- 06, Marks 6


2. Explain the stack implementation of shift reduce parsing.

dge
TECHN ICAL PUBLICATIONS® • an up-thru st for knowle
2 - 66

fj fl LR Parser t+1•■ ·J4i••


. '
Thi s is the moc,t efficie nt me tho
d of bo tto m -up pa rs ing wh ich
lar<nYe class of con tex t free gra mm ca n be use d to Pa
the a rs. T hi e; m e tho d is als o called
LR (k) pars ing I ri.,
• L c:; tandc:; for left to rig ht · IC:r,,
sca nn ing .
•R c;ta nd~ fo r rig htm ost d e ri va
ti o n in rev ers e.
• k is nu mb er of inp ut sym
bo ls . Wh en k is o mi tte d k is ass
um ed to be 1.
Properties of LR parser -
LR pa rse r is w ide ly use d for fol
low ing rea sons .
1. LR parsers can be cons tru cte d
to rec ogn ize mo st of the pro gra
mm ing lan gua ges fr,,
w hic h con tex t fre e gra mm ar can
b e wr itte n.
2. Th e cla ss o f g ram ma r tha t can
be pa rse d by LR pa rse r is a
s up ers et of class (J
gra mm ars tha t can be par sed usi
ng pre dic tiv e pa rse rs .
3. LR pa rse r wo rks usi ng no n
bac ktr ack ing shi ft red uce tec
hn iqu e yet it is effi cien:
one.
LR pa rse rs detect syn tac tic al err ots
ver y efficie ntl y.
Structure of LR parsers INP UT Token

Th e str u ctu re of LR parser is a + b $


as giv en in fol low ing Fig. 2.12.1
.
It con sists of inp ut buf fer
.,. LR parser
for sto rin g the inp ut str ing,
a
sta ck for sto rin g the gra mm Top OUTPU1
ar $
Par sing pro gra m
sym bo ls, ou tpu t and a parsin
g Stack
tab le com pri sed of tw o par ts,
na m ely act ion and got o. Th ere
Action
is on e pa rsin g pro gra m wh ich S 0 r-- =.; .;_ -4_ ~:: :.::got o
::,_ ,_, 1
is act uall y a dri vin g pro gra m S1
and rea d s the inp ut sym bol on
e
at. a tim e fro m the inp ut buf fe r.
Th e dri vin g p rog ram w o rks Parsing table
Fig . 2 -12.1 Structure of LR parser
on fol low ing line.
1. It ini ti a lize s the sta ck w ith
get next tok en. sta rt sy mb ol a nd inv ok es sca
n ner (lexicc1l ana lyLt'r 1 ,,,,
2. ft d ete rm ines ., the '> lat e cur
sym bol. 1 re~tl y
o n th e top of the sta ck an d ai . ~I.!:
the cur rent iJlf
3. Jt con s ults th e pa rsin g tabl
va lues. e fo r
the actio n Is; ,ad w h ich can hav e on e of the t·otJI

TEC HNICA L PUBLI CA TIO Ns ®


- ;,n '·
compile r Design 2 67 Syntnx Analys is

i) si m ea ns s hi ft st.,1tc i.
ii) ri mc,rns rcducL' b)' ruk, j.

iii) Accep t mc,m s s uccess fu l p,1rsi ng is duiw .


id Error indicd tl's sy ntacti cal l'tT()r.

Types of LR parse r

follow ing diagra m repres ents the types o f LR p a rser

LR parser

SLR parser LALR parser Canoni cal LR parser

Fig. 2.1 2.2 Techniques of LR parsers


parser a nd canon ical
The SLR mean s simple LR parse r, LALR means Looka head LR
of LR fam ily. The overa ll
LR or simpl y "LR" parse r - these are the three memb ers
parser s. The relativ e
struct ure of all these LR parser s is same. All are table driven
canonical LR parses
povvers of these parse rs is SLR(l ) s LALR(l) s LR(l)· That means
parser.
larger class than LALR and LALR parses larger class than SLR

Review Question
-------~-- -- --
AU : Dec.-07. Marks 4
1. What are the reasons for using LR parser techniques ?

■,fa&•f•*-
fjf j SLR Parsing
Context free grammar
• We will start by the simpl est form of LR
parsin g called SLR parse r.
• It is the weake st of the· three m ethod s but
!
Construction of canonical set
of items
it is easies t to imple ment.
• The parsin g can be d one as show n by the
steps in Fig. 2.J 3.1. Construction of SLR parsing
table
• A gramm ar for which SLR parse r ca n be
cons tructe d is called SLR gram mar.
Definition of LR (0) items and related terms - Input
Parsing of input string
string
1) The LR(O) item fo r gramm ar C is produ ction
rule in which t>ymb o l • i~ inse rted <1 1 sonw ♦
Output
positio n in R.11 .S. of the ruk>. For t'\ ,1mpll' Fig . 2.13.1 Steps for working of SLR(1)
S ~ • ABC

TECHN ICAL PUBLICA TIONs® on 11p thwst fot J.. nowledgt1


- 68 2
Syntax Anaty .
/"'~~~~ ~~-- ---- --- ---~__::=--
~ompiler Design --- ---- --- --- -~----....s,s
S ➔ A •BC
S ➔ AB• C
S ➔ ABC •
. s ~•-
The nroduc tio n S ➔ £ gen erates only one item b 1 S th
' G . h . g start sym o en augment d
2) Augmented grammar : If a gramm ar is _avm tart symbol such that S' e
· a n ew ·
gra1nm ar 1s gramm ar G' in
· which S' 1s a new 5
f . t Th t. . ~ S.
The p urpose of this gramm ar is to indicate the accepta nce O
mpu · a is when
parser is about to reduce S' ➔ S it reaches to accepta nce ate.
st
3) Kernel ite ms : It is collecti on of items S' ➔ • S and all the items whose dots are not
a t the leftmos t end of R.H .S. of the rule.
Non-K ernel items : The collecti on . of all the items in which • are at the left end of
R.H.S. of the rule.
4) Functi ons closure and goto : These are two importa nt functio ns require d to create
collect ion of canonic al s~t of items.
5) Viable prefix : It is the s~~ of prefixes . in the right sentent ial form . of product ion
A ~ a. This set can appear on the stack during shift/re duce action.
:losur e operat ion -
For a context free gramm ar G, if I is · the sef of items then the functio n closure( !) can
be constru cted using followi ng rules. ·
l. Consid er I is a set of canonic al items and ini!ially every item . I is added to
dosure (l).
2. If rule A ➔ a • BP 1s a rule m closure( !) and there 1s anothe r rule for B such as
B ➔ y then
closure (}) : A ➔ a • BP
B ➔ •y

This rule has to be applied until no -more new items can be added to cl~sure( I).
_T he m eaning of .rule ~ ➔ a • BP is that during derivat ion of the input string at some
point_ we m ay_re~uire stnngs d erivable from BP as input. A non-ter minal immediately tl1
the n ght of • md1cate s that it has to be expand ed shortly.
Goto operati on -

The fun ction goto can be defines as follo ws .


If there is a prod uction A BA h . ~
simply shifting of . ~ a • · ,_, t en goto(A ➔ a •BP,B) = A ➔ a B • P. T hat mectn-
non-terminal) Th , • oneAposition ahead o v e r th e g ra mma r symbol (m ay be ter011nc1 . I or
· e ru 1e ➔ a • Bfi is i11 l ti1 "
goto(l,B ). · en t I1 e sa me goto functio n ca n be w ri·tten as
c ompiler Design 2 - 69 Syntax Analysis

Example 2.13.1 Consider the grammar


X ➔ Xb la
Compute closure(!) and goto(]).
Solution : Let I : X ~ • Xb

Closure(I) = x~ • Xb
= x~ . a
The goto function can be computed as X ➔ {x•bgivesX ➔ X•b
goto(I,X) = X ~ X• b
X ➔ {~·-, gives X ➔ a •
Similarly goto (I, a) gives X ~ a•
Example 2.13.2 Consider the grammar
S ➔ AS I b
A ➔ SA I a
Compute closure (I) and goto (I).
Solution : We will first write the grammar using dot operator.
S ➔ •AS
S ~ •b
A~ •SA} Closure (I)
A~ • a
Let us call this as state as IO ·
Now we apply goto on each symbol in Io-

10 : S ➔ •AS
S ➔ •b
A ➔ •SA
A ➔ •a
I1 : goto (1 0 , A)
S ➔ A•S
S ➔ •b
A ➔ •SA
A ➔ •a
12 : goto (1 0 , b)
S ➔ b•
13: goto (1 0 , S)
A ➔ S•A
A ➔ •SA
A ➔ •a
S ➔ •AS
S ➔ •b
14 : goto (1 0 , a)
A➔ a •

®
TECHNICAL PUBLICATIONS - an u;rthrust for knowledge
2 - 70
Com?£p~ile~r!_06~s~,g~n_ _ _ _ _ _ _ _ _ __ __::.__:__:__ _ _ _ _ _ _ _ _ _ _ __Syntax Ana, -----!Sis
Example 2.13.3 Con~ider the followi11g gram111ar :
S ~ Ao I bAc I 8( I /?Ba
A ~d
B~d
Compute closure (]) and goto (}) .
Solution : First we will wri te the grammar using d o t o p e rator.

IO : S ➔ • Aa
S ➔ • bAc
S ➔ • Be
S ➔ • bBa
A ➔ •d
B ➔ •d
Now we will appl y goto on each symbol from state I O. Each goto (1) will generate
new s ubsequ ent states.
I1 : goto (I o , A)
S ➔ A •a
I2 goto (1 o, b)
s➔ b • AC
S ➔ b • Ba
A ➔ •d

B ➔ •d
I] goto (1 0 , B)
S ➔ B •c
1-i goto (l o, d)
A ➔ d•
B ➔ d•

Construction of canonical collection of set of item -

1. For the gra mmar G initially add S' ➔ • S in the set of item C.
. al or
~- For each set of items Ii in C and for ea ch grammar symbol X (ma y be ter~J.!l otc
non-terminal) add closure 0;, X). This process should be repeated by applying~ ol
. . C These
(11 , X) for each X m l i such that goto(½, X) is not empty and not in ·
itl'ms has to constructed until no m ore set of items can be added to C.

®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
-compiler Design 2 - 71 Syntax Analysis

items by applying
Now we will consid er one gram mar and construct the set of
closure and goto functi ons.
Example :

E➔ E+ T
E ➔ T

T ➔ T*F
T ➔ F

F ➔ (E)

F ➔ id
I then we have
In this gram mar we will ·add the augm ented gram mar E' ➔ • E in the
to apply closure(!).

E ➔ •T
T ➔ •T * F

T ➔ •F

F ➔• (E)
F ➔ •id

imme diatel y right


The item Io is const ructed starti ng from the gram mar E' ➔ • E. Now
add E-pro ductio ns with
to · • is E. Henc e we have appli ed closure(l0) and thereb y we
E + t and E ➔ •T in 10 .
• at the left end of the rule. That mean s we have added E ➔ •
have added , conta ins
But again as we can see . that the rule E ➔ • T which we
ons in 10 .
non-terminal T imme diatel y right to •. So we have to add I-pro ducti
T➔ • T * F and T➔ • F. .
have alread y added
In I-pro ducti ons after • comes T and F r.especti vely. But since we
F-pro ductio ns hav ing
T productions so we will not add those . But we will add all the
that after dot and id
dots. The F➔ • (E) and F➔ • id wiJJ be added . Now we can see
symbols and a re not
are coming in these two produ ctions . The ( and id are terminal
here. Since there is no
deriving any rule. Hence our closu re fun ction terminates over
rule furthe r we will stop creating 10 _
CompilerDesign 2 - 72
- - - - - - - - - - - - - - - - - - - - - - - - - - - -~ SYnta)( 4n illy,

•1

No w apply goto(l 0, E)

Shift dot to E'-) E •


right E-) E • -+ T

Th u~ 11 b{'co mcs,

goto(l0_ E)
11 : E' ➔ E • ·
E ➔ E • +T

Since in I1 there is no non-termina l after dot we cannot apply closure(l1).


By applying goto on T of 10 ,

goto(I0, T)

Since in 12 there is no non-termina l after dot we cannot apply closure(l2).


By applying goto on F of lo ,

goto(Io, F)
13 : T➔ F •

Since after dot in 13 there is nothing, hence we cannot apply closure(l3).


By applying goto on ( of Io. But after dot E comes hence we will
apply closure on
then on T, then on F.

goto(l0, ( )
l4 : T ➔ (• E)

E ➔ •E+T

E ➔ •T

T ➔ • T*F

T ➔•F

F ➔ • (E)

F ➔ •id
compiler Design 2 - 73 Syrtl.8Y Analy1J1•

By applyi ng goto on id o f 10,

goto(T0, id)
15 : P ➔ id •

Since in ts there is no non-terminal to the right of d ot we cannot apply closu re


function here. Thus we have completed applying gotos on r0 . We will cons idE:-r f1 for
applying goto. In 12 there are two productions E' ➔ E • and E ➔ E • + T. There is no
point applying goto on E' ➔ E •, hence we will consider E'➔ E • +T for application of
goto.

goto(I1, +)
~: E ➔ E+• .T
T ➔ •T * F
T ➔ •F
FT ➔ • (E)
F ➔ •id

There is no other rule in 11 for applying goto. So we will move to 12 . In 12 there are
two productions E ➔ T • and T ➔ T• *F. We will apply goto on *·

goto(l2, *)
l7: T ➔ T*• F
F ➔ • (E)
F - ➔ • id

The goto cannot · be applied on l3, Apply goto on E in 14_ In 14 there are two
productions having E after dot (F ➔ • E and E ➔ • E + T ). Hence we wiU apply goto on
both of these productions. The Is becomes,

goto (14, E)
Is : F ➔ (E •)
E ➔ E•+T

If we will apply goto on (14, T) but we get E ➔ T• and T ➔ TuF which is 12 onl y.
Hence we will not apply goto on T. Similarly we will not apply goto o n F, ( and id as
we get the states 13, 14, 15 again . Hence these gotos cannot be applied to avoid repe tit ion.
There is no point applying goto on f5_ H ence no w we will move a head by appl ying
goto on J6 for T.

®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
Comp iler Design 2 - 74
Syntax Ana/y .
s,s
goto(l 6, T)
19 : E➔E+T •
T ➔ T•*F

Then ,

goto(I7, F)
110 : T ➔ T* F •

Then ,

goto(l8, ))
111 : F ➔ (E) •

Appl ying goto on ~, 1 , 1 is of no use. Thus now there


10 11 is no item that can be
adde d in the set of items , The collection of set of items is
from 10 to 111 .
Construction SLR parsing table -
As we have seen in the struc ture of SLR parse r that are
two parts . of SLR parsing
table and those are action and goto. By consi derin g basic
parsi ng actio ns such as shift,
reduc e, accep t and error we will fill up the action t~ble.
The goto table can be filled up
using goto function. Let us see the algor ithm -
Input : An Augm ented gram mar G'

Output : SLR parsi ng table.


Algorithm :
1. Initially const ruct set of items C = \10, 11, 12 .. .lnl wher e C is a collection of set of
LR(O) items for the input gram mar G' .
2. The parsi ng actions are based on each item Ii. The actio
ns are as given below :
a. If A ➔ a •a~ is in Ii and goto(Ii, a) = ~ then set action
[i, a] as "shif t j". Note
that a must be a terminal symbol. · ·
b. If there is a rule A ➔ a • is in Ii then set action[i, a]
to "redu ce A ➔ a" for ,ill
symb ols a, wher e a E FOLLOW(A}. Note that A must
not be an augmented
gram mar S'. ·
c. If S' ➔ S is in Ii then the entry in the action table action[i,
$] ="acc ept".
3. The goto part of the SLR table can be filled as : The goto
trans itions for state i i'
consid ered fo r non-t ermin als only. If goto(li, A) = li then goto[
l i, A] = j.
4. A ll the entries not defin ed by rule 2 and 3 are considered
to be "erro r".

TECHNICAL PUBLICATIONS® - an up-thrust for knowle


dge

compiler Design 2 - 75 Syntax Analysis

.@•ii1Fi 1 Co11struct tire SLR(1) parsing tablf' for


IP 1) E ~ E+T 2) E T ➔

3) T ~ T * F 4) T ➔ F
5) F ~ (£) 6) F ➔ id. AU : Dec.-06, 11 , 16, May-11, Marks 10, May- 19, Marks 13

Solution :
We will first construct a collection of canonical set of items for the above grammar.
Tht:> set of items generated by this method are also called SLR(O) items. As there is
mi lookahead symbol in this set of items, zero is put in the bracket.

Io :
E'➔ •E
goto(r 1 ,+)
E➔ • E +T 16 : E➔ E + • T
E➔ •T T➔ • T*F
T➔ • T*F T➔ •F
T➔ ~F F➔ • (E)
F➔ • (E) F➔ •id
F➔ •id

goto (I0 , E) goto (1 2 , *)

11 : E'➔ E • 17 : T➔· T* • F
E➔ E • +T F➔ • (E)
F➔ •id

goto (1 0 , T)
goto (1 4 , E)
J2 : E➔ T•
18 :F~ (E•)
T➔ T• * F
E➔ E • +T

goto (1 0 , F)
goto (1 6 , T)
J3 : T➔ F •
lg: E➔ E+ T•
T➔ T • *F
goto (1 0 , ()
14 : T ➔ (• E)
E➔ • E + T goto (17 , F)
E➔ • T 110 : T➔ T*F •
T➔ • T*F
T➔ •F
F➔ • (E) goto (1 8, ) )
F➔ •id
111 : F >(E) •

goto (1 0 , id)
15 : F • id •

TECHNICAL PUB_LICA T/ONS® - an up-thrust fo,· knowledge


f I I ,

Wl' l.lll J1.•:-. 1g11 ,l I JI ·/\ trn ,1li,1v1 • ·, vi ,11 1t, •111 'i 11H follllW H :
Hl nll' 10 is initio l Htalt' . Note• th,H thp
In 1h1· hiwn 1)1 ·/\ vv, ·ry ·it.111 • iH fi11al 1-,ln ft• , Tlw
l)f,=!\ n·r11gni11·!- v1,1hlt• pn•f ixt•~.
To
l

►0
stato
17

F To state
13

To 14
sta te

id To 15
state

T F

To state
14

id To state
15

~--F----0
E

To state
+ 15
T
To state
12

F To state
id 13

Id

Fig. 2· 13 -2 DFA for set of items

TECHNICAL PUBL/CA TIONS® - an up-thrust for knowle dge


compiler Design 2 - 77 Syntax Analysis

I I ; goto (lo, E)

E'-t E•
E➔ E• +T

~ : goto (1 1, +)

E➔ E+•T +

19 : goto (~. T)

E➔ E + T • ·

For example - For the item,


The viable prefixes E, E+ and E + T are recognized here continuing in this fashion.
The DFA can be constructed for set of items. Thus DFA helps in recognizing valid
viable prefixes that can appear on the top of the stack.
Now we will also prepare a FOLLOW set for all the non-terminals because we
· require FOLLOW set according to rule 2.b of parsing table algorithm.
[Note for readers : Below is a manual method of obtaining FOLLOW. We have
already discussed the method of obtaining FOLLOW by using rules. This manual
method can be used for getting FOLLOW quickly]
FOLLOW(E') = As E' is a start symbol $ will be placed
= {$}.

FOLLOW(E):
E'➔ E that means E' = E ~ start symbol.:. we will add$.
E➔E+T the + is following E. :. we will add +.
F ➔ (E) the ) is following E. :. we will add ).
:. FOLLOW(E) = {+, ), $}

FOLLOW(T):
AB E' ➔ E, E ➔ T the E' = E = T = start symbol. :. we will add $.
E➔ E+ T
E➔ T + T ·: E ➔ T

®
TECHNICAL PUBUCA TIONS - a11 up-thmst fo, knowledge
2 - 78 Synta)( Ana,..,
- ----~1
om
~C~ ~ s,~g~n - - - - - - - - - - -= - - - - -- - - - -
~p~,l~er~De

The + is foll owing T hence we will add +.


T ➔ T • F

As * is foll owing T we will add *·


F ➔ (E)

F ➔ (T) ·: E ➔ T
As ) is follow ing T we will add )
: . FOLL OW(T ) = I+, *, ), $I
FOLL OW(F ):

As E'-t E, E ➔ T and T ➔ F the E' = E = T = F = start symb ol. We will add $.

E ➔ E+ T
E ➔ T +T ·: E ➔ T

E ➔ F + T ·: T ➔ F

The + is follow ing F hence we will add +.


T ➔ T*F
T ➔ F

As * is follow ing F we will add *·


F ➔ (E)

F ➔ (T) E ➔ T

F ➔ (F) T ➔ F

As ) is follow ing F we will add ).


FOLL OW(F ) = I+, *, ), $J

Build ing of parsin g table

Now from canonical collec tion of set of items , consi der 10.

E' ➔ • E

E ➔ •E + T

E ➔ •T
T ➔ •T * F

T ➔ •F

F ➔ • (E)

F ➔ • id

- -~ ~- ~ -- - ---- ---
TECHNICAL PUBLICA TIONs® - an up-thru st for knowle dge
_ _ ~
mpJier oe_sig_n_ _ _ _ _ __ 2 - 79
yn~t~ax~A~n~a~ly~sis
- - - -~S~
----------
~
consider F ~ • (E)
A ~ a • a~

A ::: F, a =E, a =(, S = E)

goto(lo, ( ) = l4
:.action[O, ( 1 = shi ft 4
Simila rly for F ~ • id
_ hi
Entry in the. act ion tab le action[O' 1'd] - s ft 5 ·: go to(I0, id) = 15
.
an y acf10n. Hence we will find the actions from I to
Other item m 10 do es no t giv e 0

Il I.
State Action Goto

id * ( ) $ E T · F
+
0 s5 s4
1 s6
2 s7
'3
4 s5 s4

5
s5 s4
6
s5 s4
7
s11
8 s6 ·'
s7
9
10
11
~il l fill it up
rsi ng tab le is fill ed up wi th the shift actions. No w we
Thus SLR pa
.
with reduce an d acc ep t action duction
the rul e 2.c fro m a
p· rsi ng table alg orithm, there is a pro
According to
(l, $].
d the action "accep t" in action
E' ➔ E • in I1. He nc e we will ad

Now in state I2
E➔ T •
A- 1a • rul e 2.b
A= E, a = T

FOLLOW(E) = I+, ), $I
Comp iler Design 2 - 80
Syntax An
alys;s
.Hence add rule E ➔ T in the row of state 2 and
in the colu mn of +, ) and
the give n exam ple of gram mar E ➔ T is rule num
ber 2. :.act ion[2 , +] = r2, $_ II\
actio nl 2, ))= r2, action[2, $]= r2.
Similarly, N ow in state I3
T ➔ F •
rule 2.b
A =T, a= F
FOL LOW (T)= (+, *, ), $1
Hen ce add rule T ➔ F in the row of state 3 and in
the colu mn of +, *, ) and $. ln
the give n exam ple of gram mar T ➔ F is rule num
ber 4. Ther efor e action[3, +] == r
actio n[3, )] = r4, actio n[3, $] = r4. Thu s we can find 4
the matc h for the rule A -t~ ,
from rema ining state s· 1 to 1 and fill up the actio
4 11 n table by resp ectiv e "reduce" rule.
The table with all the actio n[ ] entri es will be

Stat e Acti on Goto

id + ... ( ) $ E T F
'-- -
0 s5 s4 - ..
- - A

1 s6 -Accept
,- -
2 r2 s7 r2 r2
~

3 r4 r4 r4 r4
- -- - - -
4 s5 s4
- - - ----
5 r6 r6 r6 · r6
-- - - ---- - --
6 s5 s4
- - - ---
7 s5 s4
- ---
- - - - - -
8 s6 s11
·- - -- --
9 rl s7
-- -
rl rl
10 r3
- - - - -
-- -- - - - ..
r3 r3 r3
- -- - - -- -
N ow we
---- - 11 rs r5 r5 rs
w,·11 f·1'II up the goto table for a ll the non-
term inals . 1n ' _ ll',
5 tate
goto (I-o, .E) = 11 .' Hen ce goto[O, El =1, s imil arly goto
(lo T) = 1 . Hen ce goto [O, Tl ::: -
Con tin uing in this fash ion we can fill up the goto entri 2
~s of SLR pars ing table.

compiler Design 2 - 81 Synta>: Analysis

Finally the SLR(l) pars ing tabl e w ill look as -


---
State Action Goto

id +
,. ( $ E T F
0 s5 s4 2 3

1 s6 Accept
2 r2 s7 r2 r2

3 r4 r4 r4 r4
4 s5 s4 8 2 3

5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10

8 s6 s11
9 rl s7 rl rl
10 r3 r3 r3 r3
11 rS rS rS rS

Remaining blank entries in the table are considered as syntactical errors.

Parsing the input using parsing ta ble -


Now it's the time . to parse the actual string using above parsing table. Consider
the parsing algorithm.

Input : The input string w that is to be parsed and parsing table.

Output : Parse w if w E L(G) using bottom-up. If w e L(G) . then report syntactical e rror.

Algorithm :

1. Initially push O as initial state onto the stack and place the input s tring with $ as
end marker on the input tape.
2. If S is the s tate on the top of the stack and a is the sy mbo l from in put b ufkr
po inted by a lookahead pointe r then .
a) If action [S, a] = shift j the n pus h a, then push j onto the s tack. Ad\'c1nc1.' thl' inpu t
lookahead p o inte r.
b) If action[S, a ] = re du ce A~ f1 the n pop 2* If3I sy mbnl s . If i i..., o n thl' top n f th L' ~L1el-.
th en pus h A, the n pus h goto [i , A] o n th e top o f the ~tnck.
®
TECHNICAL PUBLICATIONS - an up-tluust for knowledge
2 - 82 Syntax Ana,
0'.:es~ig9_!_n: '.__ _ _ _ _ _ _ _ _ _~ :__- --
pi~le:_r9_
~C~o m~ - - - - -- - - -- ~

h h lt the parsing process. It indicates the successf


c) If action[S, a] = accept t en a lJ ]
parsing.
Let us take one va lid string for the grammar.

1) E ➔ E+T

2) E ➔ T
3) T ➔ T * F
4) T ➔ F
5) F ➔ (E)

6) F ➔ id

Input string : id * id + id
We will consider two data structures while · taking the parsing actions and those
are - Stack and input buffer.

Stack Input buffer Action fable Goto table Parsing action

so id*id+id$ [0,id]=s5 Shift


$0id5 ___ FL-
0,=--
*i_d_+_id-'$_ _ _ _ _ l5,*J,_=_r6_ _ _~ [~ ]=-=-3_ _ _R_e_d_u_ce~ by F_~ id__ _
$0F3 *id+id$ [ 3,~]=r4 ___ [0,T]=2 Reduce by T ➔ _F 1
---------~
$0T2 *id+id$ _·f_t ______
[ 2,*]=s7 · _-_ _ __ __ _S_hi
·- - - - - - - ~ -
$0T2*7 -= +~id::.:'$.'----,-------'--
~ i=d-=- --s5
,id~]'=
[ _7'-- Sh_i_ft_ _ __
__
$0T2*7id 5 +id$
- - - - ' - - - - - - - - - '~
[ 5,+J.=.r§_ _ _ [~l]._=_10___ Re_ducejry_ F -:7 id_
$0T2*7Fl0 ,+~]=_r_3_____[0,!]=2
_ _+_id_$ _ _ _ _ __.[__1_0'- Redu~e ~ T ➔ _T: F j
$0T2 +id$ [ 2,i~J=r2__ -- - [0-',E
"--- _ 1_ _ __ Reduce by
---"-]= E➔ T
$0E1 +id$ [ ~,+J::=5.6 Shift
SOE1+6 id$ [ 6,idJ.=s5 _ Shift __________
$0E1+6id5 $ _ _ ___ [ 5,$]=r6 . . - -~ [6-'-,F
- - --'- --3____RE:._<!.._~c~_~y F ➔ id
~]'=
SOE1+6F3 ___ j _ ~ _ __ [3,$]=r4 ___ _ [6,T]=9 Reduce b y 'I_➔ F
$0El +6T9 -- _J __ - -- _ [ 9,$J=rl ·[0,E]=l E ➔ E +T
$0E1 $ Acc~ t ___
_ _ _ _ _ _ _ _A cc_e....i;p...;..t_ _ _ _ __J

fn the above table at first row we get action[0, id] = sS, that means shift id fronl
input buffer onto the stack and then push the. state number 5. On the second row v,'e
get action [S, *] as r6 that means reduce by rule 6, F ~ id, hence in the stack id i-
replaced by F. By referring goto[0, F] we get state number 3 hence 3 is pushed ontli
st
~he a~k. No~e th~t fo r ~very reduce action goto is performed. This process of par~in~
continued 1n this fashion and finally when we get action[1, $] = Accept we halt b_
15

c;uccessfully pa rsing the input string.

®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
-
compiler Design 2 - 83 Syntax Analysis

Example 2.13.5 Construct LR(O) items for the following gramma r, G:


5->S+R I R
R->R*T I T
T->(5) I i
Solution : Refer examp le 2.13.4 assumi ng
E as S
T as Rand
Fas T
Example 2.13.6 Consider the following grammar
E ➔ E+T IT
T ➔ TF I F
F ➔ F * I a I b.
Constru ct the SLR parsing table for this grammar. Also parse the input a * b + a.
AU : Dec.-12, May-17, Marks 16

Solution : Let us numbe r the produc tion rules in the gramm ar.
1) E ➔ E + T 2) E ➔ T
3) T ➔ TF 4) T ➔ F

5) F ➔ F * 6) F ➔ a
7) F ➔ b
an
Now we will build the canonic al set of SLR (0) items. We will first introdu ce
augme nted gramm ar. We will first introdu ce an augme nted gramm ar E' ➔ •
E and
then the initial set of items I 0 will be genera ted as follows :

Io :
E ➔ •E+T "'-
E ➔ •T }" ' - - As after • the symbol E appear s we will add rules of E.
T ➔ •TF ~ • After • the T appears , so add rules of T
T➔ •F } - - - - As after • the F appear s, so add rules of
F
F➔ • F*
F➔ •a
F➔ •b
be
Now we will use goto function . From state l o goto on E, T, F and a, b w il l
applied step by step. Each goto transiti on wil l genera te new state I i .

11 : goto ( 10 , E)
E' ➔ E •
E ➔ E• + T
Compiler Design ? D4 UynttJ / An1,1
/ I"'•

I 2 : goto (lo, i)
F ➔ T •

) I • I·
I· t • I· •
I· ➔ • a
F -+ • b

h goto (1 0 , F)
T --+ F •
F .-+ F • *

14 goto Oo, a)
F ➔ a•

I i; goto (10 , b)
F -, b •

:\ow we will ~ta rt app lyi ng goto tranbition s on state f 1 . From I 1 state it is possible:'<,
apply goto tranc:; itions on ly on +. Hence

I r-, : goto (I 1 , +) ·
E-,E+ • T
T - , • TF J\s after • th e T comes wi ll ad d T tra nsitions
T-, •F ~amc is tru e fo r F.

I· -, • a

'fh<: g(llO lrd n.,iti on., wil l be: app lic:J on f 2 '> latc: now . We will chom,e I tu .1Ff' ·
f~r)to tra n'> ili"n bNa ui,c: thc:n• ii, no poin t in app lyi ng goto on T.

17 : ~:ot<, (f 2 , I )
·1 7 ·1I· •

·r ., I • •
If W<' wi ll appl y g1 ,lo on n 11r b f rnm t1 lc1 lt· I2 thc:n we wi ll gc:t F , a . or I· -1 b•
which ar<· .,ld lc.• ~ I4 anJ l r; n·~pl•( tivt•ly.

®
TECHNICAL PUBLICA TIONS • on up-thr ust for knowledqe
c ompiler Design 2 - 85 Syntax Analysis

f kncl' we wi ll no t co n ~idcr 14 and Ir; aga in . Now move to s ta te 13; from J3 the goto
tro'lnsit ion:-- on "' . I knCl'

F ➔ F "' •
/\s there is no po int in applying goto on state 14 and 15 . We will choose s tate I 6 for
~oh) trnn:: : ition.

19 : goto (1 6 , T )
E ➔ E + T•
T ➔ T• F

F ➔ •b
Now we will first obtain FOLLOW of E, T and F. As the FOLLOW computatio n 1s
required when the SLR parsing table is building.
FOLLOW(E) = {+, $1
FOLLOW(T) = {+, a, b, $I
POLLOW(F) = {+, *, a, b, $1
As by rule · 2 and 4, E ➔ T and T ➔ F we can state E = T = F. But Eis a start
symbol. Then by rule 2 and 4, T and F can act as start symbol. :. we have added $ in
FOLLOW(E).
FOLLOW(T) and FOLLOW(F).
The SLR pa rsing table can be constructed as follows.

State Action Goto

a b $ E T F
+ *
&4 <; r; "'- '\
0
/\ccept
~6
S➔ ', r; r::!
2 r2
1 ..,H r-1 rI r-1
r4
4 r(1 d1 r(, 10 1 /)

rh 11, lh
"
f,
I (1 I (J

-,,1 ,, r, ,1
·'
7 I I 1 I 1 I
r '\ •,H
I r,
8 , r; I 'j I ' I '

L 'i rI ,, I ._i:.,

---
rl

(I<)
TECI IN/CAL PURL/CATION S - An llf> thrust for knowlodqe
2 00
Comp,lor De.c;ign

1 h 1 11 t1Rln)\ nh oV l' p11rH1' lnh l, ·.


N(1\N Wl' wi ll pnr:--1' tlH' in1111t 11

St:,('k lnpul huffl-r Action

~ll 1,~1, ' .,~ ~-,lllf t

~ll,1 l ~Ii,.,~ n·d1 wt· liy I' ) ,I

S,lll .1 • ht ,1$ ~" 1(1

5-()l•,1i ti hl ,1~ rt'd, l l '' • hy I1 ' fl *

$()~?, b, n~ n•d11('( ' hy ·1· > I;

~llT2 b1 11S, Shift·


S-t11 '2b'i 111$ n•d11c1 • hy 1: >h

S,Ol':F7 1n$ n•d11n· hy T > TF

$OT~ 1-.1$ nxl un· hy I~ > T


$01: 1 rn$ Shift
$0F1+h n$ Shi fl
$lll· I +bJ4 $ rcJ11Cl' by F > a
$0E1 +hi·?, $ rcJucc by 'f' >F
El +t,1'9 $ reduce by E >E+ T
\ $0F1
- - $ Accept

Thu~ input st ring is parsed .


Example 2.13.7 Construct the co/l<•ctio11 of LR(O) item sets and draw t~e goto graph for the
followin,~ gra n111inr S - i 5 S lali:.
Indicate thP conflict~ (if nny) in the variou s states of the SLR parser.
Solution : We wi ll nu rnber the produ ction rules in the gramm
ar.
1) S - i ss

2) S -i a
?, ) ~ - > r
'\l11w let u-. u in-.tru d c::i no nica l ~l'l nf ilL'rns -
S' >- • S This is augmented gramm ar.
s ➔ • ss
S ;• a
s )•
I, goto (1 0 , S)
S' ;, • S
S 1S •S

rr c 1IN/CAI PIJIJI /CA I IONS® w, 1111 111111s t lw k11ow/uclue



2 - 87 Synta x Analy sis

S - • SS
s - •a
S4 •
I,, ; goto (¼, a)
s -a •
4: goto (I 1, S)
s ~ ss.
S ➔S •S
Th"' goto grap h w ill be

FOL WW (S) = {a,$ }


appl y goto (I;y a) then we will
In 13 state th.ere is a prod ucti on rul~ S ➔ • a . If we
a prod ucti on S ➔ SS • whic h matc hes
get 12 state . Hen ce M[3 , aJ = S2. In 13 ther e is
with A ➔ a • Rule S ➔ SS is rule num ber 1.
And FOLLOW(S) = {a,$ }

M[3, aJ = M[3 , $] = r 1
table is -
Hence conf lict occu rs in state 13 . The SLR (l) pars ing

Action Goto
St.ate

a s s

0 S2

S2 Acce pt

2 r2 r2

fo1 know ledge


TECHNICAL PUBL /CA TION s ® - an up-th rust
Smtax A,,,
2- 68
_- ] - ~ : . ..,
rl - -
~ ~--- --

(11 11 tli d l'I\Y \I ~ .


f:-hif t-rc.Juce conflict!

in Yau ,
li/' illJ srr111111111r is SL R(] ) or not. Expla
t ~,,( \ -J.. rl'!1dlta th ( fi >//t
Example 2. 13.8
111,~; a·r with m1~>• 1~.

L ~ $ 1'.
L ~ rd AU : May--04. Marks 8
(O ) it r--
R ~L
se t of ite m s. H en ce a co lle ct io n of LR #· ...:

fin-t bu ild a c.monicc1l


So lu tio n : \ \'t, \\-ill
is a~ ~>i,·1m bd ow
.
s· ~ . s Is : goto(Io, id)
~):
L➔ id •
S~ • L=R
•R 4: go to (I2, =)
S ➔
S ➔ L =•R
L➔ •* R
• id R ➔ • L
L ➔
L ➔ •* R ·

go to (Io, S) L ➔ • id
11 :

S' ➔ S• . I7 : goto (I-v R)

go to (Io, L) L ➔ *R•
I., :
S ➔ L •=R

R➔ L• 18 : go to 0-v L)

R ➔ L•

13 : go to (Io, R)
S ➔ R •

go to (lo, *) ~ : go to(I6' R)

L➔*•R S ➔ L = R •

R ➔ •L
L➔ •* R
L ➔ • id

·
CA TfO Ns ® - an u{H hru st for knowledge
TECHNICAL PUBU
► 2 - 89 Synwx Anotys,s

' \ l)\\ " ·e " 'ill \-luild th e P~1 ~i n g t 0 bl1.:' fo r the abo\'e set of ite ms .

St.1te
Action goto
• id $ s L R

: s6 / r5

s5

- - - - - -- - -
0 s-l s5

'i

s
9

.-\s " ·e are getting multiple entries in Action (2, =] = s6 and rS. That means shift
and reduce both a shift / reduce conflict occurs on input symbol ·=. Hence given
grammar is not SLR(l) grammar.
Example 2.13.9 Constrnct LR (0) parsing table fo r the given grammar.
lJE ➔ E * B
2J E ➔ E + B
3J E ➔ B
4> B ➔ 0
5) B ➔ 1 AU : Dec.- 10. Marks 10

Sol ution : The collection of canonical set o f item s for the above g rammar \"l ill be -
I{) : E' ➔ • E I6 : goto(11, +)
E ➔ • E* B

E ➔ • E+B

E ➔ • B
B ➔ • O
B ➔ • l

®
TECHNICAL PUBLICA T/ONS - an up-thrust fot knowledge
~ ompiler Design 2 - 90

I1 : goto (lo, E) E' ➔ E * B •


E' ➔ E • 18 : goto (16 , B)
E ➔ E • *B E ➔ E+B •
E ➔ E •+B

T2 : goto (10, B)
E ➔ B• Now we will compute FOLLOW.
13 : goto (10, 0) FOLLOW (E') = {$}
B ➔ 0 • FOLLOW (E) = {+, *, $}
14 : goto (10 , 1) FOLLOW (E) = {+, *, $}
B ➔ 1•
15 : goto (1 1, *)
E ➔E *• B
B ➔ •O
B ➔• 1
The parsing table will .be -

State
--
+
------
Action

0 1 $ E
Goto

B
J
0 s3 s4 1 2

1 s6 s5 Accept

2 r3 r3 r3
--- ---
3 r4 r4 r4
--·- ------
-i rS rS
- - - ----~---- ----- -
:;
s3 s4 i
--------
6 s3 8
s4
--·--
7 rl rl
rl

8 r2 r2
r2
-- -

TECHNICAL PUBLJCA TIONs®


- an up-thrust for knowledge
p
,' ~, ~,v11111l< /\11ntysm

example 2.13.1 O
.~ .... :1.~ 7,
J\·' _.; .-v,1 Al I : Mnv-1:I, Mnrk11 6
1' ~lrut· t •1 l ' 11 1kl·l i( 1 1l 11 1 1·"1H•n k, d •ii'! ,, 1 11 1· 111 •, l(,r IIH' g 1vt•n
Solution : \\\ • wil l 1
l' '

gr,in1111,1r .
14 gotn( l 11 . 11)
;\ ~ ,1 .

S ~ •b I -~ g 111(1 (1,. /\ )
/\ > SI\•
I\ ) ,,-...:,
goto (10 .S) S -t • AS
S' -t S• 5 -t • b
A -t S• A I\ -t • SA
A -t • SA A -t . ll

guto (11 • S)
S -t •AS A -t S•/\
S -t • b A -t • SI\
A -t • a

I, goto (1 0 • A) S -t • AS
S -t A-S S -t • b
S -t •AS I7 goto (12 • S)

S -t • b S -t AS •

A -t • SA A -t S•A

A -t • a A -t • SJ\
A -t • ,l
S -t • AS •
goto (10 . b) S -t • b
I~

S -t b•
We wi ll list o ut the g iven rult'~
1. S -, A S FOLL OW (S) = { a, b,$ }

2. S -; b FOi.L OW (/\) - ~a, Ii~

3. A -, SA
4. A -, a

( Il l

7FCHN ICA I 1-'U(JI IC/\ /IO N .•~ i1111111 11,11 ,sl /, ,. lln.>n 1•, /,ft•
Action got o

0
,)

54
b

S3
I $ s
l
A

1 54 S3 I Acc ept 6 5

2 S4 S3 t 7
t 2

3 r2 r2
j r2 l
4

5
r4

S4 / r3
- -+
r4

S3/r 3
-----
7 ~-
t-
~
S4 S3 6 5
6

S3/ rl rl 6 5
7 54 / rl

SLR.
Tiu s sho ws tha t the giv en gra mm ar is· not
Exa mp le 2.13.11 Consider the foll owi ng grammar
S ➔ AS I b
A ➔ SA I a.
ar. Show the actions of the parser for the
Con stru ct the SLR pf!-rse table for the gramm
AU : May -14, Marks 16
inp ut stri ng "abab ".
tabl e.
Sol uti on : Ref er exa mp le 2.13.9 for par se
_ _ _ _ _ _ _ _ _ __
Par sin g inp ut a__b_a_b_ _ _ _ _ _ _ _ _
Sta ck Inp ut buf fer Act ion

$0 aba b$ shif t 4

$0 a4 bab $ redu ce by A ➔ a

$ 0 A 2 bab $ shif t 3

$0A 2b 3 ab$ redu ce by S ➔ b

ab$ Con flict S4/ rl occu rs


$0 A2 S7 ----
Example 2.13.12 Generate SLR pars
ing table for the foll owi ng gra mm ar
S ➔ Aa I bA c I Be I bBa
A ➔ d
B ➔ d

and parse the sent ence "bdc " and "dd"


B
2 - 93 Sy nta x Analysis

-
co mp ile r De sig n

Solution : W e wi U w ri te th e gr am m a r us in g d ot op
er at or .

lo : S' ➔ • S
S ➔ • Aa

S ➔ •b A c
S ➔ •Be
S ➔ • bBa
A ➔ •d

B ➔ •d
ne w
l fr om I . Ea ch go to (I) w ill ge ne ra te
sy m bo
N ow w e w ill ap pl y go to on ea ch 0

su bs eq u~ nt st at es .
go to (I 0 , S) S' ➔ S •

go to (I 0 , A)
S ➔ A •a
go to (I 0 , b)
S ➔ b •A c
S ➔ b • Ba
A ➔ •d

B ➔ •d

go to (I o, B)
S ➔ B •c

Is go to (I 0 , d)
A ➔d •
B➔d •
go to (12 , a)
S ➔ Aa •
go to (13 , A)
s ➔ bA. C
lg go to (1 3 , B)
S ➔ bB • a

go to (l4 , c)
S ➔ Be•
-th ru st for knowledge
IC A TI ON s® - an up
TE CH NI CA L PU BL
r Syntax Ana/Ysi!
Compiler D esign 2 - 94

f 1<i : hulo(1 7 . l'. )

S ► b /\ c •
hulo (1 x . a)

S ~ bl3a •
Now we wil l compute FOLLOW of all no n-terminal symbols.

FOLLOW (S) = I $ I
FOLLOW (A) = I a, c
FOLLOW (B) = I a, c I

The SLR p arsing tab le wi ll be

Action GoTo
State

a b d $ s A B
I
C

. ss ] 2
lo S3

1,
I I
Accept

12 S6 I
13
r I S5 7 8

14 S9

ls r5/r6 r5/r6

16 rt

17 S10

11< SI I

19 r'.:'\

lio r2

III l'-1

The given g rnn,mor iA not SLR(1) oi-. tlw l.r,. ent,·y Lt", tubll! ~hows rcd m'~/ I'\',Jut"
co nflict.

ll ~(;NNIC/\1. PUIJI. ICA r,oN!i 0 "" Ill) '"' (ISi ro, /(.11ow/odlJu
-c ompiler Design 2- 95 Syntax Analysis

i) Parsing "bdc"
- - - -

r :•:k Inpu t buff er

bdc $
Action table

IO, bl = S3
Goto tabl e Pars ing action

Shift

Sh ift
I $ Ob3 de$ 13, d] = SS

$ Ob3d 5 $ [3, Al = 7 red uce by A ~ d


C [5, c] = r5 or r6
Conn ie! choo se r5
--- -
resolution
I $ Ob3A7 C $ [7, c] = 510 Shift
-- - -
redu ce by S ~ bAc
$ Ob3A7c10 $ [10, $] = r2 [O, SJ = 1
·---- - -

$ OSI $ Acce pt Acce pt

ii) Parsing "dd"

·Act ion table Goto table Parsing action


Stack Inp ut buff er
Shift
$ 0

$ OdS
dd $

d$
[O, d] = 55

No jlCtio n defin ed
No action
i
is inva lid strin g for give n gram mar .
The strin g "dd " can not be pars ed as it

Review Questions

AV : May-04, Marks 2
1. Define LR(O) items.
AU : Dec.-05, Marks 2
2. What are Kernel and non-kernel items ?
parsing table. AU : Dec.-09, Marks 8
3. Give the algo rithm for constructing an SLR
AU : Dec.-17, Marks 5
ple.
4. Explain LR parsing algorithm with an exam
s 13
AU : May-06, 07, 10, 18, Dec.-10, Mark
flII Canonical LR Parsing
bol i:-
ing tech niqu e in wh ich a look a head svm
• The cano nica l set of item s is the pars is
s. He nce the collection 0 f set of itt•m s
gene rate d whi le con stru ctin g set of item
refe rred as LR( l).
tlw
tha t the re is one loo k,1 hc.1d svm hP I in
:-('t
• The va lue ] in the brac ke t indi ca tes
of item s.
thl'
bnb jc; th a t tlw f'clr:-- (· r (\111 d l•~·id (·
• The sign ifica nce of look ahe ad sym
sy mbo k I ll'nce thi:-- p.us in~ pn)C l'dur ('
redu ctio ns base d on th <.· k look a head
beco mes a n effic ient o ne.
®
TECHNICAL PUBL ICA T/ON S - an up-th rust for A.nowledgo
Compiler Design 2 - 96 Syntax An

alys,s

• We follow the same steps as discussed in SLR p arsing technique and those are
1. Construction of can onica l set of items along with the lookahead .
2. Building canonical LR parsing table.
3. Parsing the input string u sing canonical LR parsing table.
Construction of canonical set of items along with the lookahead
· ■.r1111w•~M"'-i'im11~ana"i1
1 · .,+•u-i
1. For the grammar G initially add S' ➔ • S in the set of item C.
2. For each set of items Ii in C and for each grammar symbol X (may be terminal or
non-terminal) add closure (Ii, X). This process should be repeated by applying
goto((Ii, X) for each X in Ii such that goto((Ii, X) is not empty and not in C. The set
o f items has to constructed until no more set of items can be added to C.
3. The closure function can be computed as follows :
For each item A ➔a •X 13, a and rule X➔ y and b E FIRST$ a)
such that X ➔ • y and b is not in I then add X ➔• y, b to I.
4. Similarly the goto function can be computed as : For each item
[A ➔ a• Xj3, a] is in I and rule[A ➔ a X •13, a] is not in goto items then add
[A ➔ aX •13, a] to goto items.

This process is repeated until no more set of items can be added to the collection C.
Example 2.14.1 S' -t S
S ➔ CC

C ➔ aCld
Construct LR(l) set of items for the above grammar.
Solution : We will initially add S'➔ • S, $ as the first rule in 10 . Now match

S'➔ • S, $ with
[A ➔ a •Xl3, a]
Step 1 : S'➔ • S,$
A ➔a •Xl3, a
A = S', a = E, X = S, 13 = E, a =$
Jf the re is a p roduction X ➔ y , b then add X ➔ • y, b
:.s ➔ • CC, b E FIRST (13 a) For $ a) w e consider S' ➔ • S,$
b E FIRST(£ $) as E $ =$
b E FIRST ($)

b = l$1

- - - ---~~-:-:-:~ =~®c =.:::;-,:;-:~-;;:-------


TECHNICAL PUBLICA T/ONS - an up-thrust for knowledge
~Design 2 - 97 Syntax Analysis

Step 2 : --- S 4 CC, S wiJI be added in I



0-
~(_lW S ➔ • CC. S i.5 in lo we will match it with A ➔ a • XB, a·

A =S'. a =£, X = C. J} = C, a = S
If there i~ a production X ➔ y, b then add x ➔ • y ,b
:. C- • aC, Here b E FlRST(J3 a)
c- • d b E FIRSr(C$)

b E FIRST(C) as FIRST(C) = la, d i


b = la, d }
C ➔ • aC, a or d ,,ill be added in Jo.
Similarly C ➔ • d, a/ d '"ill be added in lo·

Hence Io:
S' ➔ • S, $
S ➔ •CC,$
C ➔ •aC, a / d
C ➔ • d, a / d

Here a/ d is used to denote a or d. That means for the production C➔ • aC, a and
C➔ •aC, d.

Step 3: Now apply goto on Jo.


S' ➔ • S, S
S ➔ • CC,S

C ➔ •aC, a/ d
C _➔ •d, a/ d

Hence

gotc>Oo, S)
11 : S' ➔ S-, $

Now apply goto on C in Io-


S ➔ C • C, i add in 12. Now as after dot C comes we will add the rules of C.
X = C, ~ =E, a =$
X ➔ • '( ,b where b E FIRST(l3 a)
C ➔ • aC b E FlRST(E $)

TECHNICAL PUBLICATIONS® - an up-thrust for knowledge


Com piler Des ign 2 - 98
Syntax An
a/Ysis
C -> • d b E FIRST ($)
llc nn·
c~ • aC, Si Mi d C ➔ • d, $ w il l be add ed to 12

t2 : );oto(\ 0, C)

S ➔ C• C, $
C > • tlC, $
C -➔ • d, $

No w we will app ly got o on a


of lo for rul e C ~ • ·a c, a / d
a / d will be add ed in 1 . tha t bec om es C---.:, • aC
· 3 . .
C -➔ a • C,a/ d '

As A = C, a = a, X = C, ~ = £, a = a/ d
H ence X -➔ • y , b
C -➔ • aC b FIRST(~ a)
E
C -➔ • d b E FIRST(i:: a) or FIRST(£ d)
b = a/d
He nce

13 : got o(Io, a)
C-➔ a • C, a / d
C-➔ • aC , a /d
C-➔ • d, a/ d

No w if w e app ly got o on d
of Io in the rule C -➔ • d, a /
a/ d hen ce 1 bec om es d the n we get ( ---) d •,
4

14 : goto(lo, d)
C -➔ d •, a/ d

As app lyin g gotos on I is ove


0 r. We wil l mo ve to 1 bu t w e
fro m 11 we w ill app ly got o 1 get no new producti0n
on C in 12 . An d the re is n o clo
sur e pos sib le in this stat<?.
15 : goto(l 2, C)
S -➔ CC •, $

We wil l app ly goto on a fro m


12 on the rul e C-➔ • aC , $
c1 nd we get the st<1tl' 1t,·

TEC HNI CAL PUB LIC ATI ON ®


S - an up-t hrus t for kno wle
dge
2- 99 Synta x Analy sis
eomp ifer Desig n

C➔ a• C, $
A➔ a •X (3, a
A = C, a = a, X = C, f3 = E, a =$
X ➔ •Y
C➔ • aC and C ➔ • d
b E FTRST(f3 a)
b E FIRST(E $)
b=$

16 : goto (l2, a)
C ➔ a• C, $
C ➔ • aC, $
C➔ • d, $

beca use the seco nd com pone nt


You can note one thing that 13 and 16 are diffe rent
in 13 and 16 is diffe rent.
Appl y goto on d of 12 for the rule C➔ • d, $.

17 : goto (I 2, d)
C ➔ d •, $
1 and 14 respe ctive ly and there
Now if we appl y goto on a and d of I3 we will get 3
y goto on C of 13 .
is no poin t in repe ating the state s. So we will appl

I8 : goto (I 3, C)
C ➔ aC •, a/d

App lying goto s on a and d of 16


For 14 and 15 there is no poin t in appl ying gotos .
on 16 for C for the rule.
gives 16 and I 7 respe ctive ly. Henc e we will appl y goto
C ~a • C, $

19 : goto( l6' C)
C➔ aC •, $
y goto . Henc e the procc~:- t)f
For the rema ining state s 17, I8 and 19 we cann ot appl
the set of LR(l ) item:- con:- i:-t-- L) f
cons tructi on of set of LR(l ) item s is comp leted . Thus
10 to 19 states.

®
TECH NICAL PUBL /CA TIONS - an up-thr ust for knowledge
Compiler Design 2 - 100 Syntax Ana/ ,
Ys,8

lo: 15 : goto (1 2 , C)
S '➔ •S , $ S➔ CC• , $
S➔ •CC. $
16 : goto (1 2, a)
C ➔ • aC , ai d
C-4 • d. a/d C '➔ a•C , $
C➔ • aC, $

11 : goto (1 0, S) C➔ • d , $

S'➔ S •, $
Ii goto (1 2, d)

12: goto (1 0, C)
C➔ d•, $

S➔ C • C, $
18: goto (1 3, C)
C---1 •aC, $
C---1 aC•, a/d .
C---1 •d, $

13: goto (10, a) · 19: goto (16, C)


C---1 a• C, a/d C---1 aC•, $
C---1 • aC, a/d
C---1 • d, a/d

14 : goto (1 0, d)
C---1 d •, a/d

Construction of canonical LR parsing table


To construct the canonical LR parsing table first of all we ·will see the actual
algorithm and then we will learn how to apply that · algorithm on some example. The
parsing table is similar to .SLR parsing table comprised of action and goto parts. ·

Input: An augmented grammar G'.

Output : The canonical LR parsing table.

Algorithm :
1. Initially construct set of items C · = 110, I1, I2 .. -1nl where C is a collection of set of
LR(1) items for the input grammar G'.
2. The parsing actions are based on each item Ii. The actions are as given below :
a) If [A ➔ a • a ~, b] is in Ii and goto(Ii, a) = Ii then create an entry in the action
table action[Ii, a] = shift j.
b) ff there is a production [A➔ a •, a] in Ii then in the action table
action[li, a] = reduce by A ➔ a. Here A should n ot be S'.
c) If there is a production S' ➔ S •, $ in Ii then action[i, $] = accept.
.3. The goto part of the LR table can be filled as : The goto transitions fo r ·state i i~
consid ered fo r non-terminals only. If goto(Ii, A) = Ii then goto[Ii, A] = j.
-J.. All the entries not defined by rule 2 and 3 are consid ered to be "error".

®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
Syntax Ano/yaw
c omp1/e1 Dos,gn 2 - 10 I

Exnmplo 2.14.2 C<>11~t, 11ct 1111' IR ( I ) 1mrsi11g tnblc for tlw f ollowi 11.1J, 8,rllllllllflr
I) S -> CC
2) C ➔ ,1C
3) C -> d.
)
Solut ion :Firs t we wi ll const ruct the set of LR( I) items.( sec o n next page
2.14.1 .
The OFA for the set nf items ca n be draw n as s hown in Fig.
atJ, b I as
Ncrw consi der 1 in w hich there is a rule match ing with IA -> a •
0
the state J:\· H e nce w e
C-> • nC, n / d a nd if the goto is applie d on a then w e get
wi ll c reate entry ciction[O, a ] = s hift 3.
lo: 15 : goto (1 2 , C)

S'➔ • S, $ S➔ CC•,$
S---+ •CC, $
16: goto (1 2, a)
C➔ • aC, a/d
C ➔ • d, a/d C➔ a• C, $
C➔ • aC, $
C➔ • d, $
t1: goto (t 0 , S)
S ' ➔ S •, $ Ii goto (1 2 , d)
C➔ d•, $
12: goto (t 0 , C)
S➔ C • C , $ 18: goto (1 3, C)
C➔ • aC, $
C➔ aC•, a/d
C➔ • d,$

19: goto (1 6 , C)
t3 : goto (1 0 , a)
C➔ aC•, $
C➔ a• C, a/d
C➔ • aC, a/d
C➔ • d, a/d

14 : goto (1 0 , d)
C ➔ d •, a/d

Simil arly,
In 10
C-> •d a/ d
A->a • a f3, b
A = C, a = £, a = d , f3 = £, b = a/ d
goto (lo, d) = 14
hence action[O, d] = s hift 4
For state 14
C-> d •, a / d

TECHN ICAL PUBL/C A TIONs ® - an up-thru st for knowle dge


Compiler Design 2 - 102
Syntax Analysis

A -, u. •, a

A = C, c.£ = d, a = a/ d
actionl4 , a l = reduce b Y C -, d i.e. rul e 3
S'-, S •, $ in I 1
So \\ e \, ill crea te action[l , SJ = accept.

C C

a C

a C

Fig. 2.14.1 DFA [goto graph]


The g o to table can be fill ed by using the goto functions.

~ ©
goto (1 0 , S) = 1
1
goto (I O, C) = I
2
goto (I 2 , C) = J
5

Fl)r instctnn.• goto (lo, S) = I 1, I fe nce g o to [O, S] = 1. Continuing in this fa shion "'·e
l ,111 till up tlw LR( I) pnrsing tab le as fo llo ws.

®
TECHNICAL PUBLICATIONS - an up-thrust for know/edge -
Compi ler Design 2 - 103 Syntax Analys is

Actio n Goto
State s
a d $ s C

0 sJ s4 l 2

Accep t

2 s6 s7 5

3 s3 s4 8

4 r3 r3

5 rl

6 s6 s7 9

7 r3
----
8 r2 r2
- ----
9 r2

as synta ctical error .


The rema ining blank entri es in the table are consi dered

Parsing the inpu t using LR(1 ) pars ing table


"aadd " as
Using abov e parsi ng table we can parse the input string

Actio n table Goto table Parsi ng actio n


Stack Inpu t buffe r

$0 aadd$ action[O,a J=s3

add$ action [3,a]= s3 Shift


$0a3
dd $ action f 3,d ]=s4 Shift
$0a3a3
[3,CJ=8 Reduc e by C ➔ d
$0a3a 3d 4 d$ action f 4,d J=r3
[3,C]= 8 Reduce by C ➔ aC
$0a3a 3C8 d$ action [8,d ]=r2

d$ action [8,d ]= r2 f0,CJ =2 Reduc e by C ➔ aC


$0a3C 8
d$ action f2,d J=s7 Shift
$0C2
$ action [7,$] =r3 [2,C]=S Reduce by C ➔ d
$0C2d 7
Reduc e by S ➔ CC

I
$ acti on(5,$ J=r1 [O,S]= l
$0C2C5

$051 $ accep t

ing LR parse r or c.m onica l LR


Thus th e g iven input s tring is s ucces sfull y pa rsed us
parse r.

® dge
TECHN ICAL PUBLI CATIO NS - an up-thru st fot knowle
- -
compiler Design

r-xample 2.14.3
2

Co11stnict CLR pnrsin,~ table jro111


104

S ~ AA
A -4 Aa I b iM•Mwtii•M?tii/tj
Sol ution : \\'hill' l'lm~tru cting CL R par<;ing tab le• firc.,t of a ll we ha vt· to built , a
. . . .
Ill
n<,r11 , ;, 1
-.l'I ,11 item~ " ·1th l111<' look.~1he.1d symbo l. The canoni ca l set o f LR(1) 1tc m H ifi a-; fcJII
{JIN<i
S' -t • S, $

S -t • AA , $
\ A ➔ • Aa , b

A ➔ • b, b
I1 goto (l0, S)

S' ➔ S•, $

L : goto(10, A)

S ➔ A • A, $

A ➔ A • a, b

A ➔ • Aa, $

A ➔ • b, $

11 : goto(lo, b)

A ➔ b • , bla

I~ goto(!.,, A)

S ➔ A A • ,$

A ➔ A • a,$

I:; : goto (1 2 , a)

A --, Aa • , b
I,, · goto (I ~, b)

A - ,. b • ,<i>la

l7 · gr ,t() (J4, d)

A ... Aa • <ii

TECHNICAL PUBLICA TIONs®


• An un thn,d r, .. ,
c ompiler Design 2 - 105 Syntax Analysis

The can o nica l LR pa~c;ing ta ble ca n be co ns tru cted as fo ll ows -

States Action Goto


a b $ s A
0 S3 2

1 Accept
- - - --
2 S5 S6 4

3 r3 r3
4 S7 rl
- - -
5 r2
6 r3 r3
7 r2

Example 2.14.4 Consider the Context-Free Grammar (CFG) depicted below where "begin ",
"end" and "x" are all terminal symbols of the grammar and stat is considered the starting
symbol f or th.is grammar. Productio.n s are numbered in parenthesis and you can abbreviate
"begin" to "b " and "end " to "e" respectively.
Stat ~ Block
Block ~ begin block end
Block ~ Body
Body~ x
i) Compu te the set of LR(l) items for the this grammar and draw the corresponding DFA.
Do not f orget to augment the gra~nmar with the initial production S ~ Start$ as the
production (0).
ii) Construct the corresponding LR parsing table. AU : May-18, Marks 13

Solution :
We will rewrite the grammar using abbreviations as

Sta rt = S, Block = B, Body = C


begin = b a nd end = e
The grammar w iU n ow b e
1. S -? B
2. B -? b Be
3. B -) C
4. C ~ x

- - - - - - -------- -- - ®
TECHNICAL PUBLICA TIONS - an up-thrust fo, knowlec lye
11 • , ort<.I ru 110n , f t,,nmu, ,1I v t , f tt,·rr I.P.(t, Id I ~),.
",' ' •"· s

,~ , . ( (

,:!OtcJI , '°:J

I .,_

B --, b • I~, S
B , • bl~:,,:
B , • C., (:

I• gcit.d l ,, , CJ

B , ( •, $

1- w,tcfl ,,, / J

( , / •, s
I r, got<fl -~ , BJ

B , bB• c•, <.,

I~ , b • Hu, c•

11 ., • ( , r•

( I • .(, ( •

ff U IN/CAI f'UBl /(.,A T/(Jf 15 , ,m 1111 tt,m~r for k11owle<11;~


comp rler Des,g n 2- 107 Synta x Analy sts

Is gotc:(1 3 , C)

B ~ C •, e

19 gotc:(13 ,x)
C ~x •, e

110 gotc:(1 6 , e)
B ~ bBe •, £

111 gotc(I 7 , B)
B ~ bB • e, e

I 12 gotc(I 11 , e)
B ~ bBe •, e
The LR pars ing ta ble will be

State Actio n Goto


-- - -
b e X s s B C
---
1 2 4
0 S3 S5 -
--
ACCE PT

r1
2
- - -
6 8
3 S7 S9

r3

- --
r4
5 -- - -
- -
S10
6
---- -- - ---
11
7
- - -- - -
8 r3

9 r4
- ----
r2
JO

11 S12

12 r2

®
TECH NICAL PUBL ICA T/ONS - an up-thrust for knowl edge
( 1'111 / u/, / / lt1•, t,/I
I
Syntri;, I\
, .,,
ll;it,,,

R1vl1w Qu11tlon•

\\ lt, tl I tit, . ,,·111/1, ,111,


,· .., /.i,,k //I/1 '1l1 I Ill
I /'I
'
I) tf1 •11 1·, 1
•. 1J•■ ?et111 1~
' I ,,,,,,,,, 1111 I I\ /l// /'•111,X ,11.~•11111/1111 Ill ,l,•l 11tl.
IJ•■ @t@Xdi§ l!t)
1 \\ 11 11 ,111 ,1l-.:,111 t/1 111 /111 ( / .I<
ti,, • 1iJ11 •, l1111/ i,11111/ I I{( / ) rll
if 1•111 ~ for Crr1111111ar C.

fl t) LALR Parsing t+i•Mtt41if@IWWI§


l11 tlw. 1v1w 111 11,1r:-,1•r llw
l11n k.1lw,1d :--y mbol is gl'
t,,t,tv 1,h1,,11wd hv th, :-- nw ne rat ed fo r each se t of
th1,d ,ire :--1Ti.1 IIN in size itern. The
'-I R .,n d I A l I{ p.m,in) th an LR(k) pa rse r. In fac
-1, ,Hl' ;·ilw.1 ys sam~. Mo:;;l t the states of
of the pr og ra mm in g
r,,r-.l'I'-.. lan gu ag es use LALR
We 11,lhiw tlw :--.rnw :--lt•ps
.1 :-- Jiscu ssl'd in SLR
an d ca no ni ca l LR pa rsi
.rn J tho:--v ;1r(• ng techn iques
1. Cnn-..trucli on of c.1n()
nicol se t o f ite ms a long wi
th the loo ka he ad .
~- lh1ildi ng L/\1 .R porsing
tab le.
'.'\ . P.:1rsi ng the inp u t :--Iring
us in g ca nonical LR pa rsi
ng table.
Construction set of LR
(1) items along with the
lookahead
Th e co ns tru ction LR(1)
ite ms is sa me as discu
differe> ncc i:-- tha t : in co ns ssed in se cti on 2.1 3. Bu
tru ction of LR(l) ite ms for t the onlv
·stales if the secon d co mp LR pa rse r, we ha ve dif fer
onen t is differen t bu t in ed the two 1

hy me rg ing o f fir st and sec thi s ca se we wi ll m er ge the two states


on d co mp onen ts fro m bo
th the sta tes.
Fo r ex am ple in sec tion
2.14 for the ex am ple 2.1
different secon d co mp on 4.2, we ha ve go t 1 an d
en ts, bu t for LALR pa rse 3 16 because ot
..,a me hy me rg ing these sta r we wi ll co ns id er th es e tw o states as
tes . i.e.
11 + 16 = 1
16
~ I<:n (P

Iv, : goto (1 , a)
0
( --, a • C ,a /d /$
C 1 • a( , a/ d / <\,
C - >• d, a/ d/ <,
Ll'I u<-, lnke on(• l' XM ll J) IP l0
pa r<,t•r. uncIc r:--la nd lhc co ns tru
cti on nf LR( I) itc rns
for L,\ L~

- - - - - - -- -- - --
TECH NICAL PUBLICAT/O
NS® - an up-thrust for
-- ----
kno wle dg e
? · 109 '";yr,t1r/ A M/yr.iii
~ -
di·c®~'ill'''
ac
s -> cc
C ~ rl
cmi~tmrt ~ct of LR(l) items for LALR parser.
Solution : rir:-t vv e vv ill constru ct set of LR( I) items.

lo : S'➔ •S,$ 15 : goto (1 2, C)


S➔ •CC , $
S- > CC • , $
C➔ • aC, a/d
C➔ • d, a/d 16 : goto (1 2, a)
C'➔ a•C,$
11 : goto (1 0 , S) C- > • aC, $
S'➔ S •, $ C➔ •d,$

12 : goto (1 0 , C)
17 : goto (1 2 , d)

S➔ C • C, $ C- > d • , $
C➔ • aC, $
C➔ • a,$
'a :goto (1 3, C)
C➔ aC •, aid
13 : goto (1 0 , a)
C➔ a• C, a/d lg : goto (1 6 , C)
C ➔ • aC, a/d C➔ aC •. $
C➔ • d, aid-

14 : goto (1 0 , d)
C ➔ d •, a/d

Now we will merge states 3, 6 then 4, 7 and 8, 9.

15 : goto (1 2 , C)
lo:
S'➔ • S, $ S➔ CC•, $

S➔ •CC, $
18 g: goto (1 3 , C)
C➔ • aC, aid
C➔ aC • aid/$
C➔ • d, aid

11: goto (1 0 , S)
S'➔ S • , $

1 : goto (1 0 , C)
2
S➔ C • C, $
C ➔ • aC , $
C➔ • d , $

TECHNICAL PUBLICATION S® · an 11r1 lh111s l fur k11owlu1l<W


Comp1/01 Design 2 - 110
Syntax Ana/y .

- s,s

130 · goto (10 , a)


C , 0 • C, aid/$
C , • aC. aid/$
C ➔ • d. aid/$

147 : goto (10 . d)


c -} d • . a id/$

We h a H ' m ergL'li two states l'.\ and 16 and made the second compon ent as a or d or
$ . The producti on rule w ill rernai.n as it is. Similarly w e di.d m 1 and 1 . The
4 7 set of
it~m~ consist of sta tes I 10, 11, 12, 136, 147, 1 , 139\
5

Construction of LALR parsing table -


Th e algorith m fo r construc tion of LALR p arsing table is as given below.
Step 1 : Constru ct the LR(l) set of items.

Step 2 : Merge the two states L and Ii if the first compon ent (i.e. the producti on rules
w ith d ots) are matchin g and create a new state replacin g one of the older
state such as Li = L u I;
Step 3 The parsing actions are based on each item L. The actions are as given
below:
a) lf [A ~ a • a 13, b I is in Ii and goto (Ii, a) = ~ then create an entry in the
action table action[I, a) = shift j.
b) lf there is a producti on [A ➔ a• , a] in Ii then in the action table
action [I i , a]= reduce by A ➔ a. Here A should not be S'.
c) lf there is a producti on S' ➔ S •, $ in Ii then action[i, $] = accept. ·

Step 4 : The goto part of the LR table can be filled as : The goto transitio ns for state i
is consid ered fo r no n-termin als only. If goto(Ii, A) = ~ then goto[li, A] = j.

Step 5 : If the parsing action conflict then the algorith m fails to produce LALR parser
and gramma r is not LALR(l) . All the entries not defined by rule 3 and 4 are considered
to be "erro r" .
Example 2.15.2 5 ~ cc
C➔ aC

C➔ d
Construe / //,e pnrsing /able for LA LR(l) pnrser.
Solution : First th e set LR(1) items can be constructed as fo llows w ith m erged states.

®
TECHNICA L PUBLICAT IONS - an up-thrust for knowledge
c0 mpile
---_r _De_s_,g_n_ _ _~ - - - -- - - -~2...=-__:_1~1!___
1 _ _ _ _ _ _ _ _ _ _~~~~
~
Synt ax Ana lysis
l o:
s·➔ • S, $ 136: goto (1 , a)
0
S➔ •CC , $ C➔ a• C, a/d/$
C➔ • aC , aid C-) • ac, a/d/$
c➔ • d , aid c-) • d , aid/$

11: goto (1 ,S)


0
14,: goto (1 , d)
S'➔ S • , $ 0
c-) d • , aid/$
li goto (1 , C)
0 15 : goto (1 , C)
S ➔ C • C, $
2
S➔ CC •,$
C➔ • aC, $
C➔ • d, $ 189 : goto (1 3, C)
C➔ aC • .a/d/$
Now con side r stat e 10 the re is a ma tch
wit h the rule [A➔ a, • a~, b] and goto (~,
a) = ~-
C ➔ • aC, a Id/ $ and if the got o is
app lied on a' the n we get the stat e 1
we will cre ate ent ry action[O, a] = shif 36 . Hen ce
t 36. Similarly,
In Io
C ➔ •d a/d
A ➔ a• a 13, b
A = C, a = £, a = d, 13 = E, b = a/ d
goto (lo, d) = 147
Hence action[O, d] = shif t 47
For stat e 147
C➔ d•,a/d/$
A ➔ a •,a
A= C, a = d, a = a/d /$
action[47, a] = red uce by C ➔ d i.e. rule 3
action[47, d] = red uce by C ➔ d i.e. rule 3
action[47, $] = red uce by C ➔ d i.e. rule 3
S' ➔ S •, $ in I1
So we will cre ate act ion [l, $] = acc ept.
The goto tab le can be filled by usi.ng the got o functions.
• ·
t O [O Sl = 1. Con tinu in this fashion '"'l'
For inst ance got o(l , S) = 11 · Hen ce ing
0 go '
can fill up the LR(1) parsing tabl e as
follows. -
Goto
State Act ion

d $ s C
a

-
2
',} 6 !>47
0

-- ---- -- -- -- -- -:=='®
TEC HNI CAL PUB LIC A
T/ON S
-
- an up- thrust for knowledge
-- - -
I fl/I IJli/rJ/ / )tl ,~/{J/1
/ I/ ;>
Synt nx Anfl /
Y'iln
/\,I,.,•I
'
~ i, \ 11 ,. 1,' ,.
)

\t, . ,,, ,,,17 Kl/


·I ,• I I I \ I 1
,,
1 I

Kll
r2

1lw , trin>~ lw l,ing i n)-!. In g ive n gr;1m mar c«:


1 11 be panH.:d usin g L ALR pa rser . The
blank
1 1' 11 ll ':-, .H'l' :-, 1Ir) I) 1)i-l d t1) lw i- y
1
1
nl act k«:1 I errn rH.
Parsi ng the input string using LALR parser

1lw :-. t rin g h.wi ng regu lar ex press ion = a~·d


a*d E g ramm ar C. W e will consider
in put
... 1,111g ,1 :-. '' ,1,1d d " for pa rsing by usin ~ LJ\L
R p arsin g table.

Stac k Input buffer Action table Goto table Parsing action 7


$1) ,1.idd $ acti on! O,a l=s3h
S,( ),1 "\ ( ' ;:idd$ acti onl 36,a l=s36 Shift
'!,0<116 ,l 1(1 dd$ actionl:16,d l=s47 Shift
-~·-
$001(,,116d4 7 d$ aclio n[47,d l=r36 [36'.C l~ 8~
Si( ).116ci 1r,ON
- Redu ce by C ➔ d
d$ acti onl 89,dl =r2
-- [36,Cl=89 Re~ u~e by C ➔ aC
'l,( Ja16(. 89 d$ ac ti onf 89,d l=r2 [O,Cl=2
--··- ·--- - Redu c~ by C ➔ aC
S,(l( 2 d$ actio nf 2,d l=s47 Shift
$0C2d4 7 $ acti on[47,$ l=r36 [2,C] =S
--· -·-- - Red_uce by C ➔ d
'l,OC2CS $ actio nI5,$ l=rl IO,Sl=l Re~uce _by S ➔ CC
$051 $ accep t

Thu c:; th e LJ\L R an.d L R pa rser will mim i


c one anot h er on the same inpu t.
Example 2.15.3 Construct LALR parsing table fo r the
following grammar :
S-, Aa
S -, bAc
S --, de
S · , bda
I\ J rt
Parse the inpu t ~Irin g iJdL u~ing table xrnera tcd by you
.

TECHNICAL PUBLICA TIONs ® - an up-th ru st


for knowledge
compiler Design 2 - 11 3 Syntax Analysis

Solution : Let UH first number the production rul es as below.


1) S ➔ Aa
2) S ➔ bAc
3) S ➔ de
4) S ➔ bda
5) A ➔ d

Now we will construct canonical set of LR(l) items for the above grammar.
lo : S' ➔ • S, $
S ➔ • Aa, $
S ➔ • bAc, $
S ➔ •de,$
S ➔ • bda, $
A ➔• d, a
In above set of items we will start from S. ➔ • S, $. The second component is $
initially. After • the S comes, hence will add the rules deriving S. Now we have got
the rule.
S ➔ • Aa, $
It is resembling with A ➔ a • x ~, a
Now we can map x to A then the second component is FIRST(~ a)
In our rule
A ➔ • d and second component is FIRST ($a) = a
Hence A ➔ • d , a will be added in Io·· ·
11 : goto (Io, S)
S' ➔ S •, $ We will carry second component as it is.
I 2 : goto (Io, A)
S ➔ A • a,$
I3 goto (J O, b)
S ➔ b • Ac,$
S ➔ b •da, $
A ➔ •d, C
._. S ➔ b • /\c, $ and 1:1RST(lt1) . 1
= FIRST(c$) - l' · llcncl'

r
Sl'Cond cnmp\l1wn t 1)1 1\ > e(i ,~ c

Qi)
TECHNICAL PUBL/CA T/ONS - an 11p-thnist for knowledge
? 114
'/
c n111r,IIr,1 I 111 • 1r111

II goto (l o, d)

s , d • <,'li
A , d •, ,,

Ir; g11lo( l 2, ,1)


'-, , 1\.i . ' '!,

11, guln (1 ,, A)
<-; I hi\ . l, 'l,

17 gl>lo (I 1, d)

~ > bd • c1, ~

A I d • ,C

I8 goto (14 , c.)


c..; > de • , 'l,
I9 goto (I 6 , r)
S ➔ hAc • , $
I 10 : goto(1 7 , c1)
S ~ bd a • , $
In above set of canonica l items no stales arc hav ing common production rulr:<:
I fence we ca nnot merge these stales. The 1,ame set of items will be considered for
building LALR parsi ng table.
W e will construct LALR parsing table using fo ll owing rules :
1. If IA -? <J. • af~, b jis in I; and goto ( I;, a) = Ii then action [i, aj = Shift j.
2. If there is a production IA ~ u. • , a I in some state l; then
action [i, a] = red uce by A ~ <J..
3. If there is a product S' ~ S • , $ in I ; then action [i, $] = accept.

State Action
Go~
a b C d $ S A
fl ', 1 ..,4 2

Acn•pt
2 <, ')

..,7 6
I l .. 1-1

- _..J- - - - - -

7[CHNICAL PUBLICA TIONSQ~ FJn up-/1,rust l o, knowledge


.-- 115

r..:
hl

Stack
Input buffe r Action
bdd,
Shift J
d,·$
Shift 7
$0b3d 7 t·$
Red llCL' by /\ )J
$0bJA6 ,·$
Shift 9
SOb3A6c9 $
Rcdw.' L' by S , bAc
$051 $
Accept

Thus the input string gets parse d comp lNl" ly.


Example 2.15.4 Show that tht> fo llowi11g grnmmnr
5 ➔ ..-\a I l>Ac I Be I bBn
A ➔ d

B ➔ d

IS LR ( I) but 11ot LALR (l). AU : Dec.-18, Marks 13


Solutio n : We will numb e r out the produ ction rules in
g iven gra rnma r.

1) S ➔ Aa
2) S ➔ bAc
3) S ➔ Be
➔) S ➔ bBa
5) A ➔ d
6) B ➔ d

~ow we wi ll firs t con s truct canon jcal set of LR(l) items .


S' ➔ • S, $
is $. As this rule
Initially for the augm ented gram mar the secon d comp onent
[S ➔ • S, $] is match ing with (A ➔ a •X ~' a] where a is£, Xis S.
Bis F and a is$.
es d eri ving S. The secon d
\\'e will applv clos ure on S and add all the produ ctio n rul

®
TECHNICAL PUBLIC A T/ONS - an up-thru st for knowle dge
2 11 6
Compiler Design

compPt1L't1 I in IIH.'Sl' p rl,ullL"l1 t 1n 11, Il .,:-< ,.viii lw ·$ · Thh-· 1 11-1 hl•CO lll-ll' fur IS' , , • S, 1, 1th' . ['
J • •
1 ,
_J
,ltlCI ,l -
_ ,,
J"I
• : ,~ - (IJ )
• • 1 11' "- 1 l ,l
~ Fll<ST (r :f,)
, ,
Fll<ST ($) $. I lt•n c.:,· w,• w ,11 gt•I
\

S' _. . ~- $

~
5 -4 • bAI..', $
S _. • lk. $
~ -4 • bB.1 , $
:'\ow we h,Wt' got one rule IS➔
•Aa, $ ·1 which is m atching with lA --4 a • Xfl, a I
anJ I-' --4 y]. Hen ce w e w ill add the closu re o n A. Hence IA ➔ •d I will be added in
this list. Nov, the second component of A ➔ •d will be d ecided. As IS ➔ • Aa, i l i.,
mdtching " -ith IA --4 A • X B, a] having X as A, ~ as a and a as $.
Then FlRST (Ba) = FIRST (a$) = FlRST (a) = a. He nce rule [A ➔ •d, a] will be add ed .

s· ➔ • S, $
S ➔ • Aa, S
S ➔• bAc, Si
S ➔ •Be.$

~
A ➔ • d, a
Now notice the rule S ➔ • Be, $. It suggest to apply closure on B (As after dot B
comes immediately) This rule is ma~ching with [A ➔ a •X ~, a] and [X ➔ y]. Hence
we will add rule deriving B. Hence B ➔ • d will be added in the above list. Now
S ➔ •Be, $ can be mapped with [A ➔ a •X ~, a].
where a = E

X= B
~ = e
a = $
Hence FJRST (!1 a) = FIRST (e $) = FJRST (c) = c.
Hence ru le [B --4 • d, e J will be added.
Hence finally I O w ill be -
lo· S' --, . c;

S -j • Aa, <J,

S--, • bAe, 'l>

TECHNICAL PUBLICATIONS® - an up-thwst to, knowledge


c ompiler Design 2 - 11 7 Syntax Analysis

S ➔ •Be,$

S ➔ • bBa, $
A ➔ • d, a
B ➔ • d, C

Continue with applying goto on each symbol.

I I : goto (1 0 , S) There is no chance of applying closure or goto in this


S' ➔ S• , $ state. Hence it will have only one rule.

12 : goto (1 0 , A) Applied goto on A But as after dot a terminal symbol


S' ➔ A• a, $ comes we cannot apply any rule further. The second
component is carried as it is.

13 : goto (1 0 , b) After dot A comes hence rule for A ➔ •d will be


S ➔ b •Ac,$ added. As {S ➔ b • Ac, $] is matching with
[A ➔ a •X ~' a and X ➔ y} FIRST (13 a) = FIRST (cs)
S ➔ b•Ba, $
= FIRST (c) = c. The. second component of A ➔ -d is c.
A ➔ •d, C
B ➔ •d, a

l4 goto (1 0 , B) The goto on B is applied and second component IS

carried as it is.
S' ➔ B•c, $

goto (Io, d) The goto on d . in state 10 IS applied with


ls
correspondin g second components as it is.
A ➔ d•,a

B ➔ d•,C

Continuing in this fa shion we will get further states.

16 : goto (1 2 , a) l9 : goto (1 3 , d)
S ➔ Aa•, $ A ➔ d•, C
B ➔ d• ,a

I7 goto (I 3 , A) l1 0 : goto (1 4 , c)
s➔ bA. C, $ S ➔ Be • , $

®
TECHNICAL PUBLICA TIONS - an up-thrust for knowledge
lg goto (1 3, B) l 11 : go to (17 , c)
S ~ bB • «, $ S ~ bA c • , S

I 12 go to (l s, a)
S ~ bBa • , $
'\o "' u,m g th -.,. abo ve set of LR(1) i terns we w i II con str uct
LR( 1) par sin g tab le =·
fl1l
kw,-. :

Act ion Go to
Sta te
a b C d $ s A B
0 s3 s5 2 -l
AC CEPT
2 -,6

~
s9 7 s
-t s lO
:; r, r6
h
rl
-,
s 11
~ c., 12

'-) r6 r5
l l)
r3
11
r2
12
r-!

'v\ e can pa rse the str ing "bd a


" us ing a bov e con str uct ed LR
(1) p ars ing tab le ,1:3 :

Sta ck
Input buffer
Ac tio n
:,0
bd a$ Sh irt 3
'>r>h3
d a$ Sh ift q
',{J..., d9
a$
SOh~B~
a$
Sh ift 12
-., 1b1B 8a1 2
$
$OS]
$

TECHNICAL PUBLI CA TIO NS®


- an up-thrust for k11o 1\ led g'-'
compl'ler Design 2 - 119 Syntax Analysis

Now ,,ve will construct a set of LALR(l) items. In this constructi on we will simply
merge the states deriving sa me production rules w hich di ffe r in their second
components only. ln above set of LR(1) items state 15 and 19 are such states i.e.
goto (1 0 , d)
'~ 19 : goto (1 3 , d)
A ➔ d• , C

B ➔ d • ,a

We wi ll form only one state by merging state 5 and 9 as


159 : goto (1 0 , d)
A ➔ d •, a/ e
B ➔ d• , a/c

Hence the LALR(l) set of items are given as :


Io : S' ➔ • S, $ 16 : goto (1 2 , a)
S ➔ • Aa, $ S ➔ Aa•, $
S ➔ • bAc, $ 17 : goto (1 3, A)
S ➔ • Be,$ s➔ bA •e, $
A ➔ • d, a Is : goto (1 3, B)
.B ➔ • d, e S ➔ bB • a, $

I1 : goto (Io, S) 110 : goto (1 4 , e)


S' ➔ S• , $ S ➔ Be • , $

I2 goto (Io , A) In : goto (1 7., e)


S ➔ A • a, $ S ➔ bAc • , $

13 goto (Io , b) 112 goto (Is , a)

s➔ b•Ae, $ S ➔ bBa • , $

S ➔ b • Ba, $
A ➔ . d, e
B ➔ • d, a
14 goto (1 0 , B)
s➔ B • e, $
I 59 : goto (1 0 , d)
A ➔ d • ,a/e

B ➔ d•, a/e

®
TECHNICAL PUBLICATIONS an up-t/1111s/ fo, kno1,toch10
Comp1l01 Oes,g n
2 - 120

Syntax Ana/

1 lw I /\ I.I{ pM:--i ng t.\b le wil l be - ~

State
Actio n Coto
-,
a b C d $ s A B
()
ii'.\ s5 1 2 4

ACCEPT

2 sh

:1 s9 7 8

4 s ill

59 r'1 r5
r6 r6

6
rl

7 sl I

8 1-1 12

10 r:1

11
r2

12
~

Thl' pa n1in >,; tnb l,· 1-1huw 1-1 rnu llipk PnlriL·H in t\ctin n
[59, al nnd Actic1n [5(), ~'l. fhi~1'
ca lll'J rt'du <.:(•/rc ducc confl ict. lk•l ',Hll'I<' of thi1-1 r 11nflil..·t
w1.• cn nnnt p.H~1..' input .
T l ,uH il i~1 Hlluw 11 lhu t Fiivt•n >,;rnn imnr i1-1 1.1{(1) bu
t 1Hit- l.1\1.R (I)
E,wm plo 2.16.6 Co11ultf c1· I lit' Ji>llow l 11,'-! ,'-! rt111111111t'
I , .' i ' I, I<
'/.. s ' I<
,l I, > M / ~
1. I, ' /,t
!' , I< ' /,
I
/ )/ti/ /1 /1/1 f/11 • / .,J\, I •l'1 /I// l'II I 11,~ ,,,,. I {111, I Ill' , I, //I ,'l 1 ' ...
l'l/11//11111' , I .i~I ,1111 .
1'111/11 ///( ,,/ l ()//1 1 t h ,,, ... ·11 " ,,
n, /111/ l' l/1 I II /llll'til llM 111/,/,•,

II r , / IN /( ,Al I 'I II II /1 '/I IION~l110 , 111 ,,11 111111 st fo, ~ ) 11 /nd tJ' '
111
c ompiler Design 2 , ,, ,

Solution: To deri v t> lhl' gi ven grn nu nor w dng 1./\ 1,1{ 11 1pt'1 ud Wfi l1,1v1• 111 111,l ld 11 11•
canon ica l co lll'( l ion o f item s w ~in g LI{ ( I ) il PlllH.
The LR(l ) ill-m s arC:'
10 s· -) • S, $ If, golu (1 , = )
2
S 4 • L = R, $ S --> I, - • I<, ~;
S -) • R, $ I{ ➔ • 1,1 $
L -) • * R, = 1$ L ➔ • * R, $
L -) • id, =I $ L -) • id , $
R -) • L, $
II : goto (10 , S) 17 goto (1 4, R)
S' -) S • , $ L -)•l: R• , = I $
'2 : goto (10 , L) 18 : goto (1 4, L)
S -) L • = R, $ R -) L • = 1$
R -) L• ,$
l9 : goto (1 6 , R)
5 -) L = R• ,$
13 : goto (10, R)
S-) R •, $ 1, 0 : goto (16, L)
I4: goto (10 , *) R -) L• ,$
L -) * • R,
= I$ 1, I : goto (1 6 *)
R -) • L, = I $ L -) * • R, $
L-) • * R, = 1$ R -) • L, $
L -) • id, = I $ L -) • * R, $
L -) • id, $
l I2 : goto (l6, id)
L -) id • , $
Is : goto (10, id) 113 : goto (I 11 , R)
L -) id •, = I $ L -) * R• , $
From above set o f item s w e have got
i) 14 and 111 g ive sa me p rodu ctio n but lookheads n n.:' d iffl' rl·n l. I ll·111:1.· 11 11 ·q·,i· ti w in 111

form 14 11
ii) 15 = 112 He nce 1512
iii) 17 = I 1:1 H ence 17 1~
iv) 18 = 110 Hence 18 10

(1,1
TEC HNICAL PUBLICATIONS - .in 11p- tl 11u ~t for kn nwln<f1ir>
Synta/ Ar ,
1 1
2 - 122
Compiler Oe~1gn

·1hc reforl' the "<:t of item~ tor LALR are


" S c:. I . L ~ * R•, = I$
I o: .S ) e , , .i, 7 11 '

S ➔ • L = R, $
S ~ • R, $ I810 ·· R ~ L • ,=1$

L ➔ • * R, = I $
L ➔ • id, =I$
R ➔ • L,$
I1 : S' ➔ S •, $
12 : S ➔ L • = R, $
R ➔ L • ,$
13 : S ➔ R •, $
1411 : L ➔ *• R, = I $
R ➔ L • ,= 1$
L ➔ • * R, = 1$
L ➔ • Id, = I$
1512 L ➔ id •, = I $
16 : S ➔ L = • R, $ L ➔ • * R, $
R ➔ • L, $ L ➔ • id,$
The parsing table can be constructed as follows -

Action Goto
State
id * = $ s L R
0 s5 s4 1 2 3
- ~
--- ----
1 ace

2 s6 r5
3
r2
-- -
4 s5 s4
8 7
5 r4 r4
6 s12 s11
10 9
7 rJ r3

J
8 r5 r5

TECHNICAL PUBLICA TIONs® _ an u


.
.
P-lhrust for knowledge -----
eomp1ter Design
2 - 123 Syn/ox Analysis
-- - -
9

10
- rl

r5
11 s12 s11 JO 13
12
r4
- - -
13
r3

The LALR parsing table is

Action Goto
State
id • = $ s L R
-
0 s512 s411 3
- l
--
2
=

-- 2 -- -
ace

S6 rS
3 r2
411 s512 s411 810 713

512 r4 r4
6 s512 s411 810 9
....
713 r3 r3
.
810 rS r5

9 r1

Example 2.15.6 Design· an LALR parser for the following grammar and parse the input
id= id
S ➔ L=R I R

L ➔ * R l id
R ➔ L AU : Dec.-13, Marks 16

Solution : Refer example 2.15.5.

Parsing input id = id
---------- -----------,
Stack Input buffer Action

$0 id = id$ shift 512

$ Oid512 = id$ red uce by L ➔ id


---
SOL2 = id $ shift 6

®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
_ ', ;r,/,1/ / . , ,., . ,
/ 12 4 1

'-, I ii . "
,. I I dI I :,,

., P l )
'"" ,, r• d II I
,, . . , I •
I

~ Ill .' ,, 1 "\ 111 n d 1J1, r' /


,, ,
'

"., (JI 2 t, f ( / r, d 1H , . ti :, , , I.
(
r'

'l, ()", I ,)< r

Example 2.15. 7 W n tt• llw nlgnn th rn f or ron.,/ructwn of 1./\ IY p1w ,tn7, tab/I, fr/r o 1/_n1-n

s, ran111zar. LJ,.., ,11g //,,· algorith111 f or ron<,lrurlwn of / ./\ I .I<. pa r<. mY, tahlf, rt/n 4-lrur I Jhv I.I-./.?
par~ing table f or the f ollowinx :zrammar
S' ~ S
S ~ aAd I bBd I aBe I b/\f
A ~ C
B~ c AU : Mav•l9, ~ U
Solution : A lgori th m for cons tructin g LA LR ta b le - Rdt=-r ¼:c.ti,,n 2.1 ::;,

Step 1 : Con sider the gi ven g ramma r, in whi ch p rod ucti<>n ruk~ ;;, n; nu ml-w•n.:d
(1) S' ➔ S

(2) S ~ aAd

(3) S ➔ bBd
(4) S ➔ aBe

(5) S ➔ bAe

(6) A ➔ c

(7) B ➔ c

Step 2 : Now we w ill cons tru ct can o ni ca l <;ef o f 1, R( I) iu-m,:

10 S' ➔ • S, $

S - > • a Ad, CS

S ~ • bBd, 'S

S - > • a Be, $
S ➔ • bA e, 'l>

fI got o (I c, , ~)

s· > c., •, i

~
rr (, lff}/( ,/1.l Pl/W J(, /1.. /l()IJ' ; nr, llf, ,,,,,,,., ,,,, ,,,,, ,,11/q1/1v ,
-- compiler Design SyntaY Ana/ySIS
2 - 125

12 goto (1 0 , a)

S ➔ a • Ad ,$

S ➔ a • Be,$

A ➔ • c, d

B ➔ • c, e
1--_, goto (1 0 , b)

S ➔ b • Bd, $

S ➔ b • Ae, $

B ➔ • c, d
B ➔ • c, e
I 4. goto (1 2 , A)

S ➔ aA • d,. $

ls goto (1 2 , B)

S ➔ aB • e, $

16 goto (1 2 , C)

A ➔ C • d
B--, c • e

I7 goto (1 3 , B)
S--, bB • d , $

Is goto (1 3 , A)
S --, bA • e, $

19 goto (13 , c)
A --, c • e
B --, C . d

1 10 goto (I 4 , d )
S --, a Ad • $

I1I goto( l 5 ,e)


S --, a Be •, ~

(I,)
TECHNICAL PUBLICA 7 IONS · An up f/1r11 st fc11 /..nowlt'ct.1,'
p_,1e
1 2_6_ _ _ _ _ _ _ _ _ _ _sYntax 4
_e_s....:ig:...n_ _ _ _ _ _ _ _ _ _ _ _ _2_ - _
_r_D

c _o_m..:.._
_
~
l12 goto (l 7 , B)
S ➔ bBd •, $

113 goto (l 8 ,e)


S ➔ bAe •, $

Now , we can cons ider the state s 16 and l9

:A ➔ c•,e
16:A 4C • ,d 1
9
B ➔ c•,e B ➔ c•, d

diffe rence is in look a head symbols,


As in both the state s prod uctio n rules are same but
we merg e them in one state I 69 . Henc e
· I 69 = A ➔ c •, d / e

B ➔ c •, d/e

The LALR pars ing table will be

Action Goto

a b C
d. e $ s A B

0 S2 S3

1 Accept

S69 4 ·s
2
8 7
3 S69

4 S10

5 S11

69 r6/r7 r6/r7
- ---
7
12
----
8 s13

10 r2

11 r4

12 r3

13 r5

the g iven gram m ar is not LA LR ( I)


As th ere ex ists redu ce -redu ce conflict in Mf69,dj,

® knowledge
TECHNICAL PUBL/CA TIONS - an up-thrust for
c ompiler Design 2- 127 Syntax Analysis

Example 2.15.8 Show that the following grammar. S ➔ Aa bAc I de I bda , A ➔ a is LALR
(1) but not SLR (l). AU : Det· .- J8. Marks 13

Solution :

Step 1 : We wiJl first find if the given grammar is SLR (1) or not. Firstly, we will write
the grammar using dot operator.

Step 2 :
Io S' ➔ • S
S ➔ •Aa
S ➔ •bAc
S ➔ •de ·
S ➔ • bda
A ➔ •a
Now we will apply goto on each symbol from 10 , each goto will generate new
subsequent states.
11 : goto(l 0 ,S)

12 goto(l 0 ,A)
S➔A •a
I3 : goto (Io, b)
S ➔ b•Ac

S ➔ b•da

A ➔ •a

I4 goto (I 0 ,d)

I5 goto(I 0 ,a)

A ➔ a•

S ➔ Aa•

17 goto (I 3 , A)

S ➔ bA • C

®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
Cw11p1lfJI U m11u11
i1 , ;,t)

S > h/ \ c •

1 11 • go l o( l ~, ;1)

S > hd ;1 •

St ep 3 ·1hl' pc1n,ing 1,, bk fo


r pc1r:-iing th e nb ov c ~r
am m nr iH
A cti on Coto
a b C d $ s A
() sr.:
• 1 S'.'\ S4 J 2
Acce pt
2 S6
~ S'i / rl SH 7
4 SY
c; r'i r'i rl
6

7 SHJ
8 sn
9
r3
10
r2
JI
r4
St ep 4 No w w e wi ll co ns tru
ct ca no ni ca l se t of ite ,...,.,..
ca no ni ca l se t of ite m s m s wi th lo ok a he ad
is i.e. LR (l •· ·
lo S' ~ • S, $
S ~ • Aa , $
S ~ • bA c, $
S ~ • d e, $
S ~ • bd a, $
A ~ • a, a

TE CH NIC AL Pl JAi 1r1


c ompder Design ? 1;,9 f ,ynfM' /\m ,/y,,ln

,, ~l)ll) (I() ' S)


S' \ s •, $
12 goto (I O , /\)

S --) /\ • n , $
1.1 goto(l 0 , b)
S ➔ b • Ac,$
S ➔ b • da, $
A➔ • a, c
14 goto(lo,d)
S ➔ d • c, $

ls goto (1 0 , a)
A ➔ a • ,a

16 goto (1 2 , a)
S➔ Aa •, $
17 goto (1 3, A)
S ➔ bA • c,$

13 goto (13,d)
S➔ bd • a,$
l9 goto (l4, c)
S➔ de• ,$
I 10 goto (1 7 , c)
S➔ bAc • , $
l11 goto (13, a)
s~ bda • , $
The pars ing table is
--
a b
Action
C d $ s
Goto
A
l
0 S5 Sl '-w '
:\ 1 n •l'l

2 ~h

1 '-,5 ",>~
4 '°l'° l

L 5 r5

(!v
TECHNICAL PUBLICATIONS · o n 11p- thw s l fn r l..11owlo<i<10
/ I 'I(}
Comp iler Design

I I
6
~ 1(1
7

-8 S il
9
r:l
10
r2
J1 r4

tab le, l hiH I.A U< (I) )!/!I fl II 11 :11·.


As there is no confl ict prt.!SL'nl in thi s
AIJ : f>v, .. W, Mn,'4, i
f j1:j Comparison of LR Parsers
Llw c o1nr11 011 fndu n ; ,,,,, 11 .,,: ,,11, ,,
It's a time to comp are SLR, LALR a nd LR porsc r for
class of CFG, effici ency and cost in terms of ti 111l' ,111J
1-:1 pnn ·.

LALR parse r Cano n ical LI< par,icr


Sr. SLR parser
No.
The LI\ LR a nd SLR h:w c U< pa n11•r ,,r rL111ord(<i / IJI
l. SLR p a rser is s malles t in rn •rMl'f ii+ i£1rg(•l-lf In /) li'r
size. the sa me s ize.

This metho d is a pplicn blc lo Thi,; rnetho J i1-1 rr11 ~11


2. It is an easies t m ethod
wid e r class tha n SLR. powe rful th<111 ',1,1< a11d
based on FOLL OW function.
LALi< .

Mos t of the s yntac tic Thi., m d hod ,-xp,,..,<•11 /,.,,-,


3. This metho d expos es less
fea tures o f a la ng uage a rc 1,ynla ctic fo,Hun.:'l th;m tlt;,t
syntac tic featur es than that
expre ssed in LALR. n( I.R p.i r ,;(; r '>.
of LR pa~s~r s_. ___ _ _
Error detect ion is n o t I mmcd ia le e rror d dccl11111 1,
4. Error detect ion is not
imme diate in SLR. imme diate in LALR. done by LI< parse r.

5. It requir es less time and The time and s pace The lime .:i nd 11pace
space comp lexity . · compl exity is more in LALR comp lexity iH mon.: for
but efficie nt m e thod s exist ca n onica l LR par"lf.: r.
for constr ucting LALR

~-- --- --- --- --- --'' --- --- =-- --- --- --- --- --- --_ ,
parse rs directly .

y is as given be lo w
Grap hjcal repre senta tion for the class of LR famil
LR (1)

LALR (1)
SLR

8
Fig. 2.1 6.1 Class ificati on of gram mars

dge
TECHN ICAL PUBLI CA T/ONs® - an up-thru st for knowle
2
pi/er oesign:___ _ _ _ _ __ _ _ _ _ _ __-_ 13_1_ _ _ _ _ _ _ _ _ _ _ __:::S'.!_y..:_: ta:..=x~A
n.:..:: :.:._a=.:.l~
_:_n is
y s.=_

oijfrrentiote SLR parser fro m LALR parser.


1. - - AU : Dec .-10, Marks 2

~ Handling Ambiguous _
G rammar AU : May-12, 18. Marks 15
~ we have _seen v ario u s pars_ing methods in which if at all the_ gran:una r is
. ous then 1t creates the conflicts and we cannot parse the input stn ng w ith s u ch
,a1J1b~guous gram.mar. But for some languages in which arithmetic expressions are given
111
a b'.gu ous gram.mar are most compact and provide more natural specification as
an1b1gu . 1 b" . .
. ared to eqmv a ent unam 1guous grammar. Secondly using ambiguous gr ammar we
C()OlP d . ti" f . 1
can add an y new pro uc ons or spec1a
constructs.

10 : E'➔ • E 15 : goto (11 , *)


E➔ • E+E
E➔ • E*E E➔ E* • E
E➔ • E+ E
E➔ • (E)
E➔ •id
E--:-+ • 'E*E
E➔ • (E)
E➔ •id
11 : goto (10• E)
E'➔ E•
E➔ E •+ E 16 : goto (12, E)
E➔ E •* E E➔ (E ·•)
E➔ E•+ E
12 : goto (10, () ·
E➔ E •* E
E'➔ (• E)
E➔• E+E 17 : goto (14 , E)
E➔ • E*E
E➔ E + E•
E➔ • (E) E➔ E•+ E
E➔ •id
E➔ E • ·* E

13 : goto (10 , id)


18 : goto (1 5, E)
E➔ •id
E➔ E* E •
E➔ E •+ E
.14 : goto (1 1 , +) E➔ E • *E
E➔ E+ • E
E➔ • E + E
E➔ • E*E 19 : goto (1 6 ,))
E➔ • (E) E➔ (E) •
E ➔ •id

using ambiguous
•v n IJ, e
\An....· . grammar fo r parsing the input string we wi ll use a ll the
disambiguating rules so that each time o nly one p a rse tree w ill be gen erated for t~at
specific ·
. input. . u o u s gra mm ar can b e u sed in contro lled , manner
Th u s am b 1g . for pars ing
. tnput • W e w ill con _s1'd e r som e a mbig uo u s g ra mma r a n d le t u~ ti -v to pa rse son'\e
the
tnput String.

TECHNICA L PUBLICA TION S® - an up-thn.ist for knowledge


;i 132 S yntax Ana1
Ct l/1 '/ ,1t,1r I)tJ:,lf /II Y~,1

I I It to rosolvo parsing action conflic ts


Ut lng procodonco nnd n11oc ot v y , .
,.
, ()ll '- 11
l
I
· r· ,11

1111lrn1111 H I'• ►: 1.rnHn.t r 1,11· ,Hithnw t.rt i' Xj)l'L'Hr-1 11 )11.
• ,,

> rd
N,,\,· w,· will 1, 1ri ld tlw ~l'I nl I .R(O) it em H for thi s gra mmar.

I kn,· FULi OW(l i) It , "', ), $I.


\Vl• h.n·,· l'nmpul l'l I 1:()1 , I,()W (E) as• it wi ll be require d whil e process ing.
N,H,. u si n M tlw :--.um· rull's nf builJin g SLR(O) parsing table we will genera
te the
p.1r:-111~ t.1hk h)I· ,1hnv l' s,·t o f ilc•ms.

f State Action Goto

id + $ E

l) !-<2 1

sS Accept
., s2 6
-
} r4 r4 r4 r4

-I ... ..,_, s2 7

5 :-3 s2 8

6 s4 sS s9

7 ~-1- or rl sS or rl r1 rl

8 s4 or r2 sS or i-2 r2 r2

9 r3 r3 r3
.__

As we ca n see m th e pa rsing table the s hift/ reduce conflicts occur at s tate


7 and 8·
We wi ll try to resolve it, how? Let us consid e r one string "id + id * id" .
r-

Stack Input Action w ith conflict resolut ion


so id + id * id$ Shift
$Ord~ + id id$
.i- Reduce by E ➔ id
~E l + id • id$ Shift
$0El +4 iJ ~ id$ Shi ft

TECHNICA L PUBLICA TIONS® - an up-thrust for knowledge


compiler Design ? - 13'3 Syntnx Anelys,s

$0 El +4jd 3 Rcd 11cc by l?r- , id


'f,Of-1 +4I 7
I SO 1:!l +41:.7• 5 id$
The conflict
Shift
(.,.in lw rc1ml vN.I by .,t, i(I 5

$0 CJ ➔ 4E7• 5 id 3

I
$ Rcd11cc by E ---1 id
SO £1 +4E7•5E8 $ Rccl ucc by E -> E • 1,:

I $0El +4-E7 $ Red ucc by E -+ E ~ E


I $0[1 $ Acccp l
L
As ,. has preced e nce over + W".... I1ave to perform multipli ca tion operation · f"1rs t . A n d
fo r tha t it is necessa ry to pus h * o n the top of the s tack. The stack position will be

By this we ca n perform E * E firs t and the n E+E. The refore as you can see in the
above table of parsing conflict can be resolved by i:ISsigning s hift operation. Hence action
(7, *] = sS.
Similarly if we consider the input "id * id + id$" for process ing .

Stack Input Action with conflict resolution

$0 id *.id *+id$ Shift

$0id3 *id *id $ Re duce by E ➔ id

$0E1 +id *id$ Shift


-
SOE1• 5 id+id$ Shift

$0 E1• 5id3 +id$ Reduce by E ➔ id

$0E1*5E8 +id$ The conflict can be resolved by reduce b y E ➔ E * E ~

$0 El +id$ Shift
--- -
$0 E l +..i id$ Reduce by E ➔ id

$0 El +4id3 $ Red uce b ):_ E ➔ E ,. E

$0E1+-!E7 $ Reduce ~ _E ➔ E + E

$0El $ Acc;e t

The conflict in action[8, +] = s4 or r2 can be resolved by allowing r2 action as we


have to perform E * E op eration firs t and then "id * id * id$" strings you can see that
the conflicts for action[7, +] and action[8, *] can be resolved respectively. The ·resolution
is action[7, +] = rl and action[8, *] = r2.
Finall y the ambiguous gramma~ without any conflict has following SLR parse table as -

®
TECHNICAL PUBLICA T/ONS - an up-thrust for knowledge
~
Com pile r Oo!.rgn
- -- 2 - 134
~
Syn tax An
IYS/3
Stale Ac tio n ----
Go to
id + ... ( ) $ E
(\ !1-_1 ~2 1
i--t st;
Accept
., ~ s2
-' r4 r4 \
- - -s2 - - r4 r4
4
5
:-.:1
~3 -- s2
h s4 s5 s9
7 rl s5 r1 rl
8 r2 r2
-- --
r2 r2
9 r3 r3 r3

Review Questions

1. Describe the conflicts that may


occur during shift reduce parsing. AU : May-12, Marks 6
? Consider the follow
ing grammar
E~ E+ E
E ~ E,. E
E ~ (E)
E ~ id
i) Find the SLR parsing table for
the given grammar.
ii) Parse the senten ce : (a + b)*c
AU : May-18. Marks 15

f j1 :j Error Handling and Recovery in Syntax An


alyzer
AU : May-06, 07, 11, Marks
8
Pa rse r em plo ys var iou s str
ate gie s to rec ov er fro m syn
{1 i) Pa nic mo de ii) Ph ras e lev
el iii) Er ror pro du cti on s
tac tic err ors . Th ese stra teg ies
are
iv) Gl ob al cor rec tio n.
No t a sin gle str ate gy is un
ive rsa lly acc ept abl e bu t can
Th ese str ate gies are giv en in be ap plied to a bro ad dom
det ail as bel ow - ain.
i) Panic mode
• Th is str ate gy is used by mo
st pa rsi ng me tho ds.
• Th is is sim ple to im ple me nt.

• 1~ thi s n_1e tho d on ~iscove_r


ing err or, the pa rse r dis car
tim e. Th is proces s 1s con tin ds inp ut sym bo l one _a~·'.
ue d un til on e of a de sig
tok ens is fou nd . Sy nch ron na ted set of syn chront l ~
iz ing tok e ns a re d e lim ite
Th ese tok ens indi.ca te an en rs s uch as sem ico lon or efl d
d of inp ut ·
s tat em en t .
• Th us in pan ic mo de rec ov
e ry a con s ide rab le am ou nt
ch eck ing it for ad d itio nal e of inp ut is s kip ped wi th001
rro rs .

TECHNICAL PUBLI CA TIO Ns®


- an uo- thn ict fnr 1m 1
2 - 135 Syntax AnalySJs

• rl hii, m<--thod ~ua rantee1, no t 10 go in


·
in f inite loop.
• Ifth,•n • a rc Ii:-.,:, numbe r of e rrors in the same statemen t then this strategy is a
be1>t LhOJ(.(• ,
ii) Phrue Jevel recov-ery

!" t h1 "
• input. m ..thod, on discoverin g e rror parser perform local correctio n on remainin g

• It can replace a prefix of remainin g input by some string. This actually heJps the
parb<?r to continu e its job .

• The local co rrection can be replacin g comma by semicolo n, deletion of extra


'>'!micoloru, o r inse rting m issing semicolo n; The type of local correctio n is decided
by co mpiler d esigner. .

• While doing the replacem ent a care should be taken for not going in an infinite
loop.

• "D1is method is used in many error-rep airing compiler s.


• The drawba ck of this method is· it finds difficult to handle the situation s where the
actual e rror has occured before the point of detection .
iiH Error product ion
• lf we have a knowled ge of common errors that can be encount ered then we can
incorpo rate these e rrors by augmen ting the gramma r of the correspo nding
111nguage with error product ions that generate the erroneou s construc ts.
• lf e rro r product ion is used then during parsing, we can generate appropr iate error
message and pars ing can be continue d.
• This method is extreme ly difficult to maintain . Because if we change the gramma r
then it becom es necessa ry to change the correspo nding error product ions.
iv) Global product ion
• We o ften want such a compile r that makes very few changes in processi ng an
incorrec t input s tring.
• We expect less number of insertion s, deletion s and changes of tokens to recover
from erroneo us input.
• Such methods increase time and space requirem ents at parsing time.
• Globa l product ion is thus simply a theoretic al concept.

Review Question

1. [ xplain in detail about the error recovery strategies in parsing.


AU : May-06, Marks 8 : May-07. 11 , Marks 6

®
TECHNIC AL PUBUCA TIONS - an up-thrust for knowledge
Compile r Design 2 - 136
- -
~
- Syntax Ana,
- Ys,,
fj(: j YACC
• YACC i~ .1ull1m ,1til' tool fo r gL'nc ra ting the parser progra m .
• YACC s ta nd ~ for Yet Anoth e r Co mpile r Co m pile r which is b asicall
y the ur i·
a\ .1 il.1bk f wm UN IX. I 1ty

• Bdsil.:c1 II~· YACC is LJ\ L R parse r gen erator .


• nw YACC can report conflic ts o r a mbig uities (if at all) in the form
of
mc~~agcs. error

• LEX and YACC work togeth er to analys e the progr am syntac tically .
• The typ ical YACC transl ato r can be repres ented as shown in Fig. 2.19.1.

• First we w rite a YACC specif ication file; let us name it as x:y. This
file is given to
the YACC compi ler b y UNIX comm and
yacc x.y
• Then it w ill gen e rate a p arser progr am using your YACC specif
ication file. This
parser p rogra m has a standa rd name as y.tab.c . This is basica lly parser
program in
C gen erated a utoma tically . You can also give the comm and with
- d option
yacc -d x.y ·
• By - d option two files will get gener ated one is y.tab. c and other
is y.tab.h. The
heade r fi le y .tab.h will store all the tokens and so you need not
have to create
y .tab .h ex p licitl y.

Specification
file y.tab.c. and.
YACC compile r with y.tab .h
x.y - d option

y.lab.c
a.out
CC - The C compil er

Input string
Execut able progra m Output
a.out

-- --------=-=-~ ~~ ~~ ~- -- -- Fig. 2.19.1 YACC Parser gener ator mode l


- Syntax Analysis
compiler Design 2 - 137

• The generated y.tab.c program wi ll th en be compiled by c compiler and generates


the executable a.out fi le.

• Then you can test your YACC program wi th the help of some va lid and in va lid
strings.

• Writing YACC specification program is the most logical activity. This specification
file contains. the context free grammar and using the production rules of context
free grammar the parsing of the input string can be done by y.tab.c.
Let us learn how to write YACC program.

fjijj■ YACC Specification


First of all we will see the Declaration section
(Ordinary C declarations)
structure of YACC
specification.
Translation rule
The YACC specification file
(Context free Grammar)
consists of three parts
declaration section, translation
rule section and supporting C Supporting C functions

functions. Fig. 2.19.2 Parts of YACC specification

The specification file with these sections can be written as

%{
/* declaration section */
%}
%%
/* Translation rule section */
%%
/* Required C functions*/
- ---------
1. Declaration part : In this section ordinary C declarations can be put. Not only this
we can also declare grammar tokens in this section. The declaration of tokens
should be w ithin %1and %).
2. The translation rule section : It consists of all the production rules of context free
gra mmar wi th corresponding actions . For instance
rule 1 action 1
rule 2 action 2

rul e n action n
If there are more than one alte rnatives to a sing le rule then those a lte rna ti ves sho uld
be c;eparated by I character. The actions a re typ ica l C statements. ff CFC is
®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
Compiler
_ ___ Design 138_ _ _ _ _ _ _ _ _ _ _sYntal( A
2 -_
_ _ _ _ _ _ __ _ _ _ _ _ _
n111Ya,,
LHS ~ alternati ve 1 I a lte rnative 2 I . . ... I a lternative n
Then
LHS : alternative 1 I actio n 11
I alte rnative 2 I actio n 21

alternative n {action nl

3. C functions section : This section consists of one main function in which th


routine yyparse() will be called . And it also consists of required C functions. e
....,.G..·►•~• Design of Syntax Analyzer for a Sample Language
ln this section we will discuss the YACC program for supporting calculator operation
S.
Programing example : Write a YACC program for implementin g d esktop calculator.
Program: We will first create LEX program named calci.l then we will write program
for YACC as calci.y. The extension to LEX program is .l and to YACC
program is .y
LEX program
/*Program name :calci.l* /
%{
#include "y.tab.h" /*defines the tokens */
#include <math.h>
%}
%%
/*To recognize a valid number*/ .
([0-9) + I([0-9) *\.(0-9) + )([eE][-+ )?[0-9) + )?) {yylval.dval = atof(yytext);
return NUMBER;}
/*For log no I LOG no (log base 10)*/
log I
LOG {return LOG;}

/*For 1n no (Natural log)*/


1n {return nLOG;}

/*For sin angle*/


sin I
SIN {return SINE ;}

/*For cos angle*/


cos I
COS {return COS;}

/*For tan angle*/


tan I
TAN {return TAN;}

®
TECHNICAL PUBLICA TIONS - an up-thrust for knowledge
cr.,mp1tor 0 11 '•''1" ;, 1y , ~/ nl a/ Anal/SI';

rFor mi,mor y•/


rocm { return ME M:}

( \ti f" ,,,nr.,rt; white fff18-Cf;R • /

/"End o f input•/
\S {rm.um O,}

/"Catch the remam mg and rnturn a amgle charact er


tok~n to the paroor •/
\n l. return yytext/0 ];
%%
The YACC program
rPtogra m Name :calc1.y */
%{
double memva r;

t•To define possibl e symbol types•/


%union
{
double dval;
}

/•Token s used which are returne d by lexer* /


%token <dval> NUMBER
%token .,..dval> MEM
%token LOG SINE nLOG COS TAN

/"Defin ing the precede nce and associat ivity*/


%left '. • •+ • /*Lowe st precede nce*/
%left .•. '/'
%Tight '".
%left LOG SfNE nLOG COS TAN /*Highest precede nce•/

/*No associa tivity*/


%nonassoc UMINUS /* Unary Minus •/

rsets the type for non-termmal •/


~ < dval > exprese lon

/*Start Btate • /
start: stateme nt '\n'
I start stateme nt '\n'

(i,J
TECHNICAL PUBLICATIONS - an up-thrust fo r knowlodge
:omo fer Design 2 - 140

·For sto1mq the am,W<'t(nwmory)*/


statement MEM f'xprf'% 1on { mornvar $3,}
e XJ"'t<'!>Ston {pnntf( " l\nswer %g\n",$1):}
•For pnnlmg the answ er*/

· F\..1r bmary anthmellc operators*/


er.:!:es£,cn expression·+· expression{$$ = $1 + $3;}
expression -· e xpression {$$ = $1 - $3;}
e>xpression * ' expression {$$ = $1 * $3;}
expression ' · expression
{ *To handle divide by zero case*/
it(S3 = = 0)
yyerror( "dlVlde by zero") :
else
$$ = $1 / $3;
1
express ion ·"' expression {$$ = pow($1,$3);}

•For unary operators*/


exp:-ess10n - expression %prec UMINUS {$$ = -$2;}
• ~prec UMINUS signifies that unary minus should have
the highest precedence*/
1 expression ')' {$$ = $2:}
LOG e xpression {$$ = log($2}/log(10) ;}
nLOG expression {$$ = log($2);}
•Tngnometnc functions*/
SINE expression {$$ = sin($2 * 3.141592654 / 180):}
COS e xpression {$$ = cos($2 * 3.141592654 / 180);}
TAN expression {$$ = tan($2 * 3.141592654 / 180);}
NU1v1BER {$$ = $1 :}
MEM {$$ = memvar;}
; ♦ Retnevmg the memory contents* I

'1f
mau {)

pr1..ctf( Erit.r.:r the exprnss10n "},


'/YIJar ,_, J

u.t ,'"'/P.rror(char , rr,Jr)


1

.1. ~i u ,error),
,tpru.tfLttl c u ,
I

II Cl/fl/( Al /'II/ii I< /\ll<>N', ell/ 11/1 //Ill/.',( flll /..//LH~lt•./,Jt'


A I
compiler Design 2 _ 141 na ys,s
- - - - - - - -- - - - - -- - - ~::_~~_ _ _ _ _ _ _ _ _ __:S yn/ax

compiling and running of LEX and y ACC programs


The output of the 11nwr.1m
~ .-::i
· g co mm a n d s
ca n hl' ()61,u. , ,·,,cd by fo ti owin
(root@localh ost) # lex calci.1
(root@localh ost)# yacc -d calci.y Note that if we use - d option then
t
tab.h gets created automatkall y a nd w e
(root@localh ost)# cc y.tab.c lex.yy.c -ll -ly -lm
(root@localh ost)# ./a.out
___
need not have to create explicitly ___,
Enter the expression : 2 + 2
Answer = 4
How to run lex and Yacc Programs together?
2*2 + 5 / 4 lex calci. 1 ~ will create lex.yy.c
Answer = 5.25 yacc - d calci.y ~ will creat y.tab.c
cc y.tab.c lex.yy.c - ll - ly - 1m ~ will compile both
mem = cos 45 lex.yy.c and y.tab.c
by linking various
sin 45 / mem
library file
Answer= 1
In 10 ./a .out~ will run executable file of program
Answer = 2.30259
Explanation of program

Declaration part -
%token is used to declare tokens in the YACC. In the above program token NUMBER
can be declared as
%token <dva1> NUMBER
The precedence and associativity can be declared in the above program %left, %right
and %nonassoc declares the associativity of the operator being left associative, right
associative or nonassociat ivity respectively.
The precedence can be declared in an increasing order.
The yylval used is the union of type double. YACC can associate data type with the
token by using this yylval.
%token <dval> NUMBER
It means token NUMBER has the data type double. Any number of data types can be
decla red in union.
%left '-' '+' /*Lowest precedence and left associative*/
%left '*' '/ '
%right '/\' /*right associate*/
%left LOC SINE nLOC COS TAN /* Hi ghest precedence* /

!*No associativity~/
%nonassoc UMINUS /* Una ry Min~•s* /
®
TECHNICAL PUBLICATION S - an up-thnist for J.. nowledgo
Com piler Des ign 2 - 142

The ope rato rs l)ll thL' .


I1111..' 1l,\ S I , re •1..· Jl'n n · Por inst a nct·
:4,l 111L' l'Lj Uil f l . '1 ' a nd , ,
sam e pre ced e nce. ht1,, ti,

The shif t / red uce con flict con be reso


lved by Y/\C C wil h th l ' he lp 0 _f lh<•s,,
rule s. If the inp ut toke n ' id ' ho~ pr,., ,.d,
m urc prc cecl enc~ th cn th c s h'.~t , 7
pe rf0r me·d . If the prec ed ence of pro duc a( lion ..,1111 ::
tion rule /\ - H1. 18 gre a te r _th " n id lh,:
A~ a. will be pl.'r form ed . If there is n r1 •0 , 1,,. t
sam e pre ced enc e the n Y AC ~ che cks
For ld t asstXia tivi ty shif t acti on will fo r a"''i<>c 1,1 11., .'
be pe rfor med and for ri g ht associa 1

action will be per form ed . ti vit y rr-d ,:,

Rule section
In the ~le sect ion : (colon) is used to
sep arate LHS and RH S of pro duction
tem1ina tion of eac h rule is giv en by ' rul(: _·rr,.
; '. The $$ is use d as attr ibu te valu
gra mm ar. e at LHS c,f tr ,

I lf there is a rule E+E then it has attr


for $2 and E = $3. To rep rese nt the RH
ibu te valu es as $1+$3. Sin ce E = $1, +
S of the gra mm ar $1, $2, ... , $n sym
is re<;en.•(;c
Finally the ans wer can be in $$ = $1. bols are uc,.:c

Sub rou tine section


In the mai n(), the imp orta nt fun ctio n
yyparse() is call ed. YA CC actu ally invo
yyp ars e() whi ch intu m calls yyl ex whe kes fir;t
n it req uire s toke ns.
The rou tine yyerror is use d to prin t
the erro r mes sag e wh en an erro r is
p arsi ng of inp ut. occurred m

Thus we hav e see n how an inp ut stri


ng is par sed and syn tact ical ly che cke
d.
f ►.Z•I Two Marks Questions with Answers
-- ----
Q.1 Explain various issues in parsing.
Ans The first imp orta nt issu e in par sing
is spe cifi cati on of syn tax in pro gram
lang uage. It sho uld be prec ise and ming
una mb igu ous . The sec ond imp ortant
par sing is rep rese ntat ion of the inp ut issue in
afte r parsing . The thir d cru cial issu e
parsing algo rith m. The parsing algo rith is use or
m can be usin g eith er the top d ow n app
usin g the bot tom up app roa ch. roach or

Q.2 Why lexical and syntax analyser are


Ans : The lexical ana lyze r scan
separated out ? l·Y•H•f#d
s the inp ut pro gra m and coll ects the
the othe r han toke ns from it. ()n
d parser bui lds a parser tree usin g the se
to ken s. The se are two import,,nt
acti viti~s and thes e activities are ind epe nde ntly carr
ied out by thes e two pha, t''
Sep arat ~ g ~ut thes e two pha ses has two adv ant age s
- Firs tly it accelerates the pnx:l·:-
of compilation and ~co ndl y the errors in the sou rce
inp ut can be ide ntif ied precise!~·

TEC HNIC AL PUBLICA TION s ® - an


up-th rust for knowledg e
c ornp1/er Design 2 - 143 Syntax AnalySIS

0 _3 What Is ambiguous grammar ? AU : M.ay-12


OR Define Ambiguous grammar. AU · M.ay-16
. mo re tha n o ne pa rse trees can
. which
•ns ·• TI,e a mbig uous gramm car is a g ramma r m
,.

be generated fo r th e same input. For example - The ~tring id +id *id ca n be represented
ilS •

E E
~ ~
E + E E E
I
id
~ . ~ I
E E E + E id
I I I I
id id id id

(a) Parse tree 1 (b) Parse tree 2


Fig. 2.20.1
Q.4 What is recursive descent parsing ?

Ans : Recursive descent parsing is a kind of parsing in which the reetusive procedures
can be used for parsing the given input string. It is a top down pa rsing, technique .
Q.S What is the function of parser ?

Ans : There are two important functions that can be performed b y the parser and
those are - parsing of input string and generating error messages if there exists errors
in the input.
Q.6 What is handle ?

Ans : "Handle of right sentential form y is a production A->{3 and a position of y


where the string (3 may be found and replaced by A to p roduce the pre,·iou s right
sentential form in rightmost derivation of Y ".
Q. 7 What is handle pruning ? AU : May-11, 18, 19, Dec.-11, 16

Ans : In bottom up parsing the process o f d etecting handle a nd u ~mg them m


reduction is called handle pruning. Fo r example -
Consider the grammar,
E ➔ E+E
E ➔ id
Now cons ide r the string id + id + id a nd the rig htmost d e rivatio n i~
E⇒ E+ E
rm
E ⇒ E+E+E
rm
E ⇒ E + E+ id
nn
E -, E + id + id
rm
E ⇒ id + id + id
rm

®
TECHNICAL PUBLICATIONS • on up-thrnst for A.no',\ /~-,
Comp1lo1 Design ? 114

IH~hl Sl'nlcntlal form Handl e Production


id ... Id • ,J id , id
· Id + ,d icl E , id
+ I· + id iJ E dd
E+E+ E E + I: C ► E+E
E+E E+ E I:. ➔ E +- E
E

Q.8 What are the two important rules used in shift reduce parsing ?
Ans: Fo llowin g are the two rules that can be used in shift reduce parsing -
• If the incom ing operat or has more priorit y than in stack operato
r trier
perfor m s hift.
• 1f in stack operat or has same or less priorit y than the priorit y of incomin
~
operat or then perfor m reduce .
Q.9 What are the disadvantages of operator precedence parsin g?

AU : May-06, Dec.-06
Ans : Follow ing are the disadv antage s of operat or preced ence parsing -
1. The operat or like minus has two differe nt preced ence
(unary and binary)
Hence it is hard to handle tokens like minus sign.
2. This kind of parsin g is applica ble to only small class of gramm ars.
Q.10 Explain the kernel and non kernel items.
Ans : Kerne l items - It is the collect ion of items S'-> • S and
all the items whose dots
a re at the left end of the R.H.S of the rule.
Non Kerne l items - It is the collect ion of all the items in which
• are the left end ot
the R.H .5. of the rule.
Q.11 What is viable prefix ?
Ans : It is the set of prefixes in the right senten tial form of a produc tion A->a.
Thi,
~ t ca n appea r on the stack during shift/r educe action.
Q.12 What is the significance of lookahead operator ?
Ana : The significance of lookah ead symbo ls is that the
parser can decide th t'
reduct ions based on the k lookah ead symbo ls. Hence this parsin g proced
ure becoolt')
an efficient one.
Q.13 Elimina te left rec ursion from the followi ng gramm ar A-➔AclAadlbd
[Refer exampl e 2 7 3]
lc . Id
f•HN1

TECHNI CAL PUBLICA T/ON S® • an up-thrust for knowled ge


--
comptler O0s19!} _ _ _ _ _ __

0 _14 What should the error


2 · 145

handler In a parser do 1
Syntox Analysis

AU : Mt1y•ll

An•. : Thl' errc•r h;i nd k•r in-parser reports lhl' sy ntncticn l l'l'l'nr if ,my.

Q.15 Which of the parser-bottom-up or top-down parser-ls called LR parser ? Why


Is It called LR ? AU : May-11

Ant. : The LR parsers .1rc ca lled bottom up pnrsl'rs becnuSL' in this type of parsing the
input is p.1rscd frorn bnttom tu top .
Compare syntax tree and parse tree. AU : Dec.-12, 17
Q.16
Ans: Parse trees arc muc h more deta iled representatio n of the source la nguage tha n ,
that of syntax trees. Syntnx trees are sma lle r than pa rse trees and these are much space
Jnd time efficient.
AU : Dec.-12
Q.17 Write the rule to eliminate left recursion in a grammar.
Ans: If the left recursive grammar is
A -> PA'
A ->Aa Eliminated as
A' -> a.A'
A ->~ A'-> E

Q.18 Eliminate the left recursion for the grammar


5->Aa I b AU : Dec.-13
A->Ac I Sd I e
Ans. : TI,e rule to eliminate left recursion is
if A ➔ A a I ~ then A ➔ ~A', A' ➔ aA' I £.

A ➔ Ac I Sd I £

We can re place S by Aa I b.
The grammar the n b ecomes -
A ➔ Ac I Aad I bd I £
. . . . d' t I ft recurs ion from A productions we get -
By elimmatmg 1mme 1a e e
S ➔ Aa I b
A ➔ bdA' I A'
A' ➔ cA' I adA' I £
AU : Dec.-14
eserit palindrome.
Q.19 Write a CF grammar to repr . .
t0 represent the palmdromc ts
Ans : The context Free(CF) g ra mmar

CFG G =(V,T,P,S) where


V is set of no nte rmin a l s ymbols = (S),
T is a set of te rm ina l f;y mbols = (a,b),
S is a s tart sy mbol
The set of producti on rul es r -- I 5-> nSa I hSb I .1 I b I c I
•~

----------- - ON S® - an up-thnisl tor knowledye


Trri-.NICA L PUBLICA Tl
2 - 146 Synrax An
omp,/01 Dos,gn
~ ~ a,,

Q.20 What 11 the role of parHr 7 . d t k


I · :11 m1lyzcrmorcr I
oc1ec - the,
Ana : l'an,cr cnlll'c:t:-; thL' 11,l-.l•n:-; troin t 1\ L' L'XI C, ' ' . ~ ~1'1.ta\
of tlw '-LlllrC'l' c,h.k It .my l:rrn, 1. LL , . ·.., I ,tectcd during the .ini1lr 1s of . yn tax, the '-\ilt
a,
, J 1':-;r,
e rror n'll'l-'-il~L':- ,ll"L' ,, Ii-o •Pl •1yl·,l h)' the n,lt'ser.
r

Q.21 Write the algorithm for FIRST and FOLLOW in parser.


Ans : FIRST fun ction
FIR~ f(n) i:-; ., set l)f terminal symbols that arc first symbols appearing at R.H.S. lr
JL•ri ,·,,t ion of ex . If ,x ..c) r tlwn t: is also in FIRST (a).
Followi ng <1rc the rull's used to compute the FIRST functions.

1. If the te rminal syrnbol a then FIRST(a) = lal.


2. If the re is a rule X - H then FlRST(X)=IEI.
3. For the rule A ~ X1 X2 X1 ... Xk FIRST(A) = (FlRST(X 1) u FIRST(X2) u FIRST(X>\
v FIRST(Xk))
where k X; :5 n such that 1:5 j :5 k - 1.
FOLLOW function
FOLLOW (A) is defined as the set of terminal symbols that appear immediately to
the ri g ht of A. In other words
*
FOLLOW(A) = I a I S ⇒ a Aa ~ where a and ~ are some grammar symbols maY
be terminal o r no n-terminall.
The rules for computing FOLLOW function are as given below -
1. For the start symbol S place $ in FOLLOW(S).

2. If the re is a production A ~ a B ~ then everything in FIRST( ~) without f is to 1't'


placed in FOLLOW(B).

3. If there is a production A ➔ a B ~ or A ➔ a B and FIRST(~)= {E} then


FOLLOW(A) = FOLLOW(B) or FOLLOW(B)=FOLLOW( A). That means e\'erythit\~
in FOLLOW(A) is in FOLLOW(B).

Q.22 Write a grammar for branching statements.


Ans : The.- context free grammar will be for if-else statement as follows
S ? iEtS
S-, iF:&S
S-, a
E -, b

- -- -
TECHNICAL PUBLICA T ®
·----- ------
IONS - an up-thnist for knowledge
-
compiler Design 2 - 14 7

lng 't
What are the various conflic ts that occur during shift reduce par•
Q.23
..
..,.■ ze--1n1ni
,•;_u
Ans. : The re a re two conflic ts tha t occur during s hift reduce parc,ing
c1 nd th< A(• Ml-'
1n~ th 1•
1. Shift/R educe conflic t : The shift reduce co nflict occurs w h en rrn n-·,,d
next input symbol and s tack to p symbol, the pa rs ing table ,:,c tion c.~ ·< ifw~ ft,
shift action as w e ll as the reduce actio n by so m e gra m ma r r ule
f ecid m Y,
2. Reduce /Reduc e conflic t : The s hift reduce conflic t o ccu r~ wht--n on
the n ext input symbo l and stack top symbol, the p a rs ing tabl e action ~pedf~
m o re than one reduce actions . The action s u gges ts reducti on by d i ffL"l'E•nt
produc tion ruJes.
Constr uct a parse tree for (id + id). AU ; ~c.- 16
Q.24
Ans : The parse tree will be -

+
/ \id
id

Fig. 2.20.2
tic error ?
Q.25 What are various stages that a parser can recove r from a syntac
AU r.>ec 12

Ans. : Parser can recove r from syntact ic error with the help o f followi
ng strateg:it:S

(1) Panic mode (2) Phrase level (3) Error produc tions (4) G loba l correct
ion.
AU ~ c l~
Q.26 Define LR(O) item.
Ans. : The LR(O) item is a produc tion G with dot at some positio n on the right side o-f
the produc tion.
Fo r examp le - S->a .BC
The LR(O) item indicat es that what is to the left of dot has jus t been
read and tne

parser is ready to read the re maind e r, after the dot.

Q..27 What is the purpos e of YACC ?


Ans. : YACC s tands for Ye t Ano the r C o mpile r Compi le r. It is
an auto m a ted tool fu r
syntax analys er.
if-else statem ent of
Q..28 Write down the contex t free gramm ar for repres enUng the
AU . DK- l l
any langua ge.
Ans. :
S ➔ i EtSeS I i EtS I id1
E ➔ id2

QI)
TECHNI CAL PUBLIC ATIONS - an up-lhn.Jsl for k1,o"'ie.1.x,r-,
2 - 148
Syntax Ana/
YS1s
Q.29 Write LMO for the word (1+1)(1*1) using the grammar,
E->E+E I E/E I E*E I (E.) .
Ans. : n,'-, I MD(L cl t Most Deriv ation ) for the string (i+i)/ (i*i)
F

E r
tE) I'
tE+l') F
l• +E) ' E
(i+i) E

l •+i) / ( E)

(i + iL ( E* E)

(i+i) ( ii<E)

(i+ i) (i~ )

DOD

TECH NICAL PUBLI CA T/ONs® - an up-thr ust for knowle


dge

You might also like