0% found this document useful (0 votes)
23 views

Notes

Uploaded by

webifyduniya54
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Notes

Uploaded by

webifyduniya54
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Compiler Design

Answer
Scoping method of keeping variables in
is
A-9C(CSAT-Sems
distinct from one another. different rent parts of
Scoping is generally divided into two classes:
A. Statie scoping: Static scoping is also
progr
called lexical
l scoping. nt
scoping a variable always referst its
.

b Dynamic scoping: In dynamic scoping, a top level env In thi


global ronment.
to the identifier associated with the
ost recent environmen dentifier rfers
Que 4.8. Differentinte between lexical (or ment.
static) anscope ant
dynamic scope.
Answer

S. No. Lexical scope


Dynamic scope
1. The binding of name The
occurrences to declarations
binding of name occurrences
is done statistically at
to declarations is done dynamically
at run-time.
compile time.
2. The structure of the The binding of variables is defined
program defines the | by the flow of control at the run
binding of variables. time.
3. A free variable in a A free variable gets its valuet from
procedure gets its value where the procedure is called.
from the environment in
which the procedure is
defined.

Que 4.9. Distinguish between static scope and dynamie seo


Briefly explain access to non-local names in static scope.
AKTU2018-19,Marks0

Answer
Differenee : Refer Q. 4.8, Page 4-9C, Unit-4.
Aecess to non-local names in static ariable!

scope:
Statie is the mechanism to implement non-local na
chain
access in static scope. n e c t s c e r t a i n activatiou

A
static ehain is a chain of static links that connects cer
record instances in the stack.
tor

record in
3. The statie link, static activation

scope pointer in a n i n s t a n c e s

A points to one of the activation recoru


subprogram
static parent.
4-10 (CST
IT-Sem-6) Symbol Tahles

when a subroutine at nesting levelj has a reference to an objeet declared


in aa static parent at the surrounding scope nested at level , then J*
t atic links forms a static chain that is traversed to get to the frame
sta
containing the object,
The compiler generates code to make these traversals over frames to
5. reach non-local names.
For example : Subroutine A is at nesting level 1 and Cat nesting level
3.When Caccesses an object of A, 2 static links are traversed to get to
A's frame that contains that object
Nesting
Static frames
B
C
static link

D
static link

B
static link.
Calls
A calls E
E calls B
static link-
B calls D A

L D calls C

|PART-3
Run-Time Administration Implementation of Simple Stack
Allocation Scheme.

Questions-Answers

Long Answer Type and Medium Answer Type Questions


ww.ww ww

ue 4.10. Draw the format of activation record in stack allocation

and explain each field in it. AKTU 2018-19, Marks 07

Answer
the information needed by a single
1tvation record is used to manage
execution of a procedure.
stack when a procedure is called
activation record is pushed into the
and it i popped when the control returns to the caller function.
Compiler Design

Format of activation records in stack allocation


4-11C (CSITS
on:
Return value
Actual parameters
Control link
Access link
Saved machine status
Local data

Temporaries
Fields of activation record are:
1. Return value It is used by
:
calling procedure t0 return a valte t.
calling procedure.
2. Actual parameter It is used by
:
calling procedures to suDnit
parameters to the called procedures.
3. Control link: It points to activation record
of the caller.
Access link: It is used to refer to non-local data held in
other activation
records.
5. Saved machine status : It holds the information about
status of
machine before the procedure is called.
6. Local data : It holds the data that is local to the execution of the
procedure.
7. Temporaries : It stores the value that arises in the evaluation of an
expression.
Que 4.11. How to sub-divide run-time memory into code
a a
data areas ? Explain. AKTU 2016-17, Marks 10
Answer
own1
Sub-division of run-time memory into codes and data areas *
Fig. 4.11.1.

Code
Static
Heap
Free Memory

Stack
Fig 4.11.1.
1 2 C(CSTT-Sem-5) Symbol Tables

Code:Itstores the executable target


Its code which is offixed size and do
change duri compilation.
not
Staticallocation
The static allocation is for all the data objects at compile time.

b. The size of the data objects is known at compile time.


The names of these objectsbound to storage at compile time
are
C. allocation.
only and such an allocation of data objects is done by static
of storage
In static allocation, the compiler can determine amount
required by each data object. Therefore, it becomes easy for a
record.
compiler to find the address of these data in the activation
addresses at which the target
e. At compile time, compiler can fill the
code can find the data on which it operates.

methods used for heap management:


Heap allocation: There are two
a. Garbage collection method:
When all access path to a object are destroyed but data object
continue to exist, such type of objects are said to be garbaged.

technique which is used to reuse


The garbage collection is a

that object space.


elements whos garbage collection
Ingarbage collection, all the
returned to the free space list.
bit is 'on' are garbaged and
b. Reference counter:
each element of heap
Reference counter attempt to reclaim
accessed.
c a n no longer be
storage immediately after it
a reference counter
i. Each memory cell on the heap has
number of values
contains a count of
associated with it that
that point to it.
incremented each time
a n e w value point to the
i The count is value c e a s e s to point to it.
each time a
cell and decremented

Stack allocation :
structure called activation
to store data
Stack allocation is used
record.
and popped a s activations begins
. activation records are
pushed
The
and ends respectively.
6 C (CS/T-Sem-5)
Symbol Tables

S. No. Name Type


int
2
msg | function
3
int

2.
Ordered list:
Id Name Type Id
Id1
int Idi
Id2
int Id2
Id3 msg function Id3
3. Search tree:

4 Hashtable:
Name 1
Data1
Link1
Name Name2
Data2
Link2
Hash table Name3
Data3
Link3
Storage table

Que 45. Describe symbol table and its entries. Also, discuss
various data structure used for symbol table.
AKTU 2015-16, Marks 100

Answer
bymbol table: Refer Q. 4.1, Page 4-2C, Unit-4.
Bntries in the symbol table as follows:
1. Variables:
are

a. Variables are identifiers whose value may change between


executions and during a single execution of a program.
b.
They represent the contents of some memory location.
The symbol table needs to record both the variable name as well as
C.

its allocated storage space at runtime.


447C (CS/T-Sen
CompilerDesign
Sem 4-8 C (CS/IT-S
fixed ALGOL, tk
that
represent a
lue that
can neye
2 C o n s t a n t s :
identifiers
declared.
Constantsare
a.
be changed.
or
procedures,
no runtime looe
location needs to 3.
Thus, the
n a m e s , Wit
Unlike variables
b. stored for constants.
locationns.
right into ode stream:
the code stream by the
These are typically placed comp The symbc
C. at compilation time. 4 of the sam
defined):
& Types(user eriot To make
A user defined type is
combination of one or more
Kisting types .
assigned t
type dat
a.
name and reference a local data.
Types are accessed by
b.
structure. b. Semantic
4 Classes: beginning
which restrict access to its momi of current
a. Classes are abstract data types mbers
and provide convenient language level polymorphism. There are
7.
This includes the location of the default constructor and destructor identifieer
b.
and the address of the virtual function table. a. Each
5. Records: b. Two
a. Records represent a collection of possibly
which can be accessed by name.
heterogeneous members cann
The scope-
b. The symbol table probably needs to record each of the record's a progra
members.
Scope of
Various
Unit-4.
data structure used
for symbol table : Refer
Q. 4.4, Page 4%
PART-2
Representing Scope Informaton
ong Answer Type and Medium
Questions-Answers Scope of
Que 4.6. Answer Iype stions
symbol table.Discuss how the scope in Scope of
information is represen
Answer
1.
Scope
information
portions
2. of
the
Different program characterizes
whe es the declaration
ofidentifiere.
a n

the
d
in l
FORTRAN,anguages here
the have different
it is
allowed to use a c h

i
S
scope
name scopes
r a t i o n sF. o r e x a m p l
of a tor
a
single
w h e r e a s
ubroutine, ue 4.7.
4-2C (CS/TT-Sem-5) Symbol Tables
PART1
Symbol Tables Data Structure for Symbol Tables.
Questions-Answers
Long Answer Type and Mediumn Answer Type QuestionsS
Que 4.1 Discuss symbol table with its capabilities?
Answer
1 A symbol table is a data structure used by a compiler to keep track of
scope, life and binding information about names.
2. These information are used in the source program to identify the various
program elements, like variables, constants, procedures, and the labels
of statements.
3. Asymbol table must have the following capabilities:
a. Lookup: To determine whether a given name is in the table.
b. Insert: To add a new name (a new entry) to the table.
C. Access: To access the information related with the given name.
d. Modify: To add new information about a known name.
e. Delete: To delete a name or group of names from the table.
Que 4.2. What are the symbol table requirements? What are the
demerits in the uniform structure of symbol table ?
Answer
The basic requirements ofa symbol table are as follows:
1. Structural flexibility : Based on the usage of identifier, the symbol
table entries must contain all the necessary information.
2 Fast lookup/search: The table lookup/search depends on the
implementation of the symbol table and the speed of the search should
be as fast as possible.
S.Efficient utilization of space: The symbol table must be able to
grow or shrink dynamically for an efficient usage of space.
Ability to handle language characteristics: The characteristic of
a language such as scoping and impicit declaration needs to be handled.
emerits in uniform structure ofsymbol table:
T h e uniform structure cannot handle a name whose length exceed
upper bound or limit or name field.
2 f the length of a name is small, then the remaining space is wasted.
3 - 2 2C ( C S T T - S e m - 5 )
ntax-Directed
s h o r t note
on x ttranslation,
postfix
Write a
Que3.19.
Answer the translation rule o
A a, ofA
1. In a production CODE translations
ofthe
appear in q.
c o n c a t e n a t i o n
non-terminals
same
order as the
to achieve postfix form
rm.
be factored
Production can
22. statement:
Postfix
translation of while
S > while
M1E do M2 S1
Production:
as:
Canbe factored
1. S+CS1
C WE do
2.
3. Wwhile
transition scheme
is given as:
Asuitable
Semanticaction
Production rule
W.QUAD = NEXTQUAD
W-while
CWEdo
CWEdo
BACKPATCH (S1.NEXT
S-CS1
C.QUAD)
S.NEXT= C.FALSE
GEN (goto C.QUAD)
(C+D)E+B
What is postfix
notations ? Translate
Que 3.20. Scheme (SDIS%
Translation
Directed
postix using Syntax AKTU 2017-18,Mark
Answer
Unit-3.
Postfix notation:ReferQ,3.6, Page 3-6C, to
s l a t i e n
translai
the
specify
Numerical:Syntax directed translation scheme spo
o
folloW
anexpression into postfix notation are as
Production:
EE+T
ET
T>T xF
F- (E)
F id
Compiler Design
3-23C(CSSIT-Sem-5)
Schemes
E.code =
E,.code 1T,code 1+
Ecode T.code
Ty.code T,.code |F.code | *
=
T,.code - F.code
F.code E.code
F.code id.code
where sign is used for concatenation.
PART-7
Procedures Call.
Questions-Answers
Long Answer Type and Medium Answer Type Questions
Que 3.21. Explain procedure call with example.
Answer
Procedures call:
1 Procedure is an important and frequently used programming construct
for a compiler.
2 It is used to generate code for procedure calls and returns.
3. Queue is used to store the list of parameters in the procedure call.
4.The translation for a call includes a sequence of actions taken on entry
and exit from each procedure. Following actions take place in a calling
sequence
a. When a procedure call occurs then space is allocated for activation
record.
b. Evaluate the argument of the called procedure.
c. Establish the environment pointers to enable the called procedure
to access data in enclosing blocks
Save the state of the calling procedure so that it can resume execution
after the call.
e, Also save the return address. It is the address of the location to
which the called routine must transfer after it is finished.
Finally generate a jump to the beginning of the code for the called
procedure.
Syntax-I
DirectedaTrandatim
3-18C (CSIT-Sem-5)
for do-while staten-
Translation
procedure ement:
Production
Semanic ruleg
S- do S1 while E
S.begin: newlable
E.trueS.begin
E.false: =S.next
S.code= S1.code |\E,nd
gen(E.true )|| ode\|
gen 'goto' S.being)
PART-55
the
alterwww Flone
Flow of Con
Statements that
Boolean Expressions, www.o
ww wwwwww
Questions-Answers
ww.
Long Answer Typeand Medium Answer
ype Questions
and semantic rules for boolean
Que 3.17. Define backpatching
for the following
expression. Derive the three address code
expression:P<QorR <SandT< U.
AKTU2015-16,Marks10
ww.w.www.oww
OR
Write short notes on backpatching.
Answer
information of laes
LBackpatching is the activity of filling up unspecified
the code generation proces
USing appropriate semantic actions during at
forward branches u
2. Backpatching refers to the process of resolving ecomes
have been used in the code, when the value of the targe
known. the
problem of proces
3. Backpatching is done to overcome the
incomplete information in one pass. expressionsand
ex
Backpatching can be used to generate code for boolean
4 flow of control statements in one pass. a r eu s e d :
To generate code using backpatching following functions o n e
newlistfro
1. Makelisti) : Makelist is a function which creates a nted
item where i is an index into the array of instructiongts pon
listspon
the
2
MergelpP»P): Mergeis afunction which conca
tenates
ted list.
DYP1 and pa, and returns a pointer to the concatenaue
Compil Design
3-19C (CS/AT-Sem-5)
0ackpatch(p, i): Inserts i as the target label for each ofthe instructions
Bac
onthe list pointed by p.
Backpa
atching in boolean expressions:
The solution 1s to generate a sequence of branching statements where
e addresses of the jumps are temporarily left
unspecified.
Ror each boolean expression E we maintain two lists:
2
a.
E.truelist which is the list of the (addresses of the) jump statements
appearing in the translation of E and forwarding to E.true.
hE.falselist which is the list of the (addresses of the) jump statements
appearing in the translation ofE and forwarding to E.false.
When the label E.true (resp. E.false) is eventually defined we can walk
down the list, patching in the value of its address.
4In thetranslation scheme below:
a. We use emit to generate code that contains place holders to be filled
in later by the backpatch procedure.
b The attributes E.truelist, E.falselist are synthesized.
C. When the code for E is generated, addresses ofjumps corresponding
to the values true and false are left unspecified and put on the lists
E.truelist and E.falselist, respectively.
5 Amarker non-terminal M is used to capture the numerical address of a
statement.
6. nextinstr is a global variable that stores the number of the next staiement
to be generated.
The grammar is as follows:
B-B| MB, I B,AND MB, B (B) |E rel E, | True | False
M>
The translation scheme is as follows:
i BB, | MB,[backpatch(B,falselist, Minstr);
B.truelist= merge (B,truelist,
Btruelist);
B.falselist-B,.falselist;)}
i B>B,AND MB,
backpatch (B,.truelist, M.instr);
B.truelist=B,truelist;
B.falselist = merge (B,.falselist,
B.falselist);}
BB,(B.truelist =B,falselist;
B.falselist=B,.truelist;}
npiler Design
3-11 C (CS/IT-Sem-5)
Number Op Argl Arg2 Location Statement
(0) uminuss
(0) (11)
(1) *
(11) b
(1) (12)
(2) uminus
(2) (13)
(3) *
(13) (3) (14)
(4) (12) (14) (4) (15)
(5) (15) (5) (16)
ree address code of given statement is:
IfA> C and B <D goto2
If A =1 goto 6
If A< = D goto 6
t=A +2
A =t
t=C+1
C t
ue 3.12.Write the quadruples, triple and indirect triple for the
llowing expression:
(x+y)* (y+2) + (x+y +z)
AKTU 2018-19, Marks07
nswer
The three address code for given expression:
t*+
tt3+
The quadruplerepresentation:
Location 0Operator Operand 1 Operand 2 Result
(1) 1
(2) 2
(3) s
(4) t
(5)
(2) and2
(3) (1)
(4) (1)
(5)
2)
(3)
ii The indirect triple representation:
(4)
Location Operator Operand 1Operand 2 Locati
(1) Location Stateme
(1)
(2)
(3) (11) (12)
(2) (1
(3) 12
(4) (11) (13
5)
(4)
(13) (14) (14
(5)
(15)
Que 3.13.| Generate three address code for the
following code:
switch a+ b
case 1:X =X+ 1
case 2:y =y +2
case 3: z = z+3
default:c=C-1
AKTU 2015-16,Marks
Answer
101:t, =a +b goto 103
102 goto 115
103: 1 goto 105
104 goto 107
105 t,=x+1
106:x =t2
107: ift= 2 goto
108 goto 111
109
109 =y+2
110:y=s
111:ift 3 goto 1113
=
112 goto 115
113: z +3
114 z =t4
115 1C-1
116:e='
117:Nextstatement
Compiler Design 3-13C (CS/AT-Sem-5)
Que 8.14. Genernte three addreas code for
CIAli, I1=Bli,jl+CLAli, j1]+ D[i, j| (You can assume any data for
solving question, if needed). Assuming that all array elements are
integer. Let A and B a 10 x 20 array with low, = low = 1.
AKTU 2017-18, Marks 10
Answer
Civen low, = 1 and low = 1, n, = 10, n, = 20.
Bli, ji =
(i xn,) +j) x w + (base-
(low, x n,) + low) x w)
Bi, jl =
((i x 20) +j) x 4+ (base -
((1 x 20)+ 1) x 4)
B,j] = 4 x (20 i + j) + (base - 84)
Similarly, Ali, jl = 4x (20 i +j) + (base - 84)
and, Di, jj = 4x (20 i +j)+ (base - 84)
Hence, C[AL, jll = 4 x (20 i + j) + (base -84) + 4 x (20 i +j)+
(base 8 4 ) + 4 x (20 i +j) + (base - 84)
= 4x (20 i +) +(base -84) [1+ 1+1
= 4 3x (20i +j) +(base 84)x3
x
1 2 x (20 i +j) + (base 84) x 3
Therefore, three address code will be
20 x i
t base -84
t 12 x t
5t4+3 xt
PART-4
Translation of Assignment Statements.
Questions-Answers
Long Answer Type and Medium Answer Type Questions
ue 3.15.How would you convert the following into intermediate
code? Give a suitable example.
i Assignment statements
, Case statements AKTU 2016-17, Marks 15
Compiler Design 3-5C(CS/IT-Sem-5)
Inherited attribute
1, An inherited attribute is one whose value at a node in a parse tree 1s
defined in terms of attributes at the parent and/or sibling of that node.
2. Inherited attributes are convenient for expressing the dependence of a
programming language construct.
For example: Syntax directed definitions that uses inherited attribute
are given as ;
D TL L.type: = T.Type
T int T.type: = integer
T real T.type: = real
LL, id Ltype:=L.type
enter (id.prt, L.type)
L id enter (id.prt, L.type)
The parse tree, along with the attribute values at the parse tree nodes, for an
input string int id, id, and id, is shown in the Fig. 3.3.2.
D
Ttype=int (T)
)L.type=int
int
Ltype=intL
d
Fig 3.3.2. Parse tree with inherited
attributesfor the string int id, id,idg
Que 3.4. What is the difference between S-attributed and
L-attributed definitions?
Answer
S. No. S-attributed definition L-attributed definition
1. It uses synthesized attributes.| It uses synthesized and inherited
attributes,
2. Semantics actions are placed at | Semantics actions are placed at
right end ofproduction. anywhere on RHS.
3. S-attributes can be evaluated L-attributes are evaluated by
during parsing. traversing the parse tree in depth
first, left to right.
Syntax-Di
DirectedT
Translat
CCSTT-Sem-6) Compiler Design
PART1 /
Annotated tree fc
Symar-Direeted
TranslationSynta r-Directed Transla
Inplementation
n
Sy
S
of
of
yn
ntta
a rr-2
Directed
Translators
Sohemes,
Questions-Answers
LongAnswer
Type and
Medium Answer
Answe
www.

Type Questions
T.val = 28(
trar
Que&.L
Define syntax
directed
*
Construct n
annotated parse tree for the expression (4 7+ 1) *2
*2, using T.val 4 T
simple desk
calculator grammar. AKTU 2018-19, Marle
id.lexval = 4
Answer
1Stntax directed definition/translation is a generalization ofcon
free grammar in which each grammar production X contet
> a is associated
with a set of semanticrules of the form a:=f6,, 6 Que 3.2 What is s
an attribute obtained from the function f.
. .
b,), whereai
actions attached to :
2 Syntax directed translation is a kind of abstract
3 Itis done for statie
analysis of the language.
specification.
4 t allows subroutines or semantic actions to
Answer
productions of a context free be attached to the Syntax directed tran
intermediate code when called grammar. These subroutines generate Semantic actions are a
at
grammar. appropriate time by a parser for tha Example : A parse tre
The syntax directed
the translation is partitioned into called an "annotated p=
synthesized and inherited two subsets calle attributes is shown int
attributes of
grammar.
Lexical analysis
Token stream E.val-2 (E
Syntax analysis
Parse tree T.val-2
Semantic analysis F.val-2 (F
Syutax directed
anslation EvaluationDependency graph
aluation order for digit
sem les digit.lexval=2
Translation of constructs
Py.3.1.
Compiler Design
3-3C (CS/IT-Sem-5)
Annotated tree for the
expression (4*7+1)*2
Lval= 58 E
E.val 29 F.
F.val-2
Eval=29 ) id.lexval =2
T.val 28(T)
F.val=1
T.val=.4 F.val 7
id.lexval = 4
id.lexval = 7
Fio. 3.1.2.
Que 3.2. What is syntax directed
translation? How are semantic
actions attached to the
production Explain with an example.
?
Answer
Syntax directed translation: Refer
Semantic actions are attached with Q. 3.1, Page 3-2C, Unit-3.
every node of annotated parse tree.
Example: A parse tree along with the values
of the attributes at nodes
(called an "annotated parse tree") for an
attributes is shown in the Fig. 3.2.1.
expression 2+3*5 with synthesized
EE.val-17
E.val-2 (E)
TT.val=15
T.val-2 (T T.val-3(T F.val-5
F.val-2 (FF) F.val-3 digit)
digit.lexval=5
digit) digit)
digit.lexval=3
digit.lexval=2
9.1
Fig. 3.2.1.
Compiler Design 2-37 C (CS/IT-Sem-5)
Since, LALR table does not contain any conflict. So, it is also LALR(1).
DFA:
S
A A
a A
b b
b
Fig.2.27.1.
VERY IMPORTANT QUESTIONS
Pollowing questions are very important. These questions
may be asked in your SESSIONALS as wellas
UNVERSITY EXAMINATION.
Q.1. What is parser ? Write the role of parser. What are the
most popul r parsing techniques ?
Ans Refer Q. 2.1.
Q.2. Explain operator precedence parsing with example.
Ans Refer Q. 2.4.
9.3. What are the problems with top-down parsing ?
Ans Refer Q. 2.7.
Q4. What do you understand by left factoring and left recursiona
and how i t is eliminated?
AR Refer Q. 2.8.
Q5. Eliminate left recursion from the following grammar
SAB,A > BS|b, B> SA]a
Ans Refer Q. 2.9.
6 . What are the problems with top-down parsing ? Write the
FOLLOW.
algorithm for FIRST and
Ans Refer Q. 2.12.
Bas ParsingTa
2-24C(CSIT-Sem-5)
Action
State/id Sa
o
S accept
2
TA
S
S
S S Sg
6
S
7 2 2
8 T2
9 |T's
ne 2.21. Perform shift reduce parsing for the given inputs
usingthe grammar S->(L)|aLL, S|S
i (a,(a, a) is. (a, a) AKTU 2018-19, Marke
Answer
L
Stack contentsS Input string ActionsS
(a, (a, a)S Shift (
(a, (a, a)$ Shifta
$a a, a))$ Reduce S>a
$CS ,(a, a))$ Reduce L>S
$L a, a))$ Shift)
$L, a, a))$ Shift(
$L,( a, a))$ Shifta
$L, (a ,a)$
Reduce S
$L, (S a))$
Reduce L>S
$L,L a)$
Shift,
$L, (L, Shifta
$L, (L,a a))$ Reduce S 0
$L, (L,S Reduce L>L,
$CL, (L $ Shift)
$L, L) Reduce S->
SL,S )$ ReduceL>L»
$L )S Shift
$L) Reduce S >
$S $ Accept
Compiler Design 2-25 C (CS/AT-Sem-5)
.
Stack contents Input string Actions
(a, a) Shift(
a, a)$ Shift a
$la ,a)$ Reduce S >a
$(S Reduce L+S
$L a)$ Shift,
$(L, a$ Shift a
$CL, a $ Reduce S>a
$L,S Reduce L->L,S
$L Shift)
$L) Reduce S>L
$S Accept
Que 2.22. Construct LR(0) parsing table for the followings
grammar
S cBceA
A cAla
B ceB| AKTU 2018-19, Marks 077|
Answer
The augmented grammar is:
SS
S cB |ecA
A cA|a
B ccB|6
The canonical collection of LR (0) items are
I,:S*s
S>cB|°ccA
A CA|°a
B ccB|° b
GOTO , )
I, =
1,: SS
1, = GOTO , c )
I,:S>e B|c ecA
A>c°A4
B->c°cB
A cA|°a
B °ccB |°6
Introduction to Conlpe

1-26 C (CSIT-Sem-5)

E EE)
Eid

for id(idid + is
Parse tree

tree is
Only one parse
possible for id(id)id+

so, the given


grammar

is unambiguous.
E E) id

id id

Que 1.27.Write short note on


i. Context free grammar
i. YACC parser generator
OR
Write a short note on YACC parser generator.
AKTU 2017-18,Marks 05

Answer
Unit-1.
i. Context free grammar: Refer Q. 1.23, Page 1-23C,
ii. YACC parser generator:
YACC (Yet Another Compiler - Compiler) is the standard parser
1.
generator for the Unix operating system.
An open source program, YACC generates code for the parser in
2.
the C programming language.
3. It is a Look Ahead Left-to-Right (LALR) parser generator, generating
a parser, the part of a compiler that tries to make syntactic sense of
the source code.

Que 1.28. Consider the grammar G given as follows:


S AB|aaB
AalAa
Bb

Determine whether the grammar G is ambiguous o r not. If G is


ambiguous then construct an unambiguous grammar equivalent
to G.
1-16C (CS/AT-Sem-5) Introduction to Compiler
3. This approach is so useful that programs called lexical analyzer
generators exist to automate the entire process.
Que 1.13.Write down the regular expression for
1. The set of all string over la, b} such that fifth symbol from right
is a.
2 The set of all
string over (0, 1) such that every block of four
consecutive symbol contain at least two zero.

AKTU 2017-18, Marks 10


Answerr
1. DFAfor all strings over la, b} such that fifth symbol from right is a:
Regular expression: (a +b)' a (a + b) (a +b) (a +b) (a +b)
2. Regular expression:
[0O(0+ 1) (0 + 1) O(0 + 1) O(0 +1) +0(0 +1) (0 + 1)0 +(0 + 1) 00(0 +1)
+(0+ 1)(0 + 1) 0 + (0 + 1) (0 + 1)00]
Que 1.14.| Convert following NFA to equivalent DFA and hence
minimize the number of states in the DFA.
.

10 (1
b. c
b
a. C

Fig. 1.14.1
AKTU
wwww.
2018-19, Marks 07

Answer
Transition table for &-NFA:

8/2
1 92

42
E-closure oflgo=lo»9 92
B-closure of lq,l = lq
B-closure of lg,) = lag
CompilerDesign
117C (CHT
NFA
T r a n s i t i o n

tablefor
S/2
lg
Let 404,4)=A
4,p-B
g=C
Transition tablefor NFA:
A A B A
B A C A
C
Transition table for DFA:
8/2
A
A
B
B A
A
C
C
A
So, DFAis
given by
a, c
C
b
a, b, c
Dead state
Fig. 1.142 a, b, c
lmplementation of
Lexical PART-4
AnalCompiler
LEX yzers,S eLeacal Analyze Generato
1-2 C (CS/IT-Sem-5) Introduction to Compiler
PART-1
Introduction to Compiler, Phases and Passes.
Questions-Answers
Long Answer Type and Medium Answer Type Questions
Que 1.1 Explain in detail the process of compilation. Illustrate
ww
the output of each phase of compilation of the input
a (b c)*(b + c)* 2".
+
AKTU 2016-17, Marks10
Answer
A compiler contains 6 phases which are as follows
i. Phase 1
(Lexical
analyzer):
a. The lexical analyzeris also called scanner.
b. The lexical analyzer phase takes source program as an input and
separates characters of source language into groups of strings
called token.
C. These tokens may be keywords identifiers, operator symbols and
punctuation symbols.
ii. Phase 2 (Syntax analyzer):
a. The syntax analyzer phase is also called parsing phase.
. The syntax analyzer groups tokens together into syntactic
structures.
C. The output of this phase is parse tree.
ii. Phase 3 (Semantic analyzer)
The semantic analyzer phase checks the source program for
a.
semantic errors and gathers type information for subsequent code
generation phase.
b. It uses parse tree and symbol table to check whether the given
program is semantically consistent with language definition.
C. The output of this phase is annotated syntax tree.
iv. Phase 4 (Intermediate code generation):
a. The internmediate code generation takes syntax tree as an input
from semantic phase and generates intermediate code.
b. It generates variety of code such as three address code, quadruple,
triple.

You might also like