Separation of Lanthanides/ Lanthanides and Actinides
Compiler worksheet
1. Prepared by R. Arthy Page 1
Department of Information Technology
WORKSHEET
CS6660 – COMPILER DESIGN
Academic Year : 2018 – 2019 [EVEN]
Year / Semester : III IT / VI
Subject In – Charge Department In – Charge
[Mrs. R. Arthy, Asst. Prof./IT] [Dr. P. Subathra, HOD/IT]
2. Prepared by R. Arthy Page 2
CS6660 – Compiler Design
UNIT I
Q1: Construct the token table for the following program segment:
int i,j;
i = (j + 50) * (j – 30) / j;
Lexemes Tokens
Q2: Construct the symbol table for the following program segment:
void add()
{
float a,b;
int c;
a = b + c;
}
Symbol Name Type Size Scope Address
Q3: Construct the symbol table for the following program segment
extern double test(double x);
double sample(int count)
3. Prepared by R. Arthy Page 3
{
double sum = 0.0;
for(int i=1; i<=count; i++)
sum+=test((double)i);
return sum;
}
Also, construct the token table for the same.
Symbol Table:
Symbol Name Type Size Scope Address
Token Table
Lexemes Tokens
4. Prepared by R. Arthy Page 4
Q4: What is the output of the syntax analyzer phase for the given program segment?
a = (b + c – d) * 40 / e
Q5: Give the Three Address Code representation for Q4
Q6: Generate the assembly code by tracing the phases of compiler for the given programming
segment a = b +(d – c) / e + f * 60. Consider all the variables are floating point values.
5. Prepared by R. Arthy Page 5
Q7: Draw the structure of phase of compiler.
CS6660 – COMPILER DESIGN
6. Prepared by R. Arthy Page 6
UNIT II – LEXICAL ANALYZER
WORKSHEET
Topic : Lexical Analyzer
Q1: Count number of tokens
int main()
{
int a = 10, b = 20;
printf("sum is :%d",a+b);
return 0;
}
Topic : Regular Expression
Identify the Regular Expression for the given string
Q1: Matching Numbers
Task Text
Match 3.14529
Match -255.34
Match 128
Match 1.9e10
Match 123,340.00
Skip 720p
Q2: Matching Phone Numbers
Task Text Capture Groups
Capture 415-555-1234 415
Capture 650-555-2345 650
Capture (416)555-3456 416
Capture 202 555 4567 202
Capture 4035555678 403
Capture 1 416 555 9292 416
7. Prepared by R. Arthy Page 7
Q3: Matching Emails
Task Text Capture Groups
Capture tom@hogwarts.com tom
Capture tom.riddle@hogwarts.com tom.riddle
Capture tom.riddle+regexone@hogwarts.com tom.riddle
Capture tom@hogwarts.eu.com tom
Capture potter@hogwarts.com potter
Capture harry@hogwarts.com harry
Capture hermione+regexone@hogwarts.com hermione
Q4: Capturing HTML Tags
Task Text Capture Groups
Capture <a>This is a link</a> a
Capture <a href='https://regexone.com'>Link</a> a
Capture <div class='test_style'>Test</div> div
Capture <div>Hello <span>world</span></div> div
Q5: Capturing Filename Data
Task Text Capture Groups
Skip .bash_profile
Skip workspace.doc
Capture img0912.jpg img0912 jpg
Capture updated_img0912.png updated_img0912png
Skip documentation.html
Capture favicon.gif favicon gif
Skip img0912.jpg.tmp
Skip access.lock
8. Prepared by R. Arthy Page 8
Q6: Matching Lines
Task Text Capture Groups
Capture The quick brown fox... The quick brown fox...
Capture jumps over the lazy dog. jumps over the lazy dog.
Write the regular expression for the given language that contains set of binary
alphabets
∑ = {0, 1}
Q1: 0 or 11 or 101
Q2: only 0s
Q3: all binary strings
Q4: all binary strings except empty string
Q5: begins with 1, ends with 1
Q6: ends with 00
Q7: contains at least three 1s
9. Prepared by R. Arthy Page 9
Q8: contains at least three consecutive 1s
Q9: contains the substring 110
Q10: doesn't contain the substring 110
Q11:contains at least two 0s but not consecutive 0s
Q12: has at least 3 characters, and the third character is 0
Q13: number of 0s is a multiple of 3
Q14: starts and ends with the same character
Q15: odd length
Q16: starts with 0 and has odd length, or starts with 1 and has even length
Q17: length is at least 1 and at most 3
Q18: Choose the regular languages that are equivalent to (0 | 1)*1(0 | 1)*
– (01 | 11)*(0 | 1)*
– (0 | 1)*(10 | 11 | 1)(0 | 1)*
– (1 | 0)*1(1 | 0)*
10. Prepared by R. Arthy Page 10
– (0 | 1)*(0 | 1)(0 | 1)*
Q19: Twelve-hour times of the form “04:13PM”. Minutes should always be a two digit
number, but hours may be a single digit.
Q20: The RE in which any number of 0′s is followed by any number of 1′s followed by any
number of 2′s is
a) (0+1+2)*
b) 0*1*2*
c) 0* + 1 + 2
d) (0+1)*2*
Q21: Which of the following is NOT the set of regular expression R = (ab + abb)* bbab
a) ababbbbab
b) abbbab
c) ababbabbbab
d) abababab
11. Prepared by R. Arthy Page 11
CS6660 – COMPILER DESIGN
UNIT II – LEXICAL ANALYZER
WORKSHEET
Topic : Finite Automata
Q1: Mathematical model of Finite Automata
Q –
∑ -
q0 –
F –
δ –
Q2: δ for DFA
Q3: δ for NFA
Q4: The following finite automata will accept only certain sequences of the symbols a and b.
Which of the following sequences will it accept?
(i) a (ii) abba (iii) bbaa (iv) bababaa (v )baaa
12. Prepared by R. Arthy Page 12
Q5: Which of these inputs are valid for the FSM below?
(i) 999 (ii) 9-3+77 (iii) 000999+ (iv) +67-4 (v) 0 + 9 + 6 + 54321 -1
Q6: Draw a finite state machine which will accept the strings
Ban Bran Brrr
Q7: Construct the transition table for the given Finite automata
13. Prepared by R. Arthy Page 13
Q8: Construct the Finite automata for the given transition table
a b
↑A - B
B A C
C D E
D A D
E* - -
Q9: Construct the NFA for the below regular expression using Thompsons algorithm
i. (a|b)*
14. Prepared by R. Arthy Page 14
ii. (a*|b)*
iii. (a*|b*)*
iv. (a*b*)*
15. Prepared by R. Arthy Page 15
Q10: Find the є – closure for the following NFA
є – closure(0) =
є – closure(1) =
є – closure(2) =
є – closure(3) =
є – closure(4) =
16. Prepared by R. Arthy Page 16
є – closure(5) =
є – closure(6) =
є – closure(7) =
є – closure(8) =
Q8: Write an augmented regular expression for the regular expression a*(a|b*a)|ba
17. Prepared by R. Arthy Page 17
CS6660 – COMPILER DESIGN
UNIT III – SYNTAX ANALYZER
Topic: Context Free Grammar
Context Free Grammar Definition
G _______________________________
V _______________________________
T _______________________________
P _______________________________
S _______________________________
1. Programs can contain errors at four different levels (and the compiler can catch errors at
only three of these). What are these different four kinds of errors?
____________________________________________________
____________________________________________
2. Consider the following grammar.
S DC | fC
C Cb | a | b
D gD | ge
Which symbols are terminals? ____________________________
Which symbols are nonterminals? _________________________
3. Consider the following grammar
S->SaS
S->b
Is the grammar recursive grammar? _________________
Give the set of strings generated by this grammar ___________________________________
4. Consider the following grammar
S-> Aa
A->Ab|c
18. Prepared by R. Arthy Page 18
Is the grammar recursive grammar? _________________
Give the set of strings generated by this grammar ___________________________________
5. Consider the following grammar
S->Aa
A->b|c
Is the grammar recursive grammar? _________________
Give the set of strings generated by this grammar ___________________________________
6. Consider the following grammar.
S aSbS | bSaS |
Show that this grammar is ambiguous by constructing two different parse trees for the
sentence “abab”
Parse Tree 1 Parse Tree 2
7. Show that grammar in Q6 is ambiguous by constructing two different leftmost derivations
for “abab”
19. Prepared by R. Arthy Page 19
Leftmost Derivation 1 Leftmost Derivation 2
8. Show that grammar in Q6 is ambiguous by constructing two different rightmost
derivations for “abab”
Rightmost Derivation 1 Rightmost Derivation 2
9. Consider the following grammar.
S (L) | a
L S,L | S
Draw parse trees for the following three sentences:
20. Prepared by R. Arthy Page 20
Parse Tree for (a,a) Parse Tree (a,(a,a),(a))
10. Show a leftmost derivation for ((a,a,a),(a),a)
11. Give the derivation for removing left recursion for a recursive grammar
21. Prepared by R. Arthy Page 21
12. Eliminate left-recursion in this grammar: S Sb | c
13. Eliminate left-recursion in this grammar: S Sa | Sb | Sc | d | e | f
14. Eliminate left-recursion in this grammar:
S A | bB
A Ac | Ad | B | bA
B fA | fb | g
22. Prepared by R. Arthy Page 22
15. Give the derivation for removing left factoring for a grammar
16. Eliminate the left factoring from the given grammar
S iEtS | iEtSeS | a
E b
17. In the non-recursive predictive parsing algorithm, what two data structures do we use
besides the input stream of tokens?
18. In the non-recursive predictive parsing algorithm, what can go onto the stack?
19. What indexes the columns of the table?
20. What indexes the rows of the table?
21. What are the kinds of entries that can go into the table?
23. Prepared by R. Arthy Page 23
22. In the algorithm to construct the table for the non-recursive predictive parser, if the
grammar in not LL(1), what will this algorithm try to do?
23. Can a non-recursive predictive parser handle all LL(1) grammars?
24. How would you describe the set of languages that can be recognized by a predictive
parser (either recursive or not)?
25. Find FIRST(S) and FOLLOW(S) for this grammar:
S dSaS
S cSbS
S
FIRST(S) _____________________________________________
FOLLOW(S) _____________________________________________
24. Prepared by R. Arthy Page 24
CS6660 – COMPILER DESIGN
UNIT III – SYNTAX ANALYZER
Topic: Context Free Grammar
26. Find the FIRST and FOLLOW sets for this grammar:
S aSbB | gA
A CB | cAh
B d | fC | C
C
FIRST (S) = ______________________________________________
FIRST (A) = _______________________________________________
FIRST (B) = _______________________________________________
FIRST (C) = _______________________________________________
FOLLOW (S) = ______________________________________________
FOLLOW (A) = ______________________________________________
FOLLOW (B) = ______________________________________________
FOLLOW (C) = ______________________________________________
27. Find the First and Follow for the following Grammars
i) S ABCDE
A a |
B b |
C c
D d |
E e |
25. Prepared by R. Arthy Page 25
Non
Terminals
FIRST FOLLOW
ii) S Bb | Cd
B aB |
C cC |
Non
Terminals
FIRST FOLLOW
iii) E TE‟
E‟ +TE‟ |
T FT‟
T‟ *FT‟ |
F id | (E)
Non
Terminals
FIRST FOLLOW
26. Prepared by R. Arthy Page 26
iv) S ACB | CbB | Ba
A da | BC
B g |
C h |
Non
Terminals
FIRST FOLLOW
v) S AA
A aA
A b
Non
Terminals
FIRST FOLLOW
vi) S aABb
A c |
B d |
Non
Terminals
FIRST FOLLOW
vii) S aBDh
B cC
C bC |
D EF
27. Prepared by R. Arthy Page 27
E g |
F f |
Non
Terminals
FIRST FOLLOW
viii) E E + T | T
T TF | F
F F* | a | b
Non
Terminals
FIRST FOLLOW
27. Construct predictive parsing table for the grammar,
S S(S)S |
Solution:
Step 1:
After Eliminating Left Recursion and Left Factoring
28. Prepared by R. Arthy Page 28
Step 2:
Non
Terminals
FIRST FOLLOW
Step 3:
Non
Terminals
Input Symbol
28. Construct predictive parsing table for the grammar,
S iEtS | iEtSeS
E b
Solution:
Step 1:
After Eliminating Left Recursion and Left Factoring
Step 2:
Non
Terminals
FIRST FOLLOW
29. Prepared by R. Arthy Page 29
Step 3:
Non
Terminals
Input Symbol
28. Construct predictive parsing table for the grammar,
S +SS | *SS | a and parse the input +*aaa
Solution:
Step 1:
After Eliminating Left Recursion and Left Factoring
Step 2:
Non
Terminals
FIRST FOLLOW
Step 3:
Non
Terminals
Input Symbol
Parse the input +*aaa
Stack Input Action
30. Prepared by R. Arthy Page 30
29. Construct the predictive parsing table for the grammar and parse the input ((a,a),a,(a,a))
S (L) | a
L L,S | S
a. Algorithm to eliminate left recursion:
1. Arrange the non-terminals in some order A1, A2 . . . An.
2. for i := 1 to n do begin for j := 1 to i-1 do begin
replace each production of the form Ai → Ajγ by the productions
Ai → δ1γ | δ2γ | . . . | δk γ, where Aj → δ1 | δ2 | . . . | δk are all the current
Aj-productions;
end
eliminate the immediate left recursion among the Ai-productions
end
b. Algorithm for FIRST and FOLLOW
FIRST(α):
If α is any string of grammar symbols, let FIRST (α) be the set of terminals that begin the
31. Prepared by R. Arthy Page 31
string derived from α . If α ε then ε is also in FIRST(α).
FOLLOW(A):
FOLLOW(A) for non terminal A to be the set of terminals a that can appear immediately to
the right of A in some sentential form, that is, the set of terminals a such that there exists a
derivation of the form S αAa β for some α and β
Rules to compute FIRST(X):
1. If X is a terminal then FIRST(X) is {X}
2. If X ε is a production then add ε to FIRST(X).
3. If X is a non terminal and X Y1Y2…..YK is a production then place „a‟ in FIRST(X) if
for some i, „a‟ is in FIRST(Yi)
Rules to compute FOLLOW(A):
1. Place $ in FOLLOW(S) where S is the start symbol and $ is the right end marker.
2. If there is a production A αBβ then everything in FIRST(β) except for ε is places 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).
c. Predictive parsing program:
The parser is controlled by a program that considers X, the symbol on top of stack, and a, the
current input symbol. These two symbols determine the parser action. There are three
possibilities:
1. If X = a = $, the parser halts and announces successful completion of parsing.
2. If X = a ≠ $, the parser pops X off the stack and advances the input pointer to the next
input symbol.
3. If X is a non-terminal , the program consults entry M[X, a] of the parsing table M. This
entry will either be an X-production of the grammar or an error entry.
If M[X , a] = {X → UVW}, the parser replaces X on top of the stack by WVU.
4. If M[X , a] = error, the parser calls an error recovery routine.
d. Algorithm for non-recursive predictive parsing:
Input: A string w and a parsing table M for grammar G.
Output: If w is in L(G), a leftmost derivation of w; otherwise, an error indication. Method :
Initially, the parser has $S on the stack with S, the start symbol of G on top, and w$ in the
32. Prepared by R. Arthy Page 32
input buffer. The program that utilizes the predictive parsing table M to produce a parse for
the input is as follows:
set ip to point to the first symbol of w$;
repeat
let X be the top stack symbol and a the symbol pointed to by ip;
if X is a terminal or $ then
if X = a then
pop X from the stack and advance ip
else error()
else /* X is a non-terminal */
if M[X, a] = X →Y1Y2 … Yk then begin
pop X from the stack;
push Yk, Yk-1, … ,Y1 onto the stack, with Y1 on top;
output the production X → Y1 Y2 . . . Yk
end
until X =else error()
33. Prepared by R. Arthy Page 33
CS6660 – COMPILER DESIGN
UNIT IV – SYNTAX DIRECTED TRANSLATION & RUN TIME ENVIRONMENT
Topic: Syntax Directed Definition
Q1: Translate the arithmetic expression a = b * -c + b * -c into syntax tree
Q2: Translate the executable statements of the following C program into three – address
code.
main()
{
int i, a[10];
i = 1;
while(i <= 10)
{
a[i] = 0;
i = i + 1;
}
}
34. Prepared by R. Arthy Page 34
Q3: Generate intermediate code for the following code segment
while(i < 10)
{
if(i%2 == 0)
es = es + i;
else
os = os + i;
}
Q4: For the input expression (4 * 7 + 1) * 2 construct an annotated parse tree according to
syntax directed definition of desk calculator.
Production Semantic Rules
E E + T
E E - T
E T
T T * F
T T / F
T F
F (E)
F num
35. Prepared by R. Arthy Page 35
Q5: For the input expression g = a + b – c * d construct an annotated parse tree according to
syntax directed definition of Three Address Code Generation.
Production Semantic Rules
S id = E
S E1 + E2
E E1 * E2
36. Prepared by R. Arthy Page 36
E -E1
E (E1)
E id
E num
Q6: Syntax Directed Definition of Construction of Syntax Tree
Production Semantic Rules
E E + T
E E - T
37. Prepared by R. Arthy Page 37
E T
T T * F
T T / F
T F
F (E)
F num
Q7: Construct the syntax tree for the expression a + a * (b – c) + (b – c) * d
P1 =
P2 =
P3 =
P4 =
P5 =
P6 =
P7 =
P8 =
P9 =
P10 =
P11 =
P12 =
P13 =
38. Prepared by R. Arthy Page 38
Q8: Syntax Directed Translation for Postfix notion
Production Semantic Rules
E E + T
E E - T
E T
T T * F
T T / F
T F
F (E)
F num
Q9: Syntax Directed Definition for Declaration
Production Semantic Rules
P D
D D ; D
D id : T
T integer
T real
T array[num] of T1
T ↑T1
Q10: Bottom – up evaluation of S – Attribute Definition
Production Semantic Rules
L E$
E E + T
39. Prepared by R. Arthy Page 39
E T
T T * F
T F
F (E)
F digit
Q11: Evaluate 3 * 5 + 4$ using above SDD
Input Stack Attribute Production Used
40. Prepared by R. Arthy Page 40
Topic: Type checking
Q12: Syntax Directed Definition for Type Checking for Declaration
Production Semantic Rules
D id : T
T integer
T char
T real
T array[num] of T1
T ↑T1
Q13: Syntax Directed Definition for Type Checking for Expression
Production Semantic Rules
E id
E literal
E num
E E1 + E2
E E1[E2]
E E1↑
Q14: Syntax Directed Definition for Type Checking for Statement
Production Semantic Rules
S id = E
S if E then S1
41. Prepared by R. Arthy Page 41
S while E do S1
Q15: Syntax Directed Definition for Type Checking for Function
Production Semantic Rules
E E1 (E2)
Q16: Syntax Directed Definition for Type Equivalence
Production Semantic Rules
E E1 + E2
42. Prepared by R. Arthy Page 42
CS6660 – COMPILER DESIGN
UNIT IV
Topic: Runtime Environment
Q1: Source Language Issues
Procedure:
A procedure definition is a ______________________ that associates an
_________________ with a _______________________. The identifier is the
__________________________ and the statement is the _________________________.
Example:
Program sort(input, output):
var a : array [0..10] of integer;
procedure readarray:
var i : integer;
begin
for i = 1 to 9 do read(a[i])
end;
function partition(y, z : integer) : integer;
var i, j, x, v : integer;
begin
…..
end;
procedure quicksort(m, n : integer):
var i : integer;
begin
if (n > m) then begin
i = partition(m,n);
quicksort(m,i-1);
quicksort(i+1, n)
end
end;
begin
a[0] = -9999; a[10] = 9999;
readarray;
quicksort(1,9)
end.
Activation Tree:
An activation tree is used to ________________________________________ and
_________________________.
In an activation tree,
1. Each node represents an _______________________________.
2. The root represents the ________________________________.
3. The node for „a‟ is the ______________ of the node for b if and only if control flows from
activation ____ to ______.
43. Prepared by R. Arthy Page 43
4. The node for „a‟ is to the ________________ of the node for „b‟ if and only if the
_____________________ occurs ________________ the _______________________.
Example
Control Stack:
A control stack is used to keep _________________________________ activations. The idea
is to ____________ the node for activation onto the control stack as the activation
__________ and to _________ the node when the activation _________.
The contents of the control stack are related to paths to the ______________ of the activation
tree. When node n is at the __________ of control stack, the stack contains the nodes along
the path from n to the _______________.
Example
Binding of names:
Environment refers to a function that maps a ______________ to a ____________________.
State refers to a function that maps a ____________________ to the ________________ held
there.
s
q(1,9)r
p(1,9) q(1,3)
44. Prepared by R. Arthy Page 44
Q2: Storage Organization
Typical subdivision of runtime memory:
Activation record:
Procedure calls and returns are usually managed by a run time stack called
___________________.
1. ________________________________
2. ________________________________
3. ________________________________
4. ________________________________
5. ________________________________
6. ________________________________
7. ________________________________
Q3: Storage Allocation Strategy
Three types:
1. __________________________________
2. __________________________________
3. __________________________________
Stack Allocation:
45. Prepared by R. Arthy Page 45
Calling Sequence:
Heap Allocation:
46. Prepared by R. Arthy Page 46
Q4: Parameter Passing
1. ______________________________________
2. ______________________________________
3. ______________________________________
Q5: Symbol Table
Definition:
Symbol table is a data structure used to _____________________________ about the
occurrence of various entities such as objects, classes, variable name, interface, function
name etc. it is used by both the _______________ and ___________________ phases.
Purpose:
The symbol table used for following purposes:
It is used to _________ the name of all entities in a structured form at one place.
It is used to ____________ if a variable has been declared.
It is used to _______________ the scope of a name.
It is used to _____________________________________ by verifying assignments
and expressions in the source code are semantically correct.
Implementation:
1. _________________________
2. _________________________
3. _________________________
Operations:
1. ___________________________
2. ___________________________
47. Prepared by R. Arthy Page 47
3. ___________________________
4. ___________________________
5. ___________________________
Insert:
int a, b;
float c;
48. Prepared by R. Arthy Page 48
CS6660 – COMPILER DESIGN
UNIT V
Topic: Code Optimization
Q1: Principal source of code optimization
Structure Preserving Code Optimization:
i. Common sub-expression elimination
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.
Before Optimization After Optimization
t1: =4*i
t2: =a[t1]
t3:=4*j
t4:=4*i
t5: =n
t6: =b[t4] + t5
Before Optimization After Optimization
c = a + b
d = c
c = c – e
a = d – e
b = b * e
b = d/b
ii. Copy propagation or Constant propagation
Assignments of the form f : = g called 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.
Before Optimization After Optimization
c = a + b
d = c
c = c – e
a = d – e
b = b * e
49. Prepared by R. Arthy Page 49
b = d/b
iii. Dead code elimination
A variable is live at a point in a program if its value can be used subsequently; otherwise, it is
dead at that point.
iv. Constant Folding
Deducing at compile time that the value of an expression is a constant and using the constant
instead is known as constant folding.
Try applying all the optimization technique
Before Optimization After Optimization
a = 10
b = 20
c = a + b
d = c
c = c – e
a = d – e
b = b * e
b = d/b
Loop Optimization
i. Code motion
Transformation takes an expression that yields the same result independent of the number of
times a loop is executed ( a loop-invariant computation) and places the expression before the
loop.
Before Optimization After Optimization
for(i = 0; i < limit – 1; i++)
{
}
ii. Elimination of induction variable
iii. Reduction in strength
Q2: Directed Acyclic Graph
Definition
A DAG for a basic block is a directed acyclic graph with the following labels on nodes:
_________________ are labelled by unique identifiers, either variable names or
constants.
50. Prepared by R. Arthy Page 50
______________________________ are labelled by an operator symbol.
Nodes are also optionally given a sequence of identifiers for labels to store the
computed values.
Purpose:
Applications of DAG
1. Automatically detect ___________________________________
2. Determine which ____________________ have their _______________ in the block.
3. Determine which statements ______________________ that could be used
______________ the block.
Algorithm for DAG
Input: A basic block
Output: A DAG for the basic block containing the following information:
1. A label for each node. For leaves, the label is an identifier. For interior nodes, an operator
symbol.
2. For each node a list of attached identifiers to hold the computed values.
Case (i)x := y OP z
Case (ii)x := OP y
Case (iii)x := y
Method:
Step 1: If y is undefined then create node(y). If z is undefined, create node(z) for case(i).
Step 2: For the case(i), create a node(OP) whose left child is node(y) and right child is
node(z) . (Checkingfor common sub expression). Let n be this node. For case(ii), determine
whether there is node(OP) with one child node(y). If not create such a node. For case(iii),
node n will be node(y).
Step 3: Delete x from the list of identifiers for node(x). Append x to the list of attached
identifiers for the node n found in step 2 and set node(x) to n.
Problem 1
(a + b) – (e – (c + d))
Solution
Three Address Code
51. Prepared by R. Arthy Page 51
Problem 2
1. t1 := 4* i
2. t2 := a[t1]
3. t3 := 4* i
4. t4 := b[t3]
5. t5 := t2*t4
6. t6 := prod+t5
7. prod := t6
8. t7 := i+1
9. i := t7
10. if i<=20 goto (1)
Solution
52. Prepared by R. Arthy Page 52
Problem 3
d = b * c
e = a + b
b = b * c
a = e – d
Solution
53. Prepared by R. Arthy Page 53
Q3: Issue in Code Generation
1. _________________________________________________
2. _________________________________________________
Types of code:
a. __________________________________________
b. __________________________________________
c. __________________________________________
3. _________________________________________________
4. _________________________________________________
Cost of below instruction
Assembly Code Cost
MOV r1, a
MOV r2, b
ADD r1, r2
MOV c, r1
t = u – v
a = b + c
54. Prepared by R. Arthy Page 54
h = t * a
Assembly Code Cost Efficient Instruction
Set
Cost
MOV r1, u
MOV r2, v
SUB r1, r2
MOV t, r1
MOV r2, b
MOV r3, c
ADD r2, r3
MOV a, r2
MOV r1, t
MOV r2, a
MUL r1, r2
MOV h, r1
5. _________________________________________________
Category:
a. __________________________________________
b. __________________________________________
6. _________________________________________________
Q4: Code Generator Algorithm
Register Allocation: __________________________________________________________
Register assignment: _________________________________________________________
Uses of register:
1.
2.
3.
Descriptor
1. Register descriptor : maintains the _________________ stored in _______________
2. Address descriptor: maintains the ___________________________ which are used in the
program
A code-generation algorithm:
The algorithm takes as input a sequence of three -address statements constituting a basic
block. For each three-address statement of the form x : = y op z, perform the following
actions:
55. Prepared by R. Arthy Page 55
1. Invoke a function getreg to determine the location L where the result of the computation y
op z should be stored.
2. Consult the address descriptor for y to determine y„, the current location of y. Prefer the
register for y„ if the value of y is currently both in memory and a register. If the value of y is not
already in L, generate the instruction MOV y, R1 to place a copy of y in R1.
3. Generate the instruction OP R1, R2 where z„ is a current location of z. Prefer a register to
a memory location if z is in both. Update the address descriptor of x to indicate that x is in
location R1. If x is in R1, update its descriptor and remove x from all other descriptors.
4. If the current values of y or z have no next uses, are not live on exit from the block, and are
in registers, alter the register descriptor to indicate that, after execution of x : = y op z , those
registers will no longer contain y or z.
Problem
t1 = b – c
t2 = e + f
t3 = a / t1
t4 = s * t2
t5 = t3 – t4
Solution
t1 = b – c
Register Descriptor
Address Descriptor
Assembly code
t2 = e + f
Register Descriptor
56. Prepared by R. Arthy Page 56
Address Descriptor
Assembly code
t3 = a / t1
Register Descriptor
Address Descriptor
Assembly code
t4 = s * t2
Register Descriptor
Address Descriptor
57. Prepared by R. Arthy Page 57
Assembly code
t5 = t3 – t4
Register Descriptor
Address Descriptor
Assembly code
58. Prepared by R. Arthy Page 58
CS6660 – COMPILER DESIGN
UNIT V
Topic: Basic Block Construction
Q6: Basic Block
A basic block is a ________________ of _______________________________ in which
flow of control ______________ at the beginning and ______________ at the end without
any halt or possibility of branching except at the end.
Q7: Algorithm: Partition into basic blocks
Input: A sequence of __________________________________
Output: A list of basic blocks with each three-address statement in exactly one block
Method:
1. We first determine the set of leaders, the first statements of basic blocks. The rules
we use are of the following:
a. The ___________ statement is a leader.
b. Any statement that is the target of a _____________________________ is a
leader.
c. Any statement that ___________________ a goto or conditional goto
statement is a leader.
2. For each leader, its basic block consists of the leader and all statements up to but
not including the next leader or the end of the program.
Q8: Problem 1: Construct the Basic Block
(1) prod := 0
(2) i := 1
(3) t1 := 4* i
(4) t2 := a[t1] /*compute a[i] */
(5) t3 := 4* i
(6) t4 := b[t3] /*compute b[i] */
(7) t5 := t2*t4
(8) t6 := prod+t5
(9) prod := t6
(10) t7 := i+1
(11) i := t7
(12) if i<=20 goto (3)
59. Prepared by R. Arthy Page 59
Flow Graph
Flow graph is a ____________________ containing the ___________________ information
for the set of basic blocks making up a program.
The nodes of the flow graph are _______________. It has a distinguished initial node.
If B1 is the initial node and B2 immediately follows B1, so there is an edge from
_____ to ________.
The target of jump from last statement of B1 is the first statement B2, so there is an
edge from ________ to _________.
B1 is the ________________ of B2, and B2 is a _____________________ of B1.
Q9: Problem 2: Draw control flow graph
60. Prepared by R. Arthy Page 60
Topic: Optimization of Basic Blocks
Q10: Classification of techniques
1. _____________________________________________
a. ____________________________________________
b. ____________________________________________
c. ____________________________________________
b. ____________________________________________
2. _____________________________________________
Topic: Introduction to Global Data Flow Analysis
Q11: Data Flow Analysis
Q12: Terminologies
From the below given flow graph identify:
Definition points : ____________________________________________
61. Prepared by R. Arthy Page 61
Reference points : ____________________________________________
Evaluation points : ____________________________________________
Number of points in each block:
B1: ______________
B2: ______________
Q13: Properties:
1. Available Expression
2. Reachable Definition
d1: i = 0
d2: j = 0
d3: t1 = 4 * i
d4: b = a[t1]
d5: i = i + 1
B1
B2
62. Prepared by R. Arthy Page 62
3. Live Variable
4. Busy Expression
Q14: Data flow analysis equation
Q15: Data flow analysis for Assignment Statement
Gen[S] = ___________________________
Kill [S] = ___________________________
In[S] = _____________________________
Out[S] = ____________________________
Q16: Data flow analysis for If….. else statement
Gen[S] = _________________________________________
Kill [S] = ________________________________________
In[S] = __________________________________________
63. Prepared by R. Arthy Page 63
Out[S] = ________________________________________
Q17: Data flow analysis for collection of statements
Gen[S] = _________________________________________
Kill [S] = ________________________________________
In[S] = __________________________________________
Out[S] = ________________________________________