Imp CS1352 APR08
Imp CS1352 APR08
PART-A
1. Differentiate compiler and interpreter.
Compiler produces a target program whereas an interpreter performs the
operations implied by the source program.
3. Construct a parse tree of (a+b)*c for the grammar E->E+E | E*E | (E) | id.
4. Eliminate immediate left recursion for the following grammar E->E+T | T, T->T
* F | F, F-> (E) | id.
The rule to eliminate the left recursion is A->Aá | â can be converted as A-> âA’
and A’-> áA’ | å. So, the grammar after eliminating left recursion is
E->TE’; E’->+TE’| å; T->FT’; T’->*FT’ | å; F-> (E) | id.
-1-
MAY/JUNE-'09/CS1352-Answer Key
7. Give syntax directed translation for the following statement Call p1(int a, int b).
param a
param b
call p1
-2-
APR/MAY-'08/CS1352-Answer Key
PART - B
11. a. i. Explain the phases of compiler, with the neat schematic. (12)
The process of compilation is very complex. So it comes out to be customary from
the logical as well as implementation point of view to partition the compilation process
into several phases. A phase is a logically cohesive operation that takes as input one
representation of source program and produces as output another representation. (2)
Source program is a stream of characters: E.g. pos = init + rate * 60 (6)
– lexical analysis: groups characters into non-separable units, called token, and
generates token stream: id1 = id2 + id3 * const
• The information about the identifiers must be stored somewhere (symbol
table).
– Syntax analysis: checks whether the token stream meets the grammatical
specification of the language and generates the syntax tree.
– Semantic analysis: checks whether the program has a meaning (e.g. if pos is a record
and init and rate are integers then the assignment does not make a sense).
:=
:=
id1
+
id1
+
id2
*
id2
*
id3 inttoreal
id3 60 60
-3-
APR/MAY-'08/CS1352-Answer Key
Error Handling
Detection of Different Errors Which Correspond to All Phases
Each phase should know somehow to deal with error, so that compilation
can proceed, to allow further errors to be detected
Source Program
1
Lexical Analyzer
2
Syntax Analyzer
3
Semantic Analyzer
5
Code Optimizer
6
Code Generator
Target Program
(4)
(OR)
Passes: (2)
several phases are implemented in a single pass consisting of reading an input file
and writing an output file. The activity of those phases can be interleaved during the pass.
-4-
APR/MAY-'08/CS1352-Answer Key
-5-
APR/MAY-'08/CS1352-Answer Key
12. a. Find the SLR parsing table for the given grammar and parse the sentence
(a+b)*c. E->E+E | E*E | (E) | id.
Given grammar: E->E*.E
1. E->E+E E->.E+E
2. E->E*E E->.E*E
3. E->(E) E->.(E)
4. E->id E->.id
Augmented grammar: I6: goto(I2, E)
E’->E E->(E.)
E->E+E E->E.+E
E->E*E E->E.*E
E->(E) I7: goto(I4, E)
E->id E->E+E.
I0: E’->.E E->E.+E
E->.E+E E->E.*E
E->.E*E I8: goto(I5, E)
E->.(E) E->E*E.
E->.id E->E.+E
I1: goto(I0, E) E->E.*E
E’->E.
E->E.+E goto(I2, ()=I2
E->E.*E goto(I2, id)=I3
I2: goto(I0, () goto(I4, ()=I2
E->(.E) goto(I4, id)=I3
E->.E+E goto(I5, ()=I2
E->.E*E goto(I5, id)=I3
E->.(E)
E->.id I9: goto(I6, ))
I3: goto(I0, id) E->(E).
E->id.
I4: goto(I1, +) goto(I6, +)=I4
E->E+.E goto(I6, *)=I5
E->.E+E goto(I7, +)=I4
E->.E*E goto(I7, *)=I5
E->.(E) goto(I8, +)=I4
E->.id goto(I8, *)=I5
I5: goto(I1, *)
-6-
APR/MAY-'08/CS1352-Answer Key
Action Goto
States
+ * ( ) id $ E
0 S2 S3 1
1 S4 S5 Acc
2 S2 S3 6
3 r4 r4 r4 r4
4 S2 S3 7
5 S2 S3 8
6 S4 S5 S9
7 S4, r1 S5, r1 r1 r1
8 S4, r2 S5, r2 r2 r2
9 r3 r3 r3 r3
0 (a+b)*c$ shift 2
0(2 a+b)*c$ shift 3
0(2a3 +b)*c$ reduce by E->id
0(2E6 +b)*c$ shift 4
0(2E6+4 b)*c$ shift 3
0(2E6+4b3 )*c$ reduce by E->id
0(2E6+4E7 )*c$ reduce by E->E+E
0(2E6 )*c$ shift 9
0(2E6)9 *c$ reduce by E->(E)
0E1 *c$ shift 5
0E1*5 c$ shift 3
0E1*5c3 $ reduce by E->id
0E1*5E8 $ reduce by E->E*E
0E1 $ accept
(OR)
b. Find the predictive parser for the given grammar and parse the sentence (a+b)*c.
E->E+E | E*E | (E) | id.
Elimination of left recursion (2)
Calculation of First (3)
Calculation of Follow (3)
Predictive parsing table (6)
Parsing the sentence (2)
-7-
APR/MAY-'08/CS1352-Answer Key
13. a. Generate intermediate code for the following code segment along with the required
syntax directed translation scheme: (8)
i) if(a>b)
x=a+b
else
x=a-b
where a and x are of real and b of int type data.
Syntax directed translation scheme for if E then S1 else S2:
E.true:= newlabel;
E.false:=newlabel;
S1.next:=S.next;
S2.next:=S.next;
S.code:=E.code || gen(E.true “:”) || S1.code || gen(‘goto’ S.next) ||
gen(E.false “:”) || S2.code
Intermediate code generated:
if a>b got L1
goto L2
L1: t1:=inttoreal(b)
x:=a+t1
goto L3
L2: t2:=inttoreal(b)
x:=a-t2
L3:
ii) int a,b; (8)
float c;
a=10;
switch(a)
{
case 10: c=1;
case 20: c=2;
}
Switch/Case statement in source language form:
switch(E)
{
Case V1: S1
..
Case Vn-1:Sn-1
default: Sn
}
Translation of source switch/case statement into intermediate language 3 A C:
Translation 1:
code to evaluate E into temporary t
goto test
L1: code for S1
goto next
-8-
APR/MAY-'08/CS1352-Answer Key
-9-
APR/MAY-'08/CS1352-Answer Key
(OR)
b. i. Generate intermediate code for the following code segment along with the required
syntax directed translation scheme: (8)
i=1; s=0;
while(i<=10)
s=s+a[i][i]
i=i+1
Semantic rules for while E do S1:
S.begin:=newlabel
E.true:= newlabel;
E.false:=S.next;
S1.next:=S.begin;
S.code:=gen(S.begin’:’) || E.code || gen(E.true “:”) || S1.code || gen(‘goto’ S.begin)
- 10 -
APR/MAY-'08/CS1352-Answer Key
14. a. i. Explain the various issues in the design of code generation. (6)
Input to the code generator
Intermediate representation of the source program, like linear
representations such as postfix notation, three address representations such as
quadruples, virtual machine representations such as stack machine code and
graphical representations such as syntax trees and dags.
Target programs
It is the output such as absolute machine language, relocatable machine
language or assembly language.
Memory management
Mapping of names in the source program to addresses of data object in run
time memory is done by front end and the code generator.
Instruction selection
Nature of the instruction set of the target machine determines the difficulty of
instruction selection.
Register allocation
Instructions involving registers are shorter and faster. The use of registers
is being divided into two sub problems:
o During register allocation, we select the set of variables that will reside in
registers at a point in the program
o During a subsequent register assignment phase, we pick the specific
register that a variable will reside in
Choice of evaluation order
The order in which computations are performed affect the efficiency of target
code.
Approaches to code generation
ii. Explain code generation phase with simple code generation algorithm. (10)
It generates target code for a sequence of three address statements. (2)
Assumptions:
For each operator in three address statement, there is a corresponding target
language operator.
Computed results can be left in registers as long as possible.
E.g. a=b+c: (2)
Add Rj,Ri where Ri has b and Rj has c and result in Ri. Cost=1;
Add c, Ri where Ri has b and result in Ri. Cost=2;
Mov c, Rj; Add Rj, Ri; Cost=3;
Register descriptor: Keeps track of what is currently in each register
Address descriptor: Keeps tracks of the location where the current value of the name
can be found at run time. (2)
- 11 -
APR/MAY-'08/CS1352-Answer Key
(OR)
b. i. Generate DAG representation of the following code and list out the
applications of DAG representation: (12)
i=1; s=0;
while(i<=10)
s=s+a[i][i]
i=i+1
Intermediate code generated: (4)
(1) i:=1
(2) s:=0
(3) if i ≠ 10 goto (5)
(4) goto (15)
(5) t1=i*10
(6) t1=t1+i
(7) t1=t1+4
(8) t2=addr(a)-44 //(1*10+1)*4
(9) t3=t2[t1] //a[i][j]
(10) t4=s+t3
(11) s=t4
(12) t5=i+1
(13) i=t5
(14) goto (3)
(15) …
DAG generation: (4)
Applications of DAG: (4)
· Determining the common sub-expressions.
· Determining which identifiers have their values used in the block
· Determining which statements compute values that could be used outside the block
· Simplifying the list of quadruples by eliminating the common sub-expressions and not
performing the assignment of the form x: = y unless and until it is a must.
- 12 -
APR/MAY-'08/CS1352-Answer Key
ii. Write short notes on next-use information with suitable example. (4)
If the name in a register is no longer needed, then the register can be assigned to
some other name. This idea of keeping a name in storage only if it will be used
subsequently can be applied in a number of contexts.
Computing next uses: (2)
The use of a name in a three-address statement is defined as follows: Suppose a
three-address statement i assigns a value to x. If statement j has x as an operand and
control can flow from statement i to j along a path that has no intervening assignments to
x, then we say statement j uses the value of x computed at i.
Example:
x:=i
j:=x op y // j uses the value of x
Algorithm to determine next use: (2)
The algorithm to determine next uses makes a backward pass over each basic
block, recording for each name x whether x has a next use in the block and if not,
whether it is live on exit from the block (using data flow analysis). Suppose we reach
three-address statement i: x: =y op z in our backward scan. Then do the following:
Attach to statement i, the information currently found in the symbol table
regarding the next use and the liveness of x, y, and z.
In the symbol table, set x to “not live” and “no next use”
In the symbol table, set y and z to “live” and the next uses of y and z to i.
- 13 -
APR/MAY-'08/CS1352-Answer Key
- 14 -
APR/MAY-'08/CS1352-Answer Key
• Copy-Restore: Hybrid between call-by-value and call-by-ref (copy in, copy out)
– Actual parameters evaluated, its r-value is passed and l-value of the actuals
are determined
– When the called procedure is done, r-value of the formals are copied back to
the l-value of the actuals
• Call by name
– Inline expansion(procedures are treated like a macro)
(OR)
- 15 -