0% found this document useful (0 votes)
21 views16 pages

CD Unoit 3

compiler design
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views16 pages

CD Unoit 3

compiler design
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

1

C HENDHURAN COLLEGE OF ENGINEERING AND

TECHNOLOGY

Pilivalam (Po), Thirumayam (Tk), Pudukkottai (Dt.) – 622 507

Department of Computer Science and Engineering


V Semester
CS3501-COMPILER DESIGN
Coaching Class Test-
Date:

Unit-III
Syntax Directed Translation and Intermediate Code Generation

PART-A

1. Write down the syntax directed definition of simple desk calculator.

2. List out the benefits of using machine independent intermediate forms .


 A Compiler for different machines can be created by attaching different back end to the existing front
ends of each machine.
 A Compiler for different source languages can be created by proving different front ends for
corresponding source languages t existing back end.
 A machine independent code optimizer can be applied to intermediate code in order to optimize the
code generation.
3. What is the idea behind generating three address code during compilation?
2

The main idea behind generating three-address code during compilation is to create a simplified,
platform-independent representation of the source code. This simplification makes it easier for the compiler
to optimize and generate code for different computer architectures while also aiding in error detection and
debugging.
4. Translate the arithmetic expression a*-(b+c) into syntax tree and postfix notation.

5. What is the intermediate code representation for the expression a or b and not c?
t1=a or b
t2=not c
t3=t1 and t2
6. What are the various methods of implementing three address statements.
 Quadruples: Use sets of four fields for operators, source operands, and a destination
variable.
 Triples: Similar to quadruples but with an implicit destination determined by position.
 Indirect Triples: Destination is represented as a reference to a memory location.

7. Give syntax directed definition for if-else statement.


1. S → if E then S1
E.true := new_label()
E.false :=S.next
S1.next :=S.next
S.code :=E.code | | gen_code(E.true ‘: ‘) | | S1.code
2. S → if E then S1 else S2
E.true := new_label()
E.false := new_label()
S1.next :=S.next
S2.next :=S.next
S.code :=E.code | | gen_code(E.true ‘: ‘) | | S1.code| | gen_code(‘go to’,S.next) | |
gen_code(E.false ‘:’) | | S2.code

8. What are the properties of intermediate languages.


 Convenient to produce in the semantic analysis phase.
 Convenient to translate into code for the desired target architecture.
3

 Each construct must have a clear and simple meaning such that optimizing transformations that
rewrite the IR can be easily specified and implemented.

9. Define backpatching
Backpatching is basically a process of fulfilling unspecified information. This information is of
labels. It basically uses the appropriate semantic actions during the process of code generation. It may
indicate the address of the Label in goto statements while producing TACs for the given expressions.
10. Mention the role of semantic analysis.
It uses syntax tree and symbol table to check whether the given program is semantically consistent
with language definition. It gathers type information and stores it in either syntax tree or symbol table.
This type information is subsequently used by compiler during intermediate-code generation.

PART-B

11. Discuss the various methods for translating the Boolean expression.
A Boolean Expression is an expression that evaluates to either true or false. We generally use Boolean
Expressions in conditional statements and loops in programming languages. It can be a simple Boolean
Variable or a combination of Boolean Variables, Constants, and Operators. Examples of Boolean Operators
include “and,” “or,” and “not
Methods to Translate Boolean Expressions
Numerical Representation
In Numerical Representation, we denote true by ‘1’ and false by ‘0’. The direction of evaluation of a
Boolean Expression is from left to right.
Let’s see some examples to understand Numerical Representation:

The translation for p AND q OR NOT s is:

f1 : = NOT s
f2 : = q OR f1
f3 : = p AND f2

Let’s translate a < b into the three-address code sequence and start numbers at 50:

50: if a < b goto 53


51: t : = 0
4

52: goto 54
53: t : = 1
54:

Now, translate a > b into the three-address code sequence and start numbers at 60:

60: if a < b goto 63


61: t : = 1
62: goto 64
63: t : = 0
64:

Translation Scheme using a Numerical Representation


Productive Rule Semantic Action

{
E.place = newtemp();
E -> E1 OR E2
Emit(E.place ‘:=’ E1.place ‘OR’ E2.place)
}

{
E.place = newtemp();
E -> E1 AND E2
Emit(E.place ‘:=’ E1.place ‘AND’ E2.place)
}

{
E.place = newtemp();
E -> NOT E1
Emit(E.place ‘:=’ ‘NOT’ E1.place)
}

{
E -> (E1) E.place = E1.place
}

E -> id1 relop id2 {


E.place = newtemp();
Emit(‘if id1.place relop id2.place ‘goto’ nextstate + 3);
Emit(E.place ‘:=’ ‘0’);
Emit(‘goto’ nextstate + 2);
5

Productive Rule Semantic Action

Emit(E.place ‘:=’ ‘1’)


}

{
E.place ‘:=’ newtemp();
E -> true
Emit(E.place ‘:=’ ‘1’)
}

{
E.place ‘:=’ newtemp();
E -> false
Emit(E.place ‘:=’ ‘0’)
}

Flow-of-Control Statements
In this method, we translate the Boolean Expression into the three-address code in terms of if-then
statements, if-then-else statements, and while-do statements.
Here, Boolean Expressions are denoted by ‘E’ and the three-address statement is symbolically labelled.

These are some points you need to keep in mind while determining the Syntax-Directed Definition:
 The Control will go to the E.true label, if the Expression ‘E’ will be true and the Control will go
to the E.false label, if the Expression ‘E’ will be false.

 The Control will flow from S.code to the three-address instruction.

 The label S.next is the first three-address instruction that needs to be executed after the S.code.

Syntax-Directed Definition for Flow-of-Control Statements


Production Semantic Rules

E.true : = newlabel;
E.false : = S.next;
S -> if E then S1
S1.next : = S.next;
S.code : = E.code || gen(E.true ‘:’) || S1.code

S -> if E then S1 else S2 E.true : = newlabel;


E.false : = newlabel;
S1.next : = S.next;
S2.next : = S.next;
6

Production Semantic Rules

S.code : = E.code || gen(E.true ‘:’) || get(‘goto’ S.next) || gen(E.false ‘:’) ||


S1.code || S2.code

S.begin : = newlabel;
E.true : = newlabel;
E.false : = S.next;
S -> While E do S1
S1.next : = S.begin;
S.code : = gen(S.begin ‘:’) || E.code || gen(E.true ‘:’) || S1.code || gen(‘goto’
S.begin)

12. (i) Give the translating schemes for converting the assignment statements in to three address code
with example. Use the scheme for generating three address code for the assignment statement
s=a+b-c*d
Translation of Assignment Statements
In the syntax directed translation, assignment statement is mainly deals with expressions. The expression can
be of type real, integer, array and records.
Consider the grammar
1. S → id := E
2. E → E1 + E2
3. E → E1 * E2
4. E → (E1)
5. E → id
The translation scheme of above grammar is given below:

Production rule Semantic actions

S → id :=E {p = look_up(id.name);
If p ≠ nil then
Emit (p = E.place)
Else
Error;
}

E → E1 + E2 {E.place = newtemp();
Emit (E.place = E1.place '+' E2.place)
}
7

E → E1 * E2 {E.place = newtemp();
Emit (E.place = E1.place '*' E2.place)
}

E → (E1) {E.place = E1.place}

E → id {p = look_up(id.name);
If p ≠ nil then
Emit (p = E.place)
Else
Error;
}

o The p returns the entry for id.name in the symbol table.


o The Emit function is used for appending the three address code to the output file. Otherwise it will
report an error.
o The newtemp() is a function used to generate new temporary variables.
o E.place holds the value of E.
8

13. What is a three address code ? What are its types ? How is it implemented? Write the intermediate
representation for the expression : (x + y) * (y + z) + (x + y + z)

Three address code is a type of intermediate code which is easy to generate and can be easily converted to
machine code. It makes use of at most three addresses and one operator to represent an expression and the
value computed at each instruction is stored in temporary variable generated by compiler. The compiler
decides the order of operation given by three address code.
Three address code is used in compiler applications:
Optimization: The three address code allows the compiler to analyze the code and perform optimizations
that can improve the performance of the generated code.
Code generation: . The three address code allows the compiler to generate code that is specific to the target
platform, while also ensuring that the generated code is correct and efficient.
Debugging: Since three address code is a low-level language, it is often easier to read and understand than
the final generated code. Developers can use the three address code to trace the execution of the program and
identify errors or issues that may be present.
Language translation: Three address code can also be used to translate code from one programming
language to another. By translating code to a common intermediate representation, it becomes easier to
translate the code to multiple target languages.
General representation –
a = b op c
Where a, b or c represents operands like names, constants or compiler generated temporaries and op
represents the operator

Example-: 1 Write three address code for following code


for(i = 1; i<=10; i++)
{
a[i] = x * 5;
}
9

Implementation of Three Address Code –


There are 3 representations of three address code namely
1. Quadruple
2. Triples
3. Indirect Triples
1. Quadruple – It is a structure which consists of 4 fields namely op, arg1, arg2 and result. op denotes the
operator and arg1 and arg2 denotes the two operands and result is used to store the result of the expression.
Advantage –
 Easy to rearrange code for global optimization.
 One can quickly access value of temporary variables using symbol table.
Disadvantage –
 Contain lot of temporaries.
 Temporary variable creation increases time and space complexity.
Example – Consider expression a = b * – c + b * – c. The three address code is:
t1 = uminus c
t2 = b * t1
t3 = uminus c
t4 = b * t3
t5 = t2 + t4
a = t5
10

2. Triples – This representation doesn’t make use of extra temporary variable to represent a single operation
instead when a reference to another triple’s value is needed, a pointer to that triple is used. So, it consist of
only three fields namely op, arg1 and arg2.
Disadvantage –
 Temporaries are implicit and difficult to rearrange code.
 It is difficult to optimize because optimization involves moving intermediate code. When a triple
is moved, any other triple referring to it must be updated also. With help of pointer one can
directly access symbol table entry.
Example – Consider expression a = b * – c + b * – c

3. Indirect Triples – This representation makes use of pointer to the listing of all references to computations
which is made separately and stored. Its similar in utility as compared to quadruple representation but
requires less space than it. Temporaries are implicit and easier to rearrange code.
11

Example – Consider expression a = b * – c + b * – c

Question – Write quadruple, triples and indirect triples for following expression : (x + y) * (y + z) + (x + y +
z)
Explanation – The three address code is:
t1 = x + y
t2 = y + z
t3 = t1 * t2
t4 = t1 + z
t5 = t3 + t4
12

4. Construct a syntax directed translation scheme that translates arithmetic expressions form
infix to postfix notation. Using semantic attributes for each of the grammar symbols and
semantic rule, evaluate the input n3*4+5*2
13

5. Explain the process of generating the code for a Boolean expression in a single pass
Backpatching is basically a process of fulfilling unspecified information. This information is of labels. It
basically uses the appropriate semantic actions during the process of code generation. It may indicate the
address of the Label in goto statements while producing TACs for the given expressions. Here basically
two passes are used because assigning the positions of these label statements in one pass is quite
challenging. It can leave these addresses unidentified in the first pass and then populate them in the second
round. Backpatching is the process of filling up gaps in incomplete transformations and information.
Need for Backpatching:
Backpatching is mainly used for two purposes:
1. Boolean expression:
Boolean expressions are statements whose results can be either true or false. A boolean expression which
is named for mathematician George Boole is an expression that evaluates to either true or false. Let’s look
at some common language examples:
 My favorite color is blue. → true
 I am afraid of mathematics. → false
 2 is greater than 5. → false
2. Flow of control statements:
The flow of control statements needs to be controlled during the execution of statements in a program. For
example:
14

The flow of control statements


3. Labels and Gotos:
The most elementary programming language construct for changing the flow of control in a program is a
label and goto. When a compiler encounters a statement like goto L, it must check that there is exactly one
statement with label L in the scope of this goto statement. If the label has already appeared, then the
symbol table will have an entry giving the compiler-generated label for the first three-address instruction
associated with the source statement labeled L. For the translation, we generate a goto three-address
statement with that compiler-generated label as a target.
When a label L is encountered for the first time in the source program, either in a declaration or as the
target of the forward goto, we enter L into the symbol table and generate a symbolic table for L.
One-pass code generation using backpatching:
In a single pass, backpatching may be used to create a boolean expressions program as well as the flow of
control statements. The synthesized properties truelist and falselist of non-terminal B are used to handle
labels in jumping code for Boolean statements. The label to which control should go if B is true should be
added to B.truelist, which is a list of a jump or conditional jump instructions. B.falselist is the list of
instructions that eventually get the label to which control is assigned when B is false. The jumps to true
and false exist, as well as the label field, are left blank when the program is generated for B. The lists
B.truelist and B.falselist, respectively, contain these early jumps.
A statement S, for example, has a synthesized attribute S.nextlist, which indicates a list of jumps to the
instruction immediately after the code for S. It can generate instructions into an instruction array, with
labels serving as indexes. We utilize three functions to modify the list of jumps:
 Makelist (i): Create a new list including only i, an index into the array of instructions and the
makelist also returns a pointer to the newly generated list.
 Merge(p1,p2): Concatenates the lists pointed to by p1, and p2 and returns a pointer to the
concatenated list.
 Backpatch (p, i): Inserts i as the target label for each of the instructions on the record pointed
to by p.
Backpatching for Boolean Expressions:
Using a translation technique, it can create code for Boolean expressions during bottom-up parsing. In
grammar, a non-terminal marker M creates a semantic action that picks up the index of the next instruction
to be created at the proper time.
For Example, Backpatching using boolean expressions production rules table:
Step 1: Generation of the production table
15

Production Table for Backpatching


Step 2: We have to find the TAC(Three address code) for the given expression using backpatching:
A < B OR C < D AND P < Q

Three address codes for the given example


16

Step 3: Now we will make the parse tree for the expression:

Parse tree for the example


The flow of Control Statements:
Control statements are those that alter the order in which statements are executed. If, If-else, Switch-Case,
and while-do statements are examples. Boolean expressions are often used in computer languages to
 Alter the flow of control: Boolean expressions are conditional expressions that change the
flow of control in a statement. The value of such a Boolean statement is implicit in the
program’s position. For example, if (A) B, the expression A must be true if statement B is
reached.
 Compute logical values: During bottom-up parsing, it may generate code for Boolean
statements via a translation mechanism. A non-terminal marker M in the grammar establishes a
semantic action that takes the index of the following instruction to be formed at the appropriate
moment.
Applications of Backpatching:
 Backpatching is used to translate flow-of-control statements in one pass itself.
 Backpatching is used for producing quadruples for boolean expressions during bottom-up
parsing.
 It is the activity of filling up unspecified information of labels during the code generation
process.
 It helps to resolve forward branches that have been planted in the code.

You might also like