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

FIRS T T E R M E XAMINATION [F EB .

2016]
SIXTH SEMESTER [B.TECH]
COMPILER DESIGN [ETCS-302]
Time : 1.5 hrs. M.M. :30

Note: Attemp t any three que sti ons inc lud ing Q. no. 1 whi ch is com pul sor y.
Q.1. (a) Discuss Merits and Demerits of Single pass compiler and Multi pass
compiler. :
Ans: Single pass Compiler is a compiler that passes through the parts of each
compilation unit only once, immediately translating each part into its final machine
|
|
code. | | a
Merits: | | ,
1. They are faster.
2. It is also-called as a Narrow compiler. |
3. The external storage for the intermediate file between two passes is slow or is
inconvenient to use.
Demerits: ea |
1. It has less efficient code optimization and code generation. |
2. Large Memory is required by compiler.
3. It has limited scope of passes. a
Multipass Compiler: It reads the program several times. Each time transforming
it in to different form. ©
Merits:
1. Better code optimization and code generation.
2. It is called wide compiler as they scan each and every portion of program.
Demcrits: | | ae
1. Slower, as no. of passes means more execution time. _
Q. 1. (6) What do you mean by Bootstrapping in Compilation?
Ans: Bootstrapping is the process of writinga compiler (or assembler) in the source
programming language that it intends to compile. Applying this technique leads to a
self-hosting compiler. An initial minimal core version of the compiler is generated in a
different language; from that point, successive expanded versions of the compiler are
run using the minimal core of the language.
>

Bootstrapping a compiler has the following advantages:


. ee fon-evAa) test of the language being compiled, and as such is a.form

ee nies only need toknow the anata being compiled. i


cob gaye FC ne i Gone in the higher eye ne ee being Cae
programs but also the oman) ~ - s back-end improve not only genera purpose
| ' é o m e 2 b e s h pk j g ,
aots comprehensive consistency check as
ics * Iti ld it shou be able to reproduce its own

Pies {c) What is the need for separating the Parser from Scanner?
S: Lexer: character stream to token stream _ |
Parser: token stream to syntax tree
.
2-—2016

I.P. University-[B. Tech]-AB Publisher 2016-3


Ri:ght sentential
ialform | Handle ; Reduction production
Boi
_ id, + id, * ids
= K > id
E+ id, *id,
id - E -> id
1 3
aa E*E K > E«E
Ans: Left Most D
{S>bBI aA,
erivation:
ae . ElE E>E+E
S->bB E.
S->bbBRB 4
Q l (f | ) eink
are the responsibibil ities of Loader, Linker, and Assemb
S ->bbaR » * . iliti ] er in t he
compiler envir onment?
S -> bbaaS
S -> bbaabR Be Ans: Assembler: : An as sembler translates as sembly language programs into
‘machine code. The outpu t of an assembler isis c called an object file, which contains a
S —> bbaabaS : | 7
combination of f m machine instructions as well as the data required to place thes the e
S —> bbaabab B . | Be
S -—>bb aababa instruct
f ions in memory. ,
Right Most Der
ivation Linker: uter program that links and merges various object files
sue? Ce secntenis file. All these files a
eos oe onder 0 , oe iled b 7
S->bB The major task of a linker is to search an
pee
oca (are
S->bbBRB ane and to determine the memory
nouunes aD ae rogr am instruction to have absolute ed aa es | :
S->bbBaS references. ee dadinc
S ~> bbBabs ee re fe is a part of operating system and is ap pee
S.-> bbBabaS a ones emory and execute them. It calculates the siz ee eee
n
o
C
(inse
tructionsaean iva creates memory space for it. It initializes vari
S-> bbBababB eaten g1
S-> bbBababa a to initiate execution.
.
S-> bbaababa Q.1. (g) Write steps involved in compil
i
ation
. e e
of following sentenc
ay >
ie e

Derivation tree es | + pos = heat + rate * 60.


An S: pos = interest + rate *60

lay,
Lexical: Ailsa
id = id + id * 60

!
Syntax Analysis
\

Parse. If w is a sentence
ee
unknown rightmost =Y ,, w h e re Y, 18 the nth righ of the =
*
deriva tions { se nt en tial form of some as yet
Exa-nple for right
it. aw, | | | —
sententia] form and a ro aesehA
‘EO E+E h a n d l e fo r grammar 2 ve int to real (60)
ee
°E > (£)
°*E id Intermediate code generation |

|
4—2016
Sixth Semester,
Compiler Design
Tl = into real (20) I.P. University-[B. Tech]—AB Publisher
2016-5
T2 = id * i Ans: A Grammar is LL(1) if for every productionA «| § —»
T3 = id +4 7?
First (a) * First (B) =

IfX € First (a) then First (6B) “ Follow (A) = 6

e IfX € First (B) then First (a) “ Follow (A) = 6


First(S) = First(X) —> First(Y) — First(Z) -> e
Code optimization
First(X) = First(Y) —> First(Z) > e
First(Y) = First(Z) > e
First(Z) = e
T2 = id* Tj
T3 First (S) = {fe} U {d} Ut&}
= id+T2

“a
id = T3 First (X) = {c} vu fe} v {a}
This Grammar is LL(1)
Q.3. Write Algorithm to construct SLR parser. Show all the steps of SLR
parsing for following grammar.
| Code generation S- R
R —> Rb
R-a
Assembly code Ans: Simple LR or . SLR parser is a type of LR parser. with small Il p parse tables and
a relatively ienale parser generator algorithm. As with other types of LR(1) parser, an
Q2. Attempt both parts
SLR parser is quite efficient at finding the single correct\bottom-up parse ina single
(a) Remove left recursion f
rom the following grammar left-to-right scan over the input stream, or backtracking. The parser is mechanically
: generated from a formal grammar for the language.
S— Aab |
A —> Ac/Sd/e | Algorithm:
Ans: As we don’t have immedia Input: An Augmented Grammar G’
te left recursion,
Step (1) Rename S, Aas A,, A, respec
tively | Output: goto
i A, ->A,alb Method:
_ A,->A, clA, dle
Step (2) Substitute value A, of statement (1) in R.H.S of st
, 1. Initially construct set of items
mmar
atement (2) C= {I,, 1,, 1.) «-- In} where C is a collection of LR(0) items for gra
A, -> A,alb Segeaies
| A, —> A, c|A, a d|bdle 2. Parsing actions are based on each item or state I;.
Step (3) Again substitute A, = S,A,=A - Various Actions are:
) | then set Ac ti on [i, a] = “s hift )’.
| S -> Aal|b | (a) If > a.af is in J, and goto (/;,a) = I j
uce A _> a for al l sy mb ol s a, where
A -—> Ac|Aad|bdle (b) IfA— a. is in J, then set Actionli , a] to red
Step (4) It has now immediate left recursion a belongs to Follow(A).
ac ti on ta bl e Ac tion li , $} = “accept”
(c) IfS 9S. is in]; then en try in
e go o
to ttraan s! tions for state t 1s
S-—>Aalb . he go to part of S L R ta bl e can be filled as: Th
S —> Aalb ; 7 _T a)=I, then goto [i Al =J
no n te rm ia ls on ly . If go to U;,
tas 3
A -> AciAad: |bdle ne e to be “e rror -
A’ -> a
ered
cA’|e rule 2 an d 3 ar e consid
aDAla’ 4. All entries not defined by
SR-
RRb >
Q.2. (6) Prove whether following:
Ra->
| § — XS|dSle
xX — Y|Zbl[aY | Item set 10
Y 5 eZ
: Z—e
S'=> S.
S’- 5 a L.P. University-[B.
Tech]|-AB Publisher
2016-7
Initial state
S—>.R
ae S—+>R.
R—> Rb RRb @-+@-
R—>Rb. |
Rye —@)-+@-!
oe R-ya eg Q-+@-" .~-
Q.4. Attemp 7—
t both Par
(a) Dra ts —i)- +0) @)
en the 9 @
recognize e ;} dentitirf,iniPrettrhigec o
perate be used by lexical]
:
analyser +
Auxiliary def
inations
letter = A/B/
C] Sales ont Z
digit = Q/1)9
Translation ru
les pee -
bregain
ae {; return 1)
Ee {return 2}
a {return 3 }
{return 4}
| {return 5}
letter (letter + dig
it)-
{Value = install ():
} Wwe. (return 6 _ Q.4. (6) What is operator grammar? Write ope
ae } rator precedence Parsing table
tValue = install ( for following gramma r:
| E> E+ E|E*E|(£)/a
return 7 3 Ans: An operator precedence grammar is a context-free
- } property that no production has either an empty right-hand side
grammar that has the
‘a
or two adjacent non
Value =] -’ terminals in its right-hand side. These properties allow precedenc
| e relations to be
return 8 defined between the terminals of the grammar. A parser that exploits these relations
is
considerably simpler than more general-purpose parsers such as LALR parsers.
A Operator-precedence parsers can be constructed for a large class of context-free
Value = 2: grammars.
| !
(return 8 First attach $ at starting and ending of string i.e. $ al + a2 + a3$. |
Put precedence relation between operators & symbos using precedence relation
table |
| = | . Value =3: | $ > -al <. + <.a2->* <.a3.>$
per : return 8 3
. String Handle Production Used
,
<> Value = 4: $<-al-> + <-a2-> *<-a3->$ | <al-> E>a
;
| return 8 $E + <-a2-> *<-a3->$ <:a2-> : —>a2 -
i| $E +E *<-a3->$ <-:a3->
:
{Value =5: $E +E * Eg Remove all non terminals
t
return 8 $+*$ Insert Precedence relation
| i io ee operators a ase
fs
=
Value =6: fant Scie
<++-> i.e. E+
P E Son 5
return 8 $<. +->$
$
SECOND TERM EXAMIN ATION APRIL
[ 9 016) LP. University-[B. Tech|—AB Publisher
SIXTH SEMESTER [B.TECH] Op atg, | are, |
COMPILER DESIGN [ETCS-302] pee ofp
Time: 1.5 hrs.
Note: Atiempi any three questions in M.M. £80 + b (0)
cluding Q. no. 1 which is compulsory,
+ (1) (0)
3 |
Q.1. Attempt any four parts
(a) What are the different representa
tions of three address state
Ans: Three-Address Code: Intermediate
. ents? ‘ blem of f code immova bility while optimiz ation, ; as th é results
Its
code generator receives inp | Pp
Se andeechanging the order or position of an expression may cause problems.
ut from it, are positional
Syntax tree then can be converted into a linear representation, e.g., postf i
ix notation cet : cniplesdiiiel is an enhancement over triple
, ;
s representation. .
It uses pointers
Intermediate code tends to be machine independent code Therefore, code generaty,
i decadiot sonition to store results. This enables the optimizers to freely re-position the
assumes to have unlimited number of memory storage (register) to generate code, sub-expression to produce an optimized code.
For example:
<a IRD oie iiéedeas Kas tobe declared before it can be used. eae A involves
a = b+c*d; apse |
: aul | : Stuy
alloca tion ot : lig ii ideati on’ keepi
and ngent
theofry
type tand na neme
instruc
the ture
The intermediate code generator will try to divide this expression into of spa ‘oned targe machi symbolin mind,
ta at butdan
it may
sub- may be coded and design urately convert a source code to its target language.
expressions and then generate the corresponding code. ed _ not always be possible to acc llection of procedures and sub-procedures, it
d ogram as a CO eG
b ae weaatle deena the names local to the procedure. Memory allocatio n is
eee SAS
r2 = b+¢ri; “a | ecom : d names are allocated to memory in the sequence: they
a ray se a tie taro: we use offset variable and set it to zero {offset = 0} that
: are declared in :
Se tee denote the base address. .
_ the target machine architecture may vary
| i in
; 2a Thye na urce
some progstra mm in g la ngue se
: 2 : 7 | | the wa s are or ed , so relative addr aid.meWheile the first name is allocated
ing ieis use
e
o in the target program.
r being used as registers
me mo ry location : e next nam e declared later,
memory st ar ti ng fr om th e
A three-address code has at most three address locations to calculate the expressi t to the first one.
s on. should be allocated memory nex
A three-addres code can be represented in two forms : quadruples and triples. 3 e:
Sa Example: | : here an integer varioy
able 1s
e t | | 4; We 3 ta2kebytethse ex ple rat mmvainrigablelangisuaasgesi’ gn© in)
Qu a d r u p l e s ; ee i of am a og
memory of anC d pr floa ¢memory.
Each instruction in quadruples presentation is divided into four fields: operator, \ cael a;
argl, arg2, and result. The above example is represented below in quadruples format: glcat
Allocation process:
Op | arg, arg, | Result {offset = 0}
: 3 int a;
. C| d : rl Se.teype
id = int
tg i
1 r2 id.width Nie.
S . fe offset = offset + id.width |
+ 92 | rl r3 {offset = 2) gk :

=os r3 2 ah float b; |
id.type = float
id.width = 4 Sik
Triples ; | offset = offset + id.width be used. This method
qa ger struet ctioea n inae tripegles esentation
; has three fields op,h argl, and arg2. The
Each in pr i B+, a a _ iota (offset = 6) his detail in a| symbol table, a proce dure enter can
results of respective sub-expressions are denoted by the position 0 expre a hil Toenter t a wing structure’
represent similarity with DAG and syntax tree. They are equivalent to DAG while . may have the follo
representing expressions.
10-2016

enter(name, typé, offset)


Sixth Semester, Compiler Design

This procedure should create an entry in the symbol table, for v


ee
ariable name, I.P. University-[B. Tech]-AB Publisher
its type set to type and relative address offset in its data hay; 2016-11
area, As in our previous exampl (E —
Q.1. (6) Give three address code for the following code segmen e E + T), the parent node F gets its v
t: chil
= d node. Synthesized attributes never take values alue from it S
from their parent
While(a<b) do ‘sibling nodes.
cee
If(e<d) then x = y+ z 2. Inherited Translation: In contrast to synthesized att
Ans: 1. If a<b go to $3 ributes, inherited
attributes can take values from parent and/or siblings. As in the fol
lowing production.
2. goto(8) S > ABC
3. If e<d goto (5) A can get values from S, B and C. B can take values from S, A, and C. Likewise, C
can take values from S, A, and B.
4. goto(8)
Expansion: When a non-terminal is expanded to terminals as per a grammatical
5. tl = y+z
Yule
6. x= tl
7. goto tl
8. end
Q.1 (c) Write Syntax directed Translation
scheme for While Loop. _
Ans: Syntax directed Translation scheme

uoyjonped
for While Loop:

Expansion
* Consider the production S > while (C)
S1 for while-statements. The Shape |
of the code for implementing this production
can take the form:
label L1: // beginning of code for S
|
code to evaluate C
if C is true gotoC.true // C.true will be set to L2
| id id
if C is false goteC. false // C false will be set to
S.next Reddetfon - When a-terminal is reduced to its corresponding non-terminal
label L2:
according to grammar rules. Syntax trees are parsed top-down and left to mght. Whenever
code to evaluate S1 reduction occurs, we apply its corresponding semantic rules (act
ions).
above tasks.
goto L1
| Semantic analysis uses Syntax Directed Translations to perform the
previous stage (syntax
S.next: // this is where control flow will go after execut
ing S Semant. ic analyzer receives AST (Abstract Syntax Tree) from its
Sem ant ic ana lyz er atta ches at ibut e info rmat ion with AST, which are called
¢ Here is an SDD for this translation: analysis).
| )
S— while(C)S1 { Attributed AST.
attribute value>
Attributes are two tuple value, <attribute name,
L1 = newlabel(); ) : For example:
L2 = newlabel(); Se int value = 9;
S1.next = L1; _ <type, “integer”’> |
C.false = S.next; : pana
<presentvalue, “5”>
rule.

DAE
C.true = L2; For every production,ctwe attach a semantic
SDD (Syntax Dire ed Defination): A CFG where each eee — -
S.code = label | | L1 |! C.code |! label |! L2 11 S1.code ‘i he ss . <.
the for m b .
=
wit h a set of sem ant ic rul es of
}1 A-> ais associated
Ma
is phe g 1
rib ute of one < : e gramm
att rib ute of Aor an inh eri ted att
bis a synthesized

Q.1. (d) Differentiate between SDT and SDD s use d in the production.
Bi wayne

‘nacl,c2, ... are att rib ute s of the sym bol


_ Ans: SDT: It is a kind of notation in which each production of CFG is associated
-

At ributes
tedd Att
ee zed and Inheriite

oalsi
athhe
Synt
with a set of semantic rules or actions and each grammar symbol is associated with a com put ed at the nod es ofa p arse tree.
Attributes are values ut e dN at a
e node N ne
Ni -
inal parse
set of attributes. Therefore the grammar and the semantic actions combine to make a e va lu es th at ar e co mp
e Synthesized attributes ar
Syntax Directed Translation. en of N and p
.

va lu es of th e ch il dr
tree from attribute tions ar e n e ) — ibutes.
It is of two types: s $$ , $1 , $2 , et c. , in Ya cc ac
-dentifier c o m p u ted bya hen
1. Synthesized Translation: These attributes get values from the attribute values ut es ca n be ea si ly
7 8 : thesized attrib th e pis! [ =
th e at tr ib ut es on
of their child nodes. To illustrate, assume the following production: oe the values of
tr ib ut ed if ev er y at tr ib ute is sy
S > ABC : e AnSDDis S-at ed
herited attributes ar e vat ues
If S is taking values from its child nodes (A, B, C), then it is said to bea synthesized — p a r e n t of N, the si
of th e
attribute, as the values of ABC are synthesized to S. | | rise attribute values
12-2016 Sixth Semester, Compiler Design
e An SDD is L-atiributed is every attribute is either
synthesized or inher;
from the left. | teq 2016-13
LP. University-[B. Tech]-AB Publisher
Q.1. (e) Write brief note on responsibilities of semantic analysis Phase,
Ans: Responsibilities of semantic analysis phase: Semantics 0 fa langy I —> I digit
provide meaning to its constructs, like tokens and syntax structure. Se a I —> digit where digit =0111.9
Mantics elpoe
interpret symbols, their types, and their relations with each other. Sem antic analygi. SDT fot it:
judges whether the syntax structure constructed in the source progra m derives any ; | Sema
meaning or not.
S.No. Production ~— ntic Acti:on
S — > E$ {Print E.VAL}
CFG + semantic rules = Syntax Directed Definitions
{E.VAL = E® VAL + E®VAL}

1] o> jou > |) |r


For example: E-—> E® +E?)
{E.VAL = E® VAL * EB) VAL}
E-> EB) * E?)
int a = “value”:
E->(E”) {E.VAL = EVAL}
should not issue an error in lexical and syntax analysis phase, as
it is lexical] Y and (E.VAL = I.VAL}
(I. VAL = 10 * 1 VAL + LEXVAL}
structurally correct, but it should generate a semantic error as the type
differs. These rules are set by the grammar of the la nguage and evaluated
of the assi &nmMent
Fo di
analysis. The following tasks should be performed in semantic analysis:
in semantic —
= =>: ae (1.VAL = LEXVAL}
¢ Scope resolution ) Sq Q.2. (b) Generate the intermediate
. code and output E. True e list and E. False
¢ Type checking c list for the following expressi0n- i ctacaciaas a
¢ Array-bound checking. . | | | a x>y and z>k and r<s from the followin
Q.2. Attempt both parts | | E->Eor ME
(a) What is syntax directed Translation? How are semantic actions
attached — E->Eand ME
to the production? Explain with Example.
| E->notE
Ans: SDT: It is a kind of notation in which each pro
duction of CFG is associated
with a set of semantic rules or actions and each grammar - E->€
symbol is associated witha
set of attributes. Therefore the grammar and the semantic
actions combine to makea _ ae Esp id
Syntax Directed Translation. | | E ->idrelop id
Any SDT can be implemented by first building a
parse tree and then performing a: M-> € |
the actions in a left-to-right depth-first order; that is, during
a preorder traversal.
Build up the parse tree.
|
SDT’s case ed1'sto ar
n albe’: usSD rt plclem
imepoim sestedof SD
asen ng parsing, withou) t building a parse tree.
ris:
duD’ _ Ans:.
¢ The underlying grammar is LR-parsable, and the SDD
is S-attributed. ;
¢ The underlying grammar is LL-parsable, and the SDD
is L-attributed. a : | | . ME
In both these cases, the semantic rules in an SDD can | sates d
be converted into an SDT
with actions that are executed at the right time. During parsing,
anactionina production _ : ad Se ;
body is executed as soon as all the grammar symbols to the left of the acti |
on have been ee td relop Id
matched. © &
; : . : A |
| SDT’s that can be implemented during parsing can be characterized by intr
oducing | |
distinct marker nonterminals in place of each embedded action; each marker M has only
|j : y
Ps a3 J | |
E
one production, M — «. If the grammar with marker non-terminals can
be parsed by a
given method, then the SDT can be implemented during parsing. :
Semantic Action: It is an action which is executed whenever parser will recognise pf ‘ €E
id relop |! IN.
_ the input string generated by CFG
Ex: A - > BC {Semantic Action}
_ Semantic Action is written in curly braces attached with a production.
Example: Grammar given is: |
: S —> E$
E —> E+E
E —> E*E
E —> (E)
E-> I

SR wey
———————— eee Oe,
OO eee ee ee. ES ree %

14—2016 Sixth Semester, Compiler Design


Q.3. Attempt both parts 2016-15
Publisher
(a) Explain L-attributed and S-attributed grammer with example, L.P. University—-(B. Tech|-AB
Ans: S-Attributed Grammars f th e + op er at or must be of type INT.
are a class of attribute grammars characterizeg 1. Both operands o
having no inherited attributes, but only synthesized attributes
. Inherited attributes whi erands of the && operator mus t be of type BOOL.
must be passed down from parent nodes to children nodes of the == ope rat or hav e the sam e (non-ERROR) type.
abstract syntax tree pie 5 oth spersni of the
les:
the semantic analysis of the parsing process, are a problem for
bottom-up parsing beca, “8 Here is the CFG and the translation ru
in bottom-up parsing, the parent nodes of the abstract syntax tree are Translation rules
created after ctegil : CFG
of all of their children. Attribute evaluation in S-attributed gram
—— oo
——
——_———_—— <<
—— —
— —_—

erm.trans == INT)
—— ——

mars can be incorporates


ee
CS

conveniently in both top-down parsing and bottom-up ex p, .t ra ns == IN T) an d (t


parsing. : exp secep +term if ((
Ifan SDT uses only synthesized attributes, it is called as S-attrib
uted SDT. Thege then exp,-trans = INT
R
attributes are evaluated using S-attributed SDTs that ha ve
their semantic actions else exp,.trans = ERRO trans == BOOL) and
(term.trans = BOOL)
written after the production (right hand side). exp ->exp&& term if ((expy-
E. Value = E. value + T. value - then exp,.trans = BOOL
R
else exp,-trans = ERRO
. == term.trans) and (exp,.trans
exp ->exp = = term if ((exp,-trans
!= ERROR))
then exp,.trans = BOOL
R
i E ©) Wig else exp,.trans = ERR O trans.
exp.trans = term.
€ j
exp -> term rn
E. value : T. value term -> true term.trans =
term -> false term.trans = BO
.trans = INT
*
* “
“Sanne?

t e r m -> in tl it er al te rm
As depicted above, attributes in S-attributed SDTs are evaluated in bottom-u term.trans = exp-tran
s
p term -> ( exp )
parsing, as the values of the parent nodes depend upon the values of the child
nodes. | Input
L-attributed grammars are a special type of attribute grammars. They allow
the
attributes to be evaluated in one depth-first left-to-right traversal of the abstract syntax
_ + 2) ==
a
tree. As a result, attribute evaluation in L-attributed grammars can be incorporat (2
ed s e errors?
conveniently in top-down parsing. a n d s e m a n t i c p h a
) Wah a t
s is leexi ca
k la ph as e, syntactic in to tokens i.e it
A syntax-directed definition is L-attributed if each inherited attribute of Xj on the (a br ea ks th e pr og ra m
ca l p h a s e er ro rs : Le xic al Phase
right side of | ri Ans: Lexi
kens, in program. aces that no pre Gx of the
A — X1 X2 ..Xn the lexical Analyzer discovers
ea.lfa : Saag processing . : are genera ted.
depends only on : an ? then lexical errors
to be read im source pr gr
1. The attributes of the symbols X1, X2, .., Xj-1 because next token
| re 2 Coe . Iti pais |
ae
2. The inherited attributes ofA _ Sr ; misspelled.
Every S-attributed syntax-directed definition is also L-attributed.
Implementing L-attributed definitions in Bottom-Up parsers requires rewriting
L-attributed definitions into translation schemes. of p r o g r a m m i i
n n g g la ng ua ge.
| ammatical rules
Many programming languages are L-attributed. Special types of compilers, the in g ; at th e en d of th e statement.
r 1. Miss
narrow compilers, are based on some form of L-attributed grammar. These are a strict |
Ri gh t Pa re nt he si s (a +( b+c)
2. Missin g a
superset of S-attributed grammars. Used for code synthesis. yw or d |
3. Misspe ll ed ke ; | ie
Either “Inherited attributes.” or “synthesized attributes” associated with the . in pl ac e of se mi co lo n ex:1= 1:
4. Colon
occurrence of symbol X1, X2...Xn.
Semantic phase errors
Q.3. (6) Write syntax directed translation scheme for multi-dimensional |
1. Non Declared variables :
arrays with suitable example. of op er an ds | |
2. Type co nf li ct
Ans: Similar Example: e ca ll s (e .g . Wr on g no . of P
3. bets procedur t a s t r u c t ures § 4
Consider the following CFG, which defines expressions that use the three operators: v a r i o u s d a
Q.4. (b) Discuss the ae
+, &&, ==. Let’s define a syntax-directed translation that type checks these expressions; | |
ample. ed for eymihge
1.e., for type-correct expressions, the translation will be the type of the expression (either ctures us
. hos, Different data stru
INT or BOOL), and for expressions that involve type errors, the translation will be the :
eh Lists
special value ERROR. We'll use the following type rules: sts
9 Self Organising Li
16—2016
r, Com ;
3. Trees moet Design
4. Hash Tables | | | LP. University-[(B. Tech]-AB Publisher
LISTS: 2016-17
- First pointer points to the be
1. It is simple and easy to; ginning of symbol table
im - If we
have names in the following
records Plement data structure for order
3->1->4-39
pee “Ymbol table in linear list - If we need a particular re
aur
i cord we need to move record
Suppose we need 4,50 new connec
tion will be
to the front of the |i

=
4->3->1-32 a

z
ol
— Me oY

—— Name 1
Data ‘1
Link 1
Name 2
Data 2
. Link 2
First oa Name 3

ie . Link 3

| NAMEN Available
Name 4

INEO
Cane
a e4
= One can use single S <_——— Available
Link

- New Names
are

Mechanism
(a) Initial stage 3->1->4->2
(6) 4» name is required
(c) move 4" to front
Disadvantages.
3—->1->4->2
1. Sequential] A
ccess. (d)4->3->1->2
2. Amount of wo Advantages:
rk do
1. Increase search efficiency
2. Promotes Reusability to some extent.
Disadvantage: -
1. Difficult to maintain so many links and connection.
2. Extra space requirement is there.
HASH TABLES:
1. Hashing is important technique used to search the records of symbol table.
2. In hashing two tables are maintained
(a) Hash table
(6) Symbol table
3. It consist of K entries from 0 to K-1.Thse entries are basically pointer to symbol
table pointing to names of symbol table.
First
4. To determine whether “name” is in symbol table we, use hash function ‘h’such
that (Name) will result integer between 0to K-1 we can search any name by position =
h(Name). :

Available 5. Using this position we can obtain the exact location of name in symbol table.
i
6. Hash function should result in distribution of name in symbol table.
be min imu m no. of coll isio ns.. Coll isio nis a situ atio n where has
7. There should
in sam e loca tion for stor ing nam es. 7 |
func tion resu lt
n reso luti on tech niqu es are Ope n Addr essi ng, Chai ning , Rehashing
8. Various collisio
h Se me st er , Co mp il er Design
Sixt
18-2016

Advantages: : | K = 324 987 626 191 539


1, Quick Searen ; . Divide this key value in to
implement | Ki = 327 parts Kl, K2, K3, K4, Ks
Disadvane’ K2 = 789
1. [tis complicated to aa
9. Extra space is requ a rth i
| K3 = 626
KA = 191
2 Obtaining Scope of variables is difficu
types of Hash Se | | K5 =253
1, Mid Square Metho : 2168
(a) Consider a key K 4. Trees:
Let K = 3230 | | 1. Symbol table can also be stored in form of ; in
(b) ~ the square of K maces
2. Each node can have atmost two children-left and right
child
a
* 2 00
ae “4 e saa 3. Key value stored in parent node is more than ke y value stored
in left of childre
(c) Apply nas and less than right child.
(;) Hash function will eliminate few digits from both sides of k2. | 4. Height of binary search tree is of the order of logn
(ii) Hence H(K)= 32 5. Maximum time required for searching and inserting is proportional to Odog n).*=
Similarly | | Algorithm:
= eee Sic | 1. Initially, ptr will point to root of tree.
K2 52359696 5929225
| 2. While ptr = Null do
H(K) 59 29 ]| 3. If Name = Name(ptr)
(d) Deleting the number of digits from both sides depend on maximum no, of ke
required, de ] cps 8 ane t be Oo same from both ends.
Be 4. oe
else if Nameoo< Name (ptr) then

a4

oe!
(2) Division Metho

a
Ptr: = Left(ptr)

**
Let Key = K

ee
»
-
5. elseptr : = Right(ptr)

-
7
No. of elements =n

ee
,
6. end of loop.

el
el ene
4
Hash function= H(K)

.
'-
H(K)=Kmodm or H(K)= K mod m+1l

~~
>
Where m = Prime no

.
i.

ae. a
,
Ex: We have to index 100 records and hence largest ie number close to 10 |
hence we will choose m = 97 |
Let K = 3230 : Semis i
H(3230) = 3230 mod 97 = 29 | eB
3. Folding Method
(t) Use this method for large key values.
(zi) We can use two methods to get hash value.
¢ Shift Folding
K = 324 987 626 191 532
Divide this key value in to parts K1, K2, K3, K4, K5
K1= 327
K2= 987
K3 = 626 a
K4 = 191 | '%

* Boundary Folding |
In this ae of sub keys are reversed to generate new simple hash values
“ES Se

[.P. University-[B. Tech]~AB Pu


blisher 2016—21
Tokens

source-code
MM. +75 —-—- lexemes | Lexic
exical

it
ii
Analyzer

it
i
nc incl1uding @. 10. 1 which is compulsory.
rs tion
s
three ques
ri all 1 0 questions.

. betwee n comp iler and interpreter. Define q r Og,


| ss compiler will be
Disting the few cases where such cro F Usefy)
1.» and jadenul |
parse tree (or syntax tree). In thi
Interpreter oa s phase, token arrangements are
3 , the source code grammar, i.e. checked against
r ,1.e. the pa
1S syntactically correct. : rser checks if the exxpressio: n made by the toketokns
e
Compiler _—_— ae Interpreter takes Single instruction > |
| Role of Parser
as input.
7 Compiler takes Entire progr 1. Performs context free syntax an
No Intermediate Object Code igs ~— alysis.
as inpat
a, One—t Code
0 ied 2. Guides context —Sensitive
analysis.
rmediate UPJ Genera 3. Constructs an intermediate re
—— presentation.
+< Generated ‘ements | Conditional Control Statements 4. Produces meaningfull analys
| | is.
Gonditional Control State are Executes slower o. Attempts Error Correction.
are Executes TI - More Memory Requirement is Less = —
Token
4. |Memory Requnn is Generated) Source :
Ca yz
Parse tree
R ‘
Intermediate
(Since Object Code 18 Gene Every time higher level program is> roan rsa ah fos — *|
esi of
frontend | representati: on
“6, | Program need not = converted into lower level program getnext token ()
compiled every NC : Errors are displayed forevery — —
6. | Errors are disp pie oa instruction interpreted (ifany)
a

a C Compiler Example : BASIC i |


7. |Example :
Sia r capable of creating ormm,
platfor
| executab le code for a platf Symbol! table

the ee ea e enn is runni ng. For exam ple, a comp iler that rung Q.1. (e) Define ambiguity in a grammar.
sie chan de that runs on Android smartphone is a Cross Ans: : If a context free grammar G has more than one der
on.a Windows
: 7 PC but
; generanes 7 enera lly found in use to generate compiles fy, wiL(G), itis called an ambiguous grammar. There exi
ivation tree for stringw
compiler. Cross compiler to's latforais. It is a tool that one must use for a platform st multiple right-most or left-
most derivations for some string generated from tha
Toler t grammar.
embedded
where systemenient
it is inconv or zaultip's p i ble to compile on : that platform, like microcontroller
or imposs Problem:
that run with a minimal amount of memory for their own purpose. It has become mo To check whether the grammar G with production rul
es —
common to use this tool for para virtualization where a system may have one or mor X>X+X 1 X*X IXl ais ambiguous or not.
platforms in use. | Solution: . :
The fundamental use of a cross compiler is to separate the build environment fron Let’s find out the derivation tree for the string “at+a*a”. It has two
the target environment. | 2 ae leftmost
derivations. .
Q.1. (6) Design the Finite Automata for the regular expression a(atb)a, _ Derivation 1
Ans: X>X+X a+X> a+X*X 5 a+a*X oataXa

ae a
Parse tree 1 —

aK
-
> KQD*+CaD a
Q.1. (ce) Mention the role of Lexical Analyser
in compiler design. ae
i

Ans: Lexical analysis is the first phase of a compiler. It takes


code from language preprocessors that are the modified sour®
written in the form of sentences. The
ana lyz
commen er bre aks th
ts in the seca es e synt axes j ies of | lexical
ni any whit, espatagyon:
| nfo a series of tokens, by removing
C o m p i lil
e er Dessiiggnn.
r,
Sixth Semeste
92-2010
x

ix reX

n
.
rivatron
- *yY .a+ara
x
2 >a +2 xX a
I.P. University-(B. Tech]-AB Publisher 2016-23 4
“es Rene et
Xo K*A“ At
parse tree 2- |
Q.1. (h) Perform recursive descent parsing over the input mpp for the 7 ~
grammar.
S— XY
xX —> m!/ mY
Yop
Ans: boolean match (token tok ){return * next++ = tok;}
boolean S ( ) { return X( ) & return Y ( )3}
boolean X( ) { return match(b) & return match (b) & return Y¥( )3)
boolean Y ( ) { return match‘ ):}
boolean S(){ token * save = next; return( next = save, $1 ( ) )j
| boolean X () { token * save = next: return ( next = save, X1() )j
Pe || (next = save X2()))}
boolean Y () (token * save = next; return ( next = save. Y1 ( ) )}
Q.1. @ What is Peephole optimization? How is it performed? Give example
of Peephole optimization.
? As there are two parse trees for a singoele string & “a + ava
a%*a”
, the sTamma, q, SmallAnsset
: Peephole optimization is a kind of optimization performed over a very
of instructions in a segment of generated code. The set is called a “pee
ambiguous. ‘ phole
: . _’ or a “window”. It works by recognising sets of instructions that can be replaced by
Q.1. () What are the advantages of performing the LR parsing? | shorter or faster sets of instructions. Common techniques applied in peephole
Ans: LR Parsing:
optimization: Ny
sae fo He
L-Scans the input from left to right. , | . * Constant folding — Evaluate constant subexpressions in Baadvance.
R-Construct the right most derivation. |
K-No. of Look-Ahead Symbols.
* Strength reduction — Replace slow operations with faster equivalents.
* Null sequences — Delete useless operations. |
Advantages:
1. Can be constructed to recognize virtually all programmi -* Combine operations — Replace several operations with one equivalent.
for which context free grammers can be written. ng langyacn * Algebraic laws — Use algebraic laws to simplify or reorder instructions.
7B Menguage eo
2. Itis more general than other type 5 * Special case instructions — Use instructions designed for special operand cases.
s of parsers such as LL(k) ,operator and ain ¢ Address mode operations — Use address modes to simplify code.
precedence eeparsers and is as efficient.
- Van detect Redundant instruction elimination
ficinet synta ctic errors as soon as itis
it j poss i ae
ne de level
ay , int
the add_
follten
owing(int
ible todo so ona left to right cay canx) be done
Ae Sea by ten(
int add_ the user:
intx) int add_ten(int x)
pa = on a eee sets for a grammar. =
generated‘FIR
by ST:
| t
a variable. Collection of termi = s
;
which are the
egie s
start symb ol of
oo int y, 2; int y; iy y = 10;
as a x +10;
all the s ring. _ y 7
Samia inal, FIRST(X) return x+y; }
= {X}. | : Z=x+y; y=X+y;
If X> €18 a production, then add ¢ to FIRST(X)

if
3. IfX is a non return Z; return y;
FIRSTY,), ... FIRST(-ter
Y,),minathen
l, and X 5 Yo¥
addctg FIRSTty), *'S ® Production,
4) | a
4)fXisa non-terminal and ¢ is in all t compilat
|
instructions redundanta in nature.
latiion level, the compiler searches for ins
* . .

if for some i, a is in ,andX YY, Thess i hes and storing of instructions may carry the same meaning even if some
of them are removed. For example:
the non-¢ 1 19 --.Y
3S ,.) can be done as fol
Olllow
oWss;: e eMOVeRO, R1
oe symbols of FIRST et PEIBSTY, Y, ...Y,). Ife € ¢ FIRST d ee y
delete the first instruction and re- 4e8
write the sentence as:
;).and 0 on, Fina) Sob ee es FIRST(Y,), add all the
ca gat MOV x, R1
..Yife e FIRST Yi) weer ve a part of the program code that is never accessed be
ted by B ¢ terminal symbols which immediately comes : +a cquse Pe
i names ae cts. Programmers may have accidently written a piece of code
A ; that can never be reached.
input end-marker, and§ a a'Bp ih ie
@ Production, A+ app tne, oeeurt symbol, $ « FOLLOW(S) Example:
void add_ten(int x)
JCF OLLOWp) °’ °" 4 Production A~ ABe, where
¢ < FIRST(p), retux r+1n0;
CCoommpi l e r D e s ign
ror,,
Sixth Semeste

ofx is%" a L.P. University-[B. Tech}-AB Publisher 2016-25


. t ement will never be executed g
sant, the printf ce | hence prin
e segment, n execute, tf can be removed
pefore it C4 a My
timization ; |
op code where the program control jumps back
|
snstance 4
es ingnificant task. These jumps can be removed Qo nt |
“ai

\ ty C
LI . GoTOL - :
L2: INCRI ii can be removed as it passes the control to L2. g .
In this code, label ontrol can directly reach L2, a Ns},
jumping to LI and then to L2, the ¢ y S Shown belay! a b a b
[t, = a + b] [t,; =t + ¢]
MOV R1, R2
GOTO L2 UNIT-I

Q.2 (a) Design a derivation tree for the following grammar:


L2: INC Rl
es si on si mp li fi cation E > E+E | E-E | E/E | E*E | a | b. Also obtain the left most and right
Al ge br ai c ex pr
mple, most derivation tree for the string “a +b * a +b”.
There are occasions where algebraic expressions can be madepresi s s i o n For exay
cpr’ ession a = a + 0 can be re pl ac ed by a it se lf an d th e e x = a)
Ans: Derivation Tree
sats be replaced by INC a. a ay Ee
f Strength reduction ge 4
Ee <« E
f There are operations that consume more time and space. Their ‘strength’, x
reduced by replacing them with other operations that consume less time and ail Cay +
produce the same result. ; "7D ate e.<« E°* E+E

For example, x * 2 can be replaced by x << 1, which involves only one left a y
a+ &.* a+6
Though the output of a * a and a” is same, a” is much more efficient
| to implemen Left Most Derivation Tree
Accessing machine instructions e r
The target machine can deploy more sophisticated instructions, which can hay |
capability to perform specific operations much efficiently. If the ; Ive
target
accommodate those instructions directly, that will not only i
but also yield more efficient result. | ee cia ty ofaj
Q.1, (7) What is DAG? What are the advantages of
using DAG?
Ans: Directed Acyclic Graph (DAG) is a-tool
that depicts the structure of ba
G provides easy transformation on b] “off
understood here: basic blocks. HAG
* Leaf nodes represent iden
tifiers, names or constants, >
. Interior nodes represent ro
operators,

ster Compiler Design
Semese ~
Sixth +

i l e
epi L ->L,id
; —— |
Replace S-> TL
eT id
$TL _
$ ail Accept not cabins
$T L;
seid to erform : 4
>
are used , i
: i Discuss all three methods: which thes ;
arse Te -up parset- It uses pa ar
Dy coer ¢ rsive, shift-reduce, Daal efficient Fy RT TTT ight:
it the mo: ds for le
deri; vatiojon 1m
assFaeof cot Faxt-th eeeegr enniay: whiel makes ar rs, wh; ere ri Ls ae st
context-fr
are als o kn ow n 4s parse?
LR (k) 7, of ght-mo
ch ni qu e. LR pa rs er s make decisio
1s to9
nstruction
an i - R stands for the co TE ser: Tt <

look ahea d symb o in g an L R pa


iw wane of nstru ct
tv icc, ; ane i aeueies av ai la bl e fo r co
ae 5 * er e ar e r thr ee wi id el y us ed algorithms
Th th
}) Find whether the grammar is ambiguous or not: ¢ SLR(1) — Simple LR Parser: of e e a k ~ssabile
Works on sm al le st cl as s
°
S AB/aaB, A> a/Aa,B — b How to remove ambiguity from this gr Few number of states, he
nce very s
e
Ans: (1) | ¢ Simple and fast Serie
e CLR(i)— Canonical LR Parser:
e t e se t of LR (@) eae danke
ce W o r k s on c o m p l
G e n e r a t e s la rg e ta bl e an d large num
°
a
e Slow eet ahaa Salid
;
LALR(1) — Look-Ahea
e ze of hee
° wank on intermediat si
me as 11
e Number of states are sa LAI <|
Difference between SLR(1), LR(1) and os CLR PARSER |
(2) .- S.NO. | SLR PARSER Ane Expensive and difficult
: Easy and cheap to im lement

a Us toaieananh
aia aay
implement
LALR and SLRthey
have
CLR
ees
parser is largest
s no. of states
2. \Smallest in size. same size, as have | insize, - sme
| less no of states Tewar detection’ can
h 3. |Error detection is Error detection is not be done immediately
Removal of Ambiguity:
1. Precedence of Operators is not respected. =z 4. \SLR fails to produce | Intermediate in power lane class of
between SLR nad CLR oe a
2. Sequence of identical operators can group ei | parsing table for
see two different parse tree for the above certain class of 5

5 [Requires less ne | Requizes more time and | Resse laxity


ie mars time an
doesn’t matter whether the group is from le
must take one. ~
land space complexity | space complexity Pp
| UNIT-I1 : aoe
S+TL; T int! float L L,id:lid> Q.4. What are the benefits for the three address code generation? Consider
Ans: - theinputstring. : }
i | a -=-a*b+-—a* band generate the following: | ae
STACK INPUT STRING pack Sey tite Tree (b) Postfix (c) Three address Code
ACTION Ans: (a) Syntax Tree:
$ int id, id: Shift
;

ie | $int id, id; Reduce by T -> int


in
A $ T id
|" L
id;
id;
Shift
Reduce by L- > L
BS ks
peal
28-2016 Sixth Semester, Compiler Design
(6) Postfix: a — b*a — b*+
(c) Three Address Code: x:=-a*b+—-a*h
tl=-a
t2 =ti*b
ts = tl * b I.P. University-(B. Tech}+AB Publisher 2016-29
t4=t1+t8
Q5. (a) Draw the Syntax
lanwuane J tr ree for the following
s ngy Piece of bias . (it) YACC:
YACC Specifications
* Similar structure to LEX
(Y¥+ 1) else Y= Y+1 | * Declaration Section
aes then X=3*
* Translation Rules
* Supporting C/C ++ code
oe , If then-else
Declaration Section: — C/C ++ Code -—

on
———
YACC definition |
* Ztoken

-
assign (=)

ee ee
>
|
° %start
i x assign (=) * Others
The rules section represents a grammar. The left-hand side of a production is

me
X Y | llowed by a colon. Actions associated with a rule are entered im braces.
;
:
Structure of a yacc: A yacc file looks much like a lex
X Jom i il file: ...definitions... %% ...rules... %% ...code...
ae
| efinitions As with lex, all code between %{ and %} is copied to the beginning of the
ssulting C file.
|

ns of pattern and action. The patterns are now
an
As with lex, a number of combi natio
iose of a context-free gr ammar, rather than of a regular grammar as was the case with
;

_ »x. code. This can be very elaborate, but the main ingre dient is the call to yyparse, the
1ee 1 -rammatical parse.
er? What
eee Write short comments on: Q.6. What is the significanc e of the symbol table in compil
’ nformation is represented by symb ol tables? Explain the data structure used
Ans: () ier is a progr (ii) YACC
ntained by
input ‘st ‘ogram generator designed fi i Ans: Symbol table is an important data structure cr eated and mai
: ne : = ic It ee a high-level, problem oriented a possi am wompilers in order to store information about the occurrence of various entities such as
ee for che
§, and produces a program in a general purpose che -ari able nam es, func tion nam es, objects, clas ses, inte rfac es, etc. Symbol table is used
age whi ecyy both the analysis and the synthesis parts of a compil
er.
serv e the foll owin g p urp ose s de pe nd in g upo n the language im
symbol table may
d:
form at one place.
¢ To store the names of all entities in a structured
° To verify if a variable has been declared.
ent type che cki ng,by veri fyin g ass ign men ts and expressions 1n the
¢ To implem
Msource code are semantically correct.
° To determine the scope of a name (scope resolution).
Different data structures used for symbol table are:
- 2. Self Organising Lists
erated in the e@ me
lan loyed or the woot 1S gen
guage isemppro a high level pai mi 3. Trees 4. Hash Tables
a vided to ae : Poon fragments. Thus.
reedom to write actions is un; g expressions to be sq LISTS: 3 list
cin the aed ia the us 1. It is simple and easy to| implement data structure for symbol table in linear
a string manipiletion [. 1S unlmpaired. This avoids for
Se € user who wishesb_. 3
and oft en ina eee inp ut ana lys is to
same
Lexe turn eae 8 expate resstring handling lan 18 1
host gen sions and sale (called S al NAME 1
recognize expr sama the generated program is named yylex. Th ie INFO
Specified actions for each oe (called input in this memo) onde oe —_
10n as it is detected perlomy IN

Souren a) pak 4
| x Lex i yylex
INDUiteeems | cate :
R [ yylex I. Output
An overview of Lex oN (———
Available
experience th
: er ey
iler Design
16 Sixth Semester, Comp?

ee FOF a
ay to store the name eager
asis. for,
2 One can use single arr
3. New names are added to. end of list on 4 I.P. University—(B. Tech}~AB Publisher 2016-31
4. Method of access 1s sequential (b) 4‘ name is required
Advantages: (c) move 4" to front
1. Minimum soace requirement. . a 95 4s g's
2. Rasy to understand and implemen (d)4->3->1->2
Disadvantages: coo ee
uential Access. eats, | . Increase search efficiency.
: iaodiak of work done required to be done 1S high | 2. Promotes Reusability to some extent.
Disadvantage:
SELF ORGANISING LISTS:
e in li st o r g anisation. | 1. Difficult to maintain so many links and connection.
1. It is used to decrease searching tim 2. Extra space requirement is there.
ceil
2. Special attribute link is added ; 4 _ HASH TABLES:
features in list, this feature help in mi ~ Dy 1. Hashing is important technique used to search the records of symbol table.
reusability to some extent. 2. In hashing two tables are maintained.
- (a) Hash table
(6) Symbol table
/ 3. It consist of K entries from 0 to K-1. These entries are basically pointer to symbol
__ table pointing to names of symbol table.
3 4. To determine whether “name” is in symbol table we, use hash function ‘h’ such
___ that (Name) will result integer between 0 to K-1 we can search any name by position=
% 5 h(Name).
a) 5. Using this position we can obtain the exact location of name in ‘dqtihel table.
6. Hash function should result in distribution of name in symbol table.
7. There should be minimum no. of collisions. Collision i is a situation where hash
Available | 1 _ function result in same location for storing names.
8.Various collision resolution techniques are Open Addressing, Chaining, Rehashing.
Advantages:
1. Quick Search
Disadvantage:
— First pointer points to the beginning of symbol table
1. It is complicated to implement.
~ If we have names in the following order 2. Extra space is required
8>1>4—>2 ‘y
3. Obtaining Scope of variables is difficult.
_If we need a particular record we need to move record e the front ofthe i
Types of Hash Function:
Suppose we need 4, so new connection will be
(1) Mid Square Method:
(a) Consider a key K
8 Let K = 3230 .
--—-—s (6) Find the square of K
aa _ K? = (8230)? = 10432900 |
(c) Apply hash function
First (¢) Hash function will eliminate few digits from both sides of k2.
(it) Hence H(K) = 32
Similarly : ;
: | K 7236 2435
Available K2 pea | pie 225
H(K)
4 (d) Deleting the number of digits from both sides depend on maximum no. of keys
~ Tequired, deletion may or may not be same from both ends.
(2) Division Method|
Mechanism | Se eT Let Key =K
pla ete
No. of elements =n
@ hop dm>2 Hash function = H(K)
C o m p i l e r Dessiign
Sixth Semester ,

32-2016 ~Kmodm +1
d m o r H(K) -[B. Tech]~AB Publisher
[.P. University
mo
H(K) = 5
Where
rx: m = Prime".
we have to index
507 records and hence largest prime number ejg, ts 2016-33
aa ‘ell choose mm =
ly, The execution of a procedure is called its activation. An activation record contain
“UH the necessary information required to call a procedure. An activation record =
saa K = 3230 y | | patain the following units (depending upon the source language used). ae
- 3930) = 3230 mod 97 = 2 _ Temporaries Stores temporary and intermediate valués of an expression
q) Folding Method fade | | a Local Data Stores local data of the called procedure.
aU ce this method for large key va a ales Machine Status Stores machine status such as Registers, Program Counter
ea We can use two methods to get hash V 7” etc., before the procedure is called.
. Ghift Folding ) | Control Link pcs the address of activation record of the caller
ii 7 626 191 532 . : a
Salat key value in to parts K1, K2, K3, K4, K5 7 Access Link Stores the information of data which is outside the local
1 rf scope.
Ki = 327 3 a Actual Parameters Stores actual parameters, i.e., parameters which are used
K2 = 987 | nae to send input to the called procedure. eo
K3 = 626 a Return Value . Stores return values.
K4 =191 | | | ene eee Runtime environment manages runtime memory requirements
a e following entities:
K5 =532
2663 . aie
;4 Code: It is known as the text part of a program that does not change at runtime.
: Boundary Folding ia r memory requirements are known at the compile time.
' f sub keys are reversed to generate new simple haa _ Procedures : Their text part is static but they are called in a random manner.
In this some 0 erase | | ashy Chat is why, stack storage is used to manage procedure calls and activations.
K = 324 987 626 19. 4 | +s K1, K2, K3, K4, K5 a Variables : Variables are known at the runtime only, unless they are global or
Divide this key value in to par » Ha, Th, DX) a cn rece memory allocation scheme is used for managing allocation and de-
K1 = 327 | _ allocation of memory for variables in runtime.
K2 = 789 a | ‘aa Static Allocation in this allocation scheme, the compilation data is bound to a fixed
K3 = 626 | _ \ocation in the memory and it does not change when the program executes. As the
KA = 191 ) | 3 | < WS _ memory requirement and storage locations are known in advance, runtime support
| | ‘i package for memory allocation and de-allocation is not required. Stack Allocation
K5 = 253 » | _ Procedure calls and their activations are managed by means of stack memory allocation.
2168 . __ It works in last-in-first-out LIFO method and this allocation strategy is very useful for
4. Trees: | Oa J ecursive procedure calls.
1. Symbol table can also be stored in form of binary search tree. ‘3 ‘Heap Allocation Variables local to a procedure are allocated and de-allocated only
2. Each node can have atmost two children-left and right child. . i eimat ee nooP aon is ae to dynamically allocate memory to the eae
se - am, , and claim it back when the variables are no more required. Except statically allocate
aaail septa 1 papery ai e ee than key value stored in lem Of chime emory area, both stack and heap memory can grow and shrink dynamically and
£ Heit of binary search ive: is Shine ORIG toga , a a Brcedly Therefore, they cannot be provided with a fixed amount of memory in the

5. Maximum time required for searching and inserting is proportional to On _ The text part of the code is allocated a fixed amount of memory. Stack and heap
Algorithm: ? | _ memory are arranged at the extremes of total memory allocated to the program. Both
(1) Initially, ptr will point to root of tree. : | __ shrink and grow against each other. |
(2) While ptr = Null do | : a Error Handling Techniques: A parser should be able to detect and report any
(3) If Name = Name(ptr) i ot '_ error in the program. It is expected that when an error is encountered, the parser
then return true | - should be able:to handle it and carry on parsing the rest of the input. Mostly it is
(4) else if Name < Name(pt ) th | | _._ expected from the parser to check for errors but errors may be encountered at various
ptr) then | stages of the compilation process. A program may have the following kinds of errors
Ptr:= Left(ptr) | | at various stages:
(5) else ptr : = Right(ptr) i ¢ Lexical : Name of some identifier typed incorrectly.
(6) end of loop. | 5 ¢ Syntactical : Missing semicolon or unbalanced parenthesis.
Q7. What is activat
- i: on Record? Brie ¢ Semantical : Incompatible value assignment.
‘erent error handling tec: ¢ Logical : Code not reachable, infinite loop. |
eee A program 18 a Sequence of Instructions ec = Sclnlaues, _ There are four common error-recovery strategies that can be implemented in the
aad Se a ey are executed sequentially. 4 into nual of procell parser to deal with errors in the code. |
‘dontifi hg inside it is calle Procedure has a startal = Panic mode s
psec ape the sequence of finite usa of the procedure. The a Whena parser encounters an error anywhere in the statement, it ignores the rest of
Side it make up the bo dys! the statement by not processing input from erroneous input to delimiter, such as semi-
aces This is the e
a
veloping infinit siest w ay of error-»recovery and I.P. University-[B. Tech]-AB Publisher
e loops. also, it prevents the Pars
Statement mode
er f ro

L \, ~

eee
id id

ASTs are important data structures in a compiler with least unnecessary


information. ASTs are more compact than a parse tree and can be easily used by a
Abstract Synta compiler.
x Trees
more Padet
rseailtr
s eethare.pact
resani
entty
atio NS are not easy to UNIT-IV
be parsed by the compil|er, as they contain Q.8. What are the criteria that need to be considered while applying the
code optimization technique? Mention the issues involved in designing of code
generation. |
Ans. Optimization is a program transformation technique, which tries to improve
the code by making it consume less resources (i.e. CPU, Memory) and deliver high
speed.
In optimization, high-level general programming constructs are replaced by very
efficient low-level programming codes. A code optimizing process must follow the three .
rules given below:
e The output code must not, in any way, change the meaning of the program.
° Optimization should increase the speed of the program and if possible, the program |
should demand less number of resources. A
¢ Optimization should itself be fast and should not delay the overall compiling
process.
Efforts for an optimized code can be made at various levels of compiling the process.
e At the beginning, users can change/rearrange the code or use better algorithms to
write the code.
e After generating intermediate code, the compiler can modify the intermediate
code by address calculations and improving loops. SG
¢ While producing the target machine code, the compiler can make use of memory a
hierarchy and CPU registers. r
Optimization can be categorized broadly into two types : machine independent and
machine dependent.
Machine-independent Optimization
- In this optimization, the compiler takes in the intermediate code and transforms a
part of the code that does not involve any CPU registers and/or absolute memory
locations. For example:
do
il
m ies 2Ot 1 mngeyey <

item =10;
value = value + item;
} while(value<100); -
a=

code invol ves repe ated assi gnme nt of the ident ifier item, which if we p ut this
This
way:

: FES ys " Baie =a ' a E SE oy os = =


36-2016 Sixth Semester, Compiler Design
Item=10; | } LP. Uni ver sit y-[ B. Tec h]- AB Pub lis her 2016-37
c
do cms s sly exw bea
on
; n important role in i iden tifying! variablestl cu ke
, whic d ic ae g
Basic BT ee a a sont basic block. If any a aaa a boas bevel an
value = value + item;
le e, she ’ re gis ter me mo ry all oca ted to that variable ne
the
}while(value<1 QO);
Block finishes execution.
should not only save the CPU cycles, but can be Control Flow Graph
used on any processor. d by means of contro | flow graphs. A
Machine-dependent Optimization ‘ ; D’ . am can be represented
d among the blocks.
Machine-dependent optimization is don Bee ava h depicts how the program control toate ae unwanted todbe ti
e after the tar get code has been gener. Sarat fak that helps in optimization by help loca 7
and when the code is transformed pone
It is a use
accor ding to the target machine archit

x
involves CPU registers and may ha ect u nates the program.
ve absolute memo ry references rather
references. Machine-dependent o than reli
ptimizers put efforts to take maximum
memory hierarchy. advant ENTER
Age Of
Basic Blocks w = 0;
|
Source codes generally have a nu X=X+Y;
mber of inst. ructions, which
sequence and are considered as the are always executed .

nomena
B1

ae te
basic blocks of the code. These
have any jump statements among basic b] IN
them, i.e., wh en the first instru
the instructions in the same basic ction is e x€cuted,
block will be e xecuted in their
without losing the flow control of sequ
ence of aPP
the program. earang,
A program can have various

: /
constructs a S basic blocks,
SWITCH-CASE conditiona 1 statemen like IF-THEN.-
ts and ] Oops such as
REPE AT-UNTIL, etc. DO-WHIL E, F OR, aand }
; B3 A
Basic block identification
?

y =z; “
We may use the following algorith rt OR |
m to fin d the basic blocks
* Search header statements in a program: |
of allthe bas; c blocks from wher _B4 =
¢ First statement of a program. e a basic block starts. EXIT
Ww = x ,

Statements that are target of any. Graph


branch (conditional/unc Basic Blocks oe
¢ Statements that follow any bran onditional).
c h statement. Loop Optimization
* Header statements and the statem imize the
necessary to optimize t
ent
run loop in the system. It becomes nect™. by the following
| in 0 ee aane CPU eyelet and memory. Loops can he optimized by
loopsMee
point of view. m both code generation and techniques.
optimization

w = 0; ther than ; each itera


with iteration.
‘ng it to be computed only once, ra - gen emr es . ois
w=0: is called’an induction variable if its valu
X=x+y-; aee ES
inauc ak Mee wn ace
X=X+y; 5 : :
y =.0;
=f}:
that consume more CPU cycles,
a 0;
of (x > 2) altered sini enction perinmigamediata
time, me and memory. These expressions should n.be replaced
with: cheaper expres ions
if (x > z)
: There are ; expressions

{ an 1S] Fo output of expressi0


without coma a ced than (x << 1) and yields the same resul t.
y=x expe a ¥ .

-code Elimination | ea aes


xX ++-
statements, which
; YX; pe code is one or more than one code
X++°
e,
e Either never executed or unreachabl oe
else 1s ne ve r us ed . | . <a
cuted, thei r
i ou tp ut i
no rol e in an y pr og ra m op er at io n and the
{ esa code plays
Ze
y=2Z,;: _ be eliminated.
yFz
Z ++:
ee ee be

} ums
3 2 *)
|

W=xt Zz;
W>=X +2°'
Source Code
Basic Blocks
38-2016 Sixth Semester, Compiler Design
never used inside the loop.Immediately after the . - P,
;
‘a’ ig sagt 2016-39
control leaves the loo : , h}+AB Publisher
the value of nt
variable ‘z’, which would be used later in the 1.P. University-[B. Tech}-
the assignme code of ‘a’ is never used anywhere, curiae ) ble noe heres he olimixiated by using &/éode-
therefore it is e igibl e to be elimina’ , -ant code is partially redundant and can y
Loop-invarian v i
is.
. | | ‘ mo tion
Anottech
herniqu e. ple |of a partially
exam i
( J y
| ine redu
dundant code can be:
| If(condition) Yj
Ze
{
inal
a=x*y;
Oca
a
oe
(
,
= 2,
seas from assignment
Likewise, the picture above depicts that the cond carr e that the values of operands ee
implying that the code, written in true itional statement is always f, ge aii ake variable c. Here, s oe
cathindat dchan
is ged
true, then Tee
case, will never be executed, hence ad can be used to eliminate this re
removed. it ¢ he Saat d twice, otherwise once. Code
m
as shown below:
rt | If(condition)
a=1;
b=10 {

. : :
ifa>b |
a =tmp;
: 3 zy
Dead Code este c's oer | a
— 7 — - a ” tm =y p
OP z;
| : | os }
| a RN tad be computeted only once.
| _ Here, whether the con dition and
is true or false; y OP ase pees +(c-d) *(a* b)
Partial Redundancy: | DAG for the expression
©
| -—-s« @.9.
ob.
Dra|w the syntax tree | ¥
Redundant expressions are comp
change in operands.whereas
uted more than once in para
partial-r edundant expression
llel path, without any
| 3 7
Ans. Syntax tree: —

eetisscl
s ar € computed more than
once in a path, without any change in operands. For example, ~

+
| e oe
a=yOPz
|

a
a
man

Co
w

[Redundant expression)
A
fr

2S Se
i ee i a
eo =f fe
M Y
Sixth Semester, Compiler
. .
Design
§

40-2016
DAG: cath FIRST TERM EXAMINATION [FEB.
2017]
SIXTH SEMESTER [B.TEC
tQ=c*d H]
COMPILER DESIGN [ETC
t= +b S-302]
ts = tl
* t2 i ¢1.5 Hrs,
t6 = td +tl
= . MLM. : 30
_ Note: Attempt three questions.
t7 = té+t3 marks _ Question No. 1. is compulsory. Each question carries 10

sabato
+(t7) Q.1. (a) Mention the role of Lexical
Analyzer in compiler Design. (2)
Ans. The main task is to read the in put cha
racters and produce as output sequence
of tokens that the parser uses for Syntax analysis.
” :

3.
Tokens
Source Lexical
rogram”| Analyzer |__ Parser
Get next Token

figs (t2)
Fig. Role of the lexical analyzer diagram
Symbol
Table

Up on receiving a “get next token” command from the parser, the lexical analyzer
reads input characters until it can identify the next token.
Its secondary tasks are:
¢ One task is stripping out from the source program comments and white space is
in the form of blank, tab, new line characters. |
e Another task is correlating error messages from the compiler with the source
program. .
Sometimes lexical analyzer is divided in to cascade of two phases.
(1) Scanning (2) Lexical analysis.
The scanner is responsible for doing simple tasks, while the lexical analyzer proper
does the more complex operations.
Q.1. (b) Explain Chomsky classification of languages. (2)
Ans. According to Noam Chomosky, there are four types of grammars “ Type 0,
Type 1, Type 2, and Type 3. The following table shows how they differ from each other —
Grammar Grammar Language Automaton
Type i Accepted _Accepted

1.TypeO | Unrestricted grammar Recursivelyenumerable | Turing '


aac language Machine
2.Typel | Context-sensitive Context-sensitive Linear-bounded
grammar language automaton
3.Type2 | Context-free grammar Context-freelanguage - | Pushdown
automaton
4, Type 3 Regular grammar Regular language Finite state
No automaton
~
2 — Ole
Si xth Se
~
mester, Compiler
Design
at the followine ;
Ing illustration, Tt Shows the =
scope of each type of sramn, I.P. University-[B.Tech.|~AB Publisher
a
The strings a and B may be empty, but y must be non-
empty.
_ The rule S > ¢ is allowed if 8 does not appear on
7 Recursirvely the right side of an
Enumerable languages generated by these grammars are recogniz
ed by a linear bounded lah The
Example Bea
,
AB — AbBc
Context-Sensitive
A—> beA
B->b_..
Context-Free Type - 0 Grammar
Type-0 grammars generate recursively enumerable languages. The productions an
have no restrictions. They are any phase structure grammar including all forma]
Regular ‘grammars.
- They generate the languages that are recognized by a Turing machine.
Type -3
The productions can be in the form of a > B where a is a string of terminals and
Grammar
nonterminals with at least one non-terminal and a cannot be null. B is a string of
angi Type e-3 ate gram mars generate regular terminals and non-terminals.
languages. Type-3 grammars must
terminal on the left-hand side have
termin : ‘ and a right-hand side consisting Example
, al or single terminal followed by a single of a Single
non-terminal. S —> ACaB
Bc > acB
CB—> DB
and a € T (Terminal) aD > Db
erule S—¢ is allowedifS does
Th
.

not appear on the right side of an


.
Q:1. (c) What are the three methods to construct LR Parsing tables.Which
Example y rule.
method is the most powerful? Justify. (2)
Xe Ans. There are three widely used algorithms available for constructing an LR
X+>al ay parser:
Y>b ° SLR(1) - Simple LR
Type - 2 Grammar -o Works on smallest class of grammar. |
Type-2 erammars ge o Few number of states, hence very small table.
nerate context-free langua
ges. | o Simple and fast construction.
The productions must
be in the form A -> Y ¢ LR( 1) - LR parser
where A « N (Non te
rminal) o Also called as Canonical LR parser.
and y e (TUN)* (Str
ing of terminals and no o Works on complete set-of LR(1) Grammar.
n-terminals),
deterministic pushdow o Generates large table and large number of states.
n automaton. -
Example o Slow construction. —
Ss ~Xa e LALR() - Look ahead LR parser
X+a
o Works on intermediate size of grammar.
: o Number of states are same as in SLR()).
XK > aX
X — abc Q.1. (d) An ambiguous grammar can never be LR .Justify the statement
with an example. (2)
Xe
Ans: An ambiguous grammar can never be LR(k) for any k, because LR(k) algorithm
Type - 1 Grammar aren’t designed to handle ambiguous grammars. It would get stuck intoundecidability
Typ
’ er problem, if employed upon an ambiguous grammar, no matter how large the constant k
meee. be in the form is. | : |
|

Q.1. (e) Design the NFA for the regular Expression a (a+b)* bb (2)
Ans. py ala + 6) * bb qi

Io & F2(a+b) * 93694 bq


» 4-2017 |
cs | Sixth Semester, Compiler Design
. a
be
c+
I.P. University—[B.Tech.|~AB Publisher 2017- 3
& Q0 <—eme, b t
” 22 Ys 93 7-4 O6>q | | Q.2. (b) Construct predictive parsing table for the following grammar( S is

| be CFG: ) S> iCtSS’la


S>bBlaA | o> Sle
¥ | | | Cb
: > = Behaie | Ans. Step 1. Eliminate left-Recursion and Left Factor the grammer if required.
For th . >ala S As, there is no left. Recursion and grammar is already left Factored.
G@) bon String bbaababa find (5), Step 2. Computation of FIRST
fas) a Most Derivative (i) S+iCtSS’la
= RightMosi Derivative .
(iii) Parse Tree Rs |, (ii) syed (8) = Heal
S’ > eSle
Ans. (i)(i Left Most Derivative
eae : Si — bB | | a FIRST (S’) = -{e.s)
| (iii) Sigh a sh
bbBB a PPR 3 FIRST (C) = {b}
bbaSB | 7 |
ead , FIRST (S) = {i, a)
bbaabBB 4 | FIRST (S')= {e, ¢}|
bbaabaSB - FIRST (C) = {b}
EN: bbaababa | of FOLLOW:
Step 3. Computation
(ii) RightMost Derivative: S°3°hR 49 _. Applying Rule (1) of FOLLOW |
bbBB = a is
bbBa — | . = © = {$}

a
bbaaAs ) Pe a| : lyingae
AApp Rule € (2) of(4/0 FOLLOW hae
bbaabSa | e | S>]i]c]¢ss’

Dk,
- bbaabaAa .

"
A>ja|B} B

Sa
ennai
bbaababa : ee a
_ >, FIRST (8) = FIRST (t SS’) = {t}.

AN
(iii) Parse tree b

ee
_.. FIRST (B) does not contain e. !
».By Rule (2a) FOLLOW (C) = (FIRST (t SS)

ae
al ee
B
Again, Apply Rule (2) on different combination
———

S>|ict|S|S'
i

Sale

>

| | oxi 7 | stAcel oe TET ete: |


“.

y X | | ‘ya 4 3 FIRST (B) = FIRST (S’) ={e, ¢} containe.


% nn By Rule (2b) FOLLOWS (S) = FIRST (S)- {e} v FOLLOW (S)
_ FOLLOW (S) = {e,¢}— U FO-{ S (S)
LLOWe}
| i \ | _
[FOLLOW (S)= (e) U FOLLOW (S)|
vy \ | — Applying Rule (3) of Follow
[s=ices|s
“TAS a [2
Ais SaviCts B=
‘ ’ ; ' us . 2 —— ieee z a

6—2017 Sixth 1 S§ emester, Com


pil: er Design

FOLLOW (8) = (FOLLOW (S) [.P. Univérsity-(B.Tech.|~AB Publisher 2017-7


(2)
S' > eS A = S’,a=e5
Rule (2) can not be applied on th; x FIRST (a) = FIRST (eS) = le}
Applying Rule (8) of FOLLOW 1s Production. Add S’ + eS toM(S’,, 4)

Write S' — é§ in front of Row (S’), column (e)


sp fe] s
(4) S°’ oe
A-|a/|B Comparing it with A+>a@

FOLLOW (S) = (FOLLOW (S’) S' +>\|s

The Renaming Production S —5 a, S’ A >la


« andC > bdo not match with an Rule
Combining Statements (1) to (5) A = S,a@=e
| - FIRST (a) = FIRST (ce) ={e}
FOLLOWS (S) = [$} © Asplying Rule (2) of Predictive Parsing Table. i.e. If ¢ is in FIRST (a)
FOLLOWS (CG) = {t} | Then FOLLOW (S’?)= {e, $}
FOLLOW (S) = {e} U FOLLOW.(S) ». Add S’ > eto M([S’;e] MIS,’ $]
FOLLOW (S’) = {FOLLOW(S)} — write S' —>s in front of Row (S') Column {(e, $)}
FOLLOW (S) = {FOLLOW (S’)}
(5) ©. =:
~. FOLLOW (S) = FOLLOW (S)=($; e} Comparing it with A-a
FOLLOW (C) = {t) C->|6
Step 4: Construction of Predictive Parsing Table. Put all Non- terminals $0 A-|a
Row wise and All terminals. i, a, b, e, $, t Column wise First (a)
| :
(1) S:> 2C ss’ | ae « Add C > b'to M[C, b]
ComparingS > iCtSS’withA->a - | - lwrite C — b in front of Row (C), Column (b)
S>/ictSS'| -. Combining statements (1) to (5) we get following Predictive Parsing table.
3 Se RUS pe ees e i t $
A- a
S S-a S39iCtsSS’
FIRST (a)= FIRST (i C tS S’)= fi} S’ | S’> eS Se
Applying Rule (1) of predictive Parsing Table: - Ss’
Add S — iCtSS’ to M[S, i] C Cb
This Grammer is not LL (1) because In Row (S’) and column (e) we have multiple
Write S— iCtSS' in front of Row (S) and Column (i) Entries for productions Ss’ > eS
Se
2S oa
S’ > eS appears as FIRST (S’) = {e}
Comparing it with A-a
S’ > « appears as FOLLOW (S’) = {e}
S >la This Grammer is not LL (1) Grammer.
Q. 3. (a) Construct SLR parsing Parsing table for the ees grammar:
Av~l/lal.

A = Sa=a s > xAy lxBy |xAz


FIRST (a) = FIRST (a) = A- aSlq
., Put S ~ a onto M[S, a] ie -Boqg
eee Step 1: Construct augmented Grammar
Write S — a in front of Row (s), colu OS > 5
mn (a) |
(3) $’ + eS Seas (1)S > xAy
Compairing it withA > « ~ (2)S — xBy
(3)S > xAz
S'—+|eS (4)A > aS
A>|la (5)BoOq
EX Perlence
thy excelle Ez
S~2017
Sixth Semester, Compiler Design
Step 2. Find Closur
Ne e & gobo functions to construct
W States and Circle LR (0) items. Here Boxes EP Tesg,
s repres ent the repeating state. IP. University~(B. Tech ~AB Pull
Closure (S’ : —
« &)
lio. . FOLLOW(B) = ly}
Rule (3) cannot be applied.
| yA
| so =< aS 3.S> xAy
i. | SsexAy lL, = goto (I5,B) Applying Rule (2a) of Follow
| Ss exBy S3+xBey
First (B) = (2)
8 ep ae :. FOLLOW(A)=(z)
Rule (3) cannot be applied.
4.A>qS
I5: goto (I9,q) Rule (2a) cannot be applied. As A> qS cannot be compared with A+ a B 8
As+geS Appling Rule (3)
| ia = goto ,.S) Age Comparing
A— qS with A-> af
H Ss >Se i Bo qe A = A,a=q,B=S
S>exAy .. FOLLOW (S) = {FOLLOW (A)}
S—exBy Rule (2a) and Rule (3) of FOLLOW cannot be applied on production A-> q and B >
S—>exAz q. Combining statements 1 to 5
I. = goto (Io, x)
B— xe Ay FOLLOW (A) = ly, Zz}

FOLLOW (s) = {$, y, z}


S-> xe Az I, =goto (I;,y) Step 4: Construction of SLR (1) Parsing Table:
Ij A> «qs I; | SoxAye
Action goto
: oa States |
> eg x y z q $ A B
0 s2
43) = eels, A)
5 SaxX I, = t (I 1 accept
AY LS EOWO gt) 9
SH~XAZ
s5 3 4
Sees
3 s6 s7
Step 3. Computation of FOLLOW
4 58
1.S—+xAy
5 s2 rd
Follow (S) = {$} 6 rl rl rl r
Applying Rules (2a) of FOLLOW
(Comparing SxaywithA+oBBp 7 r3 r3 3 7
8 r2 72 r2 =
“. FIRST (B) = FIRST (y)= {y} 9 r4 4 |
‘. FOLLOW (A) = [y) guity of a CFG.How Ambiguity can be removed from
Q.3. (b) Discuss ambiwoe
Rule| (3) cannot be applied. | the grammar ? State suitable example. ee (5)
:
Ans. If a context free grammar G has more than one derivation tree for string
|
As, S > xAy cannot be compared withA + o B we L(G), itis called an ambiguous grammar. There exist multiple right-most or left-
2,6 % xBY é most derivations for some string generated from that grammar.
Applying Rule (2a) of FOLLOW
Problem Ss
7. caret 5 > x By withA+oR B To check whether the grammar G with production rules
(B) = {y} xX > X+X | X*X |X*| ais ambiguous or not.
8-2017
Sixth Semester, Compiler Design
Step 2. Fing
osure & Cl
New ns States and Nias Soto functions to construct
Circles represent the repeating state.LR (0) items. Here Ro X€S Tepe
Prey I.P. University-/p Ae a
uae adneas.| wy A I
ie :. FOLLOW(B= )(y1
Rule (3) cannot be applied,
S* 3 6
3.S>xAy
nl S seay I, =goto (I5,B)
I Ips Soto GN Applying Rule (2a) of Follow
S.s28 5 I, S+xBey
8 S>sae First (B) = {z)
Sex aa
a “. FOLLOW(A) ={z}
Rule (3) cannot be applied.
4.A—qS
I;: goto (19,4) : q Rule (2a) cannot be applied. As A-> qS cannot be compar
A>geS ed withA+a Bg
Appling Rule (3) _
; I; = goto (Ip,S) | A-q®*
pi es ComparinA—
g qS with A> af
1
he Aacahes
S' ° 5
=e So exAy
, .. FOLLOW (S) = {FOLLOW (A)}
b S—>exBy Rule (2a) and Rule (3) of FOLLOW cannot be applied on prod
S>exAz
:
~ q. Combining statements I to 5 uc
A->ti
q and
on B -5
I, goto (Io, x) | FOLLOW (A) =, 21
S—> xe Ay FOLLOW (s) = ($, Y> zi

: S—> xe By FOLLW (8) = fy}


NS > wig a Ig = goto (I;,y)
Step 4: Construction of ae (1) Parsing
Table: |
LL] A> e aS I, | SoxAye
Action goto -
Avtegq
States | x y a a
B~ eq $ Pe su
| 10 s2 Gr)
Iz = goto(I,,
A) ‘
I, =goto 1
1) sox ay L, : a. (Is,
_ ,
accep
S»XAZ s5 3 4
Step 3. Computation of FOLLOW
3 36 af
1S—xAy 4 ; 58
5 52 —| 9
r5 -
[Follow (S) = {$} 6 rl rl
: rl ~ |
Applying Rules (2a) of FOLLOW a Le sa 38 —- +
(Comparing Sx ay withA— «B +2 12
p r2 | |
“, FIRST (8) = FIRST (vy) : 4 r4
9
=) Q.3. (b) Discuss Lace ambiguity ofa are Ambiguitre y can be remov ed from=
. FOLLOW (A) = by)
Rule (3) cannot be applied " a the gr amma textet fre
Ans. If ra ?conSta suite abgrleamma
exramplG e.has
As, 8 xAy cannot be compared ith ivat.
more than one Se ion treSee for astreing
2,8 +xBYy : Wwe L(G), itis called an ambiguous grammar. The
with A> o B
most derivations for some string generated from re — vor
Applying Rule (2a) of FOL that gre
Problem |
oe - wParing 8 + x By withA aBp To check whether the grammar G with production
X > X+X | X*X |X*1 ais ambiguous or not.
10-2017
Sixth Semester, Compiler Design
s atution
et's ff |
deri :
string “ata*a’.

It ha S two ley bpd
L.P. University-{B.Tech.}-AB Publisher 2017 —
deriv :
ations
erkceis
nd out the
erivation tree for the e string
—11
5 on 1] — L
Xs X+X-5 Notation: =F represents a compiler for Source S, Target T
Pp a+X-5 x ’
arse tree ] _ a+ X*X- a+a*X > a+a*a implemented in J. The T-diagram shown above is also used to
‘depict the same compiler.
To create a new language, L, for machine A:

1.Create ° CG , a compiler for a subset, S, of the desired language, L using|;

A, which runs on machine A. (Language A may be assembly language.)


2.Create C4 , a compiler for language L written in a subset of L.

: 3.Compile L Ce using S CG to obtain / Cc , a compiler for language L, which runs

_ on machine A and produces code for machine A.


hcg 3° ch >" Ch
L a | bene!

—|sfs a]
3 “ || | | A
Derivation 2- |
bootst r apping and
x oy xX *X ® — X+X*xX T-di i
. by| can be
—_ | | called
e+» a+ X*X eX * | . The process illustrated the -diagrams 18
*—ata >atara
Parse tree 2 — | 1 arized by the equation:
{3
Lg A+LS i =a A &

To produce a compiler for a different machine B:


4 Cc? (by han d, if nece ssar y). Reca ll that lan gua ge S is a subset
1.Convert 4 Cc into
of language L.
pro duc e L Cr acr oss -co mpi ler for L whi ch runs on machine A
2.Compile L CG to
and produces code for machine B. |

cro ss- com pil er to pro duc e L Cc , a comp iler for language
| 3.Compile 4 Cc with the
L which runs on machine B.

doesn’t matter w
|
Q.4.Write short notes on |
( a) Bootstrappining and C ut ab le : -
mp il er ca pa bl e of creati ng ex ec
a | Cross Co mp il er is a co @ co mp il er th at runs
mp il er is running. Fo r ex am pl e,
th e on e on wh ic h th e co
1. Source r i characte
nee eLanguage | ‘than : } tru ‘Android smartp -tforms is a cToss
hone from ayer.e.
one machin
2. Target Language a Windows 7 PC but generates code that runs on
p ‘crocontroller of
A cross compiler is necessary to compile for multiple
3. Implementation Language ld be inf eas ibl e for a com pil er to run on, ane © = a cea liiig sys tem.
A platform cou
bec aus e tho se sys tem s con tai n = P
an embedded system
12-2017 si xth Semester,
Si e r Design
Compil‘e
s y s t e m s , a n d a Cross [.P. University-[B Tech. |-AB Publ
, runs many ;o;peratin g isher
In paravittualisationone machine 2017-13
o m on e main source.
could generate an executable for each of them Q.A. (c) Handle and Handle Prunning (2.5)
Cross compilers are not to be confuse Ans: Handles:
compiler is for cross-platform software Se ain language to another jp 9 Ahandle ofa string is a substring that ma
source compiler translates from one program a 4 whose reduction to the nonterminal on the lefttcside
hes the right side of a production,
of the production represents one
Both are programming tools. ong the reverse of a rightmost derivation.
Uses of cross compilers
th e build environmen Precise definition of a handle:
cross com pe ris to se pa ra te for m > is a pro duc tio n A —» Sil Aiatilon at
The fundament al use ofa ee ‘ A handle of a right- sen ten tia l
be r of si tu at io ns :
target environment. This is useful in 4 Te SO Ur Ge y . W here the string B may be found and replaced by A to produce the previous right-
s extrem el y li mi te d a e :
¢ Embedded computers where 2 satan ha ely small computer to read itstoun' sentential a ot
in a rightmost derivation of y.
example, a microwave oven will have an e3 sore then A > 6 in the position following « is a handle of
display and speaker, and to Contr) . i.e., 1fS w . ids Corea neds aSw.
and door sensor.
sor, provide output to a an . ek be powerful enough
and to run 8 Co
testing ny)4ly
tired 9 The string w to the right of the handle contains only terminal symbols.
food. This compu gine debugging
machinery for cooking i
is a right sentential
t e n , is A—>b at
form w hose handle
environment. ample above, abbede
a file system, or a development ded system, cross-com,:) *
han a TT eae aAbede is a right sentential form whose handle is A — Abe at
require more resources than are available on an embed tive compilation.
il ti mpl
ror s than na
osi 10 os 3

can be less involved and less prone to er 3 : position 2. é


For Se : ae wis
bet deany a amay a s SUD
* Compiling for multiple machines. ) « Handle Pruning:
Ta
several different versions of an operating system OF 10 © A rightmost derivation in reverse can be obtained by handle pruning.
gle b
systems. By using a cross compiler, a sin OE with a .
string of termi ;
nals w that is to parse. lfw is: a sentence of the
ihe = —y,, where >y, is the nth right sentential form of some as
for each of these targets. mu lt ip le machines pW
far m. Si mi la r to co mp il in g fo r grammar a
e Compiling on a ser ver gh tm os t de rivation.
execut ed acro g, yet k n o w n ri
complicated build that involves many compile operations can be
e u n
les s of its und erl yin g Pe or the ope rat ing 5 te, 5 = Yo V1 Yo -<-Yaa fn =™
machine that is free, regard d ha nd le for grammar
Example for rig ht se nt en ti al fo rm an
ee a a. is a new platform. When developing software for a new platform
EOE+E .
or the saailalor of : future platform, one uses a Cross compiler to compile neces7 say
EOE*E
tools such as the operating syst em and a nati ve comp iler . a
¢ Compiling native code for emulators for older spaeeuare 8 orms like by E> (E) —
Commodore 64 or Apple II by enthusiasts who use crosserscomp ulers that run on a curre, E- id
ss com pil running under Wi nd ow s iP, i o n Pr od uctio |
platform (su ch as Aztec C’s MS -D OS 650 2 cro H a n d l e R e d u c t
Right Sentental 7
;
Use of virtual i (such as Java ’s 8 JVM)
machines JV reso lves some me¢ of the reasonsfyf Form ee
which cross compilers were developed. The virtual machine paradigm allows the s alle
.
ad = 2
compiler output to be used across multiple target systems, although this is not alway,ue idl + id2 * id3
id 2 :
ideal because virtual machines are often slower and the compiled program can only E + id2 * id3
run on computers with that virtual machine. | 7 id 3 E->1i
E * id 3
Q.4. (b) Difference between compiler and interpreter 5) E * E E>E*E
= E |
Ans. E+E BE o O E + E
: e
Interpreter 3 Compiler
t y e s (2.5)
1. Translates program one statement LEX
-
scanners
bd or
at a time. it as a whole into machine code. ge ne ra te s le xi
.
ca l an al yz er s (“
a] ra m that
|

p a n yLe x
;
is a R E
co mp ut er pr og
2. Ittakes less amount of time to analyze | Ans:
| It takes large amount of time to ana y2e| sriginally
the source code but the overall execution
the source code but the overall execution| “lexers”). written by Mike
time is slower.
time is comparatively faster. Lex is commonly used with cribed in 19% 5, is thestandard lexical
8. No intermediate object code is generated, ‘@ ic Schmidt and des : anaPOSIX
of the yor standard.
Generates intermediate object code which) partzer
as aly
hence are memory efficient.
further requires linking, hence requires| | an — and an equivalent oul tool is specified
lexical an
and outputs source
| ng the |
¢
more memory. Lex reads an input stre am specifyi
Continues translating the progra
m until It generates the error messag code implementing the lexer in

wns Onan enon Om PPO


the first error is met, in which e only oO
case it O pe source
n | .me versions of Lex are
‘Scanning i
debugging isthecompwhole program. Hente|
stops. Hence debugging is s proprietary software, Som AT&T code are now
5, Programming languag
easy,
ar atively hard. iginally as ted
distribu Propric’*' athe original open
e like Python,
_

_ Ruby use interpreters Programming lan i C++ use| - now open source. Open source
, a Rae ike ee ( ystems
compilers. - distributed as open source§
CJ o m p ! il er Design
r ,
Sixth Semeste
. zer”, is
source version of Lex, called flex, or the fast lexical analy Not dey \
Propnetary coding. 9 D TERM XA
Structure of a Lex file ‘nally similar to that of a yaoe fi | ay . MINATION (MAY-JUNE 2017
The structure of a Lex file is intentionaby © i ly t le; ofy SIXTH SEMEST ER ;
Ivideded
divid into ththree tions, separated by i
follows
into sections, separa ed b lines that contain only two Percen; fy
:
ny_ COMPILER DESIGN (ETCS-302
(B. TECH]

The definition section defines macros aed, erift he saniad


* a WTitten qime : 3 Hrs. |
It is also Poss
possible to write any C code here, whieh |" ™ atin ing Note: : Attempt t any five questions include Q.
eenerated source file. ; ; § ach unit.
* The rules section associates regular SRP ee Fe f eo i“ Staten, 7 6
When the lexer sees text in the input matching a given pattern, it wall exegy Q.1. Att Attempt t any fi live parts:
associated C code. a
* The C code section contain s C statements and func: tions that are COpieg example.
generated source file. These statements presumably contain code called by the walt Ans. Refer Q. No. 4 (a) First Term Examination
20 17
the rules section. In large programs it is more convenient to place this code inaew”
Q.1. (b) Differentiate between SDD and spT with Example s)
file linked in at compile time.
Example of a Lex file P Ans. SYNTAX-DIRECTED TRANSLATION(SD1) A formalism for epecifying
ae z slations for programming lang uage constru
The following is an example Lex file for the flex version of Lex. It recognizes sh Sines location, ete) ee constructs. ( attributes of a construct: type,
of numbers (positive integers) in the input, and simply prints them out. eo
P e P | . ¢ Syntax directed definition(SDD
/***
mI Defi niti on secti on ***/ ) the translation of constructs
for
) =
‘a ¢ Syntax directed translation ;scheme(SDTS) for specifying translation Postfix
/* C code to be copied verbatim */ [notation for an expression &
ey entdin ds = | ae = ¢ If E is a variable or constant, then the postfix nation for E is B itself( Et =).
ae | a e if E is an expression of the form E1 op E2 where op is abi
y 2 ae oe flex . read only one input file * / i
ah | | ‘ fc ‘the postfix of E1, o E2' is the postfix of E2 ation El E2' op is dave: = B2
yywrap —_ ¢ if E is (E1), and El is a postfix then El’ is the postfix for E
on | |
; aa eg)9-54+2>95-2+9-(5+2)5952+-
/*** Rules section *** /
/* [0-9]+ matches a string of one or more digits * / Syntax-Directed Definition(SDD) for translation
[0-9]+ { | eSDDisa set of semantic rules predefined for each productions respectively for
/ Zi yytext isa string containing the matched text. * / 2 | rah a translation.
; rintf(“Saw an integer: %s\n”, yytext); | an ‘ | ¢ A translation is an input-output mapping procedure for translation of an input
l\n { /*
. | iSi: 2, 0 construct a parse tre tree for X. o synthesize attributes over the parse tree.
6% paper ab olber iRnraciees uct ne * Suppose a node n in parse tree is labeled by X and X.a denotes the value of
/*** C Code section ***/ | y= attribute a of X at that node.
int main(void) a * compute X’s attributesX.a using the semantic rules associated with X.
( a f. - @Q.1. (c) What is Left Recursion and Left Factoring ? Explain each -
/* Call the lexer, then quit, * / Example.
3 ~ Ans. Left Recursion E Pe
me |
A grammar is left recursive iff it contains a nonterminal A, such that A>+ Ac,
a _
fs sho
If this input is give Rasta . 3 bs - where is any string. | b
compiled into an eusiatconee ge converted into a C file, lex.yy.c. This cant Grammar {S > Sa | c} is left recursive because of S =Sa
given the input: _ ualfes and outputs strings of integers. For exami Grammar {$ -»Aa, , A> Sb Bs| ch is also left recursive becauseoftop=
abe123z, ! &*2g6 | — | build a predictive wn parser for
recursive, you cannot
the program will print; oeog >i
, Ifa grammar Is left > a ee
aw an integer: 123 , , + has no idea how many times 9 must
Saw an integer: 2 , é (1) Ifa parser is trying to match 3 —— ; is always possible to find another
Saw an integer: 6 a be applied (2) Given . = comet es ge and is not left recursive. 3) The resulting
| | ce a |
grammar angua for RDP.
that ge nerates the be suitable |
grammar might or might not
»* ——— tlt 4§ = Cc

| ..experience th tial

=o
:
eR

Sixth Semester, Compiler Design


; ;
29 1 ~

EMIS; if we need
ee »7 this, left factoring, it is not aiid for RDP.
; Se recursion: Special care/Harder than left recursion/SD7p can
5a ee
A- Ac/S d/e e As we don’t have;

soe R S,A
henaMme wo, as A,,A,
1? tively Aa Ib
respecaS
Step(1)
Step(1)

+ A,clAldle
Step(2) Substitute value A, of statement is 3 R.H.S of stateme At (2) (1) E El orM E2
A, > 44
,ad lbd le al (2) | El and M E2
ns A, ap A, clA
(3) | not El
Step 7 ae (4) | ( E1)
Again substitute A, = S,A,=A |G) 1 id1 relop ia2
3 S > Aalb | : a (6) | true
| ; A > AclAadIbdle ia (7) | false .
Step (4) It has now immediate left necursion J (8)M >
S—Aalb “+ _——~—CSSyrnttlheesiizeed@ attributes truellist and falselist of non
terminal E are used to generate
S->Aalb ___ jumping code for boolean expressions. Incomplete jumps with unfilled
A-bdA'eA’ labels are placed
A-<Ac|Aad|bdlje - __ on lists pointed to by E.truelist and E-falselist.
A-cA\lada'ie i . org (e) What is the Process of identifying basic blocks in code oe

Eliminating Left Recursion Let Gbe SSA | A Note that a top-dow. Ans. OPTIMIZATION OF BASIC BLOCKS
cannot parse the grammar G, regardless of the order the productions are tie ) There are two types of basic block optimizations. They are -
productions generate strings of form AA..A i * Structure-Preserving Transformations
= They can be replaced by S >A S’ andS >AS’le a | ¢ Algebraic Transformations
Left Factoring a { Structure-Preserving Transformations:
* If a grammar contains two productions of form S > aB and § The primary Structure-Preserving Transformation on basic blocks are:
ag
‘:
“7
3 is
its
“a.

suitable for top down parsing without backtracking. Troubles


+ &eas

of this form | ¢ Common sub-expression elimination


be removed from the grammar by a technique called the left factorin
oo ae
¢o

g * . Dead code elimination


* In the left factoring, we replace {S—> aa, S aB} by {S+ as’ Ss am ¢ Renaming of temporary variables
S — a(o.!B) (Hopefully a and p start with different symbols) "AY +
Interchange of two independent adjacent statements.
* left factoring for G{S—aSb 1c | ab}S— as’ | ccf. S(=aSb | ab | __. Common sub-expression elimination:

le)>aS’1cS’'>Sb!b | oa *.
uy

wea
t
N
o Common sub expressions need not be computed over and over again. Instead they
Q.1. (d) What is Back Patching? in wi | a ‘can be computed once and kept in store from where it’s referenced when encountered
Ans. BACKPATCHING ee mine with Femme. again —of aes providing the variable values in the expression still remain constant.
Rech nepape ei implement the syntax-directed definitions for
de ce Pritt 0 passes. First, construct.a syntax tree for the input, abe a: =b+ee
ce ect oe order, computing the translations. The main problem b: =a-d
——e ot lolean expressions and flow-of-contro] tate oe c: =b+c
"ing One single pass we may not know the
tenant d: =a-d
labels ofthbranch
ements are generated. Hence, a series at conting
rol mu
stast oments
gotoe The 2nd and 4th statements compute the same expression: b+e and a-d
Pecified is eenerated. Each statement will be pill Basic block can be transformed to
quent fil]; -
, : a 1) lists of labe
Creates ls, we1; as ocg
a new
e 's backpatching
10ns :
bh: = ad
a c=a
ee makelist returns a ations 8 only i, an index into the af dh |
- Merge(p1,p2) cone © ‘ist it has mad
inter to th atenates the |; r? aeDead
code elimination: | + of dead code may exis in the
t program.
ee? “Oncatenated list ‘S'S Pointed to by pi and p2, and It’s possible thatally
a larg e amo unt ie ca ce rc ce ma r and pro cedures asforgetsforge
part
_ This might be especi caused when to
of
program — once declared and defined, one
construction or error-correction of a

ale
18-2047
Sixth Semest
remove t ester, Compileree Design
hem ine ase they LP. Univer
the code. serve no Purpose. El sity_tp
iminating these will -Tech.|~AB
definitely Ptin: Publisher
aming of tempo
.

Bottom-up Poon
rary variables
_ * Astatement : As the name suggests, bottom-y
P parsing starts w:
WIS another t: =b +c wh er e tis a temporary name can be changed to u:=b ruct the parse tree up to the
tempor
ary name, and change all uses of t to
yo .) 4, co ns t start symbol.
* In this w u. 2 Example:
an transform a basic block to its equi Input string: a+b*c
block. is valent block called none tae
Interchange of two inde production rules:
pendent adjacent statemen gE pase
ts: |
Two statements
EoE+T
tl:=b+e
t2:=x4y E>E*T
can be interchanged or reordered in its com E>?
putation in the basic block when Vali T — id
of tl does not affect the v
alue of t2. Let us start bottom-up parsing
Algebraic Transfor
mations:
a+b = ¢
Algebraic identities represent another important class
of optimization
. s on bag) Ie Read the input and check if any production matches with the input:
blocks. This includes simplifying expressions or replacing expensive
operation by Cheane,
ones i.e. reduction in strength. | at+b*c
Another class of related optimizations is constant folding. Here Wwe ee Constan T+b*c
expressions at compile time and replace the constant expressions by their values, Thy E+b*c
the expression 2*3.14 would be replaced by 6.28.
E+T*c
he relational operators <=, >=, <, >, + and = sometimes generate unexpected OMMoy
E*c
sub expressions. °
Eft:
Associative laws may also be applied to expose common sub expressions, Ry
example, if the source code has the assignments E
a :=b+c e :=c+d+b S
: a SDT for converting
ing i infix expressi
, ion to post
post fix expression
by
a
Q.1. (g) Write
the following intermediate code may be generated:
taking suitable example.
a :=b+ct :=c+d e :=t+b— res sio n, the fol low ing alg ori thm Wil l give you the |
Ans. Ss. If you start with an inf ix exp
Example:
7 equivalent postfix expression.
x:=x+0 can be removed
ve ly.
ofE andF respectiive
x:=y**2 can be replaced by a cheaper statement x:=y*y e . s E’ FP’ op
E Oo p: F become > Ww here E’ an d F
F’ ar e th e po st fi x

The compiler writer should examine the language carefully to determine what (E) beco me s E’ , wh er e E’ is th e po st fi x of E.
rearrangements of computations are permitted, since computer arithmetic does not
-
1+2/3-4*5
ote

always obey the algebraic identities of mathematics. Thus, a compiler may evaluate — ue
t

1. Start with 1+2/3-4*5 —


t

x*y-x*z as x*(y-z) but it may not evaluate a+(b-c) as (a+b)-c.


,

ret
awa

st an da rd pr ec ed en ce ) to ooo ante =
Q.1. (f) Differentiate between top-down and bottom-up parsers with | 2. Parenthesize (u si ng
example. 3
\ 4

e ru le s to ca lc ul at e P{ (1 +( 2/ 3) )- (4 ; dh,
3.| Appl y y th the abov
Appl 3
Ans. Top-down Parsing the infix expression X to postfix”.
: Siam

A. P{(14(2/3))-(4*5)}
—B. P((14(2/3))} PU(4*5)} -
C. P{1+(2/3)} P{4*5} -
D. P{1} P(2/3} + P(4} Pt5} * -
E. 1 P(2} P(3}/+45*- ioe
F 123/+45*-
i
ira. ah

Sixth Semester, Compiler Design


LP. University—[B.Tech.|-AB Publisher 2017-21 Aa
20-2017 UNIT-I
Q.2. (b) Check Whether the following grammar is LL(1) or not
mmar given below:
n r
Q.2. (a) For the gra 4 >
;. S>A/a,A>a
E 3 oa ; ( ii. S—> aSA/e A> cle
T + ff . le Ans. acc to the rule:
T 5 * T (1) First(A) > First(a) = fal
le . (2) First(S) — First (e) =e
: F
AB): ID this grammar doesnot follow the rules that’s why it is not LL(1)
construct the LL(1) parsing tab] r (5)
= Q.3. (a) What do you mean by handle? Check whether the gramma
Ans. First() : E > E+T/T
FIRST(E) = { (, 1d} T > aor (id)is LR(O) or not
FIRST(E’) ={+ , ¢ }
FIRST(T) = {(, id} Ans. Handles: a pro duc . n,
tio
sid e of
o Ahandle of a string is a substring that matches the rightduction represents one
FIRST(T") = {*, ¢ } and whose reduction to the nonterminal on the left sid
pro
e of the
FIRST(F) = {(, id }
step along the reverse of a rightmost derivation.
Follow( ): x
° Precise definition of a handle:
FOLLOW E and a position of
> Ahandle of a marig y 1s a production Aduc—e 8 the
ht-sentential form lac previous right-
(E) ={$, )})}
”) where the string B y be found and rep ed by A to pro
FOLL OW (E = { $.
gentential form in a rightmost derivation ofy. eee
FOLLOW(T) = { +, $, )}
FOLLOW(T) ={+,$,)} e posconititaionnsfolCOlowT ing oS eSatm Tey
Eee) 9 i.eThe. ifSstringaAww to aSthe, rigthehtn Aof>the inhanthdle
FOLLOW(P) = {+. * ey In the ex am pl e abo ve, abb cde is a rig ht sen ten tia l for m who se han dle isA—> bat
Predictiv poss is a rig ht sen ten tia l for m who se handle is A— Abe at
: ssaition 2. Likewise, aAbcde
N on ae tenes
7 ve ented grammar is:
: se Wei
TERMINAL é Be 5 : position 2.002 Ser ms rE

KE’ 3 ER’ > + TE’ E> ¢ ia


T dt FT To FT _ \o
T , jd EE 3 Toe , E E>E- hift en
F F>id © F > (E) EE re Ee : \1 e . —Not LR (0)
o xt Ray ees © & “4)+-E Se +E a —_t +
E
2
Te
Reduced \
Stack implementation:

id +i+ d*id*ididS | abi Sa eel


$E
$E°T id $ E> TE’ ) See
id+id*id $ To FY The LR(0) parsing table would look like
$ETF 4 id 5 |
$E’T'id id+id*id $ F > id
tea > tr
Be +id*id$
+id*id$ Te i |
+id*id$ E’ > + TE’ .
id*id$ ie Not LR)
SR confli> ct “y) l go only in: 4. fo follow (E) | =$
ays
+ id$ Bu t for SU RE > T- reddu ce ent ry wil
id* TS FT esis like |

Saar
Eas So SLR parsing table would look !
--
{Ld tit
ae T>*FP 2 CS, J :
id $

id $ F > id . J
$
$ Te
| & HY wo.
|.P. University—[B .Tech.|-AB Publisher
2017-21.
C om piler Design
Sixth Semester, whether the following grammar is LL(1) or not (5) ~
|
a 201 ‘ UNIT-I a
2 “i ; : 2- @) A/a, re A-?
Q.
ven below .
mmar £1‘v aSA/e Ao cle

gcctothe rule
i; t ‘hi
th
Q.2 (a) For e gra
ee
E— TE

le me (a)
Bats) > ERE)
ee , as

Torr
Lg
-
ig (1) *? First (6) =€

>
ie -» FP
(E ) 1D
ie,
orniic grammar7 o t f o l l o w t h e r u l e s t h a t ’ s w h y it is not LL(1)
sae pall
F S doesn
n d l e ? C h e c k w h e t h e r t h e grammar (5)
| r 4 (a) at do you mean by ha |
ing e E > E+T/T
construct the LL(1) pars eee
aor (id)is LR(0) or not
a
‘ T >
di
:EF aonit Scie
Lit
i | |
ans.Handies =C
p=i(,

me t c h e s t h e r i g h t s i d e of a p r o duction,
s t r i n g 1s a s u b string that ma esents one
FIRSTLTT)) ==F{.(, ©!idl B ib h a n d l e of a de of t h e p r o d u c t i o n r e p r
t i o n to t h e n o n t erminal on the le ft si
ad w h o s e r e d u c ion.
FIRST( g e r e v e r s e of a r i g h t m o s t d e r i v a t
SIRSTIF) = {¢[( 743a1} i a p alon th |
: = ae
i t i o n o f a h a n d l e : |
. « P r e c i s e d e f i n y is a production A— § and a pos
ition of y
ey e handla right-sentential form
of
F O L L O W E ) = { $ , ) } ' ae
d r e p l aced by A to p roduce the previous righ
t-
rOLLOWE) ={$.)) ee here the string B ma y be f
st
o u n d a n
rivation of y.
mo de
={+.$,)} aa _ gentential form in a right l l o w i n g « is a h a n d le of afw.
LL
FOo OW) 2 3 ‘ . 6 in the p o s i t i o n f o
e aw, t; hen A>
ea
a og ice, ifS oA 3

. eJ32:3 e c o n t a i n s o n l y t e r m i nal symbols.


6s@5 )} ght on handl
=E0\1Fest-s ow)ts = 3 d 3 | o Thestring / to the ri i a l f o r m w h o s e h a n dle is A> b at
7—tF,
| v e , ab eis arig t s e n t e n t
able: o s « é d t h e e x a m p l e a b o o s e h a n d le is A Abe at
Predictive parsing t | —e6C6hl(«i i g h t s e n t e n t i a l f o r m w h
a | ( ) = position 9. Likewise, aAbede is a r
—es
n || + ¢
t h e a u g m e n t e d g r a m mar is:
| position 2 BE
———
| e>Te [ a E > T+EH/T
E
a | an |
== eae ee ! E>.

e
Ct” To a
fas | T>8r |
= | Pe .
= > Ee ly |
! PS>- (r>r |
“tg Le
FE -
| F->€) | =e shiftentry —-
FF
.

=
|
-Not LR (0)
— a (Her E—>T+E

eEs>e -Tt+ElT
ONG
Suack |
|

a
ST r oe EoTE —
|
as
Tey ick
! oS
Sar eae
TFTZ CT o o k like | ire
_— - - ann R ( O ) p a r s i n g t a b l e w o u l d l
sETeriw d
idsid*i $ ae. The L
fH Pid bw
=T yeis es
s5 a
T> €
2\Giit,)
/
% 2

SET.
' s=T
_ E>+TE = |
SET ! id*idS ae
T>FPr -— | SR confli> cNtot LRO) n l y i n f o l l o w ( E )=$
SET id*ia$ aaa But for SLR E - T. reduc
ed e n t r y w i l l g o o
*ia$
F->id
So S L R p a r s i n g table w o u l d lo ok l i k e
=T
s=T *id $

id $
=
|ee $ oe
=
is :
Ese

rs te

in einai
eT

| r4 ,
kL
19 Parsing table has no multiple entries -. Grammar is LR (1)
nOLLOW(T)) =! + >) mai UNIT-2
POLLOW() = (+ "18 9)l}e: = ;
| pe an SDT to count the number of binary digits in abinary num
g tab
predictive parsin ont=
NON: anna : |
THRMINAY |__ Bo Te -
—_ Ae signed binary Maa
eLsik Example (continued) = ~

p |
ere o+TE |
a
ae
The attributed parse tree for —101:

Tria T> Fr P36 Le it a


#
NUM val -« LSTva

e DUA val = LIST vat


i — id
i ~- e etwe SIGN neg = Se
cag = eve
SIGN

on:
al
pos

a c k i m p l e m e n t a t i BiT pos «= LIST


UST. val o« BIT.val
St i -
LIST, pox = LIST pos + t

Input
or
BIT pos =

Stack ———
= LIST ual ~ Bit vat
LIST vat
Sito

id + idi*di$dS
a =i GIT val =

=er id+id* a at tributed an d L- at tr ib ut ed SD T’s. Write


s e b e t w e e n S-
id+id*id $ iffe r e n t i a t e S D T for converting
it
SET —— .eate the SDT for any
problem an d wr

sETic idtid*id $ = esi SS binary to decimal


.
g r a m m ar s c h a r a c terized
4 cl as s of attribute
+id*id$ aii itl ributed Gra m m a r s ar e
ut es . In h e r i t e d attributes,

ac
a s y n t h e s ize d attrib
sid*id$ : Sent
tr ib ut es , bu t on ly e a b s t r a c t syntax

sida*vida$s
. __ heri t e d at il dren nod e s of th
= n o d e s to ch ttom-up

er
d o w n r o m p a r e n t fo r b o
passe d f l e m
.
:
ess, are a p r o b
4 ?
a an a ly si s of th e p a r s i n g p r o c
t sy n t a x tree are
, semanti c t n o d e s of the a bs tr a c
cP R 4 ein botto m -u p p a r s i n g , the p a r e n
evalu a t i o n 1° S - a ttributed
all of t he ir ch ildren. Attribute
r s i n g a n d bo ttom-up
-eation of in b ot h top-dow n p a

SEY id“id$ ul,


a
be incorporated conv
e ni en tl y
DT. These
SEES al —
8}
ae
e s i z e d a tt ri bu te s, i tS-
i a
s t
c t
alrli
e b u
d ast e ad
ve thei
S
r s e m a n t i c actions
uses only synth
ae
h a
»

ol S D T S th at
‘ t
- al ua te d us in g S- a ttributed
id $ pene3 ! f 4 <* ev
SE’TR

Se'T\a ( r i g h t h a n d side).
SO x id$ uctio n
$
$
$
LP. University-[B
2017-23
ea (B.Tech.|~AB Publisher
N SR conilet SLR ut not LR (0) es R (1) Parsing Table
i LR) parsing table for goto
Go, the ara” | ae 3 a
ee
\
a 6 VA | 8B 4
Q.3. (b) Con ct Ss — aa/bAc/ dc/bda
sesso an a
A-d 0 s3 | 89 L 1
_—
ted grammar 18 | accept } ’ A
| | | A
ps
x | \ | :
pn 2 s- 5
S > Aa | 2
| 9 | Co boc
S + bAc | Te | | | \
| s10_|
S > Be
m
aoe
|S 8 feeb e
5 + bBa — a ec a he ad ‘|
A>d ) 4|
oe | a 5126 | \ -
set of LR (1) items . Heng, thy, ree
Step 2: Find const & goto i.e. cons truc t | \ 3 | |
| ome | 10
represents new states. | = |

I): S' >S,$ : =
, sss no multi ple entrie s -. Grammar is LR (1).
So, the LR (1) Parsi ng table has
S'eAa,$ I,= goto (Ip, a)
3's ebAc,$ : UNIT-2
S—Aae, $ the numb er of binar y di tsry number
in abina
gi
S > eBc,$ Q.4. (a) Write an SDT to count
S >ebBa,$
‘T,= goto (Ig, A
| ae
S—>bAec,$ |
A eda | ae
s i g n e d bi na ry . numbers Seimei)
Example: Evalu a t e
B-ed,c
oe The attributed parse tree for —101:
i mae
. bi. wh SSGNTT [USTs =F
I; = goto (/,,B) tS ee
t.
cee
goto (I S) S+>bBea $
om
i=
7 0? xT
as |
Sin eeeee
1

| S'>Se$
an |
SIGN. neg = Tue

ae. |
ie 2
he a

f=S'>gotoAca,$A)
Tos +t
ust ~ UST,
r
g

| ote | Steno
BorGsse i hee ite
s.Wr
; ff er en ti ; e between Sa
at tt si~~
ec io ’ = e e
ty (b ) Di
| | Q.4.
pr ob le m ane wrt EeRS)
= | the steps to cr ea te th e SD T fo r an y ars characterized
eee (Ig, b) e L l. :
Sbe Ac,$ er fr
numbAn om biib de ci ma
nary to Grammars are 4 sr of Re aed iy
cy s. S-Attr uted
S—Mabgee Ba2,$ Io = aagoto (Ly, c) a.
a t t r i b u bu
t t
e on
s ly
, sy ? esi zed a t
e a b s t r a c t ct s y syntax
x 7 4 by having no inherited n t n o d e s t o c h i
1 l d r e n n o d es of t h
bottom-uar p
S > Bee$ p a s s e d d o w n f r o m p a r e rocess, ar e 4 p r o b l e m
;
fo r
gy nt ax tr ee e
which must be
;
Boe a = :
anal y s i
:
s of th e p a r s i n s Pp
e d
uring the semantic : e n vi g a t t r i b u t
om-up P a r s i n g , t h e p a r e n t —
| n e b e a t e n in b o t t ei r ch il dr en . A t t r i ou tae
of all of th : both top-dow® P
I, = goto (Ip, B) I,, = goto (I, c) coantel after c r e a t i o n
d c o n v e n i e n t l y sn
S' > Bus o r pora t e
S > bAc- ,$ grammars c a n b e i n c
pa sputed SDT. These
a e s we an action
sy nt he si ze d att rib ute s, 11 ey
me a SD T us
eves
al on
ua ly
te d us in g S- at tr ib ut ed SD Ts t h a ,
Is = goto (Ip ; d)|
Ip = goto (Ig, a) oe are
Adsa ( r i gside).
h t h a nd
t h e p r o duc t i o n
written a f t e r
Bde '| S >bBa-$ |
eixth veSemester,
Sixth Compiler Design _ AS are. : oe weeWence }
US
|a me
Ue
LP. University-[B ‘Tech.|~AB Publisher
; se ae ‘. 2017-25
pe rands. Three such representations are:
# 7 \ P Quadruples
f ie
ft (+
pe \
: '. ‘Triples
p
.
Indirect triples
ea ;
Qua j ples:

E.value . Deval ue | _» Aquadruple is a record structure with four fields,


yas :

ea aere
oe 7

eoaon*!
* ¥ : which are, op, argl, arg2 and
o sult.
7 s

ort depicted a _» The op field contains an internal code for the operator. The
above, attributes in S-attributed SDTs are eva] tatement
three-address
xX : = y Op zis represented by placing y in arg1, zin arg2
oe ing as the values of the parent nodes depend and x in result.
upon the valet
by P aaa in
_» The contents of fields arg1, arg2 and result are normally
Ere mn : pointers to the symbol-
L-attributed gramnars are a special type of attribute Ol
grammars chithe ope t h
table the names represented by these fields. If so, temporary names must be
attributes to be aes in ary depth-first left-to-right traversa] of th 8, They , entered into entries =e symbol table ax hog ase deeuied
tree. As a result,
Doe attribute evaluation in [,-ai+r; Wee -nles: |
conveniently in top-down parsing. | attributed grammars can h |
| Strag, Trip id en entering temporary names into the symbol
dideRritine
© ine ‘h _, To avoid m table, we might refer to a
s: : Dy rary value by the position of the statement that computes it.
= ee 3 |
einition is L-attributed if each inherited attriby:, te If we do so, three-address statements can be represented by records with
right side Oo
oe
u only
te of Ki
3
= fields: op, argl and arg2.
3

A +> X1X2_ Xn 8 i
e = The fields arg1 and arg2, for the arguments of op, are either pointers e
epee only ch ‘wae
1.The attributes of the symbols . Ke XO. sya XG : | mae | symndo mi table or pointers into the triple structure sig irs AN
ee _s Since three fields are used, thisis interme
i diate code format ieis : as tripl
known triples.
. : ; aig . iples:
eee oe ee
Every S-attributed
ee . ao
Syntax-directed
;
definition
roa
js also L-attributed, ; —_ peSabai
ean tiakiig
rim of three-address
the triples themselves. This“8 code is that oe
implementation
i0n 1s} ed reo
imdirect reeT=
Implementing L-attributed definitions in Bottom-Up parsers
re — Q.5.(b) Write the three address code for:
L-attributed definitions into translation schemes.
a © While (a <5) doa:b+2
Many progr amming languages are L-attributed. §
.(Il) - a (a+b)* (c +d) + (a+b+c)
) do a:b +2
Ans. (i) While (a<5
1. if a<5d
or “synthesized
attribute
occurrence of symbol X1,X2---Xn =b+2 -
Q.5. (a) what do you mean by three address — oe 7
code? Explain how thet, .a=T
address code is represented visquadruples,triples (ii) -a (atb)*(c+d) +(a+b+c)
examples. and indirect triplesyj Ans: tl = a+b |
Ans: Three-Address | t2 = -uminus(a)
Code:
Three-address code is a sequence of statements —_ 7s eee
of the generalform - ta = t1*t3
X:=yopz
| | - | t5 = ti+c
‘where x, y and z are names, constants, or
compiler-generated temporaries; op sta
for any operator, such as a fixed- t6 = t3 +t5
, or floating-point arithmetic operator, or a log) UNIT-IL
operator on booleanvalued data. - Th “Hus a source language expression {shows
translated into’a sequence like x+ y*z mighth |
| | ea n by symbol tad
Q.6. (a) What do you mee ‘ler interact with symbol table.
tl:=y*zt2:=x+tl how different phases of ee
d and maintained by
| cent data structure pare entities such .
where tl and t2are compiler-generated temporary names | ol
Ans: Symb table 2s inanformation about| the eacur
e ren !
rfaces, etc ta le 1s us¢
ol tab
Implementation of Three Address State , t |
. ments: | compilers 1n a names eT
; ; acs te compiler
A three-ad dr es s st at em en t is an abst ra
|
ct form of in te rm ed ia te , variable names, *h synthesis P
so
these statements can be implemented code. In a compilt by both the analysis and the ey?
as records with fields for the operator
and ®
—_ a

i sey.
Sixth Semester, Compiler Desi
hang: Sy
°! table may se lve
gn
the following pur
& poses depending
"Pon
= “ore the Hames of al]
entities in a structured form
~ ay ifa Variable has been declar , at 0 Ne Plac,
ed. :
Source Toog ©imapl
re emse ent type checki-n
g, by + verifying assignment, |
or
To determinantically correct.
e the scope ofa name .
4nd «
a
_A symbol table =o. (scope resolution).
1S simply a table whic
;
maintains an entry h can be either lin
for €ac h name in the followin or
“Symbolname, ty g format:
pe, attribute> : bag, |
Q.6. (b) How
Structured I the data is sto )
red in symbol end
table for block
es? ; So the complexity is
Ans. Symbol ta ble O((n+1)/2).
7
organization for non-bloc Now let’s see various aspects:
*te<s

ked Structure (Actual use)


Ke

By @nNon-blocke
Compiled unit isas d structure language, we mean a language in Which
G@) Fixed as variable length
i entries.
module are kn ingle module that has no submodul In fixed , the nu
Own throughout the module. es. All varj abl mber of entries
Possible in q table
es gt RE ) i are fx where as in v
“ley
ite) jength it is changed. ariable
(ii) Size of key(in byte).
(iii) Method of access | _
iv)How frequent the insertion
and deletion.
sad guage specific consideration:

A. Unordered
Variables are stored Si :
The simplest method of or
ganizing a symbol table is to cletter> as <letter><digit>
the table in the
order in which the vari add the attribute en,
ordering. ables are declared: So th ith this format at the most 286 entries are
| ere jg No pax’ possible. To know the address of a
- Here the insertion is Ve , a ae we have formula as follows:
gl >
ry easy as no com
ddress> = <key> -65 OR = | _
ada = (< key — letter>— 65) * 10 + <digit> - 48
ii)FORTRAN =
ea language the fixed sized length of variable is used.
mt disadvantage is that the memory is wasted.

So disadvantage is that the memory is — =


A B b
s+ .
~— wae
——

Procedure select(table, key data value, found)


se key is known
to us Padding (This much bytes are wasted)
Var1 unteger- : |
begin i
(iii) C or PASCAL | EAS
with table q suc h hig h-l eve l lan gua ges , the var iab le ee —= o .
In
zah thod
begin With this type of key strange, we get the better memory tili
i
.

entry[n+1].key = key; is not fast.


i=1-
© on
|
3 » one
| While(entry{i) ke
y<skey
Ss az E
i=i+]; |
ifi<=p then iia,
|
20—"42V4 6 BVEZAVAR RP CSSS Oe -— FO OOO ee 2 er oS

2. Ordered Pl, aapeads ‘


In this method, the table is maintained is sorted form baseg ,
ccomplish
ce i. ae ail ra : saab Ss en ome atteibele 1 the | .
loka LP. University_tp ‘Tech. |-Ap Publishe
Tike’ acbiial thosrtsGn of new entry may generate. Some additional overs lis
Here only ptr field is manipulated, We have to do nothi : 201s
because other entries may have to be moved to get the position of insertion Mh pjnsertion
For searching a particular key, we apply Binary-Search Technique. Supp,
Ag with ‘abl$6.e.itie a
P iii. Linked List ‘Seagier 7
os (KyigoVinia) «=: (Kp V,,) are the entries in the table.
: “ey in this approach, wé combined the array and linked list
Here mid= n div 2
Algorithm is described below Casal |
Find(ow, high) a Pa
While low < high do
begin | | E
mid = (low+high) div 2; | J
ifk < entry[mid].key then | | |
high = mid; | i
Find(ow,high); e. array is for search
else 7 | ing and linked list is used for insert :
| b is no actual limit of number of entries in the table. Ton and deletion. Here
low = mid + 1; poe ch a key, §tartin § Symbobol
l can
can be bef found by comparigo ae
Find(low,high); | a A ep teate find the exact match. Mand then entries can
end a De 7
So the key for which we are sending is placed in high or low variable, 3. Tree
The time complexity of this algorithm is O(log n). , _ {nabinary — tree structured symbol table, each node have the following format:
eles of Sorting: , Left ptr | Key | Value \ Right ptr |
1. ay
; é } .
We short the entries in the table in some particular .....with arrays,
Here two new fields are present in the record structure. Thus two fields
With arrays, the searching of particular entry is very fast. But insertion ; Te are left :
and right pointer. Access to the tree is gained throug h the ... node. A
consuming. 3 . e ire
ee down the structural links of the tree until the desnode das a NULL
is found
For inserting particular entry, first we have to find its position to locate it. And; a field is encounted. ao
the entries below it are shifted down. 3 |
; I Let’s take an example of storing a string abcd in this format.

LP |a | value
| RP
l b

NULL — ee value | RP

ll. Index enc


With this method, the insertion is easy. [ob |e __| value |x|
a
Ptr K V

ae Here in case of bal


|oe among the n node is given by O(log.)
uae as a mapping 4
a.A hashhin
ingg fufunncction . or key to. addHres s tra
es nsf
aS orm
its ation
for a
that ma
ee le
vari a’ bles earee n
sh in g fun cti on :
tak rate s sto red.

KA. That is, a ha 7 ae attribu


t of ent
verte qumi
of the»
bedsr10
of recor
produces a tabl e addr ess at whic h the tiall y snde pend
time 1s esse
n

ar ch
*

e se
.

With th is m e t h o d th
the table.
,
Sixth Semester, Compiler Design

K oA
table space address space 1.P. University-[B Tech.|_Ap Publisher
Let n be the
number o f entries in the table we define loading factor, Statement mode
. When a parser encounters an error, it tries to take ‘
est of inputs of statement allow the parser to parse dest fe ne po tat ie
oad factor = Bera : ce (IAI) -- cing semicolon, replacing comma with a semicolon etc. p > ®, inserting a
oa d factor ee
isisthioh’ it<b isicandifficult cfu! tL -=re because one wrong correction may lead to ion ie ictt —_ have to be
If] high,
entries(n) manage SPthe tab le.
/ total toaddress e Error productions
Now in practical we have | * Some common errors are known to the compiler desi Sners that may occ in he
code. In addition, the designers can create
augmented etammar to be used
K>>lAl 4
| ate erron eous const ructs when these e ‘ons that gener
TTOTS are encountered,
s =

|
assign more than ddress
one key to one adere s~, ;
, there is a prob]Ae™M of Collin: aoGlobal correction |
if
So ‘We
Pre conditionine-. if i The parser considers the program in hand as a whole and tries to figure out
| ning: the program is intended to do and tries to find out a closest match for it. which ja ia
(1) Division Method:- : 4 >. When an erroneous input (statement) X is fed, it creates a parse tree for some
eg _free statement Y. This may allow the parser to make minimal hanges j
(2) Mid-squ
quare hod:
metho d:- “clos est error
. the source code, but due to the complexity
a change s in
(time and space) of this Strategy, it has not
(3) Folding Method:- Bi ‘peen implemented in practice yet.
pe ! ey ax Trees
(i) Length-dependent method:- = Abstract Synt
ea | | Me arse tree representations are not easy to be parsed by the compiler, as they contain

ee euressing: -. 0 ore details than actually needed. Take the following parse tree as an example:
(b) Chaining: |
Language:.
Symbol-Table Organization for Blocked Structured
(i) Stack symbol tables
(ii) Stack implemented tree structural tables
(iii) Stack — Implemented Hash-structured Symbol! Table:
Wha t are differ ent typ es of err ors tha t occ ur dur ing lex ical,syntag, :
Q.7. (a) Be
and semantic phase.
Ans: A parser should be able to detect and report any error in the program, Iti
expected that when an error is encountered, the parser should be able to handleitay
carry on parsing the rest of the input. Mostly it is expected from the parser to check{y_
errors but errors may be encountered at various stages of the compilation proces}
i
il l d d t
to o th ei t
r h e i r p parent
program may have the following kinds of errors at various stages: .
os el y, we fi nd mo st
age of s
the leaf i
|
d
no de s ar e si ng le ch
‘ng it to the next phase. By hiding
If watched cl
¢ Lexical : name of some identifier typed incorrectly nodes. This information can be eliminated poisson . roo |
as Sho
a tree :
¢ Svyntactical . :S suelieat
missing semicolon or unbalanced parenthesis
a .
extra "4information, we can obtainin
¢ Semantical : incompatible value assignment a ) *.
¢ Logical : code not reachable, infinite loop | a. i ; ROE S i.
There are four common error-recoy
. ery strate . | tie
cE | | y:
parser to deal with errors in the code. ; Brae ee implemen . :
(aa |
|
q |
id '

7 el
,
> . }

Panic m.
’ vi 7%
te ia r >.
~~ _-
' a a 3
; ‘ wee
i
ae
: :
a a,=
¥ 7
: Se~* e
. M
. . oe ¥
. sy y
ME iei ; +
; e
> of Ot
| ne Z ' ' .
. .
. . Aan :
. meii a
, . —— +S
; a a
:
a + . é eae = %
:
, ~,
a
Tee ’
ie

hh
=
i
| *

y 244 Aobd ro,&. .


;

: h.¢* F '
ib.

k. % 4! +

.
rn
& '
; 7 ‘4
_.
;
|
’ a

2 ,

Compiler Design
al Parameters: Stores ac}
actu the called procedure
£0
ut
and BP
Val ue: Stores return values
o Returh
s procedure is executeg eS
when ev er @ ed
>
ur e act

4+ l
as contro st ac k. Wh en a pr oc
\ Calls an Kies
aa sus
id - er 15
: call tion re

| a . jer to
cl = represent a series of a+:
| | : S Hons j
leg t
ort ant data structures in a compiler with coal
St

oe
ASTs are imp an a pa rs e tr ee an d can be
mpact th
_ formation. ASTs are more co

I y [ :

compuler- ),
_ intf(“Enter YourName-"
allocation tegies in the runtime env:
;
Q.7. : ae are the storag 4VITOp p : ;
| : Ks” username)
er: @
ae %
cant ie 3
of comp jl ti on of t e x t ( co d |
so urce code is merely a co ll ec show_da rname);
An s: A p rog r a m as a d on the ; Statemen,, . '
ac ti on s to be perf o r m e inue. .”)-
e, it re qu ir es y to co nt
and to make it aliv st ru ct io ns . A pr o ta rg et ach * pr in ti "P re s® an y ke
ne ed s m e m o r y re so ur ce s to execute in a
program e m a p p i n g wi th the E S J
ers etc., that re qu ir
fo r —— — id en ti fi
ctual TOTY locas) -ntshow_data(char*us| er) |
at runume. r o n m e n t ic ... _ ;
ec ut io n. Run t i m e e n v i
p r o g r a m in ex
, By runtime, wemesn= nment vasa ae
nviro printf(“Your name is%s”, userna
me):
y in cl ude so ft wa re li br ar ie s, e
the target machine, which ma
to provide services to the processes running in the system.
Runtim e sup por t sys tem is a pac kag e, mos tly gen era ted wit h the « - }
program itself and facilitates the process communication between the process a th e co de gi ve n.
ti on tr ee of
runtime environment. It takes care of memory allocation and de-allocation Whil
: =es Bel ow is the activa
program is being executed.
——— —s a

Activation Trees
is a se qu en ce of instru cti ons co mb ined in to a numbe “ e f
wedus
A pr og ra m
: seq uen tially . A p = 5S
are executed
Instructions in a procedure y of the procedure os tar t and s
an d ev er yt hi ng ins ide it is cal led the bod
end delimiter ins
instructions ide it , proved
d the se quen ce of finite
identifier an thebody of t ;
| | make up
procedure.
ex ec ut io n of a pr oc ed ur e is cal led its act ivatio n. An ac ti va tion record co 5 =
ine
all the necessary information required to call a procedure. An activation record r a,
u
contain the following units (depending ).
LP. University-(B.Tech.|-AB | Publisher —*
pa ra me t ers which are used to.
‘ler Design siti ters: Stores actual parameters, 1. ¢. ,
ee Sixth Semestet, actual para d
mag a8 ut to the called procedure.

a
Abstract tree can be rep res ent ed as:

al
seve” _svalue: Stores return values. also
d, its act iva tion record is stored on the stack,
Joa ute c of the
natiion
ra procedure is exec : re calls anothe r pr oc e dur e,
‘ the (execut
k. W h e n a pr oc e du At th is time, the
a t ac ni sh es ex ec ut io n.
|. own a5 me oe aed until the called procedure fi
; 18 £ the cal led pro ced ure is stored on the stack.
= cal:ler. naC
Br es
cal rece
or do
id
= Z pivatte ume that the program control flowsto imthe4 called ce" al . we
proic pe e a
We as® : led, its
i control 1 1sis transferred a iT flow
+t ret urn s the con tro l bac k to the ae ae
roce? ie “aaad act iva tio ns 1 the form ce, Known as
pr es en t a ser ies of
/ ps0 tT aaniee to re
id ; make
1
: vation tree.

tand this concept, we take a piece of code aS.am —
: .

with least Unneg, th? as


ASTs are important data structures in a compeiler an d can be easily Use ) To unders |
mo re co mp ac t tha n a Par s® tre
information. ASTs are ; ) * ccatadtat
compiler. es
the ru nt im e enyj,, 4 ; .
printf( Enter
‘ “ our Ss ?

in
2 2

es
e

st ra te gi
Q.7. (b) What :
are the storage allocation «
i ‘Te, scanf( és Jos ze ,U ser é?
_ of compiler? a ername
O );
e ade a
: Statements, sh ow _d at a( us
cod e is me re ly a col lec tio n of text (code,
Ans: A program as a source ion s to be pe rf or me d on the target MAChing | printf Press any Key
and to make it alive, it req uir es act
A Pl con tains - | se
to execut e ins tru cti ons .
program needs memory resources - ntshow_data(char*user)
es , ide nti fie rs etc ., tha t req uir e ma pp in g wi th the act ual memo ry loca,
for procedur |
at runtime.
iS a stats, -tf(“Your name ‘5%s”, username);
mean a pr og ra m in exe cut ion . Runtime Pee prin
By runtim e, we
ies , en vironment variables4
so ft wa re lib rar
the target machine, which may ince lude es ™~" return0;
ices to the proc esse s runn ing in the system. |
to provide serv
tly gen era ted wit h the eXec utahy }
Runtime support system is a pac kag e, mos
n tw ee: n the pro cess ang i, |
mmunic at io be given.
program itself and facilitates the proc ss co d e
e t r e e of t h e c o
on an d de -a ll oc ation while, i l e s :: t h e a c t i v ation
en t. It ta ke s car e of me mo ry al lo ca ti n
runtime environm
program is being executed.
|
Activati on Tr ee s
ura
A program is a sequence of instructions combined into a number of proced
Instructions in a procedure are executed sequentially. A procedure has a start anda
end delimiter and everything inside it is called the body of the procedure. The proceduy
identifier and the sequence of finite instructions inside it make up the body of th
procedure.
che execution of a procedure is called its activation. An activation record contain
all the necessary information required to call a procedure. An activation record my)
%
contain the following units (depending upon the’source language used).
Temporaries: Stores temporary and intermediate values of an expression.
Local Data: Stores local data of the called procedure.
Machine Status: Stores machine statu s such as Re i ateae
before the procedure is called. | maters, Progran: Cau .
Control Link: Stores the address of activation record of the caller procedure
. 3
Access Link: Stores the information of data which is outsid
e the local scope. — ‘
a
a
34-2017 Sixth Semester, Compile: Design
us Stag |
allocation is the best suitable form of storage for procedure activations.
Storage Allocation UNIT-4
Runtime environment manages runtime memory requirements for the fol]
entities: | : Wing
e Code : Itis known as the text part ofa
program that does not change at TUNtimg
Its memory requirements are known at the compile time. resources (i.e. CPU, Memon, ties tonnes.
is stat; arandorandom many, ann ep
called din inactivations. and delive, Ke
e j Procedures : Theirae text part aticbut they are and
a
In optimization, high-level gener
That is why, stack storage is used to Manage Bisendive calls
efficient low-level programming code
e Variables : Variables are known at the runtime only, unless they are globaj , | rules given below:
constant. Heap memory* allocation scheme is used for managins allocation ang ae
* . rT
4,2
e The output code must not, in any wa
» Optimization should increase the y, change the meaning of the
allocation of memory for variables in runtime 3 : s pro e “
the prog ram and if uat
Static Allocation program should demand less number of sche
> “he
> Optimization should itself be fast and sho>uld
In this allocation scheme, the compilation data is bound to a fixed location in th, not del
process. ay the overall compiling
memory and it does not change when the program executes. As the memory requiremen; Efforts for an optimized code can be made at various levels ¢
and storage locations are known in advance, runtime support package for memory process. Veils of compiling the
allocation and de-allocation isnot required. ' ta At
got thed
Hc beginning, users can change/re
e da
ec/rearrange the code or use better algorithms
Stack Allocation
e After generating intermediate code, the comp;
Procedure calls and their activations are managed by means of stack memo can modify the intermediate
«

er
.

k aie
\

improvin g
>

calculat ions and


.

in last-in-first-out. (LIFO) method and this allocation strategy j, code by address


allocation. It works
e While producing the target machine code. th
very useful for recursive procedure calls. | » the compiler can make use of memory
hierarchy and CPU registers.
Heap Allocation Basic Blocks: A basic blockoe is a sequence , of con secutive statements in whi
of control enters at the beginning and leaves at the end without acne which flow
Variables local to a procedure are allocated and de-allocated only at runtime. Heap or possibility
n is use d to dyn ami cal ly allo cate mem ory to the vari able s and claim it back of branching except at the end. |
allocatio
when the variables are no more required. The follow ing seque nce of three- addres s statements forms a basic block: _
tl:=a*a
t2:=a *b
Text Memory
t8:=2* t2
t4:=t1+t3
ni Static Data
th:=b*b
Stack Memory t6:=t4+td
Basic Block Construction:
Algorithm: Partition into basic blocks
Input: A sequence of three-address statements
Output: A list of basic blocks with each three-address statement in exactly one ©
block
Method:We first determine the set of leaders, the first statements of basic blocks.
The rules we use are of the following:
Heap Memory The first statement is a leader.
sta tem ent that is the targ et of a cond itio nal or unco ndit iona l goto is alea der.Any
Any
‘cotaliee ce
een esSFsso
peek as a ee y
SS

tha t imm edi ate ly foll ows a goto or cond itio nal goto sta tement is a leader.
Except statically allocated memor
: y area, both stack and he statement
: Can gr OW its bas ic blo ck con sis ts of the lea der and all sta tem ents up to but
pectedly, Therefore, they saceer ee For each leader,
and shrink dynamically and unex
fixed amount of memory in thesystem e provided with a
not including the next leader or the end of the program. a an d b of
co de for dot pr od uc t of tw o ve ct or s
Consider the following source
length 20 begin
prod :=0;
i=l:
do begin
‘ .

6-2017
36-2 (at
Sixth Semester, Compiler Desig?
prod =prod+¢ ali] * blil: LP. University-[B.Tech.|~AB Publisher

1 =14+1; | Example: Consider the block of three- address statements:


end t, =4* i
while i <= 20 q t, = alt,]
end ty = 4" 1
gee ‘ te. = Dit
The three-address code for the above source program is g1V°n 2S - — | t mee
os 4
(1) prod:=0 , | : t, = prod+t,
9) gant
( i:= t prod :=t,
(3) tl i= 4*i 3 oF , t, = i+l
/*compute ali] */ t it,
(4) t2:=al[t1]
(6) ~~ *8s= 4¥4 if i<=20 goto (1)
(6) t4:=b[t3] /*compute bli] */ | 7 “hae ; Stages in DAG Construction
(¥)- 25 c= 224 | | , (a) 1
t I Statement (1)
'
(8) t6 oa prod+t5

(9) prod:=t6.. | a io
(10) t7:=i41 | ‘ |
(li) i:=t7 3 ae | (0
(12) ifi<=20 goto (3)
Q.8. (b) Identify the basic blocks in the following code and draw the Dag
graph for the same: | , —
main() . | | okie | |
{ | , | “a i :
int i=0,n = 10; “ah : tf
int a[n]; ) | | ioe isAaa
while (i< =(n-1)) _ ay vee S identifier t3 to the existing
{ | : : i 7 node for Statement (S)
) | a 7 (+ )« 8
als) = 17i; 5 |

| id \ . 4 10
}
return; | ee Cine ann | |
7 , a
}
Ans. Agatti, for construction of DAG — | . (2)
Input: A basic block
formation:
Output: A DAG for the basic block containing the following in
a
rior nodes, an
A label for each node. For leaves, the label is anaiidentatt er. For inte
operator symbol.
For each node a list of attached identifiers to hold the computed values. a St
| —
Case (i)x:=yO Pz ‘Sa ,

Case (ii)x:=OPy
- Case (ili) x: =y
Method:
Step 1: If y is undefined then create node(y).
If zis undefined, create node(z) for case(i).
Step 2: For the case(i), create a node(OP) whose left child is node (y) and right chi i
sub expr essi on). Let n be this node . a
is node(z). ( Checking for common
aL ieoappamid
For case(ii), determine whether th ere is node(OP) with one child node(y). ifn ot¥ a
For case(iii), node n will be node(y),
| Step 3; Delete-x from the list of jidentifiers for node(x), App
end x to the list ¢ of
ched identifiers for the node n fou nd in step 2 and
set node(x) to n,

-o
ae
38-2017

2. Source intermediate
Statement (6), attach
identifier prod for - 3. Code program
prodo tS
Statement (7) 4. Intermediate target
Program cod
{SSUES IN THE
DESIGN OF ACO
:

1. Input to code generator


2. Target program
3. Memory management
4. Instruction selection
5. Register allocation
6. Evaluation order
1. Input to code generator:
(9)

Statement (8) intermediate representation.


attach identifier i for
statement (9) Intermediate
representation can be - a Linear re
Presentatj
notation b. Three address representation : uch as qu sation such. as postfix
representation such as stack machine code d. Graphical weiicidemieares Virtual
S quadruples ¢.
ek Machine
trees and dags.

t7,i Prior to code generation, the front end must be


into intermediate representation along with necess scanned, parsed
to code generation is assumed to be error-free.
ary type checking, Therefore,
ras ranslated
input
2. Target program:
The output of the code generator is the target
(h) xed e
Absolute machine language - It can be placed in a fiPe Lhe looutcatiputon may
memory
be : a
and can be
Final DAG executed im me di at el y.
b. Relocatable machine language - It allows subprograms to be compiled Separately.
c. Assembly language - Code generation is made easier.
3, Memory management:
in run-time
Names in the source program are mapped to addresses of data objects
memory by the front end and code generator.
It makes use of symbol table, that is, a name in a three-address statement refers
to a symbol-table entry for the name.
Labels in three-address statements have to be convertetod addresses of instructions.
4 3 <j, a backward jump
For example, j :goto i generates jump instruction as follows : ifi
target addr ess equa l to locat ion of code for quadruple iis generated. if
Q.9.(a) What do you mean by peephole optimization? Explain with ex instruction with
is forw ard. We must store on a list for quadrupl e i the location of the first
: i. f 6 | > j, the jump proce ssed, the machi ne locations
rate d for quad rupl e. When iis
machine instruction gene

|
Ans. Refer Q.1.(i) of End Term Examination 2016. = .
.
Q.9. (b) What are the issues that occurs during the code generation proces?! for all instructions that forward jumps to 1 are filled
io n: | 7
4. Instruction se le ct
is the co d (6.5) be co mp le te and uniform.
Ans. The final phase in compil er mo de l et ma ch in e sh ou ld
_ e generator. It takes as inputal | The instructions of targ : whenn efeffi
fi cien
ci cy en cy of -
ie intermedia te
repr esentation of the source program and d ma ch in e idio ms ar e im po rt an t factors
target program. The code generation techniques pre produces as output an equivalell | Instruction speeds an
pres ented below can be used whethe! target program is considered.
Fuk) bs

x not an optimizing phase occurs before


Ao
P35 Aaa -

code generation.
*0-2017
The deal Sixth Semester, Compiler Design
ality
he Rees : the generated code is determined by its speed and
Size.
5, Regis; © Statement can be translated into the latter statement
ag Sho |
I nst ©¥ allo cati on "by, ly)|
Mvolyj Tuctions involvin 1
g register operands are shorter an
d fastey th
~8 Operands it memo ry.
| Ma, )
: — Regis ter ali Sisters is subdivided into two subproblems :
in the : Selected.
tT allocation — th e set of variables that wil ill T reside in regist
£1Stersg .
p ro &Tam is at aDoig
. > Rep ;
Picked, sister assignment — the specific register that a variable will TeSide «
— Ce :. ; ' ;
th \

For Etethnie machine requires even-odd register pairs for some operands and reg
» Consider the division instruction of the form : D x,-y Uy
Where as, Aa ; e eo. - .
hol ds th »X dividend even register in even/odd register pall y — divisor eVen reo: its previous stage, has its own representatio ee Hach phase takes
€ remaind Bg + from £5) 1 Of source pro |
inp? tput to the next phase of the compiler. Sam, and feeds
sib 6. es,E ‘nder odd register
en order holds inthe which
: The order the computations are performeg e)
quotient
‘its ov aon g are the phases of compiler:
to hold in anne of the target code. Some computation orders require fewer reRist Fo ;vical Aa alysis
rmedi
ediate results than others.
Syntax Analysis
# Semantic Analysis
i | Intermediate Code Generation
he Code Optimization
Vv.
vi Code Generation
<i Symbol Table

High level
Language

Lexical Analyzer

Syntax Analyzer

Semantic Analyzer

Symbol Table}~
Intermediate Code
Generator

: | ~ Code Optimiser

| ~ Target Code |
-Generation

Assembly Code
\ eo a
rir

7
9.2018 Sixth Semester, Compiler Design

pattern matcher which can recognize follow; by


ng

a
Q. 1. (b) Gener ate a
« a L*0O* |an
ns: ol*
. >¥* .
patter (y |
Ans.

lyZ Sim
spt is to facilitate lexical sala eee by itself but its main
ocessing of chaay seque
u called toke..
left recursion. S—>AaR
5
(Ral is source code to produce symbol sequences
Imp rov e the gr am ma r by rem ovi ng to other
Q. 1. (c) A. program” such as parsers.
5S 10T use as input

Aab | Aba | a B > a. ei


|
[ ;
Lex Compiler Be
Ans. lex.! ex yy.x
|
S > AaB/b isnot left recursive
: Compiler eee. es
: a ig nO left recursive.
Bo | lex. yy.c ————>

Only A - Aab |Abal a is left recursive


la input =
A —> Aabla A— Aba atreant > aede a.out +——_—_—_-» Sequence of
tokens
A — BA' . lex.l is an a input file written ina language which describes the generation of
AS GAclhe steps to remove left recursion lexical analyzer. The lex compiler transforms lex.| to a C program known as lex.yy.c
Ai1sA lex.yy-c is compiled by the C compiler to a file called a.out.
ee ae; Bos ne »
eh e
ais a » The output of C compiler is the working lexical analyzer which takes stream of
Bisa . Bisa
input characters. and produces a stream of tokens.
SON oe AAT eect Ao) ee
so, GE NEueO sha red by lex ica l ana lyz er and parse: to return
e yylv al is a glo bal var iab le whi ch is
A’ -»abA’ le ....(2) A’ > baA' le ...(4)
the name and an attribute we of token. The attribute value can be numeric code,
mers
(1) and (2) are same. So. instead of using 2 gram pointer to symbol table or nothing.
. |
| =NULL
AaB/b | Q. 2.Consider the grammar given belowe
So, - S— :
A> aA’ | X > OY1 1122
ursion
Aves ab A lbaA'le Final grammer after removing left rec
| ¥52 le
Boa
| te B. Z—>lY2itle | pa
Synthesized attributes and inherited) rminals. [5\
Q. 1. (d) Differentiate between
| (2) | (a) Calculate first and follow for the non-te
attributes. pr ed ic ti ve pa rs in g ta bl e fo r th e ab ove grammar.
(b) Create a :
ae : ; [5]
| no nt er mi na l on the left-hant . () ;
Ans. A Synthesized attribut e is an at tr ib ut e of th e
Also Show that thisi ene isi not LL
ze d at tr ib ut es re pr es en t in fo rm at io n th at is being passed X -> OY111Z2
side of a production. Synthesi y
ee
up the parse tre e. For Ex : ahs
AB C |
| | S > :
B, C), th en it is sai d to be a synthesized ;
If S is taking values from its chi ld no de s (A, 7. (Bite
S.
attribute, as the values of ABC are synthesized to )
An attribute of a nonterminal on the right-hand side of a production 1s called (a) Calculate first and follow
attached
an inherited attribute. An attribute that gets its values from the attributes First (x) = {0,1} Follow (x) = {3}
the parent or siblings of its non-terminal. For Ex: ae
S — ABC Re First (y)={2,e} Follow (y)= (1,2)
d C, B can take va l
A can get va lu es fr om 8,
B.
B an
ues from S, A, and C. Likewis® First (z)=(1,e} Follow (z) = (2)
can take values from S, A, and
‘ mester, oOComp! ‘ler Design
1~2018 Sixth Se

Predictive Parsing > table Bk ball : . tp Ti

mn a CA
ee Seer ear hg ge $ gother actiors. The Yace utility te “chl-Akash, ;
0 1 ee ee 0 Cc language function that : ansfy 8 : hes
‘ ys BYIR—s 424
1Z a in the. gra mmarcal)
gt may be us: leme
at ne
resolved te feat;
ts an— RU) » wt Of & 6
y ys= é Lye
Yee ¥ +e || jansY age funciion is named yyparsen, 8e()
nteae? values. Th The value 0 jg considered ots
uate STING
Pules “Within algaleRithen
wig - th. e tes e
/ g . -
pec Set ing
Z oo ING Ze

/ entrie s exist
More than two
e is in te rs ec tion. That is why
Ther
not
this grammar Is
Q. 3. Consider the grammar
S>L=RIR
L+> *R ! id
R=> L
non- :
Design LALR parser for this grammar. [iy I g. All non-terminals must be reachable
Ans. Design LALR Parser for this grammer. 3. The grammar is not ambiguous
eae
: e0na.

S->L=R
SR
4, The gran is LALR(1) parsable.
L—*R | The yace progr am alerts us if any of these

he, | ;
tyivi al to arrange.
snsHons do natal1, AS are
Q. 4. (b) Define following terms:
R—+L@- F

s.| S'> Se, $ le 3 , & To pologic al So


rt (ii) Dependency
S'> 0S, $ : 2s Soiree cole Graph

(y) Evaluation order Att Ce co


S>eL=R$ See Male Shee - ans. (i) Topological: sort: Topological s orting fo .
S$ > eR $ esta R+Le $ | | Ey q | :

Is reat i linear ordering of vertices such that for every directed ede nt
L—s*R,=1$ Cent |
L > eid, =1$ S$— Re, $ Lak : . in the ordering. Topological Sorting for a graph is notwniec es oe ii.
For example: consider the graph given below: the Staph isnotaDac
Rot LR,= 15 | l * et
lo
.
id
R—eL,=IS +> |R—Le, =1$
L— ide, =1$
|
O—- ——>
@—
| , 3
Q. 4. (a) Describe the functioning of YACC with the help of an example,

Ans. Yacc provides a general tool for imposing stz-ucture on the input to a computer
| (5) |
GS)
program. The Yacc user prepares a specification of the input process; this includes rule: |
A topological sorting of this graph is: 12345 7 | |
describing the input structure, code to be invoked when these rules are recognized,
ani |
a low-level routine to do the basic input. Yacc then generates a function to control There are multiple topological sorting possible for a graph. For the graph given above
the
input process. This function, called a parser, calls the user-supplied low-level input _one another topological sorting is: 12354 |
routine (the lexical analyzer) to pick up the basic items (called tokens) from the input | (i) Dependency graph: A dependency graph is adirected graph representing
stream. These tokens are organized according to the input structure rules, called grammat |) dependencies of several objects towards each other.
rules; when one of these rules has been recognized, then user code supplied for this rule, }

A program is a collection of statements,


the ordering and scheduling of which depends
a an action, is invoked; actions have the ability to return values and make use of the value
dependence constraints. Dependencies are broadly classified into two categories:
an
xth Semeste
r, Compile
Stateme n r Design
Pendenc S: when State
2. Contro} ments Compute
data that are
ependen yg qd
me Program.

fi ne
Q NO.1 wh;
the following
1. De me, Token and terms a
nd ©xplain «:
(a) Ts
Pattery, we
cite A lexeme is a Se
ans:s Le art of the input stre {ence of
pes are a duce am ¢ om which TUMeri¢ ha
s an error. A
per? wea 5a the lexi lexeme i8 One
Inheriteg ca l analyzer 4
attributes ifie
d by PATTERN fo : an Instance of that ne
(iv) Cireul igi dentters - ma
Tming the To
ar SDT: Synt b grace A tc
tohe
ken isa pair cons
ax-directe to a method isting of a t
implementa d S e of Peorke.D m e is an abstrac oken, nam,
tion where the S eae cee t Symbol Tepr
1.€., based o ource language driven by the na ord, or sequen esent;
n the syntax of the language t r a nslation is co Dee ular ce of input char
acters
The Se trees ar ty et symbols t
direct sem antic analysis parsing process _ sd pat hat the Parser
,
and the translation of the so urce program. : “Red, are a
se t
processes
panep® atte - The of ru le that define q
r™ lexemes TOKEN. A
(v) Evaluation order:| of
It is the order in whicich h the attributes are e si that eat t
he sequencea tooken may take In the
Pattern 18; a desc is
ription of the
f c a s
form theekeofywoa rkde.yword a8 a token, the
- . characters that
. | fi
is Jius) Activatia
r evalua : on Record.
attributes Y and 7 need to be performed p g. 1. (b) * a ie
tion record: An 3
BEFORE the
he semantici ru le for cal
culating | S. See ees ture
that composes a
a ct ivation record ic Snot
:
her name for Stack Fi
(3)
call] Stack.A genera s
‘its the or
ta strt
hinge: l ac t i va tion record consist of
ne follo wing aa es: hold the data thatis l
» Local ie val
o c al to the
€xecution of the p
ues: stores the value rocedure.
» Tempor s that arise in
the evaluation
of an
expression. tatus: holds the
.

information about
» Machin
2 e

Status of machine Jus


5
>

t before the
| function call. link (optional): refers to non-
local data heldin other activation reco
- see pt rds.
ae Control link (optional):
i na l) : po in ts to activation record of caller.
p
2 Return Vv alue: used by the called procedure to return avalue to calling procedure
« Actual parameters
(3).
tapping: , bootstrappinReg se3 e a
: cmga self-compiling
e ee wae ies
compiler5 sar ; compiler written in the source p
is a non-trivial test of the tanguags > being compiled
a of the community only netoe| kn
dow
v i e n h de ve
the l* anCo
guampge being lopers and bug reportingp
compiled.
3
he d a n e u a
igher-level language g e be in g
sled.
compile 7
* Compiller er de ta te the co
developmen mpilerosg teev ke a not only general-purpose
* Improvemen
aiel.e —
its
programs but also the compiler ice e! its OWN
uc
ulbe
d ab le able to reprod
p ,te nc y ch ec k as it sits ho
* It i sa co m rehensive co ns is
object code.
~~

Q- 1. (a) Dj Sixth Semester, Compiler Design


rected Ac yclie
oT Graph,
Ans. DI diRE

me
rs one ACYCLIC


t

GRAPH: Directed Acyclic Graph


(D AG) is a tog
cts the
Cture of basic blocks, helps f‘ ¢ Thus, left recursig
ASIC blo to see the flow
of values flowin
Offers optimization too. DAG & aMoy ammar having right
can be understood here:
R
t .
"

recursive grammar.

Where the value odes also represent the results of expressions or the
identifje, S/;
S are to be sto red or assigned.
example: “ay

; ammar is
tj=a+b more than one way. This error is ofte 5
(LET) Caused by recy iesapr Ogram can be
t)=ty+e the system cannot determine when interpreted
one rule is completass and ar definitions where
d=t,+t,

iF)

a b
q. 1. (g) Back patching.
[bh =at+b]
Ans. Backpatching: The syntax (3)
directed
; definition can be implem
more passes when we have both Synthesized attributes and plemented
Build the tree first inherited attributa
es:
and then Walk the tr
ee in the depth-first or
der.

Q. 1. (e) Left Recursion.


Ans. Left recursion:Types of recursion
(3)
Types of Recursion
i | entries for the goto instruction
where the target address is unkonown in the forward transfer in the first pass and fillin g

J | aa |
these unknown in the second pass.
Q. 1. (h) Handle pruning.
és
Left Recursion Right Recursion General Recursion
¢ A production of grammar is said to have left recursion Ans. HANDLE PRUNING is the general approach used in shift-and-reduce parsing.
‘if the leftmost variableof
its RHS is same as variable of its LHS.A grammar containing a production having left | A Handle is a substring that matches the body of a production, Handle reduction is a
recursion is called as a Left Recursive Grammar. For example: step in the reverse of rightmost derivation. A rightmost dexvvation in = be
S —> Sa/e obtained by handle pruning. The implementation ofhandle pruning involves the following
First eliminate Left Recursion- data-structures:- a stack - to hold the grammar symbols; an input buffer that contains
If we have the left-recursive pair of productions- the remaining input and a table to decide handles.
A > Aa/B Q. 2. (a) What do you mean by front end and backendofacompiler. (4)
(Left Recursive Grammar) i
Ans. The phases of a compiler are collected nto front end and back | os front
ct
where f does not begin with an A. ee
end includes all analysis phases end the intermediate code generator
Then, 0 rer ae
also known as Analysis phase. The front end analyzes the
We can eliminate left recursion by replacing the pair of productions with- intermediate code. The analysis phase of the compiler on ae aime os
A — BA’ it into core parts and then checks for lexical, gramm
; of
on eteam and symbol table,
A- aA’/e
phase generates an intermediate representati
ut.
(Right Recursive Grammar) which should be fed to the Synthesis phase as inp

ST
se
Semesteri,
a swe
Si; xth
10—2018

Front-end

c
S

D
»
n
fe : i

Synthesis petermuns ic Push do


po"
| janguages:

Code ine ,

c ode
ae Representation 3 Code 1. First of all it should be Type 1
des the code optimization phase and final code 8eneration,bate ) 9. Left hand side of Production can
? eT only one variable
The back end incl oa the target program from the intermediate code.Thig
The back as
mthesi
Aaa

as synthesis phase. The synthesis phase
Ck.)
generates the a | al = 1. |
phase is Type 3 Regular Grammar: a
the help of intermedia te source code representa tion and symbo] tahj . ;
program with
ve many phases and passes. 7 | ‘A janguages are exactly all fanireaig be thak tate Generate the regular
compiler can ha entire Drdenai e languages These
e Pass :Apassre fers to the traversal
of a compiler through the
|

« Phase :Aphase ofa compiler is a distinguishable stage, which takes inpy; V > rere
ut for then (or)
the previous stage; processes and yields output that can be used as inp
phase.
stage. A pass can have more than one n (N FA ) an d the Vv Tevr
st ic finite au to ma to
Q. 2. (b) Construct a nondetermini
)« (5
deterministic finite automaton (DFA) for the regular expression ((a/b)c*
Ans. ((a/b) c*)* : |
((a + b)c*)* Recursively Enumerable

6

((a+b) c”)”
Context-Sensitive

Context - Free |

ol Q. 3. (a) Show that the given Grammar is LL(1) but not SLR(1). (6.5)
Q. 2. (c) Explain Chomsky hierarchy of grammars. _ (3.5) | S > AaAb | BbBa
~ Ans. According to chomsky hierarchy, grammars are divided of 4 types: A> €
: Type 0 known as unrestricted grammar: Type-0 grammars include all formal | Boe
al
grammars. Type 0 grammar language are recognized by turing machine. These languages Ans. To show that it is not SLR(1) but LL(1), consider the LR(0) items in the initi
are also known as the recursively enumerable languages. | ea set for the augmented grammar:
Type 1 known as context sensitive grammar: Type-1 grammars generate the | s-5
context-sensitive languages, The language generated by the grammar are recognizedby | - § > AaAb | BbBa
the Linear Bound Automata.In Type 1 Te Oe | 1 A <a
1. First of all Type 1 grammar should be Type 0. | Boc
2. Grammar Production in the form of , ) . lh: 83° S
a —> pF S > * AaAb
count of symbol in o is less than or equal to B | a S — * BbBa
12-2018
Sixth Semester, Compiler Desi
gn
A. -e Right most derivation

a ider tl rat FOLLOW(A) = FOLoar


Andthi cons LOW(B) = (a,b). E>E+¢T
9 ae
nian rst state we have a reduce/reduce conflict, since the FOLLOW of, F
ake RB ve input terminal ‘a’ of the token we don’t know whether to retrace the Bay > E+T*g
: orS +s. Hence inthe LR parsing table we have two conflicting entries for acti TUle \ > E + P¥q =
7,
ame holds for action[0,b]. “nO a) > E +(B)*q } ] i
Now consider the LL(1) parser for the above grammar.
> E+ (Ta i
; FIRST\S) = {a,b} ) >E+ (T*P)*q : es
FIRST (A) ) == FIRST (B) = {e) / |Ne
—> E + (T*a)*g
FIRST(A) = FIRST(B)= {a, b} —> E + (a*a)*q. J\
FOLLOW(S) = {$) — T + (a*a)*a es %
The parse table is —> F + (a*a)*g é | |
a b $ Set (a*¥a)¥g
Ss ‘| S-—AaAb | S— BbBa g. 4. (a) ae — descent Parser with example «
‘ x ise Recursive descent is a top-down par 5)
a — noo from the top and the input is read from aa technique that constructs the parse
. to right It uses procedure
B B —E Dt. tree .
al and non £ terminal entity. This parsing techni . 8 for every
No conflicts, so the grammar is LL(1). : term ake a parse tree, which may or may not re b due recursively parses the input
Q. 3. (b) Give parse tree’s and derivation’s (leftmost and rightmost) forth, | t°™ ut string means on the basis of usin fap ack-tracking. It recursively parses
grammar and input string given below. . (6) e oe 2 ttae | 6 one instance of a command or event to
genera
E > E+T iT

T— T*F/F Top-Down
F > (E)/a :
Input string: a + (a*a)*a a
Ans. Recursive Descent

E => E+T/T x meth.”


T— T* FF Back-tracking Non Back-tracking
, ——
F > (E)la
Input strong: a +(a*a)+a Left-most derivation Parse Table | 1
Left Most derivation 5 esas E | Predictive Parser
| E> E+T LVN
> T+T Bt Vee + ee
> F+T | i iS | | LL Parser |
>atT | : :
; E | Top- down parsers start from the root node (star t symb ol) and matc h the inpu t strin g
> A+ Tek
»+a+F*F 4 | against the production rules to replace them. |
E | eal - give
| n below.fn
> a+(E)*F e Q. 4. (b) Construct the canonical parsing table for the gra mma r
+> a+(T)*F ! S > aA Be aot ,
> a+(T* F)*F : “ A>Abclb
>a+(F*F)*F | /\\ 1 Bod’
> at(a*F)*F | | Ans,
>at+(a*a)*F a SO: eS > eaABe
> at+(a*ae
a)*a a S$): . S> ae ABe
i Se
ta a 5

ler Design
OniR
14-2018
sth Semester, Compt
Sixth
A-> *Abc | eb | Q. 5. (b) Translate
LP. University {
the ti “WS Tech Akash Books
A-» *Abcl *b rosea -B*(C +Dyp expres:
S2 A> be And. GUM “©Presentati on ON 80 quadrup|
S3 a Si e Be a

mere B
=C
A> A*be
g=t,+D
54: A-> Ab*c Yat, *¢

S5: A Abc* eg
S6: S+ aA*Be A =t/t,
Bo ed ,
OP ——
Si: Bo>d°* | Ps Arg1 | Arg in
minus B sand
S8: S > aABee . (1) t,
S9: S— aABe (2) 5 t
S10: S’> Se 3) b D 23
| 3 “ t, i _
t
( ) a b $ S (4) e : —

Be e ae | 2) ‘ SP Lao
/ ; ae ? :
it M1 P 12
+ ae - : ‘Triple Representation
2 3 3 at ) Number Op
Aigt | Arg2
3 [ete) {S?6) (0) |Uminus | 8
4 (1) Cc
T | S>AB SAB (2) + a) | Dp
B->Bb B>Bb|B-Bb pF ub G | @
: | 3 | | obs | | 7 (5) |i }@ le
. B->b by 1 ae | 7
oe: T ee te “A ; Q. 6. (a) What are the uses of symbol table in differen t phases of compi
US eA AN SE Ores Pee ee Se ee Design? | (3)
Q. 5. (a) Construct a syntax directed translation scheme that translate: | Ans. Symbol Table is an important data structure created and maintained by the
arithmetic expressions from infix to postfix notation. The solution should include compiler in order to keep track of semantics of variable i.e. it stores information about
context free grammar and semantic rules. Show the application of your scheme scope and binding information about names, information about instances of various
with the input given below. (6.5) | entities such as variable and function names, classes, objects, ete.
Ans. 1+2/3-4*5 | It is used by various phases of compiler as follows :- “ia
1. step '- Parenthesize (using standard precedence) to get (1+(2/3))-(4*5) _ 1, Lexical Analysis: Creates new table entries in the table, example like entries
2, Apply the above rules to calculate P{(1+(2/3))-(4*5)}, where P{X} means “convert | about token.
the infix expression X to postfix’. F ) | 2. Syntax Analysis: Adds information regarding attribute type, scope, dimension,
A. P{(1 + (2/3))-(4*5)] _ line of reference, use, etc in the table. ‘ |
for —
B. P{1 + (2/3))} P {((4*5)}-
3.Semantic Analysis: Uses available information in the table to check
ticall
pete i.e. to verify that expressions and assignments are
C. P{1 + (2/3)} P {4*5)}- semantic ally y cocorrect(type
D.

snag er sal
P {1} P {2/3} + P{4} P {5} *- ‘ecking) and update it accordingly.

‘Tntermodi ate Code gneaion


EH. 1P{2) P {3}/ Ow! uch and
+ 4 5*-
F. 123/446 *- ng
"hat type of run-time is allocated and table helps 1m 4
information.
sign
Sixt Semester, Compe
sixth

ns ee
oie eb
14-2 A eAbe st LP wRiVersity {8 Techy.
Q. 5- (b) Translate the tee


“Akash Books
A7.°A be!
pepresom a ee -B* (C+D) me oe
A> b°* “XPression to quad
~ 2° * B e Ans. Quadruple “Presentation
os S—-— fa se
Wo« B sl * t, = Uminus B
t, = Cc :

=a Aare.
mst, *¢,

= ; 5+ aA
ed tre A =ty/t,
a de
S7: Ba? OP Arg 1 Arg2 |resut |
s aABere (0) | Uminus B
Ss: ‘
) 4 4
So a A Be ( ) e
S9: Acai b
$10: Bike ' (2)
Bhi fui
+ vy) D t,
ft @ebae
(S) f 4 ts A

Triple Representation
-
N umber Op | Arg 1 Arg >

(0) | Uminus B
(1) C
|
(2) + (1) D
= ok
B->Bb/B (3) ; (0) (2)2
snmincutatiinuia bein ainasoilpacascnonauananciasis
1, j\A Aa |
fa aalA a si |
y (4)
| hat e

Bb | Bob | Bb St be
$$$}
e3
Q. 6. (a) What are the uses of symbol ta
ADs eee ee ble in different phases of compiler
Design?
|
Q. 5. (a) Construct a avntix directed translation scheme that translats! Ans. Symbol Table is an important da
arithmetic expressions from infix to postfix notation. The ta structure created and maintained by the
solution should inclu! - compiler in order to keep track of
context free grammar and semantic rules. Show the application of your schem} scope and binding semantics of variable i.e. it stores information about
about names, information about instances of various information
with the input given below. | | (63) E entities such as variable and function names, classes, objects, etc. -
Ans, 14+2/3-4*5 It is used by various phases of compiler as follows :-
. ar |
1. step ': Parenthesize (using standard precedence) to get 1, Lexical Analysis: Creates new table entries in the table, example like entries
(1+(2/3))-(4*5)
2, Apply the above rules to calculate P{(1+(
2/3))-(4*5)}, where P(X} means “convtt about token. _
the infix expression X to postfix”,
| . i 2. Syntax Analysis: Adds information regarding attribute type, scope, dimension,
A. P {(1 + (2 /8))-(4*5)} |
line of reference, use, etc in the table. wee i
B. P(1 + (2/3))} P ((4*5))-
C, P{l + (2/3)) p (4*5). ! 3.Semantic Analysis: Uses available information in the table to check for
D, P {1) P {2/3} 4 P{4} P {5} #- ¥§ Mantics i.e. to verify that expressions and assignments are semantically correct(type
E. 1P(2) P13)/ 4 4 5*_ | |
ae
pies
_ thecking) and update it accordingly.
4. Int : eneration:| Refers symbol +table for knowowi
ing ng how much aoe
and
F 123/445 %.
_& t type of run-ee
time eg is ste and table helps in adding temporary variable ©
, information.
16-2018 Sixth Semester, Compiler Design

5. Code Optimization: Uses information present in symbol table for Magh;


dependent optimization. N
6. Target Code generation: Generates code by using address informatig,
identifier present in the table.
Each entry in symbol table is associated with attributes that support COMpiley; Pro_ONe Symbol
Table x
different phases. PrO_ two Symbol
Table

Q. 6. (b) Explain how scope information is represented by symbol table ( Sa Pa int_| \ two. \ var \
one _2 |
Ans. A compiler maintains two types of symbol tables: a C8 tal tableyj var \ int
jones | var | ~ Wes var | int |
can be accessed by all the procedures and scope symbol tab sal sabes “reated ty

\
each scope in the program. To determine the scope of aname, sym are AITANge,
in hierarchical structure as shown in the example below:
one Sf YE ‘int | | one: 6] var | int x :
int value=10; a | int. |
a var | int | \one_7 | var = =
void pro_one() inners >
inner scope 1
jobal symbol table contains names ¢
~ Winer scope: 3
int one_]; re names, which should be availalie ts af rere an
int one_2; proe es mentioned in the pro_one symbol table (and all its child tabi TRE above. The
ue \ . pro two symbols and its child tables. “8) are not available
ae bol table data structure hi
This sy™ = erarchy is stored in the semantic analyzer and
int one_3;,_ _|_ inner scope 1 in a symbol table, itis searched usin qthefillowing
| whenever a name needs to be searched
orithm:
i 4; |
ee ae » first a symbol will be searched in the current scope, ie. current symbol table.
: , ifaname is found, then search is completed, else it will be searchedin the parent
| ymbol table until,
int one_5;
~ . either the name is found or global symbol table has been searched for the name.
{ Q. 6. (©) Explain different data structures for symbol table implementation
| \

int one_6; — | inner scope 2 \ Ans. Following are commonly used data structure for implementing symbol table :-

| ‘| 1. List-
int one_7; me s an d as so ci at ed in formation.
is us ed to sto re na
| / e In this method, an array an d ne w na me s
tained at en d of all st or ed re co rd s
e A pointer “available” is main we | | : ss
in th e o r d e r as th ey arrive
are adde d li st ti ll av ai la ble pointer an
start from be gi nn in g of
void pro_two() 4 ' To s e a r c h fo r a n a m e we
a r e d n a me”
ror é y s e of u n d e c l
not found we get an er | | that it is not already present
aaa
int two_l, ; ) * While inserting a new name x See RL
|
otherwise error
occu
occurs
rs i.e.
L€-
“Mult iple defin ed
ae ae O(n) on average
: rae 4
| ¢ Insertion is fast Q(1), but lookup 8 eee |
int two_2; : | Ee
» Advantage is that it takes minimum amo |
{ N
|_ immer scope 3 2. Linked List - 1 wked list. A link fieldis added to each record.
inttwo.3; : coma; lementation is using link oe link of link field.
int two_4;_ | This UP of names
.18— done i
done 10
n order poin ted
by
first record of symbo l
table
e S hing
Caearen : : ‘ntained to point to O(n) on average
/ ¢ A pointer “Pirst” is mal is slow for large tables - Uo
int two_5; “at f a s t O C ) , b u t l o o k u p
n . | © Insertion is
| P e
arch i c a l s t r u c t u r e of s y m b
1 6 : o
be re p r e s e n t e d in a h i e r
Thehe above program can
er, Compiler Design e
Sixth Semest
18-2018 University
HaHatsh Tab le - eme ined — a hash tah]
. are maintaem 1B ech}.
8, sing sch two OO a to impl ent symbol table, and 8Yp, ,
* in spe seammontly used met 10 5 0 to tablesi
ize ~ ' Theg,
a hy
a
3% 0: ; i <n; : 7)

is an array with a range:


Ee a I bol tab le. 8h sb
Ahash table lS © "+ sy«em
mb . function that will result , ps
ster pointing to names of
pone ry 1» for a name we use has any Nteg, gli a a:
e Tosearchi Th, apr’
“7 — very fast —_ O(1). \ aa
0) to tablesize 1 be made
_dinn and lookup can ae . yr son Elimination-
sa dv an tage jg that gn aS den
ie a ‘ is qui ck sea rch is pos sib le and di
. ‘aAdvantage }s ashi 15 ress! SREEEECS 18 & comp:
til to45 i implemen t.
slicataed e ions ns
ressio (i.e., » th they all ay Pile_- r optim: izae
SPtith
Sis replacing them rd, Pe Dat searches ;
vont to complement symbo
l t bl . t . "ca
perform
Search
is to use binary Seay
Tree
able
Binary =<
if ft fo expras.
a
sub © Variable an end OF instances
0 a iAe d d rig ht chi ld er
hte He jp ity gnalysis.cg. CompilPUC we tt est Clinmieag ge te Oe eee Uteeed Wty
4 two lin k fie lds i.e. left an zon o
T Wri ter
w s s distin
Gst inggn: uis h- ett
t e ion
y o n , g
© y pbasUD va} ue. The
TS eae id of root node that always follo
e All names are created as child 0 W th Pron. by is) al common subexpression ¢ imi: ti "WO lkinds of CSE ° available
WP .‘ Joe be xpres .
on sube re on w orks ; wikk: ;
tree.
spr age a io n an d lo ok up are O( log Se n) on average. pet wa TON clittination s .™4singlhaee :
se rt re ducting 7
ata flow analysis of which
e In St re ng th
’ : ds rely on WOTKS on an enitina block
on ? Ex pl ai n jap
ed code optimizati got)a program. ! “SSIONS are ayi
Q. 7. (a) Why we ne ‘mination and loop optimizat;
expression elimination, dead nope —_ 7 one tee Win, jot eel
) J for |
pict g;
+ i
phase ig 4 (
a" * *

optimization: The code optimization in the synthesis


Ans. Code
a F _pte * €;
transformation technique, which tries to improve the intermediate code by pp,
consume fewer resources so that Saat ae code will result. Come a code will be transformed to:
si a th pte:
optimizing process should meet the rouow=ne opjec
PY
The optimization must be correct, it must not, in any way, change the heal (oP
¢
of the program. : y ,2tmP ea
¢ Optimization should increase the speed and performance of the program, getmP . storing and ween al
¢ The compilation time must be kept reasonable. theae cost O P 1s less than the Cost of calculating b¥e an
¢ The optimization process should not delay the overall compiling Process, fi = code Elimination: Dead
does not affect the code elimin
progra m akation
| j ba
wee
compiler optimization to
Optimization of the code is often perfo rmed at the end of the development y , bes de which
since it reduces readability and adds code that is used to increase the perform; 4 ; a Oi nad code stripping, or dead code stripDead codeine tnt dead code
on, stre ngth redu ctio n is a op, jpova,
ey impo rtan t consideration in some contexts, and it =. shrinks —
Strength reduction: In comp iler const ructi
+, irrelevant operations, which reduces its running time. Itcan also enable
optimization where expensive operations are replaced with equivalent but less expeng “executing irt peratio
operations. The classic example of strength reduction converts “strong” multiplies i }jor oti izations by simplifying program structure. Dead code includes code that
inside a loop into “weaker” additions — something that frequently occurs in am) + wor oe be executed and code that only affects dead variabthat lesis, ir elevant to the
addressing. | 3 yl : n. For example: »
Examples of strength reduction include: Dhl:
¢ replacing a multiplication withina loop with an addition ES ae Eco
¢ replacing an exponentiation within a loop with a multiplication be:
c =7; > : : | : inti;
ane
for (i = 0; i < N; i++) : | ae: i=l: /* dead store */
Ee , | global = 1; /* dead store *!
yli] = "1; global = 2;
return;
can be replaced with successive weaker additions global = 3; /* unreachable */
P|
.

is never us.
|
: : a

the value assigned toi


| } inthe above example.
| ity
asse mbly TANB USge a6 ap <ha Maka,
vate symbolic instructions and “th Alas,
th Book
ge Management _ CTosfy Cod
meor® Meilitig ofa Mra, 18-21
appine the names In the Source Bes in CAsiap We P
‘oid FO M t end and the code. generat, e Program to we Nerating, an
voit ye i table entry for name fy. Aname ia 1 bates, ‘ Code.
| 5 OMEN fr et dats ar:
bal == 2; 2;
global . pe3? Mt jote ioned sele
rmin for ctio
the nname. °M the oynatable
trongtryoc a ment 18 done }‘
jruct — Hi
a
| 2 elatiy,
return ;

;
008 optimization is the process of increasing as
LOO
gelectin’{ions chat GE “all improve gh © offic;
com
-
. It plays an j Ui agtruc Plete and urn: “leney Of the a
ociated
heads ead with loops Mpo 4 ne 2 s a major rol ‘
making effective uge of os he Pe
speed a ‘avg iesaia efficien is conga Sram. It Neludag
| formance imize the |
‘mproving
Ea
cache per to opt imize the loops in order rallg : |, effi efricien
ey : ‘ wom then instruc eee But; eae and machine
: ies. It becomes necessary U0 OP
processing ee sparen be OD timized by the following techniques: Save (py1 Re gister allocation issues _ on Selettion Stra; * Ot care about
cycles and memory. O ve ; ‘ : :
code that resides in the loop and compute | yse
of registers make the “OMputations faster:
. ar ja nt code : A fr agme nt 0 8 fh at ntilization of registers is important The
as Parison to
a we each iteration 1s called a LO te ee ean be MO Ve d o ,ffic t e se =” ADO Useof na: that of mem
mp ut ed on ly onc e, c a e o a aa Ite rat ion | tw0 gu bp ro - | are subdivided f 80
of2 ethe aes lo p by saving it to be co
- A variable is called an induction variable if it, Val] 1, During Register allocation - we select only ai : into
¢ Induction analysis : : : on Ue ters at each point in the program OSe set otvavabes that
} jy the regis ; | | said
altered within the loop by a loop-invariant value.
ns t co ns um e mo re CP U Cyc les tin) | 9, Du ri ng 4 su bs eq ue nt Re gi st er as si gn ment oe
reenngth reduction : Theons re
¢ StStr uld exp
shoare placed thawith cheaper expressions witha
resio
be res ss the variable. | : the specific re a
and memory. These expression ; 7 OMY fp acce | bist er
is pic ked
: | gvalua tion order -
compromising the output of expression.
7 ive “the code gen erator decides the or der in whi, | |
Q. 7. (b) Write short notes on:
(i) Problems in code generation | | wrder of computat : ects the efficiency of the targetead:
: ions aff a executed. The
Ans. Problems in code generation: Code generator converts the intermedi, require only fewer registers to hold the _)
ders, Some Int erm edi y computational
emanate
that can be readi ly execu ted by the machi ne , sicking the best order in gener al case is a difficult results, However,
representation of source code into a form
code generator is expected to generate a correct code. | 3 | Approaches to code generation issues: Code NP-com plete
generato r must ,
problemalways
:
Issues in code generation: SS, genera
Input to code generator:The input to code generator is the intermediate ocd night face. Some of the design goals of code generator are:
generated by the front end, along with information in the symbol table that determine} , Correct =
the run-time addresses of the data-objects denoted by the names in the intermedia} _ aer
representation assuming that the they are free from all of syntactic and state semanti * Hasily maintainable
errors, the necessary type checking has taken place and the type-conversion operaton| * Testable
have been inserted wherever necessary.
| * Maintainable SSNs
T
IGP t PEO RM re ~
Q. 7. (b) (ii) Uses of Basic Blocks. | : , (3)
Target program is the output of the code generator. The output may be absolute |
Ans. Basic block is a set of statements which always exec inuta sequ
es ence one
machine language, relocatable machine language, assembly
language. ° 1 ther the other without getting halt in the middle or any poss
ibility of branching. Basic
a ae Ab pean language ag an output has advantages that it can be placedil} tink. 4, not contain any kind of jump statementin s them. All the 5 tatem nn a
memory location and can be immediately executed.
hsic block gets execute in the same order as theyappear in the block without nse
Be
a Ronee » - cteatcht line code sequence Wi
@] Lv control of the program. A basic block is a oe ue The code in a
Manches in exce pt to the entry and no branches out ee
basic block has: 3
“2-2018 Sixth Semester, Compiler Design

eIns ;
n tructiOne ent ry poin t, mea thin itit isi the destir stination of ajya;
nin
on anywhere in the program.g no code wit hin
|
. * One exit point, meaning only the last instruction can cause the program to b
©xecuting code gin
; na different basic block.
The code may be source code, assembly
code or some other sequence of instructio
A sequence of instructions form n,
s a basic block if:
° The instruction in each position dominate
s, or always executes before, aj] thoge
In later Positions.

ES nse
* No other instruction executes between two instructions in

gn I
the sequence,
For example: Consider the three addres
s code for the expression a=b+c+q

2
(1) T, = be e
(2) Ay Se here
(3) es Ty | |
These statements will be executed in a
sequence one after the other and
therefore they form a basic block.
.
Le
a |

——*€
Time : 1.5 hrs.
hc
+ nye ee

Q.1. (a) How dependency graph effects the evaluation order in syntax
cyan

i jirected translation? |
(2)
Ror
<0 oe

Ans. Order of evaluating attributes is important. Dependency graph is general rule


wks

| ! for ordering. Dependency gr aph is a directed graph and it shows interdependencies


+ between the attributes. If an attribute ‘b’ ata node depends on an attribute ‘c’, then the
_ semantic rule for ‘b’ at that node must be evaluated after the semantic rule that defines
¢,;
{
1
| Q. i. (b) “Lexical Analysis is the most time consuming phase in compilation”
.
justify this statement. | 3 (2)
Ans. Lexical Analysis is the first phase when compiler scans the source code. This.
_ process can be left to right, character by character, and group these characters into tokens.
| Here, the character stream from the source program is grouped in meaningful sequences
_ by identifying the tokens. It makes the entry of the corresponding tickets into the symbol
_ table and passes that token to next phase. The primary functions of this phase are:
¢ Identify the lexical unitsinasourcecode ~
¢ Classify lexical units into classes like constants, reserved words, and enter them
_ indifferent tables. It will Ignore comments in the source program.
° Identify token which is not a part of the language |
FA for the re gu la r ex pr es si on (a + b)* abb + bba. (2)
Q. 1. (c) Cr eate ND
for re gu la r expressi on : (a + b)* ab b + bb a
Ans. NDFA
a,b | |
|

b e t w e e n
e s L-Attributed defA inition and S-Attributed
Q. 1. (d) D i f f e r e n t i a t e (2)
3
definition. | it is ca ll ed as.
S DT us es on ly sy nt he si ze d attributes,
S-Attribu t e d S D T : If a n
Ans. re |
S-attributed SDT. the values of the pa re nt
al uate d in bo tt om -u p pa rsing, as
¢ S-attributed SDTs ar e ev
d nodes.
Nodes depend upon the values 0 f the chil
ac e of R H S .
e p l a ce d in r i g h t m o s t pl
¢ Semanti c a c t i o n s ar in he ri te d
ze d at tr ib ut es an d
-attri SDT: If an inh SD T us es bo th sy nt he a
si e t val ues fr om le ft si bl in gs
ib at er n tha t eri ted attrib ute can inh eri
: L- et er
' attributes with a restrictio
d as L - a t t r i b u t e d S DT.
Only, it is call e rs t an d le ft -t o- ri ght
D T s a r e e va lu at ed by de pt h- fi
t r i b u t e d S
¢ Attributes 1n L-at
Parsing manner.
99019 Sixth Semester, Compiler Design
LP. University-(
re in RHS. B,Te ch}-Akash
e Semantic actions are placed anywhe Books
;
Pee cna rs ite 201 9-3
Ifa definition is S-attr ibuted, then it is also L-attributed but NOT Vice-vop
7k 2 T > .
os 8 ’ io LU

a, 6
L-attribute SDT 7 Si
g sl 0 |

9 Tg re |
S-attribute
SDT
3 4I, |

I, \
s = Si
nce there are no muliple actions in an
ae y entry, the given grammar is LR(1). However
Q. 1. (e) Remove left recursion from the following grammer: when obtaining the LALR(1) parsing table b by Merging states, we will
merge states 1.
and lg, and the resulting state will be ag fol] ows:
AaAblIb
|
S >
lng A d., a/e
A + Ablalab’ 2) | B—>d, a/c
Ans. There is no left recursion in S > AaAb/b. Left recursion is only in: It is basically a reduce-reduce conflict. So, the given grammar is not LALR(
1).
| A —> Aalalab i Q. 3. (a) Calculate first and follow for all non-terminals for the followin“g
A — aA’labA’ grammar.
| A' > bA’/e- . E>E+TIT
Q. 2. Prove that following grammar is CLR (1) but not LALR(1) ToOTFIF
S—Aa |bAc|IBclbBa FoF*lalb
(10)
Avpd Ans. First remove left recursion from the above grammar.
E _»> TE!
Bod
BE = +TH/e
t = FT’
>) - 9 *FT/e
‘ig: (0) S'> 8,$ ES lie sesi) Tr
(1)S—> .Aa, $ 2 > aFF’
(2)S > bAc, $. Af, S> Aa, $j}. Sag. $ | i = *F'/c
(3)S—> .Bc, $ ia os | Now calculate First and Follow. .
(4)S > .bBa, $ Is: S—> bAc, $-“—(1,: S + bA.c., S$} (144: S > bAe., First (E) = First (T) = First (F) = {a, b}
(5)A—> d,a S > b.Ba,$| Bo -— = = (+, €}.-
bBa., $
S >i
lo First (2) =
(6)B—> .d.c és ResidOn (ls: S>bB.a.S): a First (T") = {* ¢}
& y : Boda | ee 7 {* <}
First (F") =
Boda]
wS2tetle |
as Follow(&) = ($}
I; A> d.,a ] Hot Set PEG, Follow (E’) = Follow (E) = (3)
=
B-d.,c
Follow (T) — = First (E’) u Follow (E)
= (+, 3}
Based on the state diagram, we derive the L.R (1) parsing table as follows:
~

Follow (T)
0 Follow (T’)
State Action | | Goto
a b c d ¢ So B Follow (F) eeoirmst (T: ’)a U waFonellow
U.
. 0 S, a ay 1 2 4 sa
Follow (F’)’ te, selleow (F)
Fo

I 2
1
S,
acc |

dictive parsin g ta bl e fo r th e rem sho -

3 So 7 8 Q. 3. (b) Create a pre


parta. —
4 510 ae,
4—2019 Sixth Semester, Compiler Design

Ans. Predictive Parsing table for previous question


+ * a b |

; E > TE’ E + TR’ Step 2: gcc lex.yy.c


BR’ BR’ —> TR! Step 3: ./a.out
T T -—> FT’ T-> FT’ —E>>
L: ">e oan: | :
5 - F > aF’ F+>bF’ |. i
‘+e F > *F : int count = 0;
Kk’ Ee a
tT a To }
ToTo
Mutliple enteries (more than one entry) [A-Z] {printf(“%s capital lett
er\n”
Q. 4. (a) How lex (flex)tool can be used to create lexical analyzer? Riu count++;|
phi 3 ge
with the help of an example. {printf(“%s not a capital letter\
4 = n’, yytext);}
Ans. FLEX (fast lexical analyzer generator) is a tool/com \n {return 0;}
puter progran %)« ToT
generating lexical analyzers. PYOBTAM {oy ) int yywrap(){}
Lex.yy.c | int main(){
Lex file ; | Sequence yylex(); 3
—__—» compiler. | C Compiler -———| a. CoE Ne nae of printf(“\nNumber of Captial letters”
Lex.| file . aeeuas “in the given input - %d\n’”, count):
Or lex |
Source program return 0;

Input Stream
}
Q. 4. (b) Define Synthesized and inherited attributes. Identify synthesized
Step 1: An input file describes the lexical analyzer to be generated nam _ and inherited attributes from the grammar given below.
ed lex lis (3)
written in lex language. The lex compiler transforms lex.] to C program, ina file
thatis Do TL . L.in=T.type; L.x=T.y
always named lex.yy.c. _
| L-L,, id L, .in=L.in; L.x=L,.x
Step 2: The C complier compile lex.yy.c file into an executable file called a.out. Tid
Step 3: The output file a.out take a stream of input characters and produce a stream Justify your answer. |
of tokens. | Ans. In Syntax Directed Definition, two attributes are used one is Synthesized
Program Structure: In the input file, there are 3 sections: attribute and another is inherited attribute. An attribute is said to be Synthesized
child nodes
attribute if its parse tree node value is determined by the attribute value at
1. Definition Section: The definition section contains the declaration of variables, value is
whereas An attribute is said to be Inherited attribute if its parse tree node
regular definitions, manifest constants. In the definition section, text is enclosed in “%| determined by the attribute value at parent and/or siblings node.
%}” brackets. Anything written in this brackets is copied directly to the file lex.yy.c _
2 :
//Definitions ve : NK L ee
Jo} T type=integer
2. Rules Section: The rules section contains a series of rules in the form: pattern | A
action and pattern must be unintended and action begin on the same line in { } brackets. int he | me
,_ inh-integer id addTypefid.entry,integer)
The rule section is enclosed in “%% %%”. | 3
[0%o
oe | ae nt eger)
pattern action : id add Ty pe ld .e nt ry ,i
Oa
To%o
3. User Code Section: This section contain C statements and additional functions. |
We can also compile these functions separately and load with the lexical analyzer. ad dT yp el id .e nt ry , in teger)
id
How to run the program: To run the program, it should be first saved with the | T is s y n t h e s i z e d at tribute.
n h e r i t e d a t t r i b u t e and
extension .1 or .lex. Run the below commands on terminal in order to run the program L is i
file. tS
LP University-(B.T
ech}-Akash Books

END TERM EXAMINATION [MAY, 2019;


tinker: Linker is a computer
ether in order to make
an ©xecutable file, All t
SIXTH SEMESTER [B.TECH]
to8 ,
geP arate assemblers. The major task of 4 linker is
to search and locate refe
COMPILER DESIGN [ETCS-309}
. in a program a refi renced
ee waking ‘ihe bleeeiie determine the memory location where these éaee Vita
joade g ; instruction to have absolute references .
Time : 3 hrs. M
Note :- Aitem pt five questions in all including Question No. 1 which is compulso M. 75
one question from each unit. "Y: Se a initiate execution.
Q.1. Attempt any five parts from the following:
q. 1. (d) Explain cross compiler
with a suitable example.
Q.1. (a) What do you mean by ‘pass’ in a compiler? Differentiate bety (5)
Ans. Refer to Q.4. (a) First Term Examinat
multipass and single pass compiler. ion 2017, (Page No. 2017).
*e Na Q. 1. (e) Write a regular expression for ea
Ans. A Compiler pass refers to the traversal of a compiler through the entire r (5) ch of the following sets of binary
strings: Use onl y the basic operations.
Multi-Pass Compiler One Pass Compiler = ~~ (5)
(i) contains the substring 110
l. Itreads the program Several 1. Itreads program only one acinar iain (ii) doesn’t contain 110 as prefix
times, each time transforming translate it at same time. Ans. (i) (1+0)*110(1+0)*
it into different forms. | (ii) 0°1(1+0)"+10°(1+0)*+ (140)°
:
2. They are “slower”, As, more 2. They are faster
number of passes means more
following grammar: ies = T
E-E a cm
Q.1. (f) Left factor the
execution time |
3. Better code optimization and 3. Less efficient code optimization Ans. Left factor of above grammar :
' code generations and code generation.
E > EE’
4. Itis also called wide compiler. 4. Itis also called narrow compiles
As they can scan each and K’ > +T/e
as it has limited scope.
every portion of the program. T > int(E)
|
®. Memory occupied by one pass . Q.1. (g) Differentiate between SDD and SDT. (5)
5. Large memory is required by compiler
can be reused by subsequent Ans. SDT: It is a kind of notation in which each production of CFG is associated
pass therefore, small memory with a set of semantic rules or actions and each grammar symbol is associated with a
is required by compiler set of attributes. Therefore the grammar and the semantic actions combine to make a
6. E.g. Modula-2 Lang uses _ 6. E.g. Pascal and Capital-lang uses one Syntax Directed Translation.
Multi-pass compilation pass cempilation. It is of two types:
1. Synthesized Translation: These attributes get values from the attribute values
Q. 1. (b) Why do we need to have lexical analyzer generators? What a its of their child nodes. To illustrate, assume the following production:
advantages? ? 6) | |
S — ABC
be a synthesized
Ans. Lex is a program designed to generate scanners, also known as tokeni
zers, IfS is taking values from its child nodes (A, B, ©), then it is said to
which recognize lexical: patterns in text. Lex is an acronym that stands for “lexic
al attribute, as the values of ABC are synthesized to S. -
analyzer generator.” It is intended primarily for Unix-based systems.Its main purpose mpl e (E > E +), the par ent nod e E get s its val ue from its
As in our previous exa
is to facilitate lexical analysis, the processing of character sequences such as sourc : rib ute s nev er tak e val ues fro m their parent nodes or any
e code child node. Synthesized att
to produce symbol sequences called tokens for use as input to other programs such as sibling nodes.
parsers. Lex can be used with a parser generator to perform lexical analysis. It is easy, nt ra st to sy nt he si ze d at tr ib ut es , in he rited
2. Inherited Translation: In co fo ll ow in g production.
for example, to interface Lex and Yacc, an open source program that generates code for and/or sib lin gs. As in the
attributes can take values from parent
the parser in the C programming language. | pecs S — ABC S, A, an d C. Likewise. C
Q. 1. (c) What are the responsibilities of Loader, Linker and Assembler in can ta ke va lu es fr om
Acan get values from S Band C.B
the compiler environment? | | ) 6) | can ta ke va lu es fr om S, A, and B. a
is exp ande d to te rm in al s as pe r a gr am matical
Ans. Assembler: An assembler translates assembly language programs into Expansio n: Wh en a no n- te rm in al
_ machine code. The output of an assembler is called an: object file, which contains a rule
combination of machine instructions as well as the data required to place these
uctions in memory,
89019 Sixth Semester, Compiler Design

LP.
Ans. Calculate First ; Universit |
YB Tech|~Akash Books
nd Follow 2019-9
First (N)
First (A) \ F

il
First (A) irst (By ~ la
. b)
(a)
First (B)

nouh
la, b)

uojonpay
@ \

First (C)

Expansion
(a)
Follow (N)
Follow (A)
(3)

+
la, b, $}
Follow (B)
la, b, $}
Follow (C)
la, b, $)
id
,
id
Reduction : When a terminal is reduced =
to its corresponding non-t b ¢ |
according to grammar rules. Syntax trees are parsed top-down and left to right Wh, Mina] A Aa
reduction occurs, we apply its corresponding semantic rules (actions) * WDeneye, A>cAC<
Semantic analysis uses Syntax Directed Transl B+ CBC
ations to perform 3 the Multiple entries in
Semantic analyzer receives AST (Abstract Syntax Tree) from its previous ae tas | B+b

Zaw
Ca A. So the grammar
analysis). Semantic analyzer attaches attribute info rmatio Be (synt.. N + AB. is not LL (1)
n with AST.
Attributed AST. whj
|
tax
v7 Which are call
NBA
Attributes are two tuple value, <attribut Q. 2. (b) Write SLR Pars
e name, attribute value> er for following gramma
r (7.5)
For example: SR R—> Rb
ae
int value = 5; R-a |
<type, “integer”> Ans. SLR Parser for the following:
<presentvalue, “5”>_— S >R, R > Rb/a
For every production, we attach a semantic rule.
SDD (Syntax Directed Defination): A CFG
where each grammar Produc
A—> a is associated with a set of semantic rules of tion ‘
the fo rm b = f(cl, c2, ..., ck); where: aa

bis a synthesized attribute of A or an inherited attribute of rpeeaae = C Ss— S.


one of the grammar symbols |
in a cl, c2, ... are attributes of the symbols used in the production.
Synthesized and Inherited Attributes | |
| S>-R | = < (Ss
—2R.ae b ‘Sa
OTE
4 R=
sen
Rb. >

* Attributes are values computed at the nodes of a parse tree. Ro Rb/ S 's
° Synthesized attributes are values that are com puted at a node N ina parse tree
from attribute values of the children of N and perhaps N itself. The identifier
s
See a Bee

“~CR> a.
$$, $1, $2, etc., in Yacc actions are synthesized attributes. Synthesi Se

zed
attributes can be easily computed by a shift-reduce parser that kee SoR
ps the values
of the attributes on the parsing stack. R- Rb/a —
¢ An SDD is S-attributed if every attribute is synthesized. Calculate First and Follow .
° Inherited attributes are values that are computed at a node N ina parse tree _ First (S) = First (R) = {a}
from attribute values of the parent of N, the siblings of N, and N itself. — Follow (S) = {$)
* An SDD is L-attributed is every attribute is either synthesized or inherited from Follow (S) {b} U Follow (S)
the left. : ~
(b, $)
UNIT-I |
Q. 2. (a) Prove that the following grammar is LL(1) by constructing predictive
parsing tale. ‘¥2@)
Prove that grammar is LL(1) |
FO

N- AB N- BA

wm
A-a A — CAC

_
Pon

B-b B > CBC


Ca
Ein

id
Ys
- » F 2
k k i
2
a ;

10-2019 Sixth Semester, Compiler Design


grammar? Construct the ope
Q.3. (a) What is operator precedence raae
ser for foll ; g.
owin
pre ced enc e par 3S_>
S_saldh I>IbSe Ic {Incorrect Question) ) A° B
AB NZAgramma T left
CB recy
js rsive? 9 If yes remove
J—>KLkKr Kod it

L-p Ans. S ‘
mar A
Consider the following gram ro AB
(L)la S>
>

b,8 0S Lis _ ie
Here Indirect left recu
rsion exists
Ans. An operator precedence grammar is a kind of grammar for forty
grammar is a context-free grammar that al CS
langua ges . An operator precedence |
z CBB/bB
ucti on na
has |
eith er an empt) y right-hand side (null prody ctions) B | =
the pro per ty that no prod Sa
t-hand side.
or or sajacent non-terminals in its righ Substitute C — Sa in place
of S
es ti on : | :
Operator prec en de nc e qu S3
=a SaBB/bRB
ng (a,(a,a)),
Construct the operator precedence parser and parse the stri Now remove left recursio <5 b
| A op pe i if n
3S
| woe Lvorligheay ; $ a
—_-
aBBs'/bB
| acho | — | -B > ;
;
Be I > > > >
|
St
| ene
e d y f
; ' ; i
;
3 ;

( < > > > : >


; i }
E>E/T {E.val=E,.val*T.val}
' i
E>E+T =
ce {E.val=E,.val-T.val}
; “4 : ? ae one es eeeee Edie r ——— —— oe cee

!
as <3 ‘
> = > : = |
; E->T {E.val=T.val}
| roe al Ss TF
: 5

ae L Ag2 <2 (T.val=F.val}


.
F>2/4/6 {F.val=2} (F.val=4}
< e > > } > (F.val=6}
!
: : Using the above SDTS construct the pars : :
; y oe 4/2+6/2 and evaluate E.val’ at root of the tree parse t tree for the 3
expres sion
| | |
ie Sef Fie eal eee meets ie ee at jie Ans. E > E/T (E.val = K,-val * T.val)
one ~eemniieem gia itate ti ti tp Rial ian em itn niall
{ Ait
ee ewes th he
Bp eet Dntiaetaeala ea oak SaneMeant Aare (omens!
Te E +> ->+ Bef (E-val = E,-val — T-val)
E = ¥ (E-val = T-val)
| Operator Precedence Table T os F (T-val = F-val)
the string as-
For parsing the given string (a, (a, a) ), insert $ symbol at both ends of F > 2/4/6 (F-val = 2) (F-wal = 4) (F-val = 6)
| 7 S$ (a; (a,a))$ Evaluate : 4/2 + 6/2 = E-val
. E=4
insert precedence operators between the string symbols as-
} $<(<a>,<(<a>,<a>)>)>$
Now scan and parse the string as-
$<(<a>,<(<a>,<a>)>)>$
$<(S,<(<a>,<a>)>)>$
$<(S,<(S,<a>)>)>$
$<(S,<(S,S)>)>$
$<(S,<(L.S)>)>$
$<(S,<(L)>)>$
$<(S,S)>$
$ <(L,S)>$
$<(L)>$
$<S>$ | ie
$ $
sign
Sixth Semester, Compiler De
12-2019
4. (b) Exp lai n S.a ttr ibu te and L-a ttr ibu te with example.
Q.
Ans.
e >; : :

class of attribute grammars charact,


S-Attributed Grammars are a bois 1. Quadruples
Ans. ose:
, doaat
a et
t only synthesized attributes. Inherited attriby ized by Quadrupl 2. Tripl
Iples
Se ‘nheridown
pelle ted attrib parentbu nodes to child
from utes, , tes,wy. : Pres- In quadry 3.1
ren nodes of the abstract syntax tree q hich following 4 different betas. ples representation, a ndirect Triples
must be ticanalysi® of the parsing process, area problem for bottom-up parsing beget op, argl, arg? rescte
me sh <up parsing, the parent nodes of the abstract syntax tree are created aftechee Here-
in boom” hildren. Attribute evaluation in S-attributed grammars can be ine a e TL; on field:
ofconve
all nient
of their chi :
ly in both top-down parsing and db bottom-up parsing. *POTateg P held is used f
Ifan SDT uses only synthesized attr a ibutes, it is called as S-attributed | SDT.
e e
° The result field T storing the tw
attributes are evaluated using S-attributed SDTs that have their semantic action, Trigies-is eka used for Storing the result of the
written after the production (right hand side). * Refe F representation,
FE. . Value = E. value + T. value * Tempovences
rary to the les
variab instru
arections are made }
not used
Indirect Triples- :
‘ * This representation is
y ‘ an enh ;
-* It uses an additional fasting aca over triples representation.
f desired GaGer. ay to list the pointers to the triples in the
i ° Thus, instead of positio
{ It aliaws the anene i pointers are used to store the results
E. value T. value aaa pumizers to easily T€-position th xe b
An 4 optimized code. € sub-expression for producing the
Sees Sle a | For example: Translate the followi
| aD a * | Wing expression to ~
As depicted above, attributes in S-attributed SDTs are evaluated in bottom-up Oe — aa c/éTf+b*e no quadruple, triple and indirect
parsing, as the values of the parent nodes depend upon the values of the child nodes, ee 4 dress Code for the given expression is-
L-attributed grammars are a special type of attribute grammars. They allow the Tl =e | f
T2=b*c
attributes to be evaluated in one depth-first left-to-right traversal of the abstract syntax
tree. As a result, attribute evaluation in L-attributed grammars can be incorporated T3=T2/7T1
conveniently in top-down parsing. ; 2 * T4=b*q@
A syntax-directed definition is L-attributed if each inherited attribute of Xj onthe — T5=a+T3.
right side of | | T6=T5+T4
A— X1 X2....An | Quadruple Representation-
depends only on ae Emeniis |
1 m |. Op Arg 1 Arg 2 Result
1. The attributes of the symbols X1, X2, .., Xj-1
2. The inherited attributes of A pie “ (0) 1T e f T1
> (1) * o =
Every S-attributed syntax-directed definition 1s also L-attributed.-
(2) / T2 Tl 73
Implementing L-attributed definitions in Bottom-Up parsers requires rewriting b : =
schem es. ° “ e s nd (3) *
L-attributed defini tions into translation
- (4) + a T3 TS
Many programming languages are L-attributed. Special types of compilers, th : TS T 70
are a strict (5)
narrow compilers, are based on some form of L-attributed grammar. These
; ee Triple Repwesentaling
superset of S-attributed grammars. Used for code synthesis.
or “synthesized attributes” associated wi | |
Either “Inherited attributes.”
occurrence of symbol X1, X2...Xn. . | e 7 | Location Op Arg1 Arg 2
(0) it e e
Q. 5. (a) What are different representation ofthree address. a
illustrate with example. ou Sy : (1) * bs; bis c
. (2) / (1) | (0)
Ans. Three-address code is an intermediate code used by optimizing con
(3) . = b a
aid in the implementation of code-improving transformations. ach TAC instruction
(4) . a (2)
at most three operands and is typically a combination of assignment and ‘a (4) (3)
1! (5) %
operator. Three Address code is implemented as a record with the address fields.
commonly used representations for implementing Three Address Code are-

i
Sixth Semester, Compiler Design
14=2019
direct Triple Representation- iP Ulva: }
Indi : catenins Aupndataeate iersity—(B.Tech|—Akash Books 2019-15
ay serve the foll
0 > owing

ca
36 i
(2) > 7. waa she Hames of all entities ca cere ealed ae Ubon She language in hand
° ‘ verify if a variable has been declared pee ee —
a (3) source tds sega are checking, by verifying assi
39 (A) ‘iG ax emantically correct enments and expressions in the
(5) fit ermine the scope of a name (scope resolut.
40
4 I ie data structures used for symbol tabl € faaare
; . Lasts
Location Op Arg 1 Arg 2 3. Trees 2. Self Organising Lists
(0) 17 e fn¢¢ LISTS: 4. Hash Tables
(1) * b e 1. It is simple and eas
t
(2) . (0) list records: yfc Haplement data structure for symbol table in linear
(3) |
a NAME 1
(4) + ay, INFO 1
(5) + (4) _ 3) NAME 2
(7 5) INFO 2
Q. 5. (b) Write three address code for following code segment.
| :
int a[10], b[10], dot_prod, 1;
int* al; int*b1;
dot_prod=0;
Ste
1=a;
: ie NAME N
By eae INFO N
. for(i=0; i<10;i++) 3 peal
=
dot_prod+="a 1 ++* *b 1++; ; 2.? One can use single array y to to store
st : associated
the name and its : :
information.
Ans. dot_prod = 0 3. New names are added to end of list on FCFS basis.
al = Ga 4. Method of access is sequential
bl = &b Advantages:
Belee) 1. Minimum soace requirement.
L1 : if (i > 10) goto L2 2. Easy to understand and implement.
rie Togas Disadvantages:
T4 = al+l 1. Sequential Access. .
‘al = T4 2. Amount of work done required to be done is high.
PSV =e SELF ORGANISING LISTS: 3
T6:= ‘bl +1 1. It is used to decrease searching time in list organisation.
bi j= 06 2. Special attribute link is added to information of name that allows dynamic
Ti venTSeTsS | features in list, this feature help in minimizing wastage of space and also promotes
T8 = dot_prod + T7 | reusability to some extent.
dot_prod = T8 | |
T9 = i+1
lite No
goto Ll
L2 Exit
UNIT-ITI :
Q. 6. (a) What is symbol table? Why symbol table is required? What typeof |
data structures are used for its implementation? : (7.5) |
Ans. Symbol table is an important data structure created and maintained by
compilers in order to store information about the occurrence of various entities such as
variable names, function names, objects, classes, interfaces, etc. Symbol table is used
by both the analysis and the synthesis parts ofa compiler. |
a University-{B Tech}-Akash Books 2019-17
16—2019 Sixth Semester, Compiler Design
9. Extra space is required
— First pointer points to the beginning of symbol table 3. Obtaining Scope of variables is diffients
— If we have names in the following order Types of Hash Function-
3—>i1->4>2 (1) Mid Square Methoa-
— If we need a particular record
we need to move record
to th ; ) Considera key K
Suppose we need 4, so new connection will be a list, at K = 3230 Bs
| (b) Find the squareof K
K* = (3230)= 10432909
(c) Apply hash function
(;) Hash function
(i) Henee HK) = will
32 eliminate | igits from both sides
digi ; of k2.
First Similarly
) K 71236 2435
K2 92359696 5929225
Avaliable ie = 29
(d) Deleting the number of digits from both sides depend on maximum no. of key=
| required, deletion may or may not be same from both ends.
(2) Division Method
Let Key =K
Mechanism : No. of elements =n
(a) Initial stage 3-> i= Hash function = H(K)
(6) 4" name is required | H(K) ea =K modm-1l
ob . Where m = Pri me
(c) move 4“ to front ge <
ss
fave to indecs 100 accueil ; crime number
dose to 1001s 97
=. OF

3—>1—>4—>2 =
3 hence we will choosem =— 97
(dJ)4—>3—>1—>2
Ad Let K = 3230
pert | e H(3230) = 3230 mod 97 = 29
1. Increase search efficiency. (3) Folding Method
2. Promotes Reusability to some extent. | | (i) Use this method for large
key values.
Disadvantage: . = (i) We can use two methods te get hash value.
1. Difficult to maintain so many links and connection. <a e — Shift Folding
2. Extra space requirement is there. ; a K = 324 987 626 191532 | '

1. Hashing is Important technique used to search the records of symbol table. Kl = 327
2. In hashing two tables are maintained. | K2 =987
(a) Hash table ns e K3 = 626
(6) Symbol table | | K4=191
3. It consist ofK entries from 0 to K-1. These entries are basically pointer to symbol K5=
table pointing to names of symbol! table. . 2663
4. To determine whether “name” is in symbol table we, use hash function ‘h’ such
that (Name) will result integer between 0 to K-1 we can search any name by position=
h(Name)
5. Using this position we can obtain the exact location of name in symbol table.
6. Hash function should result in distribution of name in symbol table. Ki =327
7. There should be minimum no. of collisions.Collision is a situation where hash K2= 789
function result in same location for storing names. - K3 = 626
8. Various collision resolution techniques are Open Addressing, Chaining, Rehashing. K4=191
Advantages: Fs " K5 = 253
1. Quick Search 2168
Disadvantage:
1. It is complicated to implement.
Sixth Semester, Compiler Design
18-2019

._ *eieliel
Rae
: 5 Aaatable
WAI
can can
havealsoatmost
be stored in : form of binary search tree. LP. University_(p.
two children-left and right
&e
i
child.
;
wie
more than key value st ed or several statically allocate
2. hacn rc
3. KeyKey va storeed in parent node is
lue St
valu of“hildre, geen in modern languages,
and less than right child.
4. Height of binary search tree is of the order of log n,
5. Maximum time required for searching and inserting is Proportional
ithm:
a) anita, ptr will point to root of tree.
(2) While ptr = Null do 3
(3) If Name = Name(ptr) |
then return true
(4) else if Name < Name(ptr) then moving the frame pointer and/or
Ptr:= Left(ptr) the Stack pointer.
Stack allocation has both advant
(5) else ptr :. = Right(ptr)
‘ © The space for the array ;
(6) end of loop.
Q. 6. (b) What are static and dynamic, storage allocation schemes use ai
data in compiler? 7 i
° Allocation is fairly quick: You just move the stack pointer or the frame pointer.
Ans. We know that arrays, records and other multi-word objects could be allo (5) Releasing the memory again is also fast — again, you only move a pointer.
either statically, on the stack or in the heap. The three
implemented and what their relative merits are: * The size of the array need not be known at compile time: The frame is at runtime
created to be large enough to hold the array. If the size of the topmost frame can be
1. Static allocation: Static allocati
on means that the data is allocated extended after it is created, you can even stack-allocate arrays during execution of the
in memory that has both known size and address at c a
function body.
. "

compile time. Furthe


t a Place

allocated memory stays allocated throughout the execut; rmore, the


¢ An unbounded number of arrays can be allocated at runtime, since recursive
functions can allocate an array in each invocation of the function.
: | 3. Heap allocation:
ograms that convert symbolic
machine code into binary machine code) usually
maintain “current address” Pointers to ° The array will not survive return from the function in which it is allocated, soit
both the text area and the data area. They als can be used only locally inside this function and functions that it calls.
o have pseudo instructions (directives)
that can place labels at these addresses and
move them. So you can allocate Space for * Once allocated, the array can not be extended, as you can not (easily) “squeeze”
Say, an array in the data space by placing more space into the middle of the stack, where the array is allocated, nor can you reallocate
a label at the current-address pointer in the
data space and then move the current-address pointe | it at the top of the stack (unless it is the same frame), as the reallocated array will be
r up by the sizeof the array. The
code can use the label to access the array. Allocatio released earlier than the original array.
n of space for an arra Ayof 1000 39.
bit integers (i.e., 4000 bytes) can look like this in sym In C, arrays that are declared locally in ,
i a func tion are stack allocated (unless
bolic code: |
¢ data # go to data area for allocation declared static, in which case they are statically allocated). C allows you to return pointers
to stack-allocated arrays and it is up to the programmer to make sure he never follows
baseofA: # label for array A
a pointer to an array that is no longer allocated. This often goes wrong. Other languages
* space 4000 # move current-address pointer up 4000 bytes (like Pascal) avoid the problem by not allowing pointers to stack-allocated data to be
* text # go back to text area for code generation returned from a function. |
cane -~ aaceines
The base address of the array A is at the label base-of A. The limitations of static allocation and stack allocation are
You might want arrays that can be resized or which survive the | oca 0 ch;
The assembler (possibly assisted by a linker) translates the symbol de rn lan gua ges all ow hea p all tio n, whic h is
ic code to binary which they are all oca ted . Hen ce, mos t mo
code where references to labels are replaced by references to
numeric addresses. In the also called dynamic memory allocation.
programming language C, all global variables, regardless of size, are
statically allocated. ; gra m ution ends, until

Limitations: Data that is heap allocated stays allocated ys te oe ontil the data is deemed
| he data is explicitly deallocated by a program stalemet OF 1 tes it. The sizeistoof
—-
¢ The size of an array must be known at compile time if it is statical
ly allocated, a _ ab a run-time memory-management system, which =
and the size can not change during execution. a : (or other data) need not be known until it is allocate 5 to the old array can be
* Furthermore, the space is allocated throu ghout the entire execution of the pe d later, a new array can be allocated and we all references to the old
program, the new array. Thebutlatter requires tna a ple, by letting
even if the array is only in-use for a small fraction of this
time
”redirectedaaa cked todown
to p nea and updated, that can be arranged i ed. Languages
.
* In essence, there is little reuse of statically allocated space array can be tracke te go through an indirection node that isn
within one program all references to ana
execution. The programming language Fortran allows the progra
mmer to specify that
Sixth Semester, Compiler Design
90—2019 LP. University-(B.
Tech |_Akash Books
<enlicitly. by. commands in . the program or automatically by the run-time me 2019-21
Saeed’ eystem. The first is called ° manual
iXD
memory management” and the an
:
control stack y 4 runtime stack called
abet be memory management” or “automatic garbage collection”, latte, the
“sl 10T é p -
¢ Each live activation has
an activation record or a fr
Q. 7. (a) What is lexical phase, syntactic and semantic phase errors? ¢ The root of activation tree
ame
is a the bottom of the stack
Ans. Lexical phase errors: Lexical Phase breaks the program in to telat i it * The current execution path Specifies the co
| 8 ntent of the stack with the last
recog nizes the token s, in progr am. activation
as record in the top of the stack’
1. If after some processing, the lexical Analyzer discovers that no prefix ate
¢ Activation record contains 7 fields whic
remaining input belongs to any token class, then lexical errors are generateg he h are-
2. It is mainly generated because next token to be read in source Program, M jg;
|
7
Returned .
misspelled.
Eg: Integer Constant out of bound( 0 to 32767) Actual parameters
Syntactic Phase Errors: These errors occur when stream of token not Control link
3
erammatical rules of programming language.
follow the
Access link
1. Missing ; at the end of the statement. Machine status
2. Missing Right Parenthesis (a +( b+c)
Local data
3. Misspelled keyword
4. Colon in place of semicolon ex: I = 1; Temporary data
Semantic phase errors Size of each field of activation record can be estimated at compile time.
1. Non Declared variables UNIT-IV
2. Type conflict of operands
Q. 8. (a) What are the main steps in local optimizations? Optimize the
3. Incorrect procedure calls (e.g.Wrong no. of Parameters), ution time.
Q. 7. (b) Di scuss storage mechanism in block struct following three address code and analyze how this is reducing exec
ured and non-block (7.5)
structured languages. |
(5) PROD=0
Ans. Compiler must carry out the storage allocation and provide access to va
riables 1=]1 .
and data. Allocation can be done in two ways:
T 1=4*1
Compiler Time Allocation or Static Storage Allocation: A static storage T2=addr(A)-4
allocation decision is taken by the compiler by looking only at the text of the progra
m T3=T2(TL
but not by looking at what the program does when it executes. Allocation is done 2
compile time } T4=addr(B)-4
Scope storage allocation includes, T5=T4(T1\
¢ Definition of procedure T6=T3*T5
¢ Declaration of a name or variable: °* PROD=PROD+T6
1 = 1+1
¢ Scope of declaration
Runtime Allocation or Dynamic Storage Allocation: A storage allocation If 1<=30 goto (stmt 3)
decision is dynamic if it can be decided only while program is running. Allocation is done Ans. |
at runtime .Storage allocation can be done for two types of data variable: (1 Tl = 4*1
(2) T2 — 4
= addr(A)
1. Local data 2. Non local data .
The local data can be handled using activation record where as non-local data can. (3) T3 = T2(T1)
(4) T5 = addr(B) — 4
be handled using scope information. The block structured storage allocation can be done
using static scope. The non-block structured allocation can be done using dynamic scope.
(5) OR, T5(T1)
(6) tts T3 * T6
Dynamic storage allocation includes, (7) PROD = PROD +87
* Activation of procedure (8) Soe a a ee T+1 goto (1) p r o v e the
Ifl< = 30, ue, wh i c h tr ie s to i m
* Binding of a name (9) a m m a e ) r e y
ti on tTetc heniqs
h speed. A
o n is | a p r o g r d e l i v e r h i g
© Lifetime of a binding Optimizati r eces (1.€.
s our
a n d
it c o n s u m e le ss three rules given below:
Activation Record code by making s t f o l l o w the
r o c e s s m u
code optimizing p change the meaning of
* Information needed for each execution of a procedure is stored in a record called c o d e m u s t not, in any Way
ut
* The outp
activation record |
LP. University-(B.Tech]-
Akas h Books
xt h Semester , C o m p iler Design 2019-23
Si GOTO LI
92-2019
u l d 11 scr eas e the spe ed of the program and if possible, the Prog,
o n s h o
. Optimizati | am L1: GOTOL2
b e r of resources.
sho uld de: man
d le ss n u m
if be fast and should not delay the overall compiling L2: INC RI
its e me "
Op timization sho uld
ia ‘ ae
Proceg 8
T cadets a stint In n thi this code, |
Q.8 (b) Explain peephole OPi ind of optimization performed 3
v r sie
ove ‘ anvi, teight s label L1 can be removed as it passes the
control to L2. So instead of
nization
wns. Peephole opti- miza tion 1s a |
d ee o e edpeby iergee
hoow
epah -_ : * nd then to L2, the control can directly reach L2, as sh ow n bel ow:
set of in st ru ct io ns in a segmen
is t
in g of be
set s ne
ofra
inve
st ruit
ct ed
io ns tha t can be re pl ac
“window”. It works by recogn MOV RI, R2
te ch ni qu es app lie d in pe ep ho le opt imization.
faster sets of instructions. Common nce. j GOTO L2
bexpressions in adva
e Constant folding — Evaluate constant su
ations with faster equivalents. pe
e Strength r eduction — Replace slow oper
operations.
e Null sequences — Delete useless L2: INC R1
e Combine operations — Replace several operations with one equivalent. Algebraic expression simplification
e Algebraic laws — Use algebraic laws to simplify or reorder instructions. There are occasions where algebraic expressions can be made simple. For example
¢ Special case instructions — Use instructions designed for special operand Cases. the expression a = a + 0 can be replaced by a itself and the expression a=a+l1 ean
e Address mode operations — Use address modes to simplify code. simply be replaced by INC a.
Redundant instruction elimination Strength reduction
|
At source code level, the following can be done by the user: There are operations that consume more time and space. Their ‘strength’ can be
int add_ten(int x) int add_ten (int x) int add_ten(int x) int add_ten(int i
| | | reduced by replacing them with other operations that consume less time and space, but
| produce the same result.
int y, Z; int y; int y = 10; return x +10; For example, x * 2 can be replaced by x << 1, which involves only one left shift.
y =10; V=LU; return x + y; hy t implement.
Though the output of a * a and a? is same, a” is much more efficiento
Z=X+Y; y=xty; } Accessing machine instructions
return y;
return Z;
The target machine can deploy more sophisticated instructions, which can have the
; can
} | }
capability to perform specific operations much efficiently. If the target code
At compilation level, the compiler searches fo r instructions redundant in nature accommodate those instructions directly, that will not only improve the quality of code,
Multiple loading and storing of instructions may carry the same meaning even if some but also yield more efficient result.
of them are removed. For example: Dra w the DAG for the foll owin g stat emen t: (7.5)
- Q. 9. (a) What is DAG ?
e MOV x, RO
T,=D+E.
« MOV RO, R1 |
ie g=AtB
We can delete the first instruction and re-write the sentence as:
. . T,=T,-C
MOV x, Rl
Unreachable code’ | | T,=T,;*T,
Unreachable code is a part of the program code that is never accessed because of T,=T gh
programming constructs. Programmers may have accidently written a piece of code T,=T Pa WA
that can never be reached. T,=!,*Ts-
Example: Ans.
void add_ten(int x)
{
return x +10;
printf(“value of x is%d”, x); |
) | | |
' In this code segment, the printf statement will never be executed as the program
control returns back before it can execute, hence printf can be removed.
‘Flow of control optimization |
There are instances in a code where the program control jumps back and forth
without performing any significant task. These jumps can be removed . Consider the
following chunk of code: ,

MOV R1, R2
Compiler Design
Sixth Semester,

h (DA G) is a gra ph tha t is directed and without


c l ic grap
c t e d a c y
4 dire » other edges. Th is means tha t itis impossible to traverse the entire 2 “les
. 2r way. Th aph
connecan £ the ot he directed graph only go one

starting 8! one ‘ng,edge.where


The each
edgesnode
of isthe in a certain order. A directed acyclic graph
© 8aphti is g
topological os ie a cyclic, or thatit is impossible to start at one point in the graph s
from an earlier edge to a late Ph and
;
ee chat, ntire graph. Each edge is directed
. . *,¢ r ed

oe + Penlain data flow analysis with reaching definitions, Be.


w of da ta in contro] tow be (5) |
is : It is the an al ys is of flo
Ans. Data Flow Analys
-» the analysis that determines the information regarding the definition and Steg |
Se of
aate in program. With the analysis optimization can be done,
help of this
Rasic Terminologies -
© Definition Point: A point in a program containing some definition.

me
« Reference Point: A point in a program containing a reference to a data item

T=
« Evaluation Point: A point in a program containing evaluation of expression, -

paar
a pre
L,:a=2 Definition Point.

een
ee
. : » 5
‘ | 2
A :

; } Y
z -
: é

: 1
‘ {
| . ¢ 7

: e
. |

L,:b=a Reference Point


Je
7

Evaluation Point L,:z=x"*y

A
Data Flow Properties —
¢ Available Expression - A expression is said to be available at a program pointx
iff along paths its reaching to x. A Expression is available at its evaluation point.
A expression a+b is said to be available if none of the operands gets modified before
their use. It is used to eliminate common sub expressions. ak
* Reaching Definition — A definition D is reaches a point x if there is path from D-
to x in which D is not killed, i.e., not redefined. It is used in constant and variable
propagation. |
* Live variable - A variable is said to be live at some point p if from p to end the - \
variable is used before it is redefined else it becomes dead. It is useful for register |
allocation and it is used in dead code elimination. 5:
* susy Expression - An expression is busy along a path iff its evaluation exists
along that path and none of its operand definition exists before its evaluation alongthe
path. It is used for performing code movement optimization. }
-

You might also like