Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

Compiler Design Project

Generation of Quadruples using pointers

Done by M. Sunil Kumar ( 08mse242 ) V. Sivaraman ( 08mse226 ) B. Thirumavalavan ( 08mse249 ) D. Vishnu Priyan (08mse271 )

AIM: C program to perform the implementation of quadruples using pointers.

THEORY: Three-address code (Quadruples) can be used as an intermediate language quadraples are close to machine instructions, but they are not actual machine instructions.

ALGORITHM: PRODUCTION Semantic Rule

S id := E { S.code = E.code||gen(id.place = E.place ;) } E E1 + E2 {E.place= newtemp ; E.code = E1.code || E2.code || || gen(E.place:=E1.place+E2.place) } E E1 * E2 {E.place= newtemp ; E.code = E1.code || E2.code || || gen(E.place=E1.place*E2.place) } E - E1 {E.place= newtemp ; E.code = E1.code || || gen(E.place = uminus E1.place) } E ( E1 ) E id {E.place= E1.place ; E.code = E1.code} {E.place = id.entry ; E.code = }

CODING: #include<stdio.h> #include<string.h> #include<ctype.h> #include<conio.h> void main() { char a[10][20],op[10],arg1[10][10],arg2[10][10],res[10][10]; int i,j,k,n; clrscr(); printf("enter the number of productions of three address codes:\n"); scanf("%d",&n); printf("\n"); printf("Enter the productions of three address code:\n"); for(i=0;i<n;i++) { scanf("%s",a[i]); }

for(i=0;i<n;i++) { j=0; k=0; for(j=0;a[i][j]!='=';j++) res[i][k++]=a[i][j]; res[i][k]=NULL; j++; if(a[i][j]=='+'||a[i][j]=='-'||a[i][j]=='*'||a[i][j]=='/')

{ op[i]=a[i][j]; k=0; for(j=j+1;j<strlen(a[i]);j++) arg1[i][k++]=a[i][j]; arg1[i][k]=NULL; arg2[i][0]=NULL; } else if(islower(a[i][j])) { k=0; while(isalnum(a[i][j])) { arg1[i][k++]=a[i][j]; j++; } arg1[i][k]=NULL; if(j==strlen(a[i])) { op[i]='='; arg2[i][0]=NULL; } else { op[i]=a[i][j]; k=0; for(j=j+1;j<strlen(a[i]);j++) arg2[i][k++]=a[i][j]; arg2[i][k]=NULL;

} } } printf("\n\n"); printf("--The Quadraple reprsentation for the given three address code is--"); printf("\n\n \top\targ1\targ2\tresult\n"); for(i=0;i<n;i++) { printf("\n\n(%d)\t%c\t%s\t%s\t%s",i,op[i],arg1[i],arg2[i],res[i]); } getch(); }

SAMPLE INPUT AND OUTPUT:

SAMPLE INPUT: Enter the number of productions of three address codes: 3 Enter the productions of three address code: T=-b T1=c*t d=t1

SAMPLE OUTPUT:

--The Quadraple representation for the given three address code is Op (0) (1) * (2) = arg1 arg2 result b c t1 t t t1 d

You might also like