Professional Documents
Culture Documents
CD-compiler Designe Akash
CD-compiler Designe Akash
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.
>
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
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) =
“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
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
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}
SR wey
———————— eee Oe,
OO eee ee ee. ES ree %
erm.trans == INT)
—— ——
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
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
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.
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
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
\ 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
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 <
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
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
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:
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 ,
} 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
Q.1. (e) Design the NFA for the regular Expression a (a+b)* bb (2)
Ans. py ala + 6) * bb qi
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
>
—|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 &
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
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
_ 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]
| ..experience th tial
=o
:
eR
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.
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
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
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
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:
on:
al
pos
Input
or
BIT pos =
Stack ———
= LIST ual ~ Bit vat
LIST vat
Sito
id + idi*di$dS
a =i GIT val =
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
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:
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
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.
LP |a | value
| RP
l b
NULL — ee value | RP
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
| *
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 —
: .
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
>
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
(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
:
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
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
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
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, }
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
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.
~~
me
rs one ACYCLIC
—
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.
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
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
T— T*F/F Top-Down
F > (E)/a :
Input string: a + (a*a)*a a
Ans. Recursive Descent
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.
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
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 never us.
|
: : a
;
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
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 |
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
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
* 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
id
Ys
- » F 2
k k i
2
a ;
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 ;
!
as <3 ‘
> = > : = |
; E->T {E.val=T.val}
| roe al Ss TF
: 5
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.
. "
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,
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
. |
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. }
-