SC practical14.docx

  • Upload
    sasori

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

Enrollment No: 120050131108 Compiler Design (170701)Practical No: 14Aim: 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 wouldotherwisenot be possible. The following are commonly used intermediate representations: 1. Postfix notation 2. Syntax tree 3. Three-addresscodePostfix NotationIn postfix notation, the operatorfollowsthe operand. For example, in the expression (ab) * (c+d) + (ab), the postfix representation is:

Syntax TreeThe 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 singleproductionsisreplacedby single link (Figure 6.6).Parse tree for the string id+id*id.Syntax tree for id+id*id.Three-Address CodeThree address code is a sequence of statements of the formx=yopz. 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 expressiona+b*c+dis:

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:

Records with fields for the operators and operands can be used to represent three-address statements. It is possible to use a record structure with four fields: the first holds the operator, thenexttwo 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 RepresentationUsing quadruple representation, the three-address statementx=yopzis represented by placingopin the operator field,yin the operand1 field,zin the operand2 field, andxin the result field. The statementx=op y, whereopis a unary operator, is represented by placingopin the operator field,yin the operand1 field, andxin the result field; the operand2 field is not used. A statement like paramt1 is represented by placing param in the operator field andt1 in the operand1 field;neitheroperand2 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 statementx= (a+b) * -c/dis shown in Table 6.1. Thenumbersin parentheses represent the pointers to the triple structure.OperatorOperand1Operand2Result

(1)+abt1

(2)ct2

(3)*t1t2t3

(4)/t3dt4

(5)=t4X

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

Program Code:#includevoid 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]; j++; } str[j-2]='\0'; } else i++; }for(i=0;i