You are on page 1of 5

Enrollment No: 120050131108

Compiler Design (170701)

Practical No: 14
Aim: Write a program which generate quadruple table for the
given postfix expression.

Theory:
While translating a source program into a functionally equivalent object code
representation, a parser may first generate an intermediate representation. This
makes retargeting of the code possible and allows some optimizations to be carried
out that would otherwise not be possible. The following are commonly used
intermediate representations:
1. Postfix notation
2. Syntax tree
3. Three-address code
Postfix Notation
In postfix notation, the operator follows the operand. For example, in the expression
( a b ) * ( c + d ) + (a b ), the postfix representation is:

Syntax Tree
The syntax tree is nothing more than a condensed form of the parse tree. The
operator and keyword nodes of the parse tree (Figure 6.5) are moved to their
parent, and a chain of single productions is replaced by single link (Figure 6.6).

Parse tree for the string id+id*id.

BIT CSE1

Page

Enrollment No: 120050131108

Compiler Design (170701)

Syntax tree for id+id*id.


Three-Address Code
Three address code is a sequence of statements of the form x = y op z . Since a
statement involves no more than three references, it is called a "three-address
statement," and a sequence of such statements is referred to as three-address
code. For example, the three-address code for the expression a + b * c + d is:

Sometimes a statement might contain less than three references; but it is still called
a three-address statement. The following are the three-address statements used to
represent various programming language constructs:

Used for representing arithmetic expressions:

Used for representing Boolean expressions:

Used for representing array references and dereferencing operations:

Used for representing a procedure call:

BIT CSE1

Page

Enrollment No: 120050131108

Compiler Design (170701)

Records with fields for the operators and operands can be used to represent threeaddress statements. It is possible to use a record structure with four fields: the first
holds the operator, the next two hold the operand1 and operand2, respectively, and
the last one holds the result. This representation of a three-address statement is
called a "quadruple representation".
Quadruple Representation
Using quadruple representation, the three-address statement x = y op z is
represented by placing op in the operator field, y in the operand1 field, z in the
operand2 field, and x in the result field. The statement x= op y , where op is a unary
operator, is represented by placing op in the operator field, y in the operand1 field,
and x in the result field; the operand2 field is not used. A statement like param t 1 is
represented by placing param in the operator field and t 1 in the operand1
field; neither operand2 nor the result field are used. Unconditional and conditional
jump statements are represented by placing the target labels in the result field. For
example, a quadruple representation of the three-address code for the
statement x = ( a +b ) * - c/d is shown in Table 6.1. The numbers in parentheses
represent the pointers to the triple structure.

Operator

(1)

(2)

(3)

(4)

(5)

Operand1
a

Operand2
b

Result
t1

t2

t1

t2

t3

t3

t4

t4

Quadruple Representation of x = ( a + b ) * c/d

BIT CSE1

Page

Enrollment No: 120050131108

Compiler Design (170701)

Program Code:
#include<stdio.h>
void main()
{
char quar[20][4];
char str[10];
int i=0,q_i=0,j,temp=65;
printf("Enter the string:");
scanf("%s",&str[i]);
while(str[i]!='\0'){
if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/')
{
quar[q_i][0]=str[i];
quar[q_i][1]=str[i-2];
quar[q_i][2]=str[i-1];
quar[q_i][3]=temp;
str[i-2]=temp;
temp++;
q_i++;
j=i;
i=0;
j++;
while(str[j]!='\0'){
str[j-2]=str[j];
BIT CSE1

Page

Enrollment No: 120050131108

Compiler Design (170701)

j++;
}
str[j-2]='\0';
}
else
i++;
}
for(i=0;i<q_i;i++)
printf("\n%c\t%c\t%c\t%c\n",quar[i][0],quar[i][1],quar[i][2],quar[i][3]);
}

Output:

Conclusion:
Thus we created a program which generates Quadruple Table for the given postfix String.

BIT CSE1

Page

You might also like