2 Compiler Design Notes
2 Compiler Design Notes
Lexical Analysis
The first phase of scanner works as a text scanner. This phase scans the source code as a stream of
characters and converts it into meaningful lexemes. Lexical analyzer represents these lexemes in the form of
tokens as:
<token-name, attribute-value>
Syntax Analysis
The next phase is called the syntax analysis or parsing. It takes the token produced by lexical analysis as
input and generates a parse tree (or syntax tree). In this phase, token arrangements are checked against the
source code grammar, i.e. the parser checks if the expression made by the tokens is syntactically correct.
Semantic Analysis
Semantic analysis checks whether the parse tree constructed follows the rules of language. For example,
assignment of values is between compatible data types, and adding string to an integer. Also, the semantic
analyzer keeps track of identifiers, their types and expressions; whether identifiers are declared before use or
not etc. The semantic analyzer produces an annotated syntax tree as an output.
Code Optimization
The next phase does code optimization of the intermediate code. Optimization can be assumed as something
that removes unnecessary code lines, and arranges the sequence of statements in order to speed up the
program execution without wasting resources (CPU, memory).
Code Generation
In this phase, the code generator takes the optimized representation of the intermediate code and maps it to
the target machine language. The code generator translates the intermediate code into a sequence of
(generally) re-locatable machine code. Sequence of instructions of machine code performs the task as the
intermediate code would do.
Symbol Table
It is a data-structure maintained throughout all the phases of a compiler. All the identifier's names along with
their types are stored here. The symbol table makes it easier for the compiler to quickly search the identifier
record and retrieve it. The symbol table is also used for scope management.
2.Explain the various phases of a compiler in detail. Also write down the output for the
following expression after each phase a:= b*c-d.(16)
3.Write in detail about the analysis cousins of the compiler.(16) (May/June-2013)
1. Preprocessor.
2. Assembler.
3. Loader and Link-editor.
PREPROCESSOR
A preprocessor is a program that processes its input data to produce output that is used as input to another
program. The output is said to be a preprocessed form of the input data, which is often used by some
subsequent programs like compilers. The preprocessor is executed before the actual compilation of code
begins, therefore the preprocessor digests all these directives before any code is generated by the statements.
1. Macro processing:
A macro is a rule or pattern that specifies how a certain input sequence (often a sequence of characters)
should be mapped to an output sequence (also often a sequence of characters) according to a defined
procedure. The mapping processes that instantiates (transforms) a macro into a specific output sequence is
known as macro expansion. macro definitions (#define, #undef)To define preprocessor macros we can use
#define. Its format is:
2.File Inclusion:
Preprocessor includes header files into the program text. When the preprocessor finds an #include directive it
replaces it by the entire content of the specified file. There are two ways to specify a file to be included:
#include "file"
#include <file>
3."Rational Preprocessors:
These processors augment older languages with more modern flow of control and data structuring facilities.
For example, such a preprocessor might provide the user with built-in macros for constructs like while-
statements or if-statements,where none exist in the programming language itself.
4.Language extension :
These processors attempt to add capabilities to the language by what amounts to built-in macros. For
example, the language equal is a database query language embedded in C. Statements begging with ## are
taken by the preprocessor to be database access statements unrelated to C and are translated into procedure
calls on routines that perform the database access.
ASSEMBLER
Typically a modern assembler creates object code by translating assembly instruction mnemonics into
opcodes, and by resolving symbolic names for memory locations and other entities. The use of symbolic
references is a key feature of assemblers, saving tedious calculations and manual address updates after
program modifications. Most assemblers also include macro facilities for performing textual substitution-e.g.,
to generate common short sequences of instructions as inline, instead of called subroutines, or even generate
entire programs or program suites.
There are two types of assemblers based on how many passes through the source are needed to produce the
executable program.
One-pass assemblers go through the source code once and assumes that all symbols will be defined
before any instruction that references them.
Two-pass assemblers create a table with all symbols and their values in the first pass, then use the
table in a second pass to generate code. The assembler must at least be able to determine the length of
each instruction on the first pass so that the addresses of symbols can be calculated.
4.Describe how various phases could be combined as a pass in a compiler? April/May 2008
Also briefly explain Compiler construction tools . (April/May 2015)
Lexical Analyzer
Syntax Analyzer
Semantic Analyzer
Intermediate Code Generator
Code Optimizer
Code Generator
5.For the following expression
Position:=initial+ rate*60
Write down the output after each phase
6. Describe the following software tools i. Structure Editors ii. Pretty printers iii. Interpreters (8)
1. Structure Editors: takes input a sequence of commands to build a source program.
Performs text creation and modification, analyzes the program text – hierarchical structure
(check the i/p is correctly formed).
2 . Pretty Printers: analyzes the program and prints the structure of the program becomes clearly
visible.
3. Interpreters: Instead of producing a target program as a translation, it performs the operations
implied by the source program.
22. What is the need for separating the analysis phase into lexical analysis and parsing?
i)Separation of lexical analysis from syntax allows the simplified design.
ii)Efficiency of complier gets increased. Reading of input source file is a time consuming process and if it is
been done once in lexical analyzer then efficiency get increased . Lexical analyzer uses buffering techniques
to improve the performances.
iii)Input alphabet peculiarities and other device specific anomalies can be restricted to the lexical analyzer.
23.Draw a transition diagram to represent relational operators.
The relational operators are:<,<=,>,>=,=,!=
24. State any reasons as to why phases of compiler should be grouped.(May/June 2014)
1.By keeping the same front end and attaching different back ends one can produce a compiler for dame
source language on different machine.
2.By keeping different fronts ends and same back end one can compile several different language on the same
machine.
25. Define a context free grammar.
A context free grammar G is a collection of the following
V is a set of non terminals
T is a set of terminals
S is a start symbol
P is a set of production rules
G can be represented as G = (V,T,S,P)
Production rules are given in the following form
Non terminal → (V U T)*
26.Draw a NFA for a*|b*.(April/May-2004,Nov/Dec-2005)
On erroneous input, LALR parser may proceed to do some reductions after the LR parser has declared
_
an error, but LALR parser never shift a symbol after the LR parser declares an error.
39.What is the syntax for YACC source specification program?
Declarations
%%
Translation rules
%%
Supporting C-routines
40.Define terminal.
Terminals are the basic symbols from which the strings are formed.
41. Define Nonterminal
Nonterminal are syntactic variables that denote set of strings.
42.What are the possible actions of a shift reduce parser?
Shift.
Reduce.
Accept.
Error.
43.List down the conflicts during shift-reduce parsing.
Shift/reduce conflict.
Parser cannot decide whether to shift or reduce.
Reduce/reduce conflict.
Parser cannot decide which of the several reductions to make.
44. Differentiate Kernel and non-Kernel items.
Kernel items, which include the initial item, S’ -> .S and all items whose dots are not at the left end. Whereas
the nonkernel items have their dots at the left end.
45. What are the components of LR parser?
An input.
An output.
A stack.
A driver program.
A parsing table.
46. List the different techniques to construct an LR parsing table?
Simple LR(SLR).
Canonical LR.
Lookahead LR (LALR).
47. Elininate left recursion from the following grammar AAc/Aad/bd/є . (May/June 2013)
AbdA'
A' aAcA' / adA' /є
48.Write the regular expression for identifier and whitespace.(Nov/Dec 2013)
An Identifier (or Name)
/[a-zA-Z_][0-9a-zA-Z_]*/
1. Begin with one letters or underscore, followed by zero or more digits, letters and underscore.
2. You can use meta character \w (word character) for [a-zA-Z0-9_]; \d (digit) for [0-9]. Hence, it can be written
as /[a-zA-Z_]\w*/.
3. To include dash (-) in the identifier, use /[a-zA-Z_][\w-]*/. Nonetheless, dash conflicts with subtraction and is
often excluded from identifier.
An Whitespace
/\s\s/ # Matches two whitespaces
/\S\S\s/ # Two non-whitespaces followed by a whitespace
/\s+/ # one or more whitespaces
/\S+\s\S+/ # two words (non-whitespaces) separated by a whitespace
49. Eliminate the left recursion for the grammar (Nov/Dec 2013)
SAa|b
AAc|Sd|є
Sol:
Let's use the ordering S, A (S = A1, A = A2).
• When i = 1, we skip the "for j" loop and remove immediate left recursion from the S productions (there is none).
• When i = 2 and j = 1, we substitute the S-productions in A → Sd to obtain the A-productions A → Ac | Aad | bd | ε
• Eliminating immediate left recursion from the A productions yields the grammar:
S → Aa | b
A → bdA' | A'
A' → cA' | adA' | ε
50. Compare the feature of DFA and NFA.(May/June 2014)
1.Both are transition functions of automata. In DFA the next possible state is distinctly set while in NFA each pair of
state and input symbol can have many possible next states.
2.NFA can use empty string transition while DFA cannot use empty string transition.
3.NFA is easier to construct while it is more difficult to construct DFA.
4.Backtracking is allowed in DFA while in NFA it may or may not be allowed.
5.DFA requires more space while NFA requires less space.
51. Eliminate left recursion for the grammar.
EE+T/T, TT*F/F,F(E)/id (April/May-08,Marks 2)
ETE’
E’+TE’/є
TFT’
T’*FT’/є
F(E)/id
PART-B
1.(i) Construct Predictive Parser for the following grammar:(May/June-2012&13)(10)
(ii) Describe the conflicts that may occur during shift reduce parsing. (may/june-2012).(6)
2.What is FIRST and FOLLOW? Explain in detail with an example. Write down the necessary algorithm.(16)
3. Consider the grammar given below:
E E+T
ET
T T*F
TF
F (E)
F id
Construct an LR parsing side for the above grammar. Give the moves of LR parser on id*id+id
4. Consider the following grammar(16). (Nov/Dec-2012)
E→E+T | T
T→TF | F
F→F* | a | b
construct the SLR parsing table for this grammar. Also parse the input a*b+a .
5. (i)Construct SLR parsing table for the following grammar (10). (Nov/Dec-2012,April/May-04,Marks 8)
S L=R|R L*R| id RL
(ii) Write down the rules for FIRST and FOLLOW (6). (Nov/Dec-2012)
6. . Check whether the following grammar is a LL(1) grammar
S iEtS | iEtSeS | a
E b
Also define the FIRST and FOLLOW procedures. (16)
7. Construct non recursion predictive parsing table for the following grammar.
EE or E /E and E/ not E / (E) / 0 /1. (Dec-12,Marks 16).
8. . i. Find the language from (4)
S → 0S1 | 0A1 A→1A0 | 10
ii.Define Parse tree , Regular Expression , Left most derivation , Right most (4)
derivation , and write example for each.
iii.Write algorithm to convert NFA from Regular expression. (4)
iv.Find the language from (4)
S→0S1 | 0A | 0 |1B | 1 A→0A | 0 B→1B|1
Hints:
i. Refer rules.
ii.Definition of parse tree, an regular expression and LMD,RMDwith examples
iii.Algorithm
9.i.Prove the grammar is ambiguous. (4)
E→E+E | E*E | (E) | id
ii. Specify the demerits of ambiguous grammar. (2)
iii. What are the rules to convert an unambiguous grammar from ambiguous grammar .Write
necessary steps for the above ambiguous grammar.
iv. Using unambiguous grammar, write Leftmost derivation ,draw parse tree for the string
id*id*id+id*id
Hints:
Draw more than parse tree for any expression.
It is difficult to find which parse tree is correct for evaluation
Refer rules.
Apply rules for conversion.
10. Write in detail about
i.Recursive descent parsing with algorithm.
ii.Top down parsing with algorithm.
Hints:
Definition of recursive descent and algorithm.
Definition of top down parser , and algorithm for top down parsing table , parsing method of top
down parser.
11. Construct predictive parsing table and parse the string NOT(true OR false)
bexpr→bexpr OR bterm | bterm
bterm→bterm AND bfactor | bfactor
bfactor→NOT bfactor | (bexpr) | true | false
Hints:
Find FIRST and FOLLOW and
construct table and
parse the string.
12. . Construct CLR parsing table to parse the sentence id=id*id for the following grammar.
S→ L=R | R
L→*R | id
R→L
Hints:
Find LR(1) items;
Construct CLR table.
Parse the string.
13. Parse the string (a,a) using SLR parsing table.
S→ (L) | a
L→L , S | S
Hints:
Find LR(0) items.
Construct SLR parsing table.
Parse the string.
14. Construct LALR parsing table for the grammar.
E→ E+T | T
T→ T*F | F
F→ (E) | id
Hints:
Find LR(1) items.
Find same core items different second component .them merge it
After merging construct LALR parsing table.
15. Write algorithms for SLR and CLR string parsing algorithm.
Hints:
Brief introduction about SLR and CLR parsers.
Algorithm
16.(i) Generate SLR Parsing table for the following grammar. (12)
S-->Aa|bAc|Bc|bBa
A-->d
B-->d
And parse the sentences “bdc”and “dd”.(April/May 2015)
17.(i) Write the algorithm to eliminate left-recursion and left-factoring and apply both to the following
grammar. (8)
E-->E+T|E-T|T
T-->a|b|(E) ( April/May 2015)
RUN-TIME ENVIRONMENT: Source Language Issues-Storage Organization-Storage Allocation-Parameter Passing-Symbol Tables-Dynamic Storage Allocation-
Storage Allocation in FORTAN.
PART-A
1.Define syntax directed definition.
Syntax directed definition is a generalization of context free grammar in which each grammar production
X→α is a associated with it a set of semantic rules of the form a:=f(b1,b2,...bk) , where a is an attribute
obtained from the function f.
2.What is mean by syntax directed definition.
It is a generalization of a CFG in which each grammar symbol has an associated set of attributes like,
synthesized attribute and inherited attribute
3.How the value of synthesized attribute is computed?
It was computed from the values of attributes at the children of that node in the parse tree.
4.How the value of inherited attribute is computed?
It was computed from the value of attributes at the siblings and parent of that node.
5.What is mean by construction of syntax tree for expression.
Construction syntax tree for an expression means translation of expression into postfix form. The nodes for
each operator and operand is created. Each node can implemented as a record with multiple fields.Following
are the function used in syntax tree for expression.
1. Mknode(op,left,right)
2. Mknode(id,entry)
3. Mkleaf(num,val)
6.What are the function of construction of syntax tree for expression? Explain.
1. Mknode(op,left,right)
This function creates a node with field operator having operator as label, and the two pointer
to left and right.
2. Mknode(id,entry)
This function creates identifier node with label id and a pointer to symbol table is given by
‘entry’.
3. Mkleaf(num,val)
This function creates node for number with label num and val is for value of that number.
7.What do you mean by DAG?
It is Directed Acyclic Graph. In this common sub expressions are eliminated. So it is a compact way of
representation. Like syntax tree DAG has nodes representing the subexpressions in the expression. These
nodes have operator, operand1 and operand2 where operands are the children of that node.
The Difference between DAG and syntax tree is that common subexpressions has more than one parent and
in syntax tree the common subexpression would be represented as duplicated subtree.
8.Construct a syntax tree and DAG for k:=k+5.
t1:=&x
a:=t1
34.Place the above code in Triplets and indirect Triplets.(April/May 2015)
Triple:
Op Arg1 Arg2
(0) * Y
(1) = X (0)
Indirect:
Op Arg1 Arg2 statement
11 * Y (0) 11
12 = (11) (1) 12
Triple:
Op Arg1 Arg2
(0) & x
(1) = a (0)
Indirect:
Op Arg1 Arg2 statement
11 & x (0) 11
12 = a (11) (1) 12
PART –B
1.Explain the concept of syntax directed definition.
2.Construct parse tree, syntax tree and annotated parse tree for the input string is 5*6+7;
3.Explain 1)Synthesized attribute 2)inherited attribute with suitable examples.
4.Write a syntax directed definition and evaluate 9*3+2 with parser stack using LR parsing method.
5.Consider the following CFG,
E→TR
R→+TR
R→-TR
R→є
T→num
With translation scheme to generate to generate postfix expression equivalent to the given infix
expression which is recognized by above grammar. All actions in the translation should be at the end of
each production
6.Explain the Implementing L-Attributed SDD's
7.(i)Given the Syntax-Directed Definition below construct the annotated parse tree for the input
expression: “int a, b, c”.
D → T L L.inh = T.type
T → int T.type = integer
T → float T.type = float
L → L1, id L1.inh = L.inh addType(id.entry,L.inh)
L → id addType(id.entry,L.inh)
(ii) Given the Syntax-Directed Definition below with the synthesized attribute val, draw the annotated
parse tree for the expression (3+4) * (5+6).
L → E L.val = E.val
E → T E.val = T.val
E → E1 + T E.val = E1.val + T.val
T → F T.val = F.val
T → T1 * F T.val = T1.val * F.val
F → ( E ) F.val = E.val
F → digit F.val = digit.lexval
8.Explain the various structures that are used for the symbol table constructions.(April/may 2012,2014)
9.(i)What are different storage allocation strategies? Explain.(8)(may/june-2013)
(ii)Specify a type checker which can handle expressions, statements and functions.(8)
10.Explain the organization of runtime storage in detail.
11.Explain about static and stack allocation in storage allocation strategies
12.Explain any 4 issues in storage allocation (4). (April/May 2015)
13.Give a Syntax directed Definitions to differentiate expressions formed by applying the arithmetic
operators + and * to the variable X and constants ; expression :X*(3*X+X*X). ( April/May 2015) (8)
14. For the given program fragment A[i,j]=B[i,k] do the following:
(i)Draw the annotated parse tree with the translation scheme to convert to three address code (6)
(ii) Write the 3-address code(6)
(iii)Determine the address of A[3,5] where , all are integer arrays with size of A as 10*10 and B as 10*10
with k=2 and the start index position of all arrays is at 1.(assume the base addresses) (4) (April/May 2015)
18(i).Apply Back-patching to generate intermediate code for the following input.
x:2+y;
If x<y then x:=x+y;
repeat y:=y*2;
while x>10 do x:=x/2;
Write the semantic rule and derive the Parse tree for the given code (12)
(ii) What is an Activation Record? Explain how its relevant to the intermediate code generation phase
with respect to procedure declarations. (4) (April/May 2015)
Unit- V Code Optimization And Code Generation
Principal Sources of Optimization-DAG- Optimization of Basic Blocks-Global Data Flow Analysis- Efficient Data Flow Algorithms-Issues in Design of a Code
Generator - A Simple Code Generator Algorithm.
1.What is meant by optimization?
It is a program transformation that made the code produced by compiling algorithms run faster or takes less space.
2.What are the principle sources of optimization?
The principle sources of optimization are, Optimization consists of detecting patterns in the program and replacing
these patterns by equivalent but more efficient constructs. The richest source of optimization is the efficient utilization
of the registers and instruction set of a machine.
3.Mention some of the major optimization techniques.
Local optimization
Loop optimization
Data flow analysis
Function preserving transformations
Algorithm optimization.
4.What are the methods available in loop optimization?
Code movement - Strength reduction- Loop test replacement- Induction variable elimination
5.What is the step takes place in peephole optimization?
It improves the performance of the target program by examining a short sequence of target instructions. It is called
peephole. Replace this instructions by a shorter or faster sequence whenever possible. It is very useful for intermediate
representation.
6.What are the characteristics of peephole optimization?
a. Redundant instruction elimination. b. Flow of control optimization c. Algebraic simplifications d. Use of machine
idioms
7.What are the various types of optimization?
The various type of optimization is, 1)Local optimization,2)Loop optimization,3)Data flow analysis,4)Global
optimization.
8.List the criteria for selecting a code optimization technique.
The criteria for selecting a good code optimization technique are, It should capture most of the potential improvement
without an unreasonable amount of effort. It should preserve the meaning of the program. It should reduce the time or
space taken by the object program.
9.What is meant by U-D chaining?
It is Use-Definition chaining. It is the process of gathering information about how global data flow analysis can be used
id called as use-definition (UD) chaining.
10.What do you mean by induction variable elimination?
It is the process of eliminating all the induction variables , except one when there are two or more induction variables
available in a loop is called induction variable elimination.
11.List any two structure preserving transformations adopted by the optimizer?
The structure preserving transformations adopted by the optimizer are, Basic blocks.-Flow graphs.
12.What are dominators?
A node of flow graph is said to be a dominator, i.e one node dominates the other node if every path from the initial
node of the flow graph to that node goes through the first node.(d Dom n).when d-node dominates n-node.
13.What is meant by constant folding?
Constant folding is the process of replacing expressions by their value if the value can be computed at complex time.
14.Define optimizing compilers.(Nov/Dec 2013)
Compilers that apply code-improving transformations are called optimizing compilers.
15.When do you say a transformation of a program is local?
A transformation of a program is called local, if it can be performed by looking only at the statement in a basic block.
16.Write a note on function preserving transformation.
A complier can improve a program by transformation without changing the function it compliers.
17.List the function –preserving transformation.
1)Common subexpression elimination.2)Copy propagation.3)Dead code elimination.4)Constant folding.
18.Define common subexpression.
An occurrence of an expression E is called a common subexpression if E was previously computed and the values of
variables in E have not changed since the previous computation.
19.What is meant by loop optimization?
The running time of a program may be improved if we decrease the number of instructions in a inner loop even if we
increase the amount of code outside that loop.
20.What do you mean by data flow equations?
A typical equation has the form out[s] = gen[s] U (in[s]-kill[s])
It can be read as information at the end of a statement is either generated within the statement or enters at the beginning
and is not killed as control flows through the statement.
State the meaning of in[s], out[s], kill[s], gen[s].
in[s]-The set of definitions reaching the beginning of S.out[s]-End of S.gen [s]-The set of definitions generated by
S.kill[s]-The set of definitions that never reach the end of S.
21.What is data flow analysis? (Nov/Dec 2012)
The data flow analysis is the transmission of useful relationships from all parts of the program to the places where the
information can be of use.
22.Define code motion and loop-variant computation.
Code motion: It is the process of taking a computation that yields the same result independent of the number of times
through the loops and placing it before the loop. Loop –variant computation: It is eliminating all the induction
variables, except one when there are two or more induction variables available in a loop
23.Define loop unrolling with example.(Nov/Dec 2013)
Loop overhead can be reduced by reducing the number of iterations and replicating the body of the loop.
Example:
In the code fragment below, the body of the loop can be replicated once and the number of iterations can be reduced
from 100 to 50.
for (i = 0; i < 100; i++)
g ();
Below is the code fragment after loop unrolling.
for (i = 0; i < 100; i += 2)
{
g ();
g ();
}
23.What is constant folding?(May/June 2013)
Constant folding is the process of replacing expressions by their value if the value can be computed at complex time.
24.How would you represent the dummy blocks with no statements indicated in global data flow
nalysis?(May/June 2013)
Dummy blocks with no statements are used as technical convenience (indicated as open circles).
25.What is meant by copy propagation or variable propagation?
One concerns assignments of the form f:=g called copy statements or copies for short
Eg: it means the use of variable V1 in place of V2
1.V1:=V2
2.f:V1+f
3.g:=v2+f-6
In statement 2, V2 can be used in place of V1 by copy propagations.
So,
1.V1=V2;
….
…
….
4.f:=V2+f
5.g:V2+f-6
This leads for common sub expression elimination further (V2+f is a common sub expression).
26.Define busy expressions.
An expression E is busy at a program point if and only if
An evaluation of E exits along some path P1,P2,….Pn starting at program point P1 and,
No definition of any operand of E exists before its evaluation along the path.
27.Define code generation. or What role does the target machine play on the code generation phase of the
compiler?(April/May 2015).
The code generation is the final phase of the compiler. It takes an intermediate representation of the source program as
the input and produces an equivalent target program as the output.
28.Define Target machine.
The target computer is byte-addressable machine with four bytes to a word and n-general purpose registers. R0, R1…
… … .Rn-1. It has two address instructions of the form Op, source, destination in which Op is an op-code, and source
and destination are data fields.
29.How do you calculate the cost of an instruction?
The cost of an instruction can be computed as one plus cost associated with the source and destination addressing
modes given by added cost.
MOV R0,R1 cost is 1
MOV R1,M cost is 2
SUB 5(R0),*10(R1) cost is 3
30.Define basic block. (Nov/Dec 2013)
A basic block contains sequence of consecutive statements, which may be entered only at the beginning and when it is
entered it is executed in sequence without halt or possibility of branch.
31.What are the rules to find “ leader” in basic block?
It is the first statement in a basic block is a leader.
Any statement which is the target of a conditional or unconditional goto is a leader.
Any statement which immediately follows a conditional goto is a leader.
32.Define flow graph.(Nov/Dec 2013)
Relationships between basic blocks are represented by a directed graph called flow graph.
33.What do you mean by DAG?
It is Directed Acyclic Graph. In this common sub expressions are eliminated. So it is a compact way of representation.
34.List the advantages of DAGs (Nov/Dec 2012)
It automatically detects common sub expression.
We can determine which identifiers have their values used in the block.
We can determine which statements compute values, and which could be used outside the block. It reconstruct a
simplified list of quadruples taking advantage of common sub expressions and not performs assignments of the form
a=b unless necessary.
35.What is meant by registers and address descriptor?
Register descriptor: It contains information’ s about,1.What registers are currently in use. 2.What registers are empty.
Address descriptor: It keeps track of the location where the current value of the name can be found at run time.
36.Explain heuristic ordering for DAG?
It is based on Node Listing algorithm.
While unlisted interior nodes remain do
begin
Select an unlisted node n , all of whose parents have been listed;
list n;
While the leftmost child ‘m’ of ‘n’ has no unlisted parents and is not a
leaf do
do
{
/*since n was just listed, surely m is not yet listed */
}
begin
list m
n=m;
end
end
37.Write labeling algorithm.
if n is a leaf then
if n is the leftmost child of its parent then
LABEL(n)=1
else
LABEL(n)=0
else
begin
let n1 ,n2 ,n3,,,nk be the children of n ordered by LABEL , so
LABEL(n1)>=LABEL(n2)>=… … … LABEL(nk)
LABEL(n)=max(LABEL(ni)+i –1)
End
38.Define live variable.(Nov/Dec 2012)
A variable is live at a point in a program if its value can be used subsequently.
39.What are the different storage allocation strategies?
1) Static allocation.-It lays out storage for all data objects at compile time.
2) Stack allocation.-It manages the runtime storage as a stack.
3) Heap allocation.-It allocates and de-allocates storage as needed at runtime from a data area.
40.What is the use of Next-use information?(Nov/Dec 2013)
If a register contains a value for a name that is no longer needed, we should re-use that register for another
name (rather than using a memory location)
So it is useful to determine whether/when a name is used again in a block
Definition: Given statements i, j, and variable x,
– If i assigns a value to x, and
– j has x as an operand, and
– No intervening statement assigns a value to x,
– Then j uses the value of x computed at i.
41.How liveness variable calculated.(April/May 2015)
A variable is live if it holds a value that will/might be used in the future. The representation of the program that we use
for liveness analysis (determining which variables are live at each point in a program), is a control flow graph. The
nodes in a control flow graph are basic statements (instructions). There is an edge from statement x to statement y if x
can be immediately followed by y (control flows from x to y).
42.Write the algorithm that orders the DAG nodes for generating optimal target code?.(April/May 2015)
(1) while unlisted interior nodes remain do begin
(2) select an unlisted node n, all of whose parents have been listed ;
(3) list n;
(4) while the leftmost child m of n has no unlisted parents
and is not a leaf do
/* since n was just listed , m is not yet listed*/
begin
(5) list m;
(6) n=m
end
end
PART –B
1.Explain the principle sources of optimization in detail.
2. Discuss about the following:
i). Copy Propagation
ii) Dead-code Elimination
iii) Code motion.
3(i).Explain optimization of basic blocks.
(ii)Explain redundant common subexpression elimination.
4.Write about data flow analysis of structural programs.
5.Optimize the following code using various optimization techniques:
i=1,s=0;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
c[i][j]=c[i][j]+a[i][j]+b[i][j];
6.(i)Explain the issues in design of code generator.
(ii)Explain peephole optimization.
7.Explain the simple code generator with a suitable example
8.Write detailed notes on Basic blocks and flow graphs.
9.Define a Directed Acyclic Graph. Construct a DAG and write the sequence of instructions for the
expression a+a*(b-c)+(b-c)*d. (May/June 2014)
10.Explain register allocation and assignment.
11(i).Explain loops in flow graphs.
(ii).Explain Local optimization.
12.(i) Write the code generation algorithm using dynamic programming and generate code for the
statement x=a/(b-c)-s*(e+f) [Assume all instructions to be unit cost] (12)
(ii) What are the advantages of DAG representation ? Give example.(4) (April/May 2015)
13.(i)Write the procedure to perform Refister Allocation and Assignment with Graph Coloring.(8)
(ii)Construct DAG and optimal target code for the expression
X=((a+b)/(b-c))-(a+b)*(b-c)+f. (April/May 2015).(8)
14.Perform analysis of available expressions on the following code by converting into basic blocks and
compute global common sub expression elimination.
II. I:=0
III. A:=n-3
IV. If i<a then loop else end
V. Label loop
VI. B:=i_4
VII. E:=p+b
VIII. D:-m[c]
IX. E:=d-2
X. F:=I-4
XI. G:=p+f
XII. M[g]:=e
XIII. I:=i+1
XIV. A:=n-3
XV. If i<a then loop else end
XVI. Label end (April/May 2015)
15.(i) Explain Loop optimization in details and apply it to the code (10)
I:=0
A:=n-3
If i<a then loop else end
Label loop
B:=i_4
E:=p+b
D:-m[c]
E:=d-2
F:=I-4
G:=p+f
M[g]:=e
I:=i+1
A:=n-3
(ii)What are the optimization technique applied on procedure calls? Explain with example .(6) (April/May
2015)