You are on page 1of 49

Experiment 01 : First () and Follow() Set

Learning Objective: Student should be able to Compute First () and Follow () set of given grammar.

Tools: Jdk1.8,Turbo C/C++, Python, Notepad++

Theory:

1.Algorithmto Compute FIRST as follows:

● Let a be a string of terminals and non-terminals.


● First (a) is the set of all terminals that can begin strings derived from a.

Compute FIRST(X) as follows:

a) if X is a terminal, then FIRST(X)={X}

b) if X→ε is a production, then add ε to FIRST(X)

c) if X is a non-terminal and X→Y1Y2...Ynis a production, add FIRST(Yi) to


FIRST(X) if the preceding Yjs contain ε in their FIRSTs

2. Algorithm to Compute FOLLOW as follows:

a) FOLLOW(S) contains EOF

b) For productions A→αBβ, everything in FIRST (β) except ε goes into


FOLLOW (B)

c) For productions A→αB or A→αBβ where FIRST (β) contains ε, FOLLOW(B)


contains everything that is in FOLLOW(A)

Original grammar:
E→E+E
E→E*E
E→(E)
E→id

This grammar is left-recursive, ambiguous and requires left-factoring. It needs to


be modified before we build a predictive parser for it:

Step 1: Remove Ambiguity.

E→E+T
T→T*F
F→(E)
F→id

Grammar is left recursive hence Remove left recursion:

E→TE'

E'→+TE'|ε
T→FT'

T'→*FT'|ε
F→(E)
F→id

Step 2: Grammar is already left factored.

Step 3: Find First & Follow set to construct predictive parser table:-

FIRST (E) = FIRST(T) = FIRST(F) = {(, id}

FIRST (E') = {+, ε}

FIRST (T') = {*, ε}

FOLLOW (E) = FOLLOW(E') = {$, )}


FOLLOW (T) = FOLLOW(T') = {+, $, )}

FOLLOW (F) = {*, +, $, )}

Example:
E→TE'

E'→+TE'|ε
T→FT'

T'→*FT'|ε
F→(E)
F→id

FIRST (E) = FIRST (T) = FIRST(F) = {(, id}

FIRST (E') = {+, ε}

FIRST (T') = {*, ε}

FOLLOW (E) = FOLLOW (E') = {$, )}

FOLLOW (T) = FOLLOW (T') = {+, $, )}

FOLLOW (F) = {*, +, $, )}

Application: To desige Top Down and Bottom up Parsers.

Design:
#include<stdio.h>

#include<ctype.h>

void Find_First(char[], char);


void Array_Manipula on(char[], char);

int limit;

char produc on[25][25];

int main()

char op on;

char ch;

char array[25];

int count;

prin ("\nEnter Total Number of Produc ons:\t");

scanf("%d", &limit);

for(count = 0; count < limit; count++)

prin ("\nValue of Produc on Number [%d]:\t", count + 1);

scanf("%s", produc on[count]);

do

prin ("\nEnter a Value to Find First:\t");

scanf(" %c", &ch);

Find_First(array, ch);

prin ("\nFirst Value of %c:\t{ ", ch);

for(count = 0; array[count] != '\0'; count++)


{

prin (" %c ", array[count]);

prin ("}\n");

prin ("To Con nue, Press Y:\t");

scanf(" %c", &op on);

}while(op on == 'y' || op on == 'Y');

return 0;

void Find_First(char* array, char ch)

int count, j, k;

char temporary_result[20];

int x;

temporary_result[0] = '\0';

array[0] = '\0';

if(!(isupper(ch)))

Array_Manipula on(array, ch);

return ;

for(count = 0; count < limit; count++)

if(produc on[count][0] == ch)


{

if(produc on[count][2] == '$')

Array_Manipula on(array, '$');

else

j = 2;

while(produc on[count][j] != '\0')

x = 0;

Find_First(temporary_result, produc on[count][j]);

for(k = 0; temporary_result[k] != '\0'; k++)

Array_Manipula on(array,temporary_result[k]);

for(k = 0; temporary_result[k] != '\0'; k++)

if(temporary_result[k] == '$')

x = 1;

break;

if(!x)
{

break;

j++;

return;

void Array_Manipula on(char array[], char value)

int temp;

for(temp = 0; array[temp] != '\0'; temp++)

if(array[temp] == value)

return;

array[temp] = value;

array[temp + 1] = '\0';

}
C Program To Find Follow of a Grammar using Array

#include<stdio.h>

#include<ctype.h>

#include<string.h>

int limit, x = 0;

char produc on[10][10], array[10];

void find_first(char ch);

void find_follow(char ch);

void Array_Manipula on(char ch);

int main()

int count;

char op on, ch;

prin ("\nEnter Total Number of Produc ons:\t");

scanf("%d", &limit);

for(count = 0; count < limit; count++)

prin ("\nValue of Produc on Number [%d]:\t", count + 1);

scanf("%s", produc on[count]);

do

{
x = 0;

prin ("\nEnter produc on Value to Find Follow:\t");

scanf(" %c", &ch);

find_follow(ch);

prin ("\nFollow Value of %c:\t{ ", ch);

for(count = 0; count < x; count++)

prin ("%c ", array[count]);

prin ("}\n");

prin ("To Con nue, Press Y:\t");

scanf(" %c", &op on);

}while(op on == 'y' || op on == 'Y');

return 0;

void find_follow(char ch)

int i, j;

int length = strlen(produc on[i]);

if(produc on[0][0] == ch)

Array_Manipula on('$');

for(i = 0; i < limit; i++)


{

for(j = 2; j < length; j++)

if(produc on[i][j] == ch)

if(produc on[i][j + 1] != '\0')

find_first(produc on[i][j + 1]);

if(produc on[i][j + 1] == '\0' && ch != produc on[i][0])

find_follow(produc on[i][0]);

void find_first(char ch)

int i, k;

if(!(isupper(ch)))

Array_Manipula on(ch);

}
for(k = 0; k < limit; k++)

if(produc on[k][0] == ch)

if(produc on[k][2] == '$')

find_follow(produc on[i][0]);

else if(islower(produc on[k][2]))

Array_Manipula on(produc on[k][2]);

else

find_first(produc on[k][2]);

void Array_Manipula on(char ch)

int count;

for(count = 0; count <= x; count++)

{
if(array[count] == ch)

return;

array[x++] = ch;

Result and Discussion:

Learning Outcomes: The student should have the ability to


LO1: Identify type of grammar G.
LO2: Define First () and Follow () sets.
LO3: FindFirst () and Follow () sets for given grammar G.
LO4: Apply First () and Follow () sets for designing Top Down and Bottom up Parsers

Course Outcomes: Upon completion of the course students will be able to analyze the analysis
and synthesis phase of compiler for writhing application programs and construct different parsers for
given context free grammars.

Conclusion:

The first and follow sets are used in compiler designing process. The first set is used in LL parsers which
are top – down parsers that read or interpret from le to right.

The follow set is used in LR parsers which are bo om-up parsers that read or interpret from right to le .

The LL parsers use le most deriva ons whereas the LR parsers use rightmost deriva ons. A predic ve
parser is constructed using first and follow sets that are associated with Grammar G.

For Faculty Use

Correction Formative Timely completion Attendance /


Parameters Assessment of Practical [ 40%] Learning
[40%] Attitude [20%]

Marks
Obtained
Experiment 02 : Code optimization Techniques

Learning Objective: Student should be able to Analyse and Apply code optimization techniques
to increase efficiency of compiler.

Tools: Jdk1.8,Turbo C/C++, Python, Notepad++

Theory:

Code optimization aims at improving the execution efficiency. This is achieved in two ways.
1. Redundancies in a program are eliminated.
2. Computations in a program are rearranged or rewritten to make it execute efficiently.
The code optimization must not change the meaning of the program.

Constant Folding:
When all the operands in an operation are constants, operation can be performed at
compilation time.

Elimination of common sub-expressions:


Common sub-expression are occurrences of expressions yielding the same value.
Implementation:
1. expressions with same value are identified
2. their equivalence is determined by considering whether their operands have
the same values in all occurrences
3. Occurrences of sub-expression which satisfy the criterion mentioned earlier
for expression can be eliminated.
Dead code elimination
Code which can be omitted from the program without affecting its result is called dead code.
Dead code is detected by checking whether the value assigned in an assignment statement is used
anywhere in the program
Frequency Reduction
Execution time of a program can be reduced by moving code from a part of program which is
executed very frequently to another part of program, which is executed fewer times. For ex.
Loop optimization moves, loop invariant code out of loop and places it prior to loop entry.
Strength reduction
The strength reduction optimization replaces the occurrence of a time consuming operations by
an occurrence of a faster operation. For ex. Replacement of Multiplication by Addition
Example:
A=B+C
B=A-D
C=B+C
D=A-D

After Optimization:
A=B+C
B=A-D
C=B+C
D=B

Application: To optimize code for improving space and time complexity.

Result and Discussion:

Learning Outcomes: The student should have the ability to


LO1: Define the role of Code Optimizer in Compiler design.
LO2: List the different principle sources of Code Optimization.
LO3: Apply different code optimization techniques for increasing efficiency of compiler.
LO4: Demonstrate the working of Code Optimizer in Compiler design.

Course Outcomes: Upon completion of the course students will be able to Evaluate the
synthesis phase to produce object code optimized in terms of high execution speed and less
memory usage.
Conclusion:

For Faculty Use

Correction Formative Timely completion Attendance /


Parameters Assessmen of Practical [ 40%] Learning
t [40%] Attitude [20%]

Marks
Obtained
Experiment 04 : Two Pass MacroProcessor

Learning Objective: Student should be able to Apply Two-pass Macro Processor.

Tools: Jdk1.8,Turbo C/C++, Python, Notepad++

Theory:
Macro processor is a program which is responsible for processing the macro.

There are four basic tasks/ functions that any macro instruction processor must perform.

1. Recognize macro definition:


A macro instruction processor must recognize macro definitions identified by the
MACRO and MEND pseudo-ops.
2. Save the definitions:
The processor must store the macro instruction definitions, which it will need for
expanding macro calls.
3. Recognize calls:
The processor must recognize macro calls that appear as operation mnemonics.
This suggests that macro names be handled as a type of op-code.
4. Expand calls and substitute arguments:
The processor must substitute for dummy or macro definition arguments the
corresponding arguments from a macro call; the resulting symbolic (in this case,
assembly language) text is then substituted for the macro call. This text, of course,
may contain additional macro definitions or calls.
In summary: the macro processor must recognize and process macro definitions and macro calls.

Two Pass Macro processor:


The macro processor algorithm will two passes over the input text, searching for the macro
definition & then for macro calls.

Data bases required for Pass1 & Pass2 Macro processor:


The following data bases are used by the two passes of the macro processor:
Pass 1 data bases:
1. The input macro source deck
2. The output macro source deck copy for use by pass 2
3. The Macro Definition Table (MDT), used to store the body of the macro definitions
4. The Macro Name Table (MNT), used to store the names of defined macros
5. The Macro Definition Table Counter (MDTC), used to indicate the next available entry in the
MDT
6. The Macro Name Table Counter (MNTC), used to indicate the next available entry in the
MNT
7. The Argument List Array (ALA), used to substitute index markers for dummy arguments
before storing a macro definition
Pass 2 data bases:
1. The copy of the input macro source deck
2. The output expanded source deck to be used as input to the assembler
3. The Macro Definition Table (MDT), created by pass 1
4. The Macro Name Table (MNT), created by pass 1
5. The Macro Definition Table Pointer (MDTP), used to indicate the next line of text to be used
during macro expansion
6. The Argument List Array (ALA), used to substitute macro call arguments for the index
markers in the stored macro definition

Format of Databases:
1) Argument List Array:
- The Argument List Array (ALA) is used during both pass 1 and pass 2.
During pass 1, in order to simplify later argument replacement during macro expansion, dummy
arguments in the macro definition are replaced with positional indicators when the definition is
stored: The ith dummy argument on the macro name card 'is represented in the body of the
macro by the index marker symbol #.
Where # is a symbol reserved for the use of the macro processor (i.e., not available to the
programmers).
- These symbols are used in conjunction with the argument list prepared before expansion
of a macro call. The symbolic dummy arguments are retained on the macro name card to
enable the macro processor to handle argument replacement by name rather than by
position.

- During pass 2 it is necessary to substitute macro call arguments for the index markers
stored in the macro definition.

Argument List Array:

Index 8 bytes per entry

0 "bbbbbbbb" (all blank)


2 "DATA3bbb"
3 "DATA2bbb"
4 "DATA1bbb"

2) Macro Definition Table:

- The Macro Definition Table (MDT) is a table of text lines.


- Every line of each macro definition, except the MACRO line, is stored in the MDT. (The
MACRO line is useless during macro expansion.)
- The MEND is kept to indicate the end of the definition; and the macro name line is
retained to facilitate keyword argument replacement.

Macro Definition Table

8O bytes per entry


Index Card
. .
. .
15 &LAB INCR &ARG1,&AAG2,&AAG3
16 #0 A 1, #1
17 A 2, #2
18 A 3,#3
19 MEND
. .
. .
. .

2) The Macro Name Table (MNT) :


- MNT serves a function very similar to that of the assembler's Machine-Op Table (MOT)
and Pseudo-Op Table(POT).
- Each MNT entry consists of a character string (the macro name) and a pointer (index) to
the entry in the MDT that corresponds to the beginning of the macro definition.

Index 8 Bytes 4 Bytes


. . .
. . .
3 “INCRbbbb” 15
. . .
. . .

ALGORITHM
PASS I-MACRO DEFINITION: The algorithm for pass-l tests each input line. If-it is a
MACRO pseudo-op:
1) The entire macro definition that follows is saved in the next available locations in the
Macro Definition Table (MDT).
2) The first line of the definition is the macro name line. The name is entered into
the Macro Name Table (MNT), along with a pointer to the first location of the
MDT entry of the definition.
3) When the END pseudo-op is encountered, all of the macro definitions have been
processed so control transfers to pass 2 in order to process macro calls.
PASS2-MACRO CALLS AND EXPANSION:
The algorithm for pass 2 tests the operation mnemonic of each input line to see if it is a name in
the MNT. When a call is found:-
1) The macro processor sets a pointer, the Macro Definition Table Pointer (MDTP) , to
the corresponding macro definition stored in the MDT. The initial value of the MDTP
is obtained from the "MDT index" field of the MNT entry.
2) The macro expander prepares the Argument List Array(ALA) consisting of a table of
dummy argument indices and corresponding arguments to the call.
3) Reading proceeds from the MDT; as each successive line is read, the values
from the argument list are substituted for dummy argument indices in the macro
definition.
4) Reading of the MEND line in the MDT terminates expansion of the macro, and
scanning continues from the input file.
5) When the END pseudo-op is encountered, the expanded source deck is transferred to
the assembler for further processing.
Flowchart of Pass1 & Pass2 Macro processor:
Pass 1: processing macro definitions
Pass2 : processing macro calls and expansion

Application: To design two-pass macroprocessor.


Design:

Result and Discussion:

Here we used macro call within the macro feature of the macro processor and then at the result
view we showed the Mnemonic table and Macro definition table along with the final result
which is our expanded code.
Learning Outcomes: The student should have the ability to

LO1: Describe the different database formats of two-pass Macro processor with the help of
examples.
LO2: Design two-pass Macro processor.
LO3: Develop two-pass Macro processor.
LO4: Illustrate the working of two-pass Macro-processor.

Course Outcomes: Upon completion of the course students will be able to Use of macros in modular
programming design

Conclusion:
Here in this experiment we are able to gain understanding about the macro processor and how to
use different feature of macro processor and implement the code and found the result in the form
of expanded code.

For Faculty Use

Correction Formative Timely completion Attendance /


Parameters Assessment of Practical [ 40%] Learning
[40%] Attitude [20%]
Marks
Obtained
Experiment 05 : Lexical Analyzer

Learning Objective: Student should be abletodesign handwritten lexical analyser.

Tools:Jdk1.8,Turbo C/C++, Python, Notepad++

Theory:

Design of lexical analyzer


. Allow white spaces, numbers and arithmetic operators in an expression
. Return tokens and attributes to the syntax analyzer
. A global variable tokenval is set to the value of the number
. Design requires that
- A finite set of tokens be defined
- Describe strings belonging to each token
Regular Expressions
• We use regular expressions to describe tokens of a programming language.
• A regular expression is built up of simpler regular expressions (using defining
rules)
• Each regular expression denotes a language.
• A language denoted by a regular expression is called as a regular set.
Regular Expressions (Rules)
Regular expressions over alphabet S
Regular Expression Language it denotes
ε {ε}
aЄ∑ S {a}
(r1) | (r2) L(r1) È L(r2)
(r1) (r2) L(r1) L(r2)
(r)* (L(r))*
(r) L(r)
• (r)+ = (r)(r)*
• (r)? = (r) | ε
• We may remove parentheses by using precedence rules.
* highest
concatenation next
| lowest

How to recognize tokens


Construct an analyzer that will return <token, attribute> pairs
We now consider the following grammar and try to construct an analyzer that will return
<token, attribute> pairs.
relop < | = | = | <> | = | >
id letter (letter | digit)*
num digit+ ('.' digit+)? (E ('+' | '-')? digit+)?
delim blank | tab | newline
ws delim+
Using set of rules as given in the example above we would be able to recognize the tokens. Given
a regular expression R and input string x, we have two methods for determining whether x is in
L(R). One approach is to use algorithm to construct an NFA N from R, and the other approach is
using a DFA.

Finite Automata
• A recognizer for a language is a program that takes a string x, and answers “yes” if x is a sentence
of that language, and “no” otherwise.
– We call the recognizer of the tokens as a finite automaton.

• A finite automaton can be: deterministic(DFA) or non-deterministic (NFA)


• This means that we may use a deterministic or non-deterministic automaton as a lexical analyzer.
• Both deterministic and non-deterministic finite automaton recognizes regular sets.
• Which one?
– deterministic – faster recognizer, but it may take more space
– non-deterministic – slower, but it may take less space
– Deterministic automatons are widely used lexical analyzers.

• First, we define regular expressions for tokens; Then we convert them into a DFA
to get a lexical analyzer for our tokens.

Algorithm1: Regular Expression NFA DFA (two steps: first to NFA, then to DFA)
Algorithm2: Regular Expression DFA (directly convert a regular expression into a DFA)
Converting Regular Expressions to NFAs
• Create transition diagram or transition table i.e. NFA for every expression
• Create a zero state as start state and with an e-transition connect all the NFAs and prepare a
combined NFA.

Algorithm:for lexical analysis


1) Specify the grammar with the help of regular expression
2) Create transition table for combined NFA
3) read input character
4) Search the NFA for the input sequence.
5) On finding accepting state
i. if token is id or num search the symbol table
1. if symbol found return symbol id
2. elseenter the symbol in symbol table and return its id.
ii. Else return token
6) Repeat steps 3 to 5 for all input characters.
Input:
#include<stdio.h>
void main()
{
inta,b;
printf(“Hello”);
getch();
}
Output:
Preprocessor Directives: #include
Header File: stdio.h
Keyword : void main intgetch
Symbol: <> , ; ( ) ; }
Message: Hello

Application: To designlexical analyzer.

Design:
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

using namespace std;

int isKeyword(char buffer[]){


char keywords[32][10] = {"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while"};
int i, flag = 0;
for(i = 0; i < 32; ++i){
if(strcmp(keywords[i], buffer) == 0){
flag = 1;
break;
}
}
return flag;
}

int main(){
char ch, buffer[15], operators[] = "+-*/%=";
ifstream fin("program.txt");
int i,j=0;
if(!fin.is_open()){
cout<<"error while opening the file\n";
exit(0);
}
while(!fin.eof()){
ch = fin.get();

for(i = 0; i < 6; ++i){


if(ch == operators[i])
cout<<ch<<" is operator\n";
}

if(isalnum(ch)){
buffer[j++] = ch;
}
else if((ch == ' ' || ch == '\n') && (j != 0)){
buffer[j] = '\0';
j = 0;

if(isKeyword(buffer) == 1)
cout<<buffer<<" is keyword\n";
else
cout<<buffer<<" is indentifier\n";
}

}
fin.close();
return 0;
}

Result and Discussion:


Learning Outcomes:The student should have the ability to

LO1: Appreciate the role of lexical analyzer in compiler design


LO2: Define role of lexical analyzer.

Course Outcomes: Upon completion of the course students will be able to design handwritten
laexical analyzer using HL programming language.

Conclusion:

For Faculty Use

Correction Formative Timely completion Attendance /


Parameters Assessment of Practical [ 40%] Learning
[40%] Attitude [20%]

Marks
Obtained
Experiment 06 : Intermediate Code Generator

Learning Objective: Student should be able to Apply Intermediate Code Generator using 3-
Address code.

Tools: Jdk1.8,Turbo C/C++, Python, Notepad++

Theory:

Intermediate Code Generation:

In the analysis-synthesis model of a compiler, the front end translates a source program into an
intermediate representation from which the back end generates target code. Details of the target
language are confined to the backend, as far as possible. Although a source program can be
translated directly into the target language, some benefits of using a machine-independent
intermediate form are:

1. Retargeting is facilitated; a compiler for a different machine can be created by attaching


a back end for the new machine to an existing front end.
2. A machine-independent code optimizer can be applied to the intermediate
representation.

Position of intermediate code generator.

(Intermediate Languages) Intermediate Code Representation:

a) Syntax trees or DAG

b) postfix notation

c) Three address code

Three-Address Code:
Three-address code is a sequence of statements of the general form

x:= y op z

where x, y, and z are names, constants, or compiler generated temporaries; op stands for any
operator, such as fixed-or floating-point arithmetic operator, or a logical operator on Boolean-
valued data.

A source language expression like x+y+z might be translated into a sequence

t 1 := y*z

t 2:=x+t1

where t1 and t2 are compiler-generated temporary names.

Example: a: =b* -c +b * -c

Types of Three-Address Statements:

Three-address statements are akin to assembly code. Statements can have symbolic labels and
there are statements for flow of control. A symbolic label represents the index of a three-
address statement in the array holding intermediate code.

Actual indices can be substituted for the labels either by making a separate pass, or by using
"back patching,"

Here are the common three-address statements:

I. Assignment statements of the form x= y op Z, where op is a binary arithmetic or logical


operation.
2. Assignment instructions of the form x : = op y, where op is a unary operation. Essential unary
operations include unary minus, logical negation, shift operators, and conversion operators that,
for example, convert a fixed-point number to a floating-point number.

3. Copy statements of the form x : = y where the value of y is assigned to x.

4. The unconditional jump goto L. The three-address statement with label L is the next to be
executed.

5. Conditional jumps such as if x relop y goto L. This instruction applies a relational operator «,
=. >=, etc.) to x and y, and executes the statement with label L next if x stands in relation relop
to y. If not, the three-address statement following if x relop y goto L is executed next, as in the
usual sequence.

6. param x and call p, n for procedure calls and return y, where y representing a returned value
is optional. Their typical use is as the sequence of three-address statements

param Xl

param X2

param Xn

call p,n

generated as part of a call of the procedure p (Xl, X2, ……….. Xn) The integer n indicating the
number of actual parameters in "call p, n" is not redundant because calls can be nested.

7. Indexed assignments of the form X : = y[ i] and x[ i] : = y. The first of these sets x to the value
in the location i memory units beyond location y. The statement x[ i] : = y sets the contents of
the location i units beyond x to the value of y. In both these instructions, x, y, and i refer to data
objects. Address and pointer assignments of the form x : = &y, x : = *y, and *x : = y.

Implementations of Three-Address Statements

A three-address statement' is an abstract form of intermediate code. In a compiler, these


statements can be implemented as records with fields for the operator and the operands. Three
such representations are quadruples, triples, and indirect triples.

Quadruples:
a) A quadruple is a record structure with four fields:
op, arg 1, arg 2, and result.

b) The op field contains an internal code for the operator.


c) The three-address statement x : = y op z is represented by placing y in arg1, Z in arg 2,
and x in result.
d) Statements with unary operators like x : = -y or x : = y do not use arg 2. Operators like
param use neither arg 2 nor result.
e) Conditional and unconditional jumps put the target label in result.
f) The quadruples are for the assignment a : = b * - c + b* - c.
They are obtained from the three-address code in Fig. (a).

g) The contents of fieldsarg I, arg 2, and result are normally pointers to thesymbol-table
entries for the names represented by these fields. If so, temporary names must be
entered into the symbol table as they are created.

Triples:

a) To avoid entering temporary names into the symbol table, refer to a temporary value
by the position of the statement that computes it.
b) Three-address statements can be represented by records with only three fields: op,
arg1 and arg2, as in Fig.(b).
c) The fieldsarg1 and arg2, for the arguments of op, are pointer to the symbol table.
Since three fields are used, this intermediate code format is known as triples.

Quadruple and triple representations of three-address statements.


A ternary operation like x[ i] := y requires two entries in the triple structure, as shown in Fig.(a),
while x : = y[ i] is naturally represented as two operations in Fig. (b).

More triple representations.

Indirect Triples:

Another implementation of three-address code that has been considered is that of listing
pointers to triples, rather than listing the triples themselves. This implementation is naturally
called indirect triples.

For example, let us use an array statement to list pointers to triples in the desired order.

Indirect triples representation of three-address statements

Input:

a : = b * - c + b* - c.
Output:
Application: Intermediate code can be easily produced to the target code.

Design:

Result and Discussion:

Learning Outcomes: The student should have the ability to


LO1 Define the role of Intermediate Code Generator in Compiler design.
LO2: Describe the various ways to implement Intermediate Code Generator.
LO3: Specify the formats of 3 Address Code.
LO4: Illustrate the working of Intermediate Code Generator using 3-Address code
.
Course Outcomes: Upon completion of the course students will be able to Evaluate the
synthesis phase to produce object code optimized in terms of high execution speed and less
memory usage.
Conclusion:

For Faculty Use

Correction Formative Timely completion Attendance /


Parameters Assessmen of Practical [ 40%] Learning
t [40%] Attitude [20%]

Marks
Obtained

You might also like