Professional Documents
Culture Documents
R.D.Foundation Group of Institution Faculty of Engineering Department of Computer Science & Engineering
R.D.Foundation Group of Institution Faculty of Engineering Department of Computer Science & Engineering
R.D.Foundation Group of Institution Faculty of Engineering Department of Computer Science & Engineering
FOUNDATION GROUP OF INSTITUTION FACULTY OF ENGINEERING Department of Computer Science & Engineering
DEPTT.:CSE
R.D.FOUNDATION GROUP OF LABORATORY INSTITUTION,FACULTY OF MANUAL ENGINEERING PRACTICAL INSTRUCTION SHEET EXPERIMENT TITLE: EXPERIMENT ISSUE NO. : 00 ISSUE DATE: NO. RDFGIT01/06/2013 CSE/CDL REV. NO. : 01 REV. DATE : NIL PAGE: 1 LABORATORY: Compiler Design SEMESTER: VI Lab(ECS-653)
S.No.
1 2 3 4 5 6
Experiment
Program to check a string under a given grammar. Program to check for keywords in a given string. Program to check for identifiers in a given string. Program to check for constants in a given string. Program to check for relational operators in a given string. Program for Lexical Analyzer.
Issue Date
7 8 9 10
Implementation of stack using C. Implementation of shift reduce parsing using C. Implementation of three addresses code using Quadruples, Triples, Indirect Triples. Write a program in c to check a real number
R.D.FOUNDATION GROUP OF LABORATORY INSTITUTION,FACULTY OF MANUAL ENGINEERING PRACTICAL INSTRUCTION SHEET REV. NO. : 01 DEPTT.:CSE REV. DATE : 01/06/2013 LABORATORY: Compiler Design Lab(ECS-653) PAGE: 2 SEMESTER: VI
EXPERIMENT NO-01
OBJECTIVE: WRITE A PROGRAM IN C TO CHECK A STRING UNDER THE GIVEN GRAMMER. INTRODUCTION:
A recognizer for a language is a program that takes a string x as an input and Answers yes if x is a sentence of the language and no otherwise .One can compile any regular expression into a recognizer by constructing a generalized transition diagram called a finite automation . A finite automation can be deterministic means that more than one transition out of a State may be possible on a same input symbol. Both automata are capable of recognizing what regular expression can denote.
R.D.FOUNDATION GROUP OF LABORATORY INSTITUTION,FACULTY OF MANUAL ENGINEERING PRACTICAL INSTRUCTION SHEET REV. NO. : 01 DEPTT.:CSE REV. DATE : 01/06/2013 LABORATORY: Compiler Design Lab(ECS-653) PAGE: 3 SEMESTER: VI
Algorithm:
Step 1: Declare the grammar and store the string in str. Step 2: (flag=0) Step 3: do for j=0 to strlen(str) Step 4: if(j==0) Step 5: if(str[j]!=0) Step 6: flag=1 Step 7: else Step 8: if(str[j]!=b) Step 9: flag=1 Step 10: if(flag==0) Step 11: Display that string exist for the grammar Step 12: else Step 13: display the string is not valid for the grammar
R.D.FOUNDATION GROUP OF LABORATORY INSTITUTION,FACULTY OF MANUAL ENGINEERING PRACTICAL INSTRUCTION SHEET REV. NO. : 01 DEPTT.:CSE REV. DATE : 01/06/2013 LABORATORY: Compiler Design Lab(ECS-653) PAGE: 4 SEMESTER: VI
if(string[count]=='b') { flag=1; continue; } else if((flag==1)&&(string[count]=='a')) { printf("the string does not belong to the specified grammer"); break; } else if(string[count]=='a') continue; else if((flag==1)&&(string[count]=='\0')) { printf("the string belongs to the grammer"); break; } }//for terminates }//if terminates else printf("the string does not belong to the grammer"); getch(); }
R.D.FOUNDATION GROUP OF LABORATORY INSTITUTION,FACULTY OF MANUAL ENGINEERING PRACTICAL INSTRUCTION SHEET REV. NO. : 01 DEPTT.:CSE REV. DATE : 01/06/2013 LABORATORY: Compiler Design Lab(ECS-653) PAGE: 6 SEMESTER: VI
EXPERIMENT -2
OBJECTIVE: WRITE A PROGRAM IN C FOR IDENTIFICATION OF KEYWORDS IN A GIVEN STRING. INTRODUCTION: The initial phase analyzes the source program. The lexical analyzer phase reads the character in the source program and groups them in to the stream of tokens; each token represents a logical cohesive sequence of characters such as identifiers, operators and keywords. Source program is a stream of characters: pos = init + rate*60 Lexical analysis groups characters into non-separable units, called token, and generates token stream: Id1=id2 + id3 *const The information about the identifiers must be stored in symbol the Symbol Table.
R.D.FOUNDATION GROUP OF LABORATORY INSTITUTION,FACULTY OF MANUAL ENGINEERING PRACTICAL INSTRUCTION SHEET REV. NO. : 01 DEPTT.:CSE REV. DATE : 01/06/2013 LABORATORY: Compiler Design Lab(ECS-653) PAGE: 7 SEMESTER: VI
ALGORITHM: Step1: Declare the Keywords in a array. Step2: enter the string and a space in the end. Step3: for(i=0;i<strlen(arr);i++) Step4: if(arr[i] is anything else i.e. not an alphabets) Step5: copy the word in w. Step6: check the keyword with the given list of keywords. Step7: if keyword exist display the keyword.
R.D.FOUNDATION GROUP OF LABORATORY INSTITUTION,FACULTY OF MANUAL ENGINEERING PRACTICAL INSTRUCTION SHEET REV. NO. : 01 DEPTT.:CSE REV. DATE : 01/06/2013 LABORATORY: Compiler Design Lab(ECS-653) PAGE: 8 SEMESTER: VI
if((a[i]==' ') || (a[i]=='\0')) { temp[j]='\0'; flag=1; for(k=0;k<2;k++) { if(strcmp(temp,keyword[k])==0) { t[k]++; temp=0; } } for(i=0;i<2;i++) printf("keyword { printf(keyword[i]); } } } no. of occurrence");
R.D.FOUNDATION GROUP OF LABORATORY INSTITUTION,FACULTY OF MANUAL ENGINEERING PRACTICAL INSTRUCTION SHEET REV. NO. : 01 DEPTT.:CSE REV. DATE : 01/06/2013 LABORATORY: Compiler Design Lab(ECS-653) PAGE: 9 SEMESTER: VI
Experiment no. 3
Objective: WRITE A PROGRAM IN C FOR IDENTIFICATION OF IDENTIFIERS IN A GIVEN STRING. INTRODUCTION: The initial phase analyzes the source program. The lexical analyzer reads the character in source program and groups them into the stream of tokens; each token represent a logical cohesive sequence of characters such as identifiers, operators and keywords. Source program is a stream of characters: pos = init+rate*60 Lexical analysis groups characters into non-separatable units, called tokens, and generates tokens of stream: Id1 = id2 + id3 * const The information about the identifiers must be stored in symbol the Symbol table.
R.D.FOUNDATION GROUP OF LABORATORY INSTITUTION,FACULTY OF MANUAL ENGINEERING PRACTICAL INSTRUCTION SHEET REV. NO. : 01 DEPTT.:CSE REV. DATE : 01/06/2013 LABORATORY: Compiler Design Lab(ECS-653) PAGE: 10 SEMESTER: VI
{ y = 0; while( a[n] != ' ') { b[x][y] = a[n]; n++; y++; } n++; i=n; for(l=0; l<10; l++) { if( strcmp(b[x],s[l] !=0 )) { g = b[x][0]; if(g>= 65 || g<= 90 || g>= 97 || g<=122) { printf("\t%s\n",b[x]); count++; break; } } }
SUBMITTED BY: ROMA
R.D.FOUNDATION GROUP OF LABORATORY INSTITUTION,FACULTY OF MANUAL ENGINEERING PRACTICAL INSTRUCTION SHEET REV. NO. : 01 DEPTT.:CSE REV. DATE : 01/06/2013 LABORATORY: Compiler Design Lab(ECS-653) PAGE: 13 SEMESTER: VI
for(k=0;k<j;k++) { if(isdigit(temp[k])!=0) con+=0; else con+=1; } if(con==0) printf("%s is a constant \n",temp); i++; j=0; con=0; } temp[j]=s[i]; j++; } getch(); }
R.D.FOUNDATION GROUP OF LABORATORY INSTITUTION,FACULTY OF MANUAL ENGINEERING PRACTICAL INSTRUCTION SHEET REV. NO. : 01 DEPTT.:CSE REV. DATE : 01/06/2013 LABORATORY: Compiler Design Lab(ECS-653) PAGE: 15 SEMESTER: VI
R.D.FOUNDATION GROUP OF LABORATORY INSTITUTION,FACULTY OF MANUAL ENGINEERING PRACTICAL INSTRUCTION SHEET REV. NO. : 01 DEPTT.:CSE REV. DATE : 01/06/2013 LABORATORY: Compiler Design Lab(ECS-653) PAGE: 16 SEMESTER: VI
{ printf("\n%c",a[i]); c++; } } } } if(c==0) printf("\n\n No relational operator is found."); else printf("\n\n No. of relational operators found is=%d.",c); getch(); }
R.D.FOUNDATION GROUP OF LABORATORY INSTITUTION,FACULTY OF MANUAL ENGINEERING PRACTICAL INSTRUCTION SHEET REV. NO. : 01 DEPTT.:CSE REV. DATE : 01/06/2013 LABORATORY: Compiler Design Lab(ECS-653) PAGE: 18 SEMESTER: VI
INTRODUCTION:
Token A lexical token is a sequence of characters that can be treated as a unit in the grammar of the programming languages Example of tokens: Type token (id, num, real,) Punctuation token (IF, Void, return,) Alphabetic tokens (keywords) Example of non-tokens: Comments, preprocessor directive, macros, blanks, tabs, newline
R.D.FOUNDATION GROUP OF LABORATORY INSTITUTION,FACULTY OF MANUAL ENGINEERING PRACTICAL INSTRUCTION SHEET REV. NO. : 01 DEPTT.:CSE REV. DATE : 01/06/2013 LABORATORY: Compiler Design Lab(ECS-653) PAGE: 20 SEMESTER: VI
{ } } }
printf("\n%c",a[i]); c++;
} if(c==0) printf("\n\n No relational operator is found."); else printf("\n\n No. of relational operators found is=%d.",c); getch(); }
R.D.FOUNDATION GROUP OF LABORATORY INSTITUTION,FACULTY OF MANUAL ENGINEERING PRACTICAL INSTRUCTION SHEET REV. NO. : 01 DEPTT.:CSE REV. DATE : 01/06/2013 LABORATORY: Compiler Design Lab(ECS-653) PAGE: 22 SEMESTER: VI
case 2: i=pop(); printf("Value returned from pop function is %d ",i); break; default: printf("Invalid Choice . "); } printf(" Do you want to do more operations on Stack ( 1 for yes, any other key to exit) "); scanf("%d" , &will); } //end of outer while } //end of main void push(int y) { if(top>MAXSIZE) { printf(" STACK FULL"); return; } else { top++; stack[top]=y; }} int pop() { int a; if(top<=0) { printf(" STACK EMPTY return 0; } else { a=stack[top]; top--; } return(a); getch(); }
");
R.D.FOUNDATION GROUP OF LABORATORY INSTITUTION,FACULTY OF MANUAL ENGINEERING PRACTICAL INSTRUCTION SHEET REV. NO. : 01 DEPTT.:CSE REV. DATE : 01/06/2013 LABORATORY: Compiler Design Lab(ECS-653) PAGE: 24 SEMESTER: VI
char tb[30][6]={"E->Te"," "," ","E->Te"," "," ", " ","e->+Te"," "," ","e->0","e->0", "T->Ft"," "," ","T->Ft"," "," ", " ","t->0","T->*Ft"," ","t->0","t->0", "F->i"," "," ","F->(E)"," "," "}; char tab[30][3]= {"eT"," "," ","eT"," "," ", " ","eT+"," "," ","0","0", "tF"," "," ","tF"," "," ", " ","0","tF*"," ","0","0", "i"," "," ",")E("," "," "}; int num[30]={2,0,0,2,0,0, 0,3,0,0,1,1, 2,0,0,2,0,0, 0,1,3,0,1,1, 1,0,0,3,0,0}; printf("\n\n\tPredictive Parsing Table:\n\n"); for(int j=0;j<6;j++) printf("\t%c",ter[j]); printf("\n"); for(int i=0;i<30;i++) { if(i%6==0) printf("\n");
SUBMITTED BY: ROMA
printf("\t%s",tb[i]); } printf("\n\n\n Enter the string----"); gets(a); l=strlen(a); for(i=0;i<l;i++) ip[i]=a[i]; ip[i]='$'; ip[i+1]=NULL; while(stk[top]!='$') { ters=ip[k]; if(stk[top]==ters) { k++; top--; continue; } for(i=0;i<6;i++) { if(ters==ter[i]) { terc=i;
SUBMITTED BY: ROMA
c=1; break; }} if(c==0) { printf("\n\wrong terminal"); getch(); exit(0); } c=0; nons=stk[top]; for(i=0;i<5;i++) { if(nons==non[i]) { nonc=i; break; }} pos=6*nonc+terc; no=num[pos]; for(i=0;i<no;i++) b[i]=tab[pos][i]; b[i]=NULL;
SUBMITTED BY: ROMA
if(no==0) { printf("\n\nString is not accepted"); getch(); exit(0); } else if(no==1 && tab[pos][0]!='i') { top--; } else { for(i=0;i<no;i++) { stk[top]=b[i]; top+=1; } top-=1; }} if(stk[top]=='$') printf("\n\n String is accepted"); getch(); }
SUBMITTED BY: ROMA
R.D.FOUNDATION GROUP OF LABORATORY INSTITUTION,FACULTY OF MANUAL ENGINEERING PRACTICAL INSTRUCTION SHEET REV. NO. : 01 DEPTT.:CSE REV. DATE : 01/06/2013 LABORATORY: Compiler Design Lab(ECS-653) PAGE: 29 SEMESTER: VI
EXPERIMENT NO-09 //Implementation of three address code using Quadruples, Triples, Indirect Triples.
#include<stdio.h> #include<conio.h> #include<string.h> #include<iostream.h> void main() { clrscr(); char str[20],a[10],ch; int l,k=0,c=0,b[10],count=0,pos,ch1,eq=0,ind1=14; printf("\n1.Quadruple"); printf("\n2.Triple"); printf("\n3.Indirect Triple"); printf("\n\nEnter your choice----"); scanf("%d",&ch1);
SUBMITTED BY: ROMA
printf("\n\nEnter the string----"); scanf("%s",&str); l=strlen(str); str[l]=NULL; //int sou=0; if(str[0]=='-') { a[k]=str[0]; b[k]=0; k++; count++; c=c+1; }
for(int x=c;x<l;x++) { if(str[x]=='-' && (str[x-1]=='+' || str[x-1]=='*' || str[x-1]=='/' || str[x1]=='=')) { a[k]=str[x]; b[k]=x; count++;
SUBMITTED BY: ROMA
k++; } }
for(int j=c;j<l;j++) { if(str[j]=='+' || (str[j]=='-' && str[j-1]!='+' && str[j-1]!='*' && str[j-1]!='/' && str[j-1]!='=')) { a[k]=str[j]; b[k]=j; k++; }
SUBMITTED BY: ROMA
while(co<count) {
SUBMITTED BY: ROMA
ch=a[co]; pos=b[co]; if(ch=='-' && !((int)str[pos-1]>=97 && (int)str[pos-1]<=122)) { printf("%d\tU%c\t%c\t \tT%d\n",eq,ch,str[pos+1],ind); str[pos]=ind; str[pos+1]=ind; ind++; co++; eq++; } }
int co1=count; while(co1<k) { ch=a[co1]; pos=b[co1]; if(ch!='=') { if( ((int)str[pos-1]>=0 && (int)str[pos-1]<=30) && ! ( (int)str[pos+1]>=0 && (int)str[pos+1]<=30 )) printf("%d\t%c\tT%d\t%c\tT%d\n",eq,ch,(int)str[pos1],str[pos+1],ind);
SUBMITTED BY: ROMA
if( ((int)str[pos+1]>=0 && (int)str[pos+1]<=30) && ! ( (int)str[pos-1]>=0 && (int)str[pos-1]<=30 )) printf("%d\t%c\t%c\tT%d\tT%d\n",eq,ch,str[pos-1], (int)str[pos+1],ind); if( ((int)str[pos-1]>=0 && (int)str[pos-1]<=30) && ( (int)str[pos+1]>=0 && (int)str[pos+1]<=30 )) printf("%d\t%c\tT%d\tT%d\tT%d\n",eq,ch,(int)str[pos-1], (int)str[pos+1],ind); if( !((int)str[pos-1]>=0 && (int)str[pos-1]<=30) && ! ( (int)str[pos+1]>=0 && (int)str[pos+1]<=30 )) printf("%d\t%c\t%c\t%c\tT%d\n",eq,ch,str[pos1],str[pos+1],ind); str[pos]=ind; int m1,m2; m1=pos+1; m2=pos-1; while(m1<l) { if(str[m1]=='+' || str[m1]=='-' || str[m1]=='/' || str[m1]=='*' || str[m1]=='=') break; str[m1]=ind; m1++; } while(m2>=0) {
SUBMITTED BY: ROMA
if(str[m2]=='+'|| str[m2]=='-' || str[m2]=='*' || str[m2]=='/' || str[m2]=='=') break; str[m2]=ind; m2--; } } if(ch=='=') printf("%d\t%c\tT%d\t \t%c",eq,ch,(int)str[pos+1],str[pos-1]); ind++; co1++; eq++; } break;
if(ch=='-' && !((int)str[pos-1]>=97 && (int)str[pos-1]<=122)) { printf("%c\t%c\t \n",ch,str[pos+1]); str[pos]=ind-1; str[pos+1]=ind-1; ind++; co++; eq++; } } co1=count; while(co1<k) { ch=a[co1]; pos=b[co1]; if(ch!='=') { if( ((int)str[pos-1]>=0 && (int)str[pos-1]<=30) && ! ( (int)str[pos+1]>=0 && (int)str[pos+1]<=30 )) printf("%c\t%d\t%c\n",ch,(int)str[pos-1],str[pos+1]); if( ((int)str[pos+1]>=0 && (int)str[pos+1]<=30) && ! ( (int)str[pos-1]>=0 && (int)str[pos-1]<=30 )) printf("%c\t%c\t%d\n",ch,str[pos-1],(int)str[pos+1]);
if( ((int)str[pos-1]>=0 && (int)str[pos-1]<=30) && ( (int)str[pos+1]>=0 && (int)str[pos+1]<=30 )) printf("%c\t%d\t%d\n",ch,(int)str[pos-1],(int)str[pos+1]); if( !((int)str[pos-1]>=0 && (int)str[pos-1]<=30) && ! ( (int)str[pos+1]>=0 && (int)str[pos+1]<=30 )) printf("%c\t%c\t%c\n",ch,str[pos-1],str[pos+1]); str[pos]=ind-1; int m1,m2; m1=pos+1; m2=pos-1; while(m1<l) { if(str[m1]=='+' || str[m1]=='-' || str[m1]=='/' || str[m1]=='*' || str[m1]=='=') break; str[m1]=ind-1; m1++; } while(m2>=0) { if(str[m2]=='+'|| str[m2]=='-' || str[m2]=='*' || str[m2]=='/' || str[m2]=='=') break; str[m2]=ind-1; m2--;
SUBMITTED BY: ROMA
} } if(ch=='=') printf("%c\t%c\t%d",ch,str[pos-1],(int)str[pos+1]); ind++; co1++; eq++; } break; case 3:co=0; printf("\n\nOp\tArg1\tArg2\n\n"); while(co<count) { ch=a[co]; pos=b[co]; if(ch=='-' && !((int)str[pos-1]>=97 && (int)str[pos-1]<=122)) { printf("%c\t%c\t \n",ch,str[pos+1]); str[pos]=ind1; str[pos+1]=ind1; ind1++; co++; eq++;
SUBMITTED BY: ROMA
} } co1=count; while(co1<k) { ch=a[co1]; pos=b[co1]; if(ch!='=') { if( ((int)str[pos-1]>=0 && (int)str[pos-1]<=30) && ! ( (int)str[pos+1]>=0 && (int)str[pos+1]<=30 )) printf("%c\t%d\t%c\n",ch,(int)str[pos-1],str[pos+1]); if( ((int)str[pos+1]>=0 && (int)str[pos+1]<=30) && ! ( (int)str[pos-1]>=0 && (int)str[pos-1]<=30 )) printf("%c\t%c\t%d\n",ch,str[pos-1],(int)str[pos+1]); if( ((int)str[pos-1]>=0 && (int)str[pos-1]<=30) && ( (int)str[pos+1]>=0 && (int)str[pos+1]<=30 )) printf("%c\t%d\t%d\n",ch,(int)str[pos-1],(int)str[pos+1]); if( !((int)str[pos-1]>=0 && (int)str[pos-1]<=30) && ! ( (int)str[pos+1]>=0 && (int)str[pos+1]<=30 )) printf("%c\t%c\t%c\n",ch,str[pos-1],str[pos+1]); str[pos]=ind1; int m1,m2; m1=pos+1; m2=pos-1;
SUBMITTED BY: ROMA
while(m1<l) { if(str[m1]=='+' || str[m1]=='-' || str[m1]=='/' || str[m1]=='*' || str[m1]=='=') break; str[m1]=ind1; m1++; } while(m2>=0) { if(str[m2]=='+'|| str[m2]=='-' || str[m2]=='*' || str[m2]=='/' || str[m2]=='=') break; str[m2]=ind1; m2--; }} if(ch=='=') printf("%c\t%c\t%d",ch,str[pos-1],(int)str[pos+1]); ind1++; co1++; eq++;} break; }
R.D.FOUNDATION GROUP OF LABORATORY INSTITUTION,FACULTY OF MANUAL ENGINEERING PRACTICAL INSTRUCTION SHEET REV. NO. : 01 DEPTT.:CSE REV. DATE : 01/06/2013 LABORATORY: Compiler Design Lab(ECS-653) PAGE: 41 SEMESTER: VI
EXPERIMENT NO-10
OBJECTIVE: WRITE A PROGRAM IN C TO CHECK A REAL NUMBER.
#include<stdio.h> #include<ctype.h> void main() { char ch; ch=getchar(); if(ch==+|| ch==-) ch=getchar(); while(isDigit(ch)) ch=getchar(); if(ch==.) ch=getchar(); else error(); while(isDigit(ch)) ch=getchar(); printf(OK\h); }