You are on page 1of 48

NARAYANA ENGINEERING COLLEGE::NELLORE CSE DEPARTMENT III-B.

TECH II-SEM

LANGUAGE PROCESSORS LAB

NARAYANA ENGINEERING COLLEGE::NELLORE DEPARTMENT : CSE

LANGUAGE PROCESSORS LAB


III-B.TECH II-SEM (2007-08)
1. Develop a lexical analyzer to recognize a few patterns in PASCL, C and FORTRAN (EX: identifiers, Constants, comments, operators, etc) 2. Write a program to parse using brute force technique of top down parsing 3. Develop on LL(1) parser (construct parse table also). 4. Develop an operator precedence parsed (construct parse table also) 5. Develop a recursive descent parser 6. Write a program for generating for various intermediate code forms i) Three address code ii) Polish notation

7. Write a program to simulate heap storage allocation strategy 8. Generate Lexical analyzer using LEX. 9.Generate YACC specification for a few syntactic categories. 10.Given any intermediate code form implement code optimization techniques.

PROGRAM ---1

/ ************************************************************** ********

C-program to implement Lexical Analyzer to recognize keywords, Data types, identifiers, numbers, parenthesis, symbols, and operators....
************************************************************** ********/

Algorithm: 1.start. 2.Intialize the symbol table with keywords. 3.Read token by token from the input string. 4.Using finite automation check for keywords, identifiers, constants & then Operators successively. 5.If nothing matches print an error message. 6.Until all tokens are over, repeat above three steps. 7.Print token information. 8.Stop.

Aim:- C-program to implement Lexical Analyzer to recognize keywords, Data types, identifiers, numbers, parenthesis, symbols, and operators....

#include<stdio.h> #include<conio.h> #include<ctype.h> #include<stdlib.h> #include<conio.h> void main() { char ipstr[300],temp,tmp[15],fname[50]; char parn[6]={'(',')','{','}','[',']'}; char symb[10]={'.',',',':',';','<','>','?','$','#'}; char ops[6]={'+','-','=','/','*','^'}; char keywd[8][10]={"main","if","else","switch","void","do","while","for"}; char daty[6][10]={"int","char","float","double","string","longint"}; int pos=0,i,j,k,flagi,ip; FILE *fid; clrscr(); /* Input is taken from fname file*/ printf("enter filename which is to be parsed\n"); scanf("%s",fname); fid=fopen(fname,"r"); while((ip=getc(fid))!=EOF) ipstr[pos++]=ip; ipstr[pos]='\0'; printf("%s",ipstr); printf("\t\tlexicanalysis\n________________________________________\n"); for(i=0;ipstr[i]!='\0';i++) { if((ipstr[i]!=' ')&&(ipstr[i]!='\0')&&(ipstr[i]!='@')&&(ipstr[i]!='\n')) { temp=ipstr[i]; /* Check for parenthesis*/ for(j=0;j<6;j++) { if(temp==parn[j]) printf("\n%c \t paranthesis",temp); } /*Check for symbols*/ for(j=0;j<10;j++) { if(temp==symb[j]) printf("\n%c \t symbol",temp); }

/* Check for operators*/ for(j=0;j<6;j++) { if(temp==ops[j]) printf("\n%c \t operator",temp); } /* Check for digits*/ if(isdigit(ipstr[i])) { k=0; while((isdigit(ipstr[i]))||ipstr[i]=='.') tmp[k++]=ipstr[i++]; tmp[k]='\0';i--; printf("\n%s \t number",tmp); } /* Check for alphabets (keywords, data types, identifiers) */ if(isalpha(ipstr[i])) { k=0; while(isalpha(ipstr[i])) tmp[k++]=ipstr[i++]; tmp[k]='\0';i--; flagi=1; for(j=0;j<8;j++) { if((strcmp(tmp,keywd[j]))==0) { printf("\n %s keyword",tmp);flagi=0;break;} } for(j=0;j<6;j++) { if((strcmp(tmp,daty[j]))==0) { printf("\n %s DataType",tmp);flagi=0;break;} } if(flagi==1) printf("\n %s \t Identifiers",tmp); }/*Alphabets loop closing*/ }/*If loop closing*/ }/* Main for loop closing*/ getch(); }

Output:Enter file name to be parsed: void min() {a=b-c; 234=200+34;} Lexical analysis void -----keyword min -----Identifiers ( -----Parenthesis ) -----Parenthesis { -----parenthesis a -----Identifiers = -----Operator b -----Identifiers -----Operator c -----Identifiers ; -----Symbol 234 -----Number = -----operator 200 -----number + -----Operator 34 -----number ; -----Symbol } -----Parenthesis

d:/padmaja/lp/ip.txt

PROGRAM --- 2

/ ************************************************************** ********

Write a C-program to delete comment lines from a file and print with line numbers
************************************************************** ********/

Algorithm: Start Read file name Copy entire file content in to rr.txt file by removing comment lines Read the contents of file rr.txt and print them. If new line comes then first print the line number. Stop.

Aim:- Write a C-program to delete comment lines from a file and print with line numbers #include<stdio.h> #include<conio.h> main() { int ln_no=1; char fname[20],ch1,ch2,ch; FILE *fp1,*fp2,*fp; clrscr(); printf("Enter filename with path"); gets(fname); fp=fopen(fname,"r"); fp1=fopen("rr.txt","w"); while((ch1=fgetc(fp))!=EOF) { if(ch1=='/') { if((ch2=fgetc(fp))=='*') { while((ch1!='*')&&(ch2=fgetc(fp))!='/') {ch1=ch2;ch2=fgetc(fp);} } else { fputc(ch1,fp1); fputc(ch2,fp1); } } else fputc(ch1,fp1); } fclose(fp); fclose(fp1); getch(); /* Reading data from file rr.txt which contains xxx.txt file data by removing the comment lines

read contents from file rr.txt and print them with line numbers*/ fp2=fopen("rr.txt","r"); printf("%d",ln_no); while((ch=fgetc(fp2))!=EOF) { if(ch=='\n') printf("\n%d ",++ln_no); else printf("%c",ch); } fclose(fp2); getch(); }

Output:Enter filename with path d:/padmaja/lp/ip.txt

1/program for checking comment lines/ 2 3 void min() 4 { a=b-c; 5 6 234=200+34; 7}

PROGRAM ---3

/ ************************************************************** ********

C-program to generate intermediate codes a) Polish notation b) Reverse polish notation c) Three address code generation
************************************************************** ********/

Program 3 a) To convert a infix expression into reverse polish (postfix) expression. ALGORITHM:1. Start 2. Read the infix expression from keyboard. 3. Assume that the infix expression you have read is stored in the character Array infix. 4. Push # in to stack. 5. For each character in infix array perform the following actions Begin

if character is digit or alphabet then enter the same in to postfix array(resultant array) . if character is open braces then , push that into the stack if character is closed braces then, pop the stack top elements until top of the stack contains open braces. if character contains any arithmetic operators, Begin

if top of stack element is equal to # then, push the operator into stack. Else if the top of stack element priority is less than the priority of the new character then push the character in to stack. Else pop the elements from stack until the priority of the top of the stack element is less than the character priority that is to be inserted. End End 6. If no more characters are presented in infix expression then pop elements from Stack until top of stack element is # and push that element in to postfix array.

Push Function: 1. Start 2. If top of the stack is reached max size of the stack then, the stack is full. 3.Else Insert the element into stack 4. End

Pop Function:

1. start 2. if top of the stack is null then, the stack is empty

3.else Delete the element from the stack positioned by the top 4. End Aim:- Generating intermediate code in reverse polish notation. # include<stdio.h> #include<conio.h> #include<ctype.h> #include<math.h> int top=-1; char stack[20]; main() { char infix[20]; clrscr(); printf("enter the infix expression"); /* Read infix expression */ gets(infix); /* Convert infix to reverse polish notation */ intopo(infix); getch(); } intopo(char infix[20]) { char postfix[20],s,t; int index=0,pos=0,l; l=strlen(infix); /* L string length*/ push('#'); /* Read one by one character */ while(index<l) { s=infix[index]; switch(s) { case '(': push(s); break; case ')':t=pop(); while(t!='(') { postfix[pos++]=t;

t=pop(); } break; case'+': case'-': case'*': case'/': case'^':while(pre(stack[top])>=pre(s)) { t=pop(); postfix[pos++]=t; } push(s); break; default: postfix[pos++]=s; break; } index++; } while(top>0) { t=pop(); postfix[pos++]=t; } postfix[pos++]='\0'; puts(postfix); } push(char c) { top++; stack[top]=c; } pop() { char c; c=stack[top]; top--; return(c); }

pre(char c ) { if(c=='^') return(5); else if(c=='/'|| c=='*') return (4); else if( c=='+'|| c== '-') return(3); else return(2); }

OUTPUT :1) Enter the infix expression a+b*c/(d-e^f) The equivalent reverse polish notation is. abc*def^-/+ 2) Enter the infix expression a*b^c The equivalent reverse polish notation is. abc^*

Program 3 b) To convert a infix expression into polish (prefix) expression. ALGORITHM:1. Start 2. Read the infix expression from keyboard. 3. Assume that the infix expression you have read is stored in the character Array infix. 6. Push # in to stack. 7. For each character in infix array perform the following actions read characters from last Begin

if character is digit or alphabet then enter the same in to postfix array(resultant array) . if character is closed braces then , push that into the stack if character is open braces then, pop the stack top elements until top of the stack contains open braces. if character contains any arithmetic operators, Begin if top of stack element is equal to # then, push the operator into stack. Else if the top of stack element priority is greater than or equal to the priority of the new character then push the character in to stack.

Else pop the elements from stack until the priority of the top of the stack element is less than the character priority that is to be inserted. End End 6. If no more characters are presented in infix expression then pop elements from Stack until top of stack element is # and push that element in to postfix array. 7. Finally reverse the resultant array then that is the polish notation for the infix expression. Push Function: 1. Start 2. If top of the stack is reached max size of the stack then, the stack is full. 3.Else Insert the element into stack 4. End

Pop Function:

1. start 2. if top of the stack is null then, the stack is empty 3.else Delete the element from the stack positioned by the top 4. End

Program 3 b) To convert a infix expression into polish (prefix) expression. /* INFIX TO PREFIX */ # include<stdio.h> #include<conio.h> #include<ctype.h> #include<math.h> int top=-1; char stack[20]; main() { char infix[20]; clrscr(); printf("enter the infix expression"); /* Read infix expression */ gets(infix); /* Convert infix to polish notation */ intopo(infix); getch(); } intopo(char infix[20]) { char prefix[20],s,t; int index,pos=0,l,i,len; l=strlen(infix); index=l-1; push('#'); while(index>=0) { s=infix[index]; switch(s) {

case ')': push(s); break; case '(':t=pop(); while(t!=')') { prefix[pos++]=t; t=pop(); } break; case'+': case'-': case'*': case'/': case'^':while(pre(stack[top])<pre(s)) { t=pop(); prefix[pos++]=t; } push(s); break; default: prefix[pos++]=s; break; } index--; } while(top>0) { t=pop(); prefix[pos++]=t; } prefix[pos++]='\0'; len=strlen(prefix)-1; for(i=len;i>=0;i--) printf("%c",prefix[i]); } push(char c) { top++; stack[top]=c; }

pop() { char c; c=stack[top]; top--; return(c); } pre(char c ) { if(c=='^') return(5); else if(c=='/'|| c=='*') return (4); else if( c=='+'|| c== '-') return(3); else return(6); }

OUTPUT :1) Enter the infix expression a+b*c/(d^e+f) The equivalent reverse polish notation is. /*+abc^d+ef 2) Enter the infix expression a*b^c The equivalent reverse polish notation is.^*abc

Program 3 c) To generate Three Address code. ALGORITHM:1. Start 2. Read the infix expression from keyboard. 3. Assume that the input expression you have read is stored in the character Array infix. 8. Push # in to stack. 9. For each character in infix array perform the following actions Begin

if character is digit or alphabet then enter the same in to postfix array(resultant array) . if character is open braces then , push that into the stack if character is closed braces then, pop the stack top elements until top of the stack contains open braces. if character contains any arithmetic operators, Begin if top of stack element is equal to # then, push the operator into stack. Else if the top of stack element priority is less than the priority of the new character then push the character in to stack.

Else pop the elements from stack until the priority of the top of the stack element is less than the character priority that is to be inserted. End End 6. If no more characters are presented in infix expression then pop elements from Stack until top of stack element is # and push that element in to postfix array. 7. For each character in postfix array do the following operation. Begin If the character is alphabet push character in to stack. If the character is operator then pop two characters from stack and store those in op1 and op2 variables. begin Print those in the format of Ti=op1 character op2 Increment I for generating temporary variables. Push the new temporary variable Ti in to stack. End End. Push Function: 1. Start 2. If top of the stack is reached max size of the stack then, the stack is full. 3.Else Insert the element into stack 4. End

Pop Function: 1. start 2. if top of the stack is null then, the stack is empty 3.else Delete the element from the stack positioned by the top 4. End Aim:- Generating intermediate code in reverse polish notation. /* INFIX TO POSTFIX */ # include<stdio.h> #include<conio.h> #include<ctype.h> #include<math.h> int top=-1; char stack[20],postfix[20],infix[20],ipst[20]; main() { int i; clrscr(); printf("Enter infix expression\n in the form of assignment stmt.like var=exp..."); gets(ipst); for(i=2;i<strlen(ipst);i++) infix[i-2]=ipst[i]; infix[i-2]='\0'; puts(infix); intopo(); getch(); evalua(); getch(); } intopo() { char s,t; int index=0,pos=0,l; l=strlen(infix); push('#');

while(index<l) { s=infix[index]; switch(s) { case '(': push(s); break; case ')':t=pop(); while(t!='(') { postfix[pos++]=t; t=pop(); } break; case'+': case'-': case'*': case'/': case'^':while(pre(stack[top])>=pre(s)) { t=pop(); postfix[pos++]=t; } push(s); break; default: postfix[pos++]=s; break; } index++; } while(top>0) { t=pop(); postfix[pos++]=t; } postfix[pos++]='\0'; } push(char c) { top++; stack[top]=c;

} pop() { char c; c=stack[top]; top--; return(c); } pre(char c ) { if(c=='^') return(5); else if(c=='/'|| c=='*') return (4); else if( c=='+'|| c== '-') return(3); else return(2); } evalua() { char res[20],ch,ch1,ch2; int i,t=1; for(i=0;i<strlen(postfix);i++) { ch=postfix[i]; if(isalnum(ch)) push(ch); else { ch1=pop(); ch2=pop(); printf("\n T%d=",t); if(ch2=='1'||ch2=='2'||ch2=='3'||ch2=='4'||ch==5||ch==6|| ch=7||ch=8||ch=9) printf("T%c",ch2); else printf("%c",ch2); printf("%c",ch);

if(ch1=='1'||ch1=='2'||ch1=='3'||ch1=='4'||ch==5||ch==6|| ch=7||ch=8||ch=9) printf("T%c",ch1); else printf("%c",ch1); switch(t) { case 1:ch='1'; break; case 2:ch='2'; break; case 3:ch='3'; break; case 4:ch='4'; break; case 5:ch='5'; break; case 6:ch='6'; break; case 7:ch='7'; break; case 8:ch='8'; break; case 9:ch='9'; break; } push(ch); t++; } }/* for loop closing*/ ch=pop(); printf("\n %c=T%c",ipst[0],ch); }

OUTPUT :-

1) Enter the infix expression In the form of assignment statement like var=exp...R=(a+b*c)-(d^e+f) T1=b*c T2=a+T1 T3=d^e T4=T3+f T5=T2-T4 R=T5

2) Enter the infix expression in the form of assignment statement like var=exp...R=(a^b*c)-(d-e) T1=a^b T2=T1*c T3=d-e T4=T2-T3 R=T4

PROGRAM --- 4

/ ************************************************************** ********

Write a C-program to implement BRUTE FORCE Method of parses(with backtracking)


**************************************************************** ******/

ALGORITHM:Arithmetic: procedure S() begin if input symbol ='a' then begin advance the input pointer; if B() then begin if input symbol = 'c' begin advance the input pointer; if input pointer character is end of string then return true; else return false; end; else

return false; end; else return false end; end;

procedure a() begin binpt :=inputpointer; if inputsymbol ='c' then begin advance the input pointer; binpt1:=inputpointer; if inputsymbol='d' then begin advance the input pointer; return true; end; else begin inputpointer:=binpt1; if inputsymbol='e' then begin advance the input pointer; return true; end; else return false; end; Input pointer :=binpt; if inputsymbol =b' then begin advance the input pointer; return true; end; else return false; end;

begin

/*main procedure */

read the terminal string if S() then print "string in passed" else print "invalid string" end; /* Program to develop recursive decent parsing with backtracking*/ #include<stdio.h> #include<conio.h> char *inpt; /* pointer to the input*/ main() { char ipst[20]; int len; clrscr(); printf("The productions are.....\n s->aBc \n B->b/cd/ce\n"); printf("Enter the string to be parsed \t "); gets(ipst); inpt=ipst; if(S()) printf("****String is Parsed***\n"); else printf("****Invalid String******\n"); getch(); } /* closing of main*/ S() { if(*inpt=='a') { inpt++; if(B()) { if(*inpt=='c') {

inpt++; if(*inpt=='\0') return(1); else return(0); } else return(0); } else return(0); } }/* closing of s procedure*/ B() { char *binpt,*binpt1; binpt=inpt; if(*inpt=='c') { inpt++; binpt1=inpt; if(*inpt=='d') { inpt++; return(1); } else { inpt=binpt1; if(*inpt=='e') { inpt++; return(1); } else return(0); } } else { inpt=binpt; if(*inpt=='b')

{ inpt++; return(1); } else return(0); } }/* Closing of B procedure */

OUTPUT :-

1 ) The productions are..... s->aBc B->b/cd/ce Enter the string to be parsed abc

****string is parsed***

2 ) The productions are..... s->aBc B->b/cd/ce Enter the string to be parsed acdc

****string is parsed*** 3 ) The productions are..... s->aBc B->b/cd/ce Enter the string to be parsed acec

****string is parsed***

4 ) The productions are..... s->aBc B->b/cd/ce Enter the string to be parsed acecd

****Invaid string *** PROGRAM --- 5

/ ************************************************************** ********

Write a C-program to implement Recursive Decent parsing with out backtracking)


**************************************************************** ******/

ALGORITHM:procedure e() begin T(); EPRIME(); end procedure EPRIME() begin if inputsymbol='+' then begin advance the input pointer; T(); EPRIME(); end; end; procedure T() begin

F(); TPRIME(); end; procedure T() begin F(); TPRIME(); end; procedure TPRIME() begin if inputsymbol='*' then begin advance the input pointer; F(); TPRIME(); end; end; procedure f() begin if inputsymbol = 'id' then advance the input pointer; else if inputsymbol='l' then begin advance the input pointer; E(); if inputsymbol =')' then advance the input pointer; else error(); end; else error(); end; begin /*main program */ read any terminal string E(); end;

/* Program to develop recursive decent parsing with backtracking*/ #include<stdio.h> char *a; main() { char s[10]; clrscr(); printf("ENTER THE STRING"); gets(s); a=s; if(E()) printf("STRING IS PARSED"); else printf(invalid string"); getch(); } int E() { T(); EPRIME(); } int EPRIME() { if(*a=='+'' ) { a++;; T(); EPRIME(); }

} int T() { F(); TPRIME(); } int TPRIME() { if(*a=='*') { a++;; F(); TPRIME(); } } int F() { if(*a=='i') a++ ; else if(*a==')') { a++;; E(); if(*a==')') a++ ; else error(); } else error(); } int error() { return(0); } input:-ENTER THE STRING :(i+i) output:-STRING IS PARSED

PROGRAM ---6
/ ************************************************************** ******** ************************************************************** ********/

C-program to implement Heap storage Allocation

ALGORITHM:To implement dynamic memory allocation and deal location we are Implementing heap storage allocation 1. Start 2. Assume memory size as 50 bytes 3. Allocation:-- Read variable name and size. If continuous available free space is greater than or equal to variable size then allocate. And make those bits in allocation array as 1. If space not sufficient then allocation is not possible. 4. Deal location:-- Read variable name Compare variable name with variable name presented in heap allocated array. If match is found free the memory space allocated for that variable.

Make valid bit as zero in heap array for that variable. Allocation bits in array make as 0 for that particular variable memory. 5. Deal located memory space will be utilized when new variable requires Memory.
6. Stop.

Aim:- To implement dynamic memory allocation and deal location we are Implementing heap storage allocation #include<stdio.h> #include<conio.h> #include<string.h> struct heaps { char var[10]; int no_byte; int st_byte; int valid; }heaparr[20]; int alloc[50],harpos=0; void main() { int ch,i; /* if memory allocated then alloc[pos]=1 else 0*/ for(i=0;i<50;i++) alloc[i]=0; while(1) { clrscr(); printf("\n enter u r choice\n"); scanf("%d",&ch); switch(ch) { case 1:allocat(); break; case 2:deallocat(); break;

disp(); break; case 4: exit(0); getch(); }/*Closing of switch*/ }/* Closing of while*/ }/* Closing of main*/

case 3:

allocat() { char vname[10],s; int vsize,i,count,st,k; printf("\n enter variable name:"); scanf("%s",&vname); printf("\n enter how many bytes required for %s",vname); scanf("%d",&vsize); for(i=0;i<50;i++) { count=0; while((alloc[i]==1)&&(i<50)) i++; st=i; while(count<vsize) { if(alloc[i]==0) count++; else break; i++; } if(count==vsize) { strcpy(heaparr[harpos].var,vname); heaparr[harpos].valid=1; heaparr[harpos].no_byte=vsize; heaparr[harpos].st_byte=st; for(k=st;k<st+vsize;k++) alloc[k]=1; harpos++; break;

} }/*Closing of for loop*/ if(i>50) printf("\n Allocation is not possible\n"); else printf("\n Allocated succesfully\n"); }/*Closing of allocation*/ disp() { int i; for(i=0;i<harpos;i++) { if(heaparr[i].valid==1) { printf("\n Name of variable %s",heaparr[i].var); printf("\n starting byte %d",heaparr[i].st_byte); printf("\n number of bytes%d",heaparr[i].no_byte); } } }/* Closing of display*/

deallocat() { int i; char vnam[10]; int sta_byte,len_byte,end_byte; printf("variables presented are...\t"); for(i=0;i<harpos;i++) printf("%s",heaparr[i].var); printf("ENTER VARIABLE NAME WHICH IS TO BE DEALLOCATED\t"); scanf("%s",vnam); for(i=0;i<harpos;i++) { if(heaparr[i].valid==1) {

if(strcmp(vnam,heaparr[i].var)==0) break; } } if(i<harpos) { heaparr[i].valid=0; sta_byte=heaparr[i].st_byte; len_byte=heaparr[i].no_byte; end_byte=sta_byte+len_byte; for(i=sta_byte;i<end_byte;i++) alloc[i]=0; } }/* Closing of deal location*/

OUTPUT :-

enter u r choice 1 enter variable name: a enter how many bytes required for a Allocated successfully Enter u r choice 1 Enter variable name: b Enter how many bytes required for b Allocated successfully Enter u r choice 1 Enter variable name:c Enter how many bytes required for c Allocation is not possible Enter u r choice 3

10

20

30

Name of variable a starting byte 0 number of bytes10 bytes allocated are Name of variable b starting byte 10 number of bytes20 bytes allocated are

0..9

10..29

Enter u r choice 1 Enter variable name:c

Enter how many bytes required for c Allocated successfully Enter u r choice 3 Name of variable a Starting byte 0 Number of bytes10 Bytes allocated are Name of variable b Starting byte 10 Number of bytes20 Bytes allocated are Name of variable c Starting byte 30 Number of bytes10 Bytes allocated are

10

0..9

10..29

30..39

Enter u r choice 2 variables presented are... a b c ENTER VARIABLE NAME, WHICH IS TO BE DEALLOCATED Deal located successfully Enter u r choice 3 Name of variable a Starting byte 0 Number of bytes10 Bytes allocated are Name of variable c Starting byte 30 Number of bytes10 Bytes allocated are Enter u r choice 4.

0..9

30..39

#include<stdio.h> #include<ctype.h> int top=-1; char stack[20],inpst[20]; main() { clrscr(); printf("enter the string that is to be parsed...\t"); gets(inpst); strcat(inpst,"$"); /*puts(inpst);*/ constructop(); opprec(); getch(); } constructop() { int len,i,j=0,oplen; char op[20],prect[20][20]; len=strlen(inpst); for(i=0;i<len;i++) { if(!present(inpst[i],op)) op[j++]=inpst[i]; } op[j]='\0'; printf("THE OPERATOR PRECEDANCE PARSER IS.....\n\t"); for(i=0;i<strlen(op);i++) printf("%c\t",op[i]); oplen=strlen(op)-1; for(i=0;i<=oplen;i++) { printf("\n%c",op[i]); for(j=0;j<=oplen;j++) { if(((i==0)&&(j==0))||((i==oplen)&&(j==oplen))) prect[i][j]=' '; else if(prec(op[i])>prec(op[j])) prect[i][j]='>'; else if(prec(op[i])<prec(op[j])) prect[i][j]='<'; else if(op[i]==op[j])

prect[i][j]='>'; else prect[i][j]='='; printf("\t%c",prect[i][j]); } printf("\n"); } }

present(char ch,char opt[20]) { int i; for(i=0;i<strlen(opt);i++) { if(ch==opt[i]) return(1); } return(0);

opprec() { char ch,ch2,s1[20],tost; int len,i,le; len=strlen(inpst); push('$'); tost='$'; for(i=0;i<len;i++) { ch=inpst[i]; if((tost=='$')&&(ch=='$')) break; if(prec(tost)<prec(ch)) { tost=ch; push(ch);} else { strcpy(s1,"\0"); le=strlen(s1); while(prec(tost)>prec(ch)) { ch=pop(); s1[le++]=ch; while((isalpha(stack[top]))&&(stack[top]!='i')) { s1[le++]=pop(); } tost=stack[top]; } s1[le]='\0'; tost=stack[top]; if(strcmp(s1,"i")==0) push('E'); if(strcmp(s1,"E+E")==0) push('E'); if(strcmp(s1,"E*E")==0) push('E'); if(strcmp(s1,"E-E")==0) push('E'); i--;

}/*closing of ELSE*/ }/*closing of for loop*/ stack[++top]='\0'; if((strcmp(stack,"$E")==0)&&(ch=='$')) printf("string acepted"); else printf("string is not parsed \n"); }/*closing of MAIN*/ push(char c) { top++; stack[top]=c; } pop() { char c; c=stack[top]; top--; return(c); } int prec(char c) { if(c=='i') return(5); else if(c=='^') return(4); else if(c=='/'|| c=='*') return (3); else if( c=='+'|| c== '-') return(2); else if(c=='$') return(1); else return(0); }

You might also like