R.D.Foundation Group of Institution Faculty of Engineering Department of Computer Science & Engineering

You might also like

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

R.D.

FOUNDATION GROUP OF INSTITUTION FACULTY OF ENGINEERING Department of Computer Science & Engineering

Compiler Design Lab


SUBMITTED TO: SACHIN GOEL A.P.(CSE) SUBMITTED BY: ROMA

R.D.FOUNDATION GROUP OF INSTITUTION FACULTY OF ENGINEERING N.H.58,MODINAGAR,GHAZIABAD


SUBMITTED BY: ROMA

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

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: 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.

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: 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

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: 4 SEMESTER: VI

//Program to check a string under a given grammar


#include<stdio.h> #include<conio.h> #include<string.h> void main() { char string[50]; int count=0; int flag; clrscr(); printf("the grammer is s->as,s->sb,s->ab\n"); printf("enter the string to be checked\n"); gets(string); if(string[0]=='a') { flag=0; for(count=1;string[count-1]!='\0';count++) {
SUBMITTED BY: ROMA

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(); }

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: 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.

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: 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.

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: 8 SEMESTER: VI

//Program to check for keywords in a given string.


#include<stdio.h> #include<conio.h> void main() { char a[20]; char d[20]; char f[20][20]={"do","while"}; int t[20]; int i=0; int j=0,flag=1,k=0; clrscr(); for(i=0;i<2;i++) t[i]=0; printf("enter the string"); gets(a); for(i=0;i<strlen(a);i++) {
SUBMITTED BY: ROMA

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");

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: 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.

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: 10 SEMESTER: VI

//Program to check for identifier in a given string.


#include<stdio.h> #include<conio.h> #include<string.h> void main() { char s[10][10] = {"if","else","for","do","int"}; char a[50],b[10][10],c[50]; int m,n=0,g,i=0,k,l=0,x=0,y; printf("\n\tEnter the String----\t"); gets(c); m=strlen(c); for(k=0; k<m; k++) a[k]=c[k]; a[m]=' '; len = strlen(b); for( i < len )
SUBMITTED BY: ROMA

{ 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

x++; } if(count== 0) printf("\n\tNo Identifiers."); else printf("\t\n%d Identifier."); 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: 13 SEMESTER: VI

EXPERIMENT NO-04 //Program to check for constant in a given string


#include<stdio.h> #include<conio.h> #include<string.h> void main() { char s[50],temp[10]; int con=0,i=0,j=0,k; clrscr(); printf("enter the string\n"); gets(s); printf("\n"); for(i=0;i<=strlen(s);i++) { if(s[i]==' ' || s[i]=='\0') { temp[j]='\0';
SUBMITTED BY: ROMA

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(); }

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: 15 SEMESTER: VI

Experiment no-05 Objective:


WRITE A PROGRAM IN C FOR INDENTIFICATION OF OPERATOR 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 character such as identifiers, operator and keywords. Source program is a stream of characters: pos= init +rate*60 Lexical analysis groups character into non-separatable units, called token, and generates token stream: Id1=id2+id3*const The information about the identifiers must be stored in symbol the Symbol Table.

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: 16 SEMESTER: VI

//Program to check for relational operator in a given string.


#include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[50]; int i,l,k,c=0; clrscr(); printf("\n\tEnter the expression :-\t"); gets(a); l=strlen(a); for(i=0;i<l;i++) { if(a[i]>=60 && a[i]<=62||a[i]==33) { if(a[i+1]==61&&a[i]!=61) { printf("\n%c%c",a[i],a[i+1]); i++; c++; } else { if(((a[i]==60||a[i]==62)&&(a[i+1]==60||a[i+1]==62))|| a[i]==61&&a[I +1]==61) { printf(" "); i++; } else
SUBMITTED BY: ROMA

{ 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(); }

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: 18 SEMESTER: VI

EXPERIMENT NO-06 OBJECTIVE:


WRITE A PROGRAM IN C FOR LEXICAL ANALYZER.

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

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: 20 SEMESTER: VI

//Program for Lexical Analyzer


#include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[50]; int i,l,k,c=0; clrscr(); printf("\n\tEnter the expression :-\t"); gets(a); l=strlen(a); for(i=0;i<l;i++) { if(a[i]>=60 && a[i]<=62||a[i]==33) { if(a[i+1]==61&&a[i]!=61) { printf("\n%c%c",a[i],a[i+1]); i++; c++; } else { if(((a[i]==60|| a[i]==62)&&(a[i+1]==60||a[i+1] ==62))||a[i]==61&&a[i+1]==61) { printf(" "); i++; } else
SUBMITTED BY: ROMA

{ } } }

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(); }

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: 22 SEMESTER: VI

EXPERIMENT NO-07 //Program for Stack implementation through Array


#include <stdio.h> #include<ctype.h> # define MAXSIZE 200 int stack[MAXSIZE]; int top; //index pointing to the top of stack void main() { void push(int); int pop(); int will=1,i,num; clrscr(); while(will ==1) { printf(" MAIN MENU: 1.Add element to stack 2.Delete element from the stack "); scanf("%d",&will); switch(will) { case 1: printf("Enter the data... "); scanf("%d",&num); push(num); break;
SUBMITTED BY: ROMA

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(); }

");

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: 24 SEMESTER: VI

EXPERIMENT NO-08 //Implementation of shift reduce parsing using C.


#include<stdio.h> #include<conio.h> #include<iostream.h> #include<string.h> #include<process.h> void main() { clrscr(); int l,c=0,pos,k=0,nonc,terc,top=1,no; char a[10],ip[10],stk[10],s,nons,ters,b[10]; stk[0]='$'; stk[1]='E'; stk[2]=NULL; char non[5]={'E','e','T','t','F'}; char ter[6]={'i','+','*','(',')','$'};
SUBMITTED BY: ROMA

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 i=c;i<l;i++) { if(str[i]=='*' || str[i]=='/') { a[k]=str[i]; b[k]=i; 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

for(int p=c;p<l;p++) { if(str[p]=='=') { a[k]=str[p]; b[k]=p; k++; } } a[k]=NULL;

int ind=1,z=1; int len=strlen(a);

switch(ch1) { case 1:int co=0; printf("\n\n\tOp\tArg1\tArg2\tResult\n\n");

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;

case 2:co=0; printf("\n\nOp\tArg1\tArg2\n\n");

while(co<count) { ch=a[co]; pos=b[co];


SUBMITTED BY: ROMA

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]);

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("%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; }

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: 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); }

SUBMITTED BY: ROMA

You might also like