Compiler Key2
Compiler Key2
10. Draw the diagram of the general activation record and give the purpose of any
two fields.
Returned value
Actual parameters
Optional control link
Optional access link
Saved machine status
Local data
temporaries
Temporaries are used to hold values that arise in the evaluation of expressions.
Returned value field is used by the called procedure to return a value to the calling
procedure
PART – B
11. a. i. Write about the phases of compiler and by assuming an input and show the
output of various phases. (10)
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)
-2-
MAY/JUNE-'07/CS1352-Answer Key
– 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
– Intermediate code generation, intermediate code is something that is both close to the
final machine code and easy to manipulate (for optimization). One example is the three-
address code:
dst = op1 op op2
• The three-address code for the assignment statement:
temp1 = inttoreal(60);
temp2 = id3 * temp1;
temp3 = id2 + temp2;
id1 = temp3
-3-
MAY/JUNE-'07/CS1352-Answer Key
Source Program
1
Lexical Analyzer
2
Syntax Analyzer
3
Semantic Analyzer
5
Code Optimizer
6
Code Generator
Target Program
(2)
(OR)
The algorithm is syntax directed in that it uses the syntactic structure of the
regular expression to guide the construction process. First, parse the regular expression r
into its constituent sub expressions. Then using various rules, construct NFA’s for each
of the basic symbols in r.
-4-
MAY/JUNE-'07/CS1352-Answer Key
-5-
MAY/JUNE-'07/CS1352-Answer Key
-6-
MAY/JUNE-'07/CS1352-Answer Key
-7-
MAY/JUNE-'07/CS1352-Answer Key
ii. What are the different strategies that a parser can employ to recover from syntax
errors? (6)
Panic mode recovery
On discovering an error, the parser discards input symbols one at a time until
one of a designated set of synchronizing tokens is found.
Phrase level recovery
On discovering an error, the parser may perform local correction on the
remaining input; e.g. replace prefix of the remaining input by some string that allow
the parser to continue
Error productions
Augment the grammar for the language with productions that generate the
erroneous constructs. If it is being used by the parser, generate appropriate error
diagnostics to indicate the erroneous construct that has been recognized in the input
Global correction
It does minimal changes in the incorrect input string to obtain a globally least-cost
correction.
(OR)
Action Goto
States
a b $ S A
0 s3 1 2
1 acc
2 r2, r3, s5 r2, r3, s3 4
3 r3 r3 r3
4 s5
5 r2 r2
-8-
MAY/JUNE-'07/CS1352-Answer Key
-9-
MAY/JUNE-'07/CS1352-Answer Key
Triples
Op arg1 arg2
(0) uminus c
(1) * b (0)
(2) uminus c
(3) * b (2)
(4) + (1) (3)
(5) assign a (4)
Indirect Triples
Op arg1 arg2 Statement
(14) uminus c (0) (14)
(15) * b (14) (1) (15)
(16) uminus c (2) (16)
(17) * b (16) (3) (17)
(18) + (15) (17) (4) (18)
(19) assign a (18) (5) (19)
ii. Give the syntax-directed definition for flow of control statements. (8)
Flow of control statements:
S-> if E then S1 | if E then S1 else S2 | while E do S1
If-statement: (4)
Semantic rules for if E then S1:
E.true:= newlabel;
E.false:=S.next;
S1.next:=S.next;
S.code:=E.code || gen(E.true “:”) || S1.code
Semantic rules 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
Example:
Statement: a and b and c
if a were false, then we need not evaluate the rest of the expressions. So, we insert
labels E.true and E.false in the appropriate places.
if a goto E.true
goto E.false
E.true: if b goto E1.true
goto E.false
E1.true: if c goto E2.true
goto E.false
E2.true : exp =1
E.false: exp =0
- 10 -
MAY/JUNE-'07/CS1352-Answer Key
b. i. How back patching can be used to generate code for Boolean expressions and
flow of control statements. (10)
Back patching is the activity of filling up unspecified information of labels using
appropriate semantic actions in during the code generation process. In the semantic
actions the functions used are mklist(i), merge_list(p1,p2) and backpatch(p,i). (2)
- 11 -
MAY/JUNE-'07/CS1352-Answer Key
- 12 -
MAY/JUNE-'07/CS1352-Answer Key
14. a. i. Write in detail about the issues in the design of a code generator. (10)
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.
- 13 -
MAY/JUNE-'07/CS1352-Answer Key
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. What are steps needed to compute the next use information? (6)
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.
(OR)
- 14 -
MAY/JUNE-'07/CS1352-Answer Key
t1
*
a
4 i
- 15 -
MAY/JUNE-'07/CS1352-Answer Key
• Constant Folding
x := 32
x := x + 32 becomes x := 64
• Unreachable Code
An unlabeled instruction immediately following an unconditional jump is removed.
goto L2
x := x + 1 unneeded
• Flow of control optimizations
Unnecessary jumps are eliminated.
goto L1
…
L1: goto L2 becomes goto L2
• Algebraic Simplification
x := x + 0 unneeded
• Dead code elimination
x := 32 where x not used after statement
y := x + y y := y + 32
• Reduction in strength
Replace expensive operations by equivalent cheaper ones
x := x * 2 x := x + x
- 16 -
MAY/JUNE-'07/CS1352-Answer Key
Define a portion of a flow graph called a region to be a set of nodes N that includes a
header, which dominates all other nodes in the region. All edges between nodes in N are
in the region, except for some that enter the header. The portion of a flow graph
corresponding to a statement S is a region that obeys the further restriction that control
can flow to just one outside block when it leaves the region.
gen[S] is the set of definitions “generated by S”.
kill[S] be the set of definitions that never reach the end of S, even if they reach the
beginning.
Both are synthesized attributes; they are computed bottom-up, from the smallest
statements to the largest.
Data-flow equations for reaching definitions:
- 17 -
MAY/JUNE-'07/CS1352-Answer Key
(OR)
- 18 -
MAY/JUNE-'07/CS1352-Answer Key
• Call by reference
• If an actual parameter is a name or expression having L-value, then that l-
value itself is passed
• However, if it is not (e.g. a+b or 2) that has no l-value, then expression is
evaluated in the new location and its address is passed.
• 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)
- 19 -