0% found this document useful (0 votes)
66 views

Compiler Design Project: Generation of Quadruples Using Pointers

This document describes a C program that implements generation of quadruples using pointers. The program takes as input a number of three-address code productions, splits each production into its components (operator, operands, result), and outputs the productions in quadruple form with those components separated into columns. It first reads in the number of productions and the productions themselves. It then parses each production string to extract the operator, operands, and result, and stores them in arrays. Finally, it prints the quadruple representation with the extracted components arranged in columns.

Uploaded by

Johnson Joseph
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Compiler Design Project: Generation of Quadruples Using Pointers

This document describes a C program that implements generation of quadruples using pointers. The program takes as input a number of three-address code productions, splits each production into its components (operator, operands, result), and outputs the productions in quadruple form with those components separated into columns. It first reads in the number of productions and the productions themselves. It then parses each production string to extract the operator, operands, and result, and stores them in arrays. Finally, it prints the quadruple representation with the extracted components arranged in columns.

Uploaded by

Johnson Joseph
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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