CD Question Bank
CD Question Bank
10. What are the phases involved in the front end and the back end of
the compiler? (Remember)
Front end:
Lexical analyzer
Syntax analyzer
Semantic analyzer
Intermediate code generator
Back end:
Intermediate code generator
Code optimizer
Code generator
12. What are the other names for the compiler construction tools?
(Remember)
Compiler - compilers, compiler – generators or translator - writing
systems.
19. What is token, pattern and lexeme? (April/May ‟11 & May/June
2013) (Nov/Dec 2016) (Apr/May ‟19)(Remember)
Token is a classification of lexical units.
Ex: id, num
Patterns are rules describing the set of lexemes belonging to a token.
Ex: letter followed by letters and digits.
Lexemes are the specific character strings that make up a token.
Ex: abc and 123
20. List the various error recovery strategies for a lexical analysis.
(May/June ‟12 & May/June2015, Apr/May „18) (Apr/May „22 )
(Remember)
Possible error recovery actions are:
· Panic mode recovery
· Deleting an extraneous character
3
· Inserting a missing character
4
· Replacing an incorrect character by a correct character
· Transposing two adjacent characters
21. What is sentinel? (Remember)
A sentinel is a special character that cannot be part of source
program.
5
29. What are character classes? (Remember)
The notation [abc] where a, b and c are alphabet symbols denoting the
regular expression a | b | c. The abbreviated character class such as [a –
z] denotes the regular expression a | b |….| z.
6
34. Compare syntax tree and parse tree.(Nov/Dec ‟12, Nov/Dec „17)
(Analyse)
SYNTAX TREE PARSE TREE
1 interior nodes are ―operators‖, interior nodes are non-
leaves are terminals, leaves are terminals
operands
2 when representing a program in rarely constructed as a data
a tree structure usually use a structure
syntax tree
3 Represents the abstract syntax Represents the concrete syntax
of a program (the semantics) of a program
35. Mention the issues in Lexical Analyzer (May/June 2013)
(Remember)
1. Simpler design is perhaps the most important consideration. The
separation of lexical analysis from syntax analysis often allows us
to simplify one or the other of these phases. For example, a parser
embodying the conventions for comments and white space is
significantly more complex than one that can assume comments
and white space have already been removed by a lexical analyzer.
2. Compiler efficiency is improved. A separate lexical analyzer
allows us to construct a specialized and potentially more efficient
processor for the task. It can significantly speed up the
performance of a compiler.
3. Compiler portability is enhanced. Input alphabet peculiarities
and other device-specific anomalies can be restricted to the lexical
analyzer.
36. Write the regular expression for white space and identifier
(Nov/Dec2013) (Remember)
Identifier: letter (letter/digit)*
White Space : delim -> blank | tab | newline
ws -> delim
7
40. Write a grammar for branching statement? (May/June 2016)
(Remember)
stmt if expr then stmt
| if expr then stmt else stmt
| other
41. List the rule that form the BASIS (Nov/Dec 2016) (Remember)
The rules that form the BASIS are
Any identifier is an expression
Any number is an expression
42. Draw a syntax tree for the expression a:=b*-c + b*-c (Nov/Dec ‟12,
Nov/Dec ‟11, Nov/Dec ‟17) (Create)
assign
a +
* *
uminus b uminus
b
b b
Syntax Tree
Uminus
+
id id
DFA NFA
DFA only has one state transition NFA is allowed to have more than
for every symbol of the alphabet one state transition for an input
symbol of the alphabet
DFA cannot use empty string NFA can use empty string
transition transition
8
PART B
1. Explain the various phases of a compiler and trace the program segment
a: =b+c* a-4. (April /May ‟11,Nov/Dec ‟11, May/June‟12, Nov/Dec
‟16, Nov/Dec ‟17) (Understand)
2. Explain the various phases of a compiler in detail .Write the output for
each phases of the compiler for the expression c:=a + b * 12.(Apr/May
‟19) (Understand)
3. Explain briefly about compiler construction tools and Write note on each
compiler construction tool. Outline the construction tools can be used to
implement various phases of a compiler. (May/June 2015, Nov/Dec
‟16,Nov/Dec‟17,Apr/May‟18,Nov/Dec‟18, Apr/May ‟19) (Understand)
4. Explain in detail the various phases of compilers with an example
(Nov/Dec ‟17) (Understand)
5. Describe the following software tools (Understand)
i. Structure Editors, ii. Pretty printers iii. Interpreters
6. Elaborate on grouping of phases in a compiler. (Nov/Dec ‟16) (create)
7. What are the phases of the compiler? Explain the phases in detail. Write
down the output of each phase for the expression a: = b + c *50
(Nov/Dec‟12 & May/June„13) (Remember)
8. Describe the various phases of compiler and trace it with the program
segment (position= initial + rate * 60) (Apr/May „22 ). (May/June 2016
.2018) (Understand)
9. Explain the need for grouping of phases. (May /June 2016)
(Understand)
10. Explain various Error encountered in different phases of compiler
(May /June 2016, Nov/Dec ‟16) (Understand)
11. Draw a diagram for the compilation of a machine language processing
system. (Apr/May ‟18) (create)
12. Write note on front and back end of compiler (Remember)
13. Explain Symbol table management and error handling (Understand)
14. Compare NFA and DFA. Construct a DFA directly from an augmented
regular expression ((ε| a)b*)*. (Nov/Dec‟12) (Analyse)
15. a.)What are the issues in Lexical analysis? (May/June„12) (Remember)
b.)Elaborate in detail recognition of tokens (Create)
16. Construct the minimized DFA for a Regular expression (0+1)* (0+1)10
(Nov/Dec ‟11) (Apr/May „22 ) (Apply)
17. Draw the DFA for the augmented regular expression (a|b)*# directly
using syntax tree. (Nov/Dec 2013) (Create)
18. Define the following terms : (May/June 2014)
i. Compiler, Interpreter, Translator and differentiate
between them. (Remember)
ii. Differentiate between lexeme, token and
pattern.(Apr/May‟17) (Analyse)
iii. What are the issues in lexical analysis? .(Apr/May‟17)
Remember)
iv. Elaborate specification of tokens (May/June‟13, Nov/Dec
‟18) (create)
9
19. Write notes on regular expression to NFA. Construct Regular
expression to NFA for the sentence (a/ b)* a and construct DFA to the
above expression (May/June 2016). (Remember) (Create)
20. Discuss how finite automata used to represent tokens and perform LA
with example (Nov/Dec 2016) (Create)
21. a) Conversion of a regular expression (a/b) * abb to NFA (Nov/Dec
2016) (Create)
b) Write an algorithm for minimizing the number of states of a DFA
(Nov/Dec 2016) (Create)
22. Explain in detail about the role of Lexical analyzer with the possible
error recovery actions. (May/June„13)(May/June 2015, Nov/Dec ‟16,
Nov/Dec ‟18 ) (Understand)
23. Draw the transition diagram for relational operators and unsigned
numbers. (Apr/May ‟17, Apr/May ‟18, Apr/May ‟19) (Create)
24. Convert the regular expression abb(a|b)* to DFA using direct method
and minimize it. (Apr/May ‟17) (Create)
25. Considering the alphabet ∑={0,1}. Construct a Non-Deterministic
Finite Automata (NFA) using the Thompson construction that is able to
recognize the sentences generated by the regular expression. (1*01*0)*1*
(Apr/May ‟18) (Apply)
PART C
1. Illustrate the output of each phase of compilation for input a:=b*c-d.
(Understand)
10
ASSIGNMENT
11
UNIT – II
SYNTAX ANALYSIS
PART -A
12
8. How to represent strings of terminals? (Remember)
Lower case letters late in the alphabet, chiefly u,v, …z , represent
strings of terminals.
14. Eliminate the left recursion for the grammar A-> Ac | Aad | bd| ε
(May/June‟13, Apr/May „17) (Analyse)
A->bdA‘
A‘->cA‘| adA‘| ε
15. Perform the left factor for the grammar
stmtif expr then stmt else stmt | if expr then stmt
Soln:
stmtif expr then stmt stmt‘
stmt‘else stmt |
16. Give examples for non context free languages. (Remember)
L1={wcw | w is in (a|b)*}
L2={anbmcnd m | n>=1 and m>=1}
L3={anbncn | n>=0}
13
17. What do you mean by top-down parsing? (Remember)
This is the parsing technique were the parse tree is created from root to
leaf.
Two techniques of top-down parsing are
1. Recursive Descent Parsing
2. Predictive Parsing.
24. What are the various conflicts that occur during shift reduce
parsing? (Apr/May „17) (Remember)
The various conflicts that occur during shift reduce parsing are
1. Shift/Reduce conflict. 2.Reduce/Reduce conflict.
14
for constructing right most derivation in reverse, and ―k‖ for the number of
symbols of look ahead that are used in making parsing decisions.
27. Write the algorithm for FIRST and FOLLOW in parser (May/June
2016) (Remember)
FIRST
1. If X is terminal, then FIRST(X) IS {X}.
2. If X → ε is a production, then add ε to FIRST(X).
3. If X is non terminal and X → Y1,Y2..Yk is a production, then place
a in FIRST(X) if
for some i , a is in FIRST(Yi) , and ε is in all of FIRST(Y1),…FIRST(Yi-
1);
FOLLOW
1. Place $ in FOLLOW(S),where S is the start symbol and $ is the
input right endmarker.
2. If there is a production A → αBβ, then everything in FIRST(β)
except for ε is placed in FOLLOW(B).
3. If there is a production A → αB, or a production A→ αBβ where FIRST(β)
contains ε ,then everything in FOLLOW(A) is in FOLLOW(B).
30. Eliminate the left recursion from the grammar (Nov/Dec 2013)
(Analyse) SAa|b AAc | Sd | e
Ans: S Aa|b
A SdA‘ | eA‘
A‘ cA‘ |
15
31. Summarize the merits and demerits of LALR Parser?
(Apr/May‟18) (Remember)
Merits:
● Maintains lookahead information
● Keeps automaton small
● Resulting automaton has same size as LR(0) automaton
Demerits:
● Takes time to construct the LALR Parsing table.
PART B
1. Construct predictive parsing table for the grammar (May/June‟13&
May/June„12) (Create)
S (L)/a LL,S/S.
and check whether the following sentences belong to that grammar or
not.
(a,a)
(a, (a , a))
(a, (a , (a , a))) (Apr/May ‟17) (Evaluate)
2. What are the different strategies that a parser can employ to recover from
syntax errors?
3. Explain in detail the conflicts that may occur during shift- reduce parsing
(May/June„12) (Understand)
4. Construct the SLR parsing table from SAA A Aa/b.
5. What is FIRST and FOLLOW? Explain in detail with an example. Write
down the necessary algorithm. (Remember)
6. Check whether the following grammar is SLR (1) or not. Explain your
answer with reasons.
a. S L=R
b. SR
16
c. L*R
d. Lid
e. RL (Create)
7. Check whether the following grammar is a LL (1) grammar(May/June
2016) (Apr/May‟ 22) (Analyze)
S iEtS | iEtSeS | a
E b Also define the FIRST and FOLLOW procedures.
8. Construct an SLR Parsing table for the following grammar. (April/May
‟11, Nov/Dec ‟11) (Create)
EE+T
ET
TT*F
TF
F (E)
Fid
9. Explain the error recovery strategies in syntax analysis (Understand)
10. List LR(0) items for the following grammar (May/June„13) (Evaluate)
S -> AS/b A -> SA/a.
11. Construct a non recursive Predictive Parsing table for the following
grammar: (Nov/Dec‟12) (Create)
1. E -> E or E=E and E | not E | (E) | 0 | 1
12. Construct SLR Parsing Table for the grammar (Nov/Dec‟12, Nov/Dec
‟16, Apr/May „17) (Create)
(i) EE+T | T
(ii) TTF | F
(iii) F F* | a | b
13. Design an LALR Parser for the following grammar and parse the input
id=id. (Nov/Dec 2013) (Create)
SL=R|R
L*R|id
RL
14. Construct the SLR parse table for the grammar .Show the actions of
the parser for the input string ―abab‖ (May/June 2014) (Create)
15. What is an ambiguous grammar? Is the following grammar
ambiguous? Prove EE+| E(E)|id.The grammar should be moved to the
next line ,centered. (May/June 2014) (Remember)
16. Draw NFA for the regular expression ab*/ab.(May/June 2014)
((Analyse))
17. Construct Stack implementation of shift reduce parsing for the
grammar
(i) E E+E
(ii) E E*E
(iii) E (E)
(iv) E id and the input string idl + id2 *id3 (June 2016)
(Create)
18. Write an algorithm for Non recursive predictive parsing. (May/June
2016) (Understand)
19. Explain Context free grammars with examples (May/June 2016)
(Understand)
17
20. Explain LR parsing algorithm with an example (Nov/Dec ‟17)
(Understand)
21. Explain the non-recursive implementation of predictive parsers with
the help of the grammar (Nov/Dec ‟17) (Understand)
EE+T |T
TT*F | F
F (E) | id.
22. Construct parse tree for the input string w=cad using top down
parsing
i. S cAD
ii. A ab | a (Nov/Dec ‟16) (Create)
23. Construct parsing table for the grammar and find the moves made by
predictive parser on input id+id*id and find FIRST and FOLLOW
(Nov/Dec ‟16) (Create)
24. Explain ambiguous grammar G: E->E+E | E*E | (E) | id for the
sentence id+id*id (Nov/Dec ‟16) (Understand)
25. Consider the context free grammar (CFG) depicted below where
“begin”, “end” and “x‖ are all terminal symbols of the grammar and stat
is considered the starting symbol for this grammar. Productions are
numbered in parenthesis and you can abbreviate ―begin‖ to ―b‖ and ―end‖
to ―e‖ respectively. (Apr/May ‟18) (Apply)
stat Block
Block begin Block end
Block Body
Body x
i) Compute the set of LR(1) items for this grammar and draw the
corresponding DFA. Do not forget to augment the grammar with
the initial production S->Start$ as the production(0).
ii) Construct the corresponding LR parsing table
iii)
26. Consider the following CFG grammar over the non-terminals {X,Y,Z}
and terminals {a,c,d} with the productions below and start symbol Z.
Xa XY
Z d
ZXYZ
Y c
Yε
Compute the FIRST and FOLLOW sets of every non-terminal and the
set of non-terminals that are nullable. (Apr/May ‟18) (Apply)
27. Consider the following CFG grammar (Apr/May ‟18) (Apply)
S aABe
A Abc | b
B d
Where a,b,c,d,e are terminals , ‗S‘ (Start symbol) , A, B are non-
terminals.
a) Parse the sentence ―abbcde‖ using right most derivation
b) Parse the sentence ―abbcde‖ using left most derivation
c) Draw the parse tree.
18
28. Construct a predictive parser for the grammar. Verify whether the
input string id+id*id is accepted by the grammar or not
E TE‘
E‘ +TE|
T FT‘
T‘*FT‘|
F (E)| id (APR/MAY ‟22)
PART C
1. Consider the following grammar (Apr/May ‟18) (Apply)
EE+E
EE*E
E(E)
E id
i) Find the SLR parsing table for the given grammar.
ii) Parse the sentence : (a + b ) * c
19
ASSIGNMENT
SAa|bAc|Bc|bBa
Ad
Bd
is LR(1),but not LALR(1). (Nov/Dec ‟18) (Understand)
4) Write the algorithm for construction of LALR parsing table for a given
grammar.Using the algorithm for construction of LALR parsing table
construct the LALR parsing table for the following grammar
S‘S
SaAd|bBd|aBe|bAe
Ac
Bc (Apr/May ‟19) (Remember)
UNIT-3
INTERMEDIATE CODE GENERATION
PART A
20
The various intermediate representation are syntax trees, postfix
notation and three address code.
a + a +
* * *
uminus b uminus
uminus b
b b
b b
21
10. Write any four types of three address statement? (Remember)
(Nov/Dec ‟11)
1) Assignment statements
2) Assignment Instructions
3) Copy statements
4) Unconditional jump
5) Conditional jump
6) Indexed assignments
7) Address and pointer assignments
8) Procedure calls and return
22
16. How to address the ith element in a single dimensional array?
(Understand)
If the width of each array element is w, then the ith element of array A
begins in location
base + ( i-low) * w
17. What are the two forms to store the two dimensional array?
(Remember)
A two-dimensional array is normally stored in one of two forms, either
row-major (row-by-row) or column-major (column-by-column).
In the case of a two-dimensional array stored in row-major form, the
relative address of A[ i1 ,i2] can be calculated by the formula
base + (( i1 - low1) x n2 + i2 – low2 ) x w
21. What are the three functions used to manipulate the list of labels
in backpatching? (Remember)
mklist(i) creates the new list. The index i is passed as an argument to
this function where I is an index to the array of quadruple.
merge_list(p1,p2) this function concatenates two lists pointed by p1
and p2. It returns the pointer to the concatenated list.
backpatch(p,i) inserts i as target label for the statement pointed by
pointer p.
23
2. Find which value in the list of cases is the same as the value of the
expression. The default value matches the expression if none of the
values explicitly mentioned in cases.
3. Execute the statement associated with the value found.
26. Translate the arithmetic expression a*-(b+c) into syntax tree and
postfix notation. (Understand) (Nov/Dec 2013)
Postfix notation abc+-*
Syntax tree
*
a uminus
b c
27. Write down the syntax directed definition of a simple desk
calculator? (Remember) (Nov/Dec „16)
24
PART B
1. Give the syntax-directed definition for flow of control statements.
(Remember)
2. How Back patching can be used to generate code for Boolean expressions
and flow of control statements. (Remember) (Nov/Dec ‟11)
3. How would you generate the intermediate code for the flow of control
statements? Explain with examples. (Remember) (May/June„13)
4. What are the various ways of calling procedures? Explain in detail.
(Remember)
5. Describe the method of generating syntax-directed definition for Control
statements. (Remember)
6. Give the semantic rules for declarations in a procedure. (Remember)
7. Explain about back patching with an example. (Remember)
(May/June‟13, May/June 2014)
8. Explain how the types and relative addresses of declared names are
computed and how scope information is dealt with. (Remember)
9. Describe in detail the syntax-directed translation of case statements.
(Remember)
10. Write down the translation scheme to generate three address code for
assignment statements. (Remember) (April/May ‟11, Nov/Dec ‟11 &
May/June„13) (Apr/May‟ 22)
11. (i) Translate the following switch statement into intermediate code.
(Understand) (Nov/Dec‟12)
Switch E
begin
case V1:S1
case V2:S2
case Vn-1:Sn-1
default:Sn
end
(ii) Generate three address code for the Boolean expression a<b or c<d
and e<f.
12. Translate the executable statements of the following C program into
three address code
(Understand) (Nov/Dec‟12)
main()
{
int i,a[10];
i=1;
while (i<=10){
a[i]=0;i=i+I;}
}
13. Define three-address code? Describe the various methods of
implementing the three address statements with an example. (Remember)
(April/May ‟11, Nov/Dec ‟11, May/June„13)
14. Discuss the various methods for translating Boolean expression
(Remember) (April/May‟11, May/June‟12)
25
15. (i) Describe the various types three-address statements. (Remember)
(May/June‟12) (Apr/May‟22)
(ii) How names can be looked up in the symbol table? Discuss.
(Remember)
16. Explain the following grammar for a simple procedure call.
(Understand) (May/June‟12)
statement S - > call id(Elist)
17. Write the translation scheme for the flow of control statements.
(Remember) (Nov/Dec 2013)
18. (i) Write the grammar and translation scheme for procedure call
statements. (Remember) (Nov/Dec 2013)
(ii) Draw the DAG for the following three address code. (Understand)
d=b*c; e=a+b; b=b*c; a=e-d
19. How would you convert the following into intermediate code?Give a
suitable example.
1) Assignment Statements. 2)Case Statements (Remember)
(May/June 2014)
20. Explain the sequence of stack allocation process for a function call.
(Remember) (May/June 2014)
21. For the given program segment a[i,j]=B[i,k] do the following (Create
and Analyse) (May/June 2015)
(i) Draw the annotated parse tree with the translation scheme to
convert to the three address code.
(ii) Write the 3-address code.
(iii) Determine the address of a[3,5] where all are integer arrays with
size 10 * 10 each.
22. What are the various inherited and synthesized attributes, explain.
(Remember)
23. Explain the evaluation order for SDD‘s with dependency graphs and
attributes. (Understand)
24. Explain the applications of syntax directed translations. (Understand)
25. Give syntax directed definition to differentiate expressions formed by
applying the arithmetic operators + and * to the variable x and
constants; expression : x*(3*x+x*x). (Remember) (May/June 2015)
26. What are the various syntax directed translation schemes.
(Remember)
PART C
1. Given the Syntax-Directed Definition below construct the annotated
parse tree for the input expression: ―int a, b, c‖. (Create)
2. Construct a syntax directed definition for constructing a syntax tree for
assignment statements. (Create)
S->id :=E
E->El +E2
E->El *E2
E -> -EI
E ->(El)
E -> id
3. Construct a Syntax-Directed Translation scheme that translates
arithmetic expressions from infix into postfix notation.Using the semantic
26
attributes for each of the grammar symbols and semantic rules, Evaluate
the input ―3*4+5*2‖. (Create) (Apr/May‟18)
ASSIGNMENT
27
UNIT-IV
PART - A
1. What do you mean by binding of names? (Apr/May „17) (Remember)
Even if each name is declared once in a program, the same name may
denote different data objects at run time. The informal term ―data object‖
corresponds to a storage location that can hold values.
28
}
int *dangle()
{
int i=23;
return &i;
}
29
main()
Show_data(username)
Calling sequences and the layout of activation records may differ greatly,
even among implementations of the same language. The code in a calling
sequence is often divided between the calling procedure (the "caller") and
the procedure it calls (the "callee").
There is no exact division of run-time tasks between caller and callee; the
source language, the target machine, and the operating system impose
requirements that may favor one solution over another.
13. How allocation of storage for variables and access to those variables
is performed that do not allow nested procedure declarations?
(Remember)
30
A direct implementation of the normal static scope rule for nested
functions is obtained by adding a pointer called the access link to
each activation record.
If procedure p is nested immediately within procedure q in the source
code, then the access link in any activation of p points to the most
recent activation of q.
The nesting depth of q must be exactly one less than the nesting depth
of p. Access links form a chain from the activation record at the top of
the stack to a sequence of activations at progressively lower nesting
depths. Along this chain are all the activations whose data and
procedures are accessible to the currently executing procedure.
Most programs exhibit a high degree of locality; that is, they spend
most of their time executing a relatively small fraction of the code and
touching only a small fraction of the data.
We say that a program has temporal locality if the memory locations it
accesses are likely to be accessed again within a short period of time. We
say that a program has spatial locality if memory locations close to the
31
location accessed are likely also to be accessed within a short period of
time.
23. What are the two data structures that are useful to support
coalescing of adjacent free blocks? (Remember)
Boundary Tags, A Doubly Linked, Embedded Free List
32
2. Address descriptor
27. What is the use of function getreg? (Remember)
The function getreg returns the location L to hold the value of x for
the
I. performing the assignment of the form x := y unless and until
it is a must.
28. What are the uses of register and address descriptors in code
generation? ( Nov/Dec‟12) (Remember)
The code-generation algorithm uses descriptors to keep track of
register contents and addresses for names.
1. A register descriptor keeps track of what is currently in each
register. It is consulted whenever a new register is needed. We assume that
initially the register descriptor shows that all registers are empty. As the
code generation for the block progresses, each register will hold the value of
zero or more names at any given time.
2. An address descriptor keeps track of the location where the current
value of the name can be found at run time. The location might
be a register, a stack location, a memory address. This information can be
stored in the symbol table and is used to determine the accessing method for
a name.
33
33. Write the steps to find the leader of a basic block. (Remember)
1. The first statement is a leader.
2. Any statement that is the target of a conditional or unconditional
goto is a leader.
3. Any statement that immediately follows a got o or conditional goto
statement is a leader
PART B
PART C
1. Explain the simple code generator algorithm with a suitable example.
(Analyse) (Apr/may 2019)
2. Illustrate the concept of reduction of fragmentation using B e s t - F i t
and N e x t - F i t Object Placement techniques.(Create)
3. Explain auxiliary array called display for accessing and
maintaining the nonlocal data with suitable diagram.
(Analyse)
ASSIGNMENT
34
UNIT-V
PART – A
1. What is an Optimizing compiler (Nov/Dec 2013) (June 16)
(Remember)
Optimization is the process of transforming a piece of code to make more
efficient(either in terms of time or space) without changing its output or
side-effects. The only difference visible to the code‘s user should be that it
runs faster and/or consumes less memory. It is really a misnomer that
the name implies you are finding an ―optimal‖ solution— in truth,
optimization aims to improve, not perfect; the result.There should not be
any unreachable code. Dead code should be completely removed from
source language
2. What are the properties of optimizing compiler? (May/June‟13,
Nov/Dec‟17) (Remember)
The source code should be such that it should produce minimum amount
of target code.
There should not be any unreachable code.
Dead code should be completely removed from source language.
The optimizing compilers should apply following code improving
transformations on source language.
i) common sub expression elimination
ii) dead code elimination
iii) code movement
iv) strength reduction
3. What are the four structure preserving transformations on basic
block? (Apr / May ‟11, Apr/May „18) (Remember)
1. Common sub expression elimination
2. Dead – code elimination
3. Renaming of temporary variables
4. Interchange of two independent adjacent statements
4. Define Common sub expression (Nov/Dec ‟18) (Understand)
An occurrence of an expression E is called a common sub expression if
E was previously computed; and the values of variables in E have not
changed since the previous computation. We can avoid recomputing the
expression if we can use the previously computed value.
5. What is the criteria for code improving transformation? (Nov/Dec
‟11) (Remember)
The transformations provided by an optimizing compiler should have several
properties.
1. A transformation must preserve the meaning of programs.
2. A transformation must, on the average, speed up programs by a
measurable amount.
3. A transformation must be worth the effort.
6. What do you mean by copy propagation? (Apr/May „17) (Remember)
One concerns assignments of the form f:=g call copy statements, or
copies for short. The idea behind the copy-propagation transformation is to
use g for f, whenever possible after the copy statement f:=g.
35
7. Define constant folding. (May/June„13) (Remember)
Deducing at compile time that the value of an expression is a constant
and using the constant instead is known as constant folding.
8. What is an induction variable? (Remember)
Consider block B3
j:=j-1
t4=4*j
The values of j and t4 remain in lock-step; every time the value of j
decreases by 1, that of t4 decreases by 4 because 4*j is assigned to t 4.Such
identifiers are called induction variables.
9. Define dead-code elimination.(April/May‟11) (Remember)
A variable is live at a point in a program if its value can be used
subsequently; otherwise, it is dead at that point. The dead code may appear
as the result of previous transformations.
Consider debug := false
…
if (debug)
print …
By data flow analysis, each time the program reaches the if statement,
the value of debug is false.
10. What is loop optimization? Name the techniques in loop
optimization (April/May‟11) (May/June14, Apr/May „18) (Remember)
The running time of a program may be improved if we decrease the number
of instructions in an inner loop, even if we increase the amount of code
outside that loop. Three techniques are important for loop optimization:
(1) Code motion,
(2) Induction variable elimination,
(3) Reduction in strength
11. Define loop unrolling with example (Nov/Dec 2013) (Remember)
Loop unrolling, also known as loop unwinding, is a loop transformation
technique that attempts to optimize a program's execution speed at the
expense of its binary size. The transformation can be undertaken manually
by the programmer or by an optimizing compiler.
Normal loop After loop unrolling
int x;
int x; for (x = 0; x < 100; x+=5) {
for (x = 0; x < 100; x++) delete(x);
{ delete(x+1);
delete(x); delete(x+2);
} delete(x+3);
delete(x+4); }
12. What is a basic block? (May/June‟13, Nov/Dec ‟11, Apr/May „17)
(Remember)
A basic block is a sequence of consecutive statements in which flow
of control enters at the beginning and leaves at the end without halt or
possibility of branching except at the end.
36
13. Write the steps to find the leader of a basic block. (Remember)
1. The first statement is a leader.
2. Any statement that is the target of a conditional or unconditional
goto is a leader.
3. Any statement that immediately follows a got o or conditional goto
statement is a leader
14. What do you mean by algebraic transformation? (Remember)
Countless algebraic transformations can be used to change the
set of expressions computed by a basic block into an algebraically
equivalent set. The useful ones are those that simplify expressions or
replace expensive operations by cheaper ones. Ex: x := x + 0 or x := x * 1
can be eliminated from a basic block without changing the set of
expressions t computes.
15. What is a flow graph? (May/June‟13 & May/June ‟12, Nov/Dec
‟11) (Remember)
We can add the flow of control information to the set of basic
blocks by constructing a directed graph called a flow graph. The nodes of
the flow graph are the basic blocks. One node is distinguished as initial;
it is t he block whose leader is the first statement.
16. What is a loop? (Remember)
A loop is a collection of nodes in a flow graph such that
1. All nodes in the collection are strongly connected
2. The collection of nodes has a unique entry
17. Give any four applications /advantages of DAG. (Nov/Dec ‟12
&May/June„13) (Remember)
II. Determining the common sub-expressions.
III. Determining which names are used inside the block and
computed outside the block.
IV. Determining which statements of the block could have their
computed value outside the block.
V. Simplifying the list of quadruples by eliminating the common
su-expressions and not performing the assignment of the
form x := y unless and until it is a must.
18. What is the purpose of next use information? (Apr / May ‟11)
(Remember)
We need to know, for each use of a variable in a basic block, whether
the value contained in the variable will be used again later in the block. If a
variable has no next-use we can reuse the register allocated to the variable.
We also need to know whether a variable used in a basic block
Is live-on-exit , i.e. if the value contained in the variable has a use
outside the block.
19. How to perform register assignment for outer loops (May/June‟12)
(Remember)
If an outer loop L1 contains an inner loop L2, the allocated registers in
L2 need not be allocated registers in L1 – L2· However, if name X is allocated
a register in loop L1 but not L2, we must store x on entrance to L2 and load
x if we leave L2 and enter a block L1 – L2. Similarly, if we choose to allocate
37
x a register in L2 but not L1 we must load x on entrance to L2 and store x
on exit from L2.
20. What is DAG? (Nov/Dec ‟11)(June 16) (Remember)
Directed acyclic graphs (dags) are useful data structures for
implementing transformations on basic blocks. A dag gives a picture of how
the value computed by each statement in a basic block is used in
subsequent statements of the block.
21. What is the use of next-use information (Nov/Dec 2013)
(Remember)
We need to know, for each use of a variable in a basic block, whether the
value contained in the variable will be used again later in the block. If a
variable has no next-use we can reuse the register allocated to the variable.
We also need to know whether a variable used in a basic block
Is live-on-exit, i.e. if the value contained in the variable has a use outside
the block.
22. Define symbol table. (May/June14) (Remember)
Symbol table provides the associations between all occurrences of
each name given in the program. It provides a link between each name and
it declaration.
23. Define a natural loop. (Remember)
One important application of dominator information is in determining the
loops of a flow graph suitable for improvement. There are two essentials
properties of such loops.
1. A loop must have a single entry point called header.
2. There must be at least one way to iterate the loop i.e., at least
one path back to the header.
24. What is a pre-header? (Remember)
Pre-Header is also a header that appears before the header. The
preheader has only the header as the successor, and all edges which
formerly entered the header of L from outside L instead enter the pre-header.
25. When a graph is reducible? (Remember)
Exclusive use of structured flow-of-control statements such as if-then-
else, while-do, continue, and break statements produces programs whose
flow graphs are always reducible.
26. Write a general data flow equation for global data flow analysis.
(Remember)
The general data flow equation for global data flow analysis is given as
out[S]=gen[S]U(in[S]-kill[S])
27. Define point and path. (Remember)
Point: Point between two statements as the point before the first statement
and after the last statement.
Path: A path from p1 to pn is a sequence of points p1,p2,…,pn such that for
each i between 1 and n-1,either
1. pi is the point immediately preceding a statement and pi+1 is the
point immediately following that statement in the same block, or
2. pi is the end of some block and pi+1is the beginning of a
successor block.
28. What is live variable?(Nov/Dec‟12) (Remember)
38
A variable is live at a point in a program if its value can be used
subsequently; otherwise, it is dead at that point.
29. What is dataflow analysis? (Nov/Dec‟12) (Remember)
Data-Flow Analysis is a standard way to formulate intra-procedural program
analysis. t is the process of collecting information about the way the
variables are used, defined in the program.
30. List out two properties of reducible flow graph? (May/June„12)
(Remember)
A flow graph G is reducible if and only if we can partition the edges into two
disjoint groups, often called the forward edges and back edges, with the
following two properties:
1. The forward edges form an acyclic graph in which every node can be
reached from the initial node of G.
2. The back edges consist only of edges whose heads dominate their tails.
31. What is a Dag?(June 16) (Remember)
A dag gives the same information but in more compact way because
common subexpression are identified
32. Draw a syntax tree and dag for a:=b*-c + b*-c (Nov/Dec ‟12,
Nov/Dec ‟11) (Create)
assign assign
a + a +
* *
*
uminus b uminus
uminus b
b b
b b
a -
* b
a b
39
PART B
40
15. Discuss in detail about global data flow analysis (Nov/Dec ‟11,
May/June„13) (Nov/Dec‟12) (Apr/May‟ 22) (Create)
16. Write an algorithm to construct the natural loop of a back edge.
(April/May ‟11, (Nov/Dec ‟16) (Remember)
17. Construct DAG and optimal target code for the following expression.
x=((a+b)/(b-c))-(a+b)*(b-c)+f (Create)
18. Determine the basic blocks of instructions ,Control Flow Graph(CFG)
and the CFG dominator tree for the following the code (Apr/May‟18)
(Analyze)
01 a=1
02 b=0
03 L0: a = a + 1
04 b =p + 1
05 if (a > b) goto L3
06 L1: a = 3
07 if (b > a) goto L2
08 b=b+1
09 goto L1
10 L2: a = b
11 b=p+q
12 if (a > b) goto L0
13 L3: t1= p * q
14 t2= t1 + b
15 return t2
19. Construct a code sequence and DAG for the following syntax directed
translation of the expression :(a + b) – (e –( c + d )). (Apr/May‟18)
(Create)
20. Write and explain the algorithm for construction of basic
blocks.(Apr/May‟19) (Understand)
21. Explain in detail about Peep-hole optimization(Apr/May‟22)
(Understand)
22. Construct the DAG for the following basic block
x = a[i]
a[j]=y
z=a[i] (Apr/May‟19) (Apply)
PART C
1. Draw the symbol tables for each of the procedures in the following PASCAL
code (including main) and show their nesting relationship by linking
them via a pointer reference in the structure (or record) used to
implement them in memory.Include the entries or fields for the local
variables, arguments and any other information you find relevant for the
purposes of code generation, such as its type and location at run-time .
(Apr/May ‟18) (Understand)
41
04: integer a, b;
05: call f2(b, a)
06: end;
07: procedure f2(y, z)
08: integer y, z ;
09: procedure f3(m, n);
10: integer m, n;
11: end;
12: procedure f4(m, n);
13: integer m, n;
14: end;
15: call f3(c,z);
16: call f4(c,z);
17: end;
18: …
19: call f1 (a, b);
20: end;
2. For the flow graph of shown below, compute the live variables, The gen
and kill sets for each block, (Understand)
42
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
c[i][j]=c[i][j] +a[i][k]*b[k][j];
i. Translate the program into three-address statements. Assume
the matrix entries are numbers that require 8 bytes, and that
matrices are stored in row-major order.
ii. Construct the flow graph for the code from 1
iii. Identify the loops in the flow graph from 2 (April / May 2019)
(Analysis)
ASSIGNMENT
1. Explain in detail about code-improving transformations(Knowledge)
2. construct a DAG and write the sequence of instruction for the expression
a=(a*b+c)-(a*b+c). (Design)
PROGRAMS
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct sym
{
int pos;
char n[20],typ[20];
int allst;
int addr;
}s[20];
int main()
{
int i=0,p=0,n,j,k;
char input[20],typ[20];
char dt[5][20]={"int","char","float","long","double"};
FILE *fp;
clrscr();
fp=fopen("H:\\input.txt","r");
fscanf(fp,"%s",input);
while(strcmp(input,"EOF")!=0)
{
for(j=0;j<5;j++)
43
{
if(strcmp(input,dt[j])==0)
{
strcpy(typ,input);
fscanf(fp,"%s",input);
while(strcmp(input,";")!=0)
{
if(strcmp(input,",")!=0)
{
p++;
s[p].pos=++i;
strcpy(s[p].n,input);
strcpy(s[p].typ,typ);
fscanf(fp,"%s",input);
}
if(strcmp(input,";")!=0)
fscanf(fp,"%s",input);
}
}
}
fscanf(fp,"%s",input);
}
n=i+1;
s[0].allst=0;
s[0].addr=1000;
for(k=1;k<n;k++)
{
s[k].addr=s[k-1].addr+s[k-1].allst;
if(strcmp(s[k].typ,"int")==0)
s[k].allst=2;
else
if((strcmp(s[k].typ,"float")==0)||(strcmp(s[k].typ,"double")==0))
s[k].allst=4;
else if(strcmp(s[k].typ,"char")==0)
s[k].allst=1;
else if(strcmp(s[k].typ,"long")==0)
s[k].allst=8;
}
printf("\n\t\t\tSymbol Table");
printf("\nPosition\tName\tType\tAllocatedStorage\tAddress");
for(k=1;k<n;k++)
printf("\n%d\t\t%s\t%s\t\t%d\t\t%d",s[k].pos,s[k].n,s[k].typ,s[k].allst,s[k].
addr);
getch();
return 0;
}
OUTPUT
44
input.txt:
void main ()
{
int a , d ;
float b ;
c = a + b;
}
EOF
%{
%} Declarations section
%%
[A-Z] {printf(“%s”,yytext);}
.; Transition rules section
%%
main()
{printf(“Enter Some string”);
yylex();
} Auxillary procedures
int yywrap()
{return 1;
}
Input
Enter Some string
Panimalar Engineering College
Output
PEC
2.b Program to count the number of vowels and consonants in a given string.
%{
45
#include<stdio.h>
int v=0;
int c=0;
%}
%%
\n return 0;
[aeiouAEIOU] { ++v; }
[^aeiouAEIOU] { ++c; }
%%
int yywrap()
{
return 1;
}
main ()
{
printf("Enter the string : ");
yylex();
printf("No. of Vowels = %d \n No. of Consonants=%d", v,c);
}
Input
Enter the string : Computer
Output
No. of Vowels =3
No. of consonants=5
2.c. Program to count the number of characters, words, spaces, end of lines in a given
input file.
%{
#include<stdio.h>
int lines=0, words=0,s_letters=0,c_letters=0, num=0, spl_char=0,total=0;
%}
%%
\n { lines++; words++;}
[\t ' '] words++;
[A-Z] c_letters++;
[a-z] s_letters++;
[0-9] num++;
. spl_char++;
%%
main(void)
{
yyin= fopen("myfile.txt","r");
yylex();
total=s_letters+c_letters+num+spl_char;
printf(" This File contains ...");
46
printf("\n\t%d lines", lines);
printf("\n\t%d words",words);
printf("\n\t%d small letters", s_letters);
printf("\n\t%d capital letters",c_letters);
printf("\n\t%d digits", num);
printf("\n\t%d special characters",spl_char);
printf("\n\tIn total %d characters.\n",total);
}
int yywrap()
{
return(1);
}
Sample output
'myfile.txt' contains this.
PROGRAM :
%{
#include<stdio.h>
#include<string.h>
struct st
{
char LEXeme[25];
char name[25];
}ST[100];
int cnt=0;
%}
ID [a-zA-Z][a-zA-Z0-9]*
DIGIT [0-9]
%%
47
{DIGIT}+ {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"const integer
literal");cnt++;}
{DIGIT}+"."{DIGIT}+
{strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"const float
literal");cnt++;}
"#include"|"#define" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"pp
directive");cnt++;}
{ID}".h"
{strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"includefile");cnt++;}
main |void |switch |case
|continue|break|do|while|for|if|else|int|float|char
{strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"keyword");cnt++;}
"\""{ID}"\"" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"string
literal");cnt++;}
{ID} {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"identifier");cnt++;}
"+"|"-"|"#"|"/"|"%"
{strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"Arithmetic OP");cnt++;}
"&"|"|"|"^"+"~" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"Bitwise
OP");cnt++;}
"<<"|">>" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"Bitwise SHift
OP");cnt++;}
"&&"|"||"|"!" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"Logical
OP");cnt++;}
"<"|">"|"<="|">="
{strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"Realational OP");cnt++;}
"=="|"!=" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"Equality
OP");cnt++;}
"[" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"OSB");cnt++;}
"]" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"CSB");cnt++;}
"{" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"OCB");cnt++;}
"}" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"CCB");cnt++;}
"(" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"ORB");cnt++;}
")" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"CRB");cnt++;}
":" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"Semicolon");cnt++;}
"++" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"IncOP");cnt++;}
"--" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"DecOp");cnt++;}
"?" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"Ternary OP");cnt++;}
"=" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"Assignment
OP");cnt++;}
%%
main(int argc,char *argv[])
{
int i=0;
48
yyin=fopen(argv[1],"r");
yylex();
printf("\nTOKEN TABLE");
printf("\nLEXEME\t\t\tNAME\n");
printf(" \t\t \n");
for(i=0;i<cnt;i++)
{
printf("\n%s",ST[i].LEXeme);
printf("\t\t\t%s",ST[i].name);
}
printf("\n");
}
int yywrap()
{
return 1;
}
INPUT C PROGRAM:
#include<stdio.h>
main()
{
int a,b,c=0;
c=a+b;
return c;
}
OUTPUT:
TOKEN TABLE
LEXEME NAME
#include pp directive
< Realational OP
stdio.h includefile
> Realational OP
main string literal
( ORB
) CRB
{ OCB
int identifier
a identifier
b identifier
c identifier
= Assignment OP
0 const integer literal
c identifier
= Assignment OP
49
a identifier
+ Arithmetic OP
b identifier
return identifier
c identifier
} CCB
3. Implement an Arithmetic Calculator using LEX and YACC
PROGRAM
Ex4c.l
%{
%}
DIGIT [0-9]+\.?|[0-9]*\.[0-9]+
%option noyywrap
%%
[]
{DIGIT} { yylval=atof(yytext); return NUM;}
\n|. {return yytext[0];}
Ex4c.y
%{
#include<ctype.h>
#include<stdio.h>
#define YYSTYPE double
%}
%token NUM
%%
50
;
%%
#include "lex.yy.c"
int main()
{
printf("Enter the expression: ");
yyparse();
}
Output
[csestaff@localhost csestaff]$ lex ex4c.l
[csestaff@localhost csestaff]$ yacc -d ex4.y
[csestaff@localhost csestaff]$ cc y.tab.c
[csestaff@localhost csestaff]$ ./a.out
Enter the expression: 2+3
Answer: 5
Enter:
ewe
syntax error
Error: Enter once more.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct op
{
char l;
char r[20];
}
op[10],pr[10];
void main()
{
int a,i,k,j,n,z=0,m,q;
char *p,*l;
char temp,t;
51
char *tem;
clrscr();
printf("Enter the Number of Values:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("left: ");
op[i].l=getche();
printf("\tright: ");
scanf("%s",op[i].r);
}
printf("Intermediate Code\n") ;
for(i=0;i<n;i++)
{
printf("%c=",op[i].l);
printf("%s\n",op[i].r);
}
for(i=0;i<n-1;i++)
{
temp=op[i].l;
for(j=0;j<n;j++) {
p=strchr(op[j].r,temp);
if(p) {
pr[z].l=op[i].l;
strcpy(pr[z].r,op[i].r);
z++; }}}
pr[z].l=op[n-1].l;
strcpy(pr[z].r,op[n-1].r);
z++;
printf("\nAfter Dead Code Elimination\n");
for(k=0;k<z;k++) {
printf("%c\t=",pr[k].l);
printf("%s\n",pr[k].r);
}
for(m=0;m<z;m++) {
tem=pr[m].r;
for(j=m+1;j<z;j++) {
p=strstr(tem,pr[j].r);
if(p) {
t=pr[j].l;
pr[j].l=pr[m].l;
for(i=0;i<z;i++) {
l=strchr(pr[i].r,t) ;
52
if(l) {
a=l-pr[i].r;
printf("pos: %d",a);
pr[i].r[a]=pr[m].l; }}}}}
printf("Eliminate Common Expression\n");
for(i=0;i<z;i++)
{
printf("%c\t=",pr[i].l);
printf("%s\n",pr[i].r);
}
for(i=0;i<z;i++)
{
for(j=i+1;j<z;j++)
{
q=strcmp(pr[i].r,pr[j].r);
if((pr[i].l==pr[j].l)&&!q)
{
pr[i].l='\0';
strcpy(pr[i].r,'\0');
}
}
}
printf("Optimized Code\n");
for(i=0;i<z;i++)
{
if(pr[i].l!='\0')
{
printf("%c=",pr[i].l);
printf("%s\n",pr[i].r);
}
}
getch();
}
OUTPUT
53
6. Implement back-end of the compiler for which the three address code is
given as input and the 8086 assembly language code is produced as output.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<graphics.h>
typedef struct
{
char var[10];
int alive;
}
regist;
regist preg[10];
void substring(char exp[],int st,int end)
{
int i,j=0;
char dup[10]="";
for(i=st;i<end;i++)
dup[j++]=exp[i];
dup[j]='0';
strcpy(exp,dup); }
54
int getregister(char var[]) {
int i;
for(i=0;i<10;i++) {
if(preg[i].alive==0) {
strcpy(preg[i].var,var);
break; }}
return(i); }
void getvar(char exp[],char v[]) {
int i,j=0;
char var[10]="";
for(i=0;exp[i]!='\0';i++)
if(isalpha(exp[i]))
var[j++]=exp[i];
else
break;
strcpy(v,var); }
void main() {
char basic[10][10],var[10][10],fstr[10],op;
int i,j,k,reg,vc,flag=0;
clrscr();
printf("\nEnter the Three Address Code:\n Type exit at the end\n");
for(i=0;;i++) {
gets(basic[i]);
if(strcmp(basic[i],"exit")==0)
break; }
printf("\nThe Equivalent Assembly Code is:\n");
for(j=0;j<i;j++) {
getvar(basic[j],var[vc++]);
strcpy(fstr,var[vc
-1]);
substring(basic[j],strlen(var[vc
-1])+1,strlen(basic[j]));
getvar(basic[j],var[vc++]);
reg=getregister(var[vc-1]);
if(preg[reg].alive==0)
{
printf("\nMov R%d,%s",reg,var[vc-1]);
preg[reg].alive=1;
}
op=basic[j][strlen(var[vc-1])];
substring(basic[j],strlen(var[vc-1])+1,strlen(basic[j]));
getvar(basic[j],var[vc++]);
switch(op)
{
case '+': printf("\nAdd"); break;
case '-': printf("\nSub"); break;
case '*': printf("\nMul"); break;
case '/': printf("\nDiv"); break;
}
55
flag=1;
for(k=0;k<=reg;k++)
{
if(strcmp(preg[k].var,var[vc-1])==0)
{
printf("R%d, R%d",k,reg);
preg[k].alive=0;
flag=0;
break;
}
}
if(flag)
{
printf(" %s,R%d",var[vc-1],reg);
printf("\nMov %s,R%d",fstr,reg);
}
strcpy(preg[reg].var,var[vc-3]);
getch();
}
}
Output:
Enter the Three Address Code:
a=b+c
c=a*c
exit
The Equivalent Assembly Code is:
Mov R0,b
Add c,R0
Mov a,R0
Mov R1,a
Mul c,R1
Mov c,R1
56