Sei sulla pagina 1di 8

IEC College of Engineering & Technology

Department of Computer Science

Compiler Design (ECS-603)


Assignment -1

1. Explain why should one study about compilers?


2. Explain the major kind of compiler with an example.
3. What are the two parts of compilation? Explain them using a diagram.
4. Consider the following statement :
Firstvalue = nextvalue + rate* 19
Compile this statement in all six phases.
5. Consider the context-free grammar
S -> S S + / S S * / a
(a) Show how the string a a + a * can be generated by the following grammar.
(b) Construct a parse tree for this string.
6. Construct a phrase-structured grammar for the set of all strings containing an even
number of a’s and an even number of b’s.
7. Describe the language denoted by the following regular expressions:
(a) 0(0/1)*0
(b) ((ε / 0) 1* )*
(c) ( 0/1 )* 0 (0/1)(0/1)
(d) 0* 10*10*10*
8. Find a regular expression for the set of all strings that begin
or end with 00 or 11, over{ 0,1 }
9. Find a regular expression for the set of all strings with either
no ‘1’ preceding a ‘0’ or no “0’ preceding a ‘1’, over {0,1}
10. Find a regular expression for the set of all strings over{0,1}
having atmost one pair of 0’s or atmost one pair of 1’s.
11. Write a regular expression for the language consisting of all
strings of 0’s and 1’s, in which the number of 1’s is a multiple of 3.
12. Convert the following regular expression into NFA using
Thomson’s construction algorithm. ( a / b)* b ( a / b)
13. What is a cross compiler? How is bootstrapping of a
compiler done to a second machine.

14. Find regular expression corresponding to the following given automata:

15. Construct a DFA equivalent to NDFA given under:


IEC College of Engineering & Technology
Department of Computer Science

Assignment -2

1. Construct the parsing table for the following LL (1) grammar:


SaA/B
A b B / a
Bc/bAc
2. Construct an LR (1) parsing table for the following grammar:
T  int
L  L, id / id
3. Construct an LALR (1) parsing table for the following grammar:
DL :T
L  L id /id
T  integer
4. Construct an SLR parsing table for the following grammar:
S  A)
S  A,P/ ( P,P
P{num,num}
5. Eliminate the left recursion from the following grammar:
S  a / Λ / (t)
TTS/S
6. Construct the following grammar:
E  TE’
E’  +E / ε
T  FT’
T’  T / ε
F  PF’
F’  *F’ / ε
P  (E)/ a / b/ ε
(a) Compute FIRST and FOLLOW for each nnon-terminal of above grammar.
(b) Show that the grammar is LL(1)
Construct LR(0) parsing table for the following
grammar:
E  E + T /T
T  T*F / F
IEC College of Engineering & Technology
Department of Computer Science
F  (E) / id

8. Find the FIRST and FOLLOW sets of each of


the non-terminals for the following grammar:
S  aAB / bA / ε
A  aAb / ε
B  bB / c
Define CLOSURE( I ) and GOTO( I, X )
functions.
Consider the grammar S  aS / aSbS / ε
This grammar is ambiguous. Show in particulars that the string aab has two:
(i) Leftmost derivation
(ii) Rightmost derivation
(iii) Parse tree
Show that the following grammar is ambiguous:
S  a / abSb / aAb
A  bS / aAAb
Consider the following grammar:
S  Aa / bAc / Ba / bBa
Ad
Bd
Construct the transition table and Action /Goto table of the given grammar.
13. Give algorithm for construction of predictive
parsing table. Consider the following grammar and construct predictive parsing
table.
S  i E t S S1
S1  e S / E
E  id
Give the algorithm for computing precedence function .
Consider the following grammar
SAaAb/BbBa
Aε
Bε
IEC College of Engineering & Technology
Department of Computer Science
Test whether the grammar is LL(1) or not , and construct a predictive parsing
table for it.
IEC College of Engineering & Technology
Department of Computer Science
ASSIGNMENT NO:3

1. Construct a syntax-directed translation scheme that translates arithmetic


expression from infix notation into prefix notation.
2. consider the following code fragment ,Generate the quadruple for it
switch (a + b)
{
case 1: x = x + 1;
case 2: y = y + 2;
case 3: z = z + 3;
default: c = c -1;
}
3. What is post-fix notation? Translate (a + b) * (c + d) into post-fix.
4. For the following assignment statement:
A = -B * (C + D)
Write its three-address statement code and quadruple representation.
5. Translate the following program segment into three-address segment:
Switch( a + b )
{
case 2: { x = y; break; }
case 5: { switch( x )
{
case 0:{ a = b + 1; break:}
case 1:{a = b + 3; break;}
default :{a = 2;}
}
break;
case 9: {x = y – 1; break;}
default: {a = 2;}
}

6. What is a basic-block? How can a sequence of three-address statements be


transformed into a list of basic blocks. Show it with example.
7. Explain the following categories of intermediate codes:
i. Three-address codes
ii. Quadruples
iii. Triples
IEC College of Engineering & Technology
Department of Computer Science
8. Consider the following sequence of three address code:
(1) PROD =0
(2) I = 1
(3) T = 4*I
(4) T2 = addr( A )-4
(5) T3 = T2 [T1]
(6) T4 = addr( B )-4
(7) T5 = T4[T1]
(8) T6 = T3 * T5
(9) PROD = PROD + t6
(10) I = I+1
(11) If I <= 20 goto 3
(ii) Find the basic blocks and construct a flow graph.
(iii) Eliminate common sub-expression
(iv) Move the loop invariant computation out of the loop.
(v) Find the induction variables and eliminate them where
possible.
9. translate the following statement to quadruple:
- (a + b)*(c + d)-(a + b + c)
10. Consider the following switch statement:
Switch (i + j)
{
case 1: x = y + z;
case 2: u = v + w
default: p = q + r
}
generate its quadruple.
11.Explain the backpatching. Also generate three address code for the following
program segment.
While( a < c and b > d) do
if a = 1 then
c = c + 1;
else
while( a <= d) do
a = a + 3;
12.Translate the following statement to quadruple:
If a > b then x = a + b
Else x = a – b
13.What are the different adderessing schemes for generating code?
14Write a short note on poloish notation.
15.Write a short on conversion of infix expression to postfix expression.
IEC College of Engineering & Technology
Department of Computer Science

Assignment-4

1. What is symbol table ? Discuss the various approaches used for organization of
symbol table.
2. What are the different data structures for the symbol table implementation.
3. Explain various allocation storage strategies. Which storage allocation model is to
be used if a language permits recursion?
4. What do you mean by heap allocation ? Explain the following terms releted to
heap allocation:
i) Fragmentation
ii) Free List
iii) Bit Map
iv) Reference Count
5. What are the various types of errors that may appear in
compilation process ? Explain the function of Error handling phase of a compiler.
6. Explain activation record and display structure.
7. Discuss the following parameter passing technique with suitable
example:
Call by value
Call by name
Call by reference
What is printed by the program given under :
a. Call–by-name
b. Call-by –reference
c. Call-by-value
Procedure PROC (X,Y,Z)
begin
Y = Y+1
Z=Z+X
end PROC
begin
A=2
B=3
PROC (A+B,A,A)
Print A
End
9. Explain static allocation and discuss its drawback.
10. What are the goals of error handler in a parser?
11. What is parameter passing? Differentiate between call-by-name and call-by-
reference.
12. What do you understand by Lexical phase error and syntactic phase error?
13. Discuss various methods for recovery of errors?
14. Define attributes of symbol table:
i) Class and related attributes
ii) Scope attributes
IEC College of Engineering & Technology
Department of Computer Science

Assignment-5

1. Give the algorithm for the elimination of local and global common sub-
expression. Discuss the algorithm with the help of an example.
2. Write short notes on following:
i) Directed Acyclic Graph
ii) Yacc parser generator
3. Give the sequence of three-address code instructions corresponding to each of the
arithmetic expressions:
2+3+4+5
4. Discuss loop optimization technique with suitable examples.
5. Explain global data flow analysis.
6. Describe how a for-statement can be systematically turned into a corresponding
while statement. Does it make sense to use this to generate code?
7. Discuss the algorithm for the elimination of induction variables with the help of
example. Also generalize this algorithm to the case where multiplicative constants
can be negative.
8. Explain the following term:
i) Shift-reduce parsing
ii) Activation Record

. Show the DAG for the following statement


Z = X – Y + X * Y *U – V/W +X +V

10. Suppose parameter are passed by value instead by reference. Can


two names be aliases of one another? What if copy-restore linkage is used?

1. What do you mean by code optimization? What are the areas of optimization?
2. Explain how DAG helps in elimination of common sub expression?
3. Construct DAG for the following code sequence:
A [I] = B
*P = C
D = A [J]
E = *P
*P = A [I]
Assume that :
a. P can point anywhere
b. P point to only B or D
4. What is global data flow analysis? What is its use in code optimization?

Potrebbero piacerti anche