Вы находитесь на странице: 1из 37

PRIST UNIVERSITY VALLAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

COMPILER DESIGN LAB MANUAL

EX: NO: 1

LEXICAL ANALYSER

AIM:
To write a c program to implement lexical analysis

ALGORITHM:
STEP 1: Start the program. STEP 2: Include header files, whatever need in program. STEP 3: Declare the variable. STEP 4: Declare the function. STEP 5: Define value whatever using in variable. STEP 6: Get source program STEP 7: Analysis what are the keyboard using. STEP 8: Print the source program. STEP 9: Stop the program.

PROGRAM: LEXICAL ANALYSER #include<stdio.h> #include<conio.h> #include<ctype.h> #include<string.h> char str[100]; char symboltable[25][25]; char attributetable[25][25]; int firstindex=0; void main() { void tokenseperation(); void printtokens(); int i; clrscr(); printf("\n enter the source program \n"); gets(str); tokenseperation(); printtokens(); getch(); } void tokenseperation() { int i,j,k,l,len; int keyword; char *keywords[]={"if","else","while","void","switch","int","main","case"}; char *operators[]={"<",">","=","+","-","*","/"}; char punctuation[]="{}[];:"; char token[20]; len=strlen(str); i=j=0; while(i<len) { if(isalpha(str[i])) { while(isalpha(str[i])||isdigit(str[i])) { token[j++]=str[i]; i++; } token[i]='\0'; strcpy(symboltable[firstindex],token); keyword=0; for(k=0;k<=0;k++) if(strcmp(keywords[k],token)==0)

{ strcpy(attributetable[firstindex++],"key");keyword=1; break; } if(keyword==0) strcpy(attributetable[firstindex++],"var"); } j=0; if(str[i]=='"') { while(str[++i]!='"') token[j++]=str[i]; token[j]='\0'; strcpy(symboltable[firstindex],token); strcpy(attributetable[firstindex++],"l"); } j=0; if(isdigit(str[i])) { while(isdigit(str[i])||(str[i]=='.')) token[j++]=str[i++]; token[j]='\0'; strcpy(symboltable[firstindex],token); strcpy(attributetable[firstindex++],"c"); } j=0; token[j++]=str[i]; token[j++]='\0'; for(k=0;k<11;k++) { if(strcmp(operators[k],token)==0) { strcpy(symboltable[firstindex],token); strcpy(attributetable[firstindex++],"operator"); break; } } for(k=0;k<12;k++) { if(punctuation[k]==str[i]) { strcpy(symboltable[firstindex],token); strcpy(attributetable[firstindex++],"p"); break; } }

j=0; i++; }} void printtokens() { int i; for(i=0;i<firstindex;i++) printf("\n%s\t%s\n",symboltable[i],attributetable[i]); getch(); } OUTPUT: Enter the source program a+b=c; a + b = c ; var operator var operator var p

EX: NO: 2

SHIFT REDUCE PARSER

AIM:
To write a c program to perform shift reduce parser.

ALGORITHM:
STEP 1: Start the program. STEP 2: Include header files, whatever need in program. STEP 3: Declare the variable and its value. STEP 4: Get the production to shift reduce parsing. STEP 5: Get the input string to check the grammar. STEP 6: Display it accepted or not accepted. STEP 7: Run the program STEP 8: Stop the program.

PROGRAM: SHIFT REDUCE PARSER #include<stdio.h> #include<conio.h> #include<string.h> #include<ctype.h> #include<process.h> typedef struct { char num[10]; int top; }stack; stack s; void st_push(char a) { s.num[s.top++]=a; } char st_pop() { int a; if(s.top==0) return-1; s.top=s.top-1; a=s.num[s.top]; s.num[s.top]='\0'; return(a); } char *substring(char *str,int start) { char *sub=""; int i=0; while (str[start]!='\0') sub[i++]=str[start++]; sub[i]='\0'; return sub; } void main() { char lhs[10],rhs[10][10]; char sub[10],ipstring[10]; char doller[3],*substr; int i,j,k,l,m,n,flag; int length=0,index=0; clrscr(); printf("\n Enter the no. of productions:"); scanf("%d",&n); printf("\n Enter the production in following form"); printf("\nlhs\trhs\n"); for(i=0;i<n;i++) { fflush(stdin); scanf("%c",&lhs[i]); scanf("%s",&rhs[i]);

} doller[0]='$'; doller[1]=lhs[0]; doller[2]='\0'; printf("\nEnter the input string to be checked"); scanf("%s",&ipstring); strcat(ipstring,"$"); st_push('$'); length=strlen(ipstring); i=0; printf("\n\t stack\tinput\taction"); printf("\n\t%s\t%s\t",s.num,ipstring); st_push(ipstring[i++]); substr=substring(ipstring,1); printf("\n\t%s\t%s\tSHIFT",s.num,substr); while(i<length) { for(j=1;j<s.top;j++) { flag=0; index=0; if(s.top==0) sub[0]='\0'; else for(k=j;k<s.top;k++) sub[index++]=s.num[k]; sub[index]='\0'; for(k=0;k<n;k++) { if(strcmp(sub,rhs[k])==0) { m=0; while(m<strlen(rhs[k])) { st_pop(); m++; } st_push(lhs[k]); flag=1; substr=substring(ipstring,i); printf("\n\t%s\t%s\tREDUCE",s.num,substr); } } if(flag==1) break; } if(flag==0) { if(ipstring[i]!='$')

{ st_push(ipstring[i++]); substr=substring(ipstring,i); printf("\n\t%s\t%s\tSHIFT",s.num,substr); } else { if(strcmp(s.num,doller)==0) printf("\n\t%s\t%s\tACCEPT",s.num,substr); else printf("\n\t%s\t%s\tERROR",s.num,substr); getch(); exit(0); } } } } OUTPUT: Enter the no. of productions:3 Enter the production in following form lhs rhs S CC C cC C d Enter the input string to be checkedcdcd stack input action $ $c $cd $cC $C $Cc $Ccd $CcC $CC $S $S cdcd$ dcd$ cd$ cd$ cd$ d$ $ $ $ $ $ SHIFT SHIFT REDUCE REDUCE SHIFT SHIFT REDUCE REDUCE REDUCE ACCEPT

EX: NO: 3

RECURSIVE DESENT PARSING

AIM:
To perform the implementation of recursive desent parsing.

ALGORITHM:
STEP 1: Start the program. STEP 2: Enter the grammar without error recursion. STEP 3: Set the input expression. STEP 4: Implement it by using sequence of production. STEP 5: Display the output. STEP 6: Stop the program.

PROGRAM: RECURSIVE DESDENT PARSING #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> #include<ctype.h> char ipsym[15],ipptr=0; void eprime(); void e(); void tprime(); void t(); void advance(); void e(); void f(); void e() { printf("\n \t \t E-->TE'"); t(); eprime(); } void eprime() { if(ipsym[ipptr]=='+') { printf("\n \t \t T-->+TE'"); advance(); t(); eprime(); } else printf("\n \t \t E'-->e"); } void t() { printf("\n \t \t E'-->FT'"); f(); tprime(); } void tprime() { if(ipsym[ipptr]=='*') { printf("\n \t \t E'-->FT'"); advance(); f(); tprime();

} else printf("\n \t \t T'-->e"); } void f() { if((ipsym[ipptr]=='i')||(ipsym[ipptr]=='I')) { printf("\n\t\tF-->i"); advance(); } else if(ipsym[ipptr]=='c') { advance(); e(); if(ipsym[ipptr]==')') { advance(); printf("\n\t\tF-->(E)"); } } else { printf("\n\t syntax error"); getch(); exit(1); } } void advance() { ipptr++; } void main() { int i; clrscr(); printf("\n\t\tINPUT"); printf("\n\t\tGrammar without error recursion"); printf("\n\t\tE-->TE'\n\t\tE'-->+TE'|e\n\t\tT-->FT'"); printf("\n\t\tT'-->*FT'|e\n\t\tF-->(E)|i"); printf("\nENTER THE IP EXPRESSION"); gets(ipsym); printf("\n\t\toutput"); printf("\n sequence of production rules"); e(); for(i=0;i<strlen(ipsym);i++)

{ if(ipsym[i]!='+'&&ipsym[i]!='*'&&ipsym[i]!='('&&ipsym[i]! =')'&&ipsym[i]!='i'&&ipsym[i]!='c') { printf("\n syntax error"); break; } } getch(); } OUTPUT: INPUT Grammar without error recursion E-->TE' E'-->+TE'|e T-->FT' T'-->*FT'|e F-->(E)|i ENTER THE IP EXPRESSIONi+i Output sequence of production rules E-->TE' E'-->FT' F-->i T'-->e T-->+TE' E'-->FT' F-->i T'-->e E'-->e

EX:NO: 4

COMPUTATION OF LEADING

AIM:
To write a c program to perform the computation of leading

ALGORITHM:
STEP 1: Start the program. STEP 2: Include header files. STEP 3: Declare the variable, whatever need in c program. STEP 4: Get the variable value or declare the value. STEP 5: Get the number of productions. STEP 6: Get the production of following form. STEP 7: Print the leading of non terminal symbol. STEP 8: Stop the program.

PROGRAM: COMPUTATION OF LEADING

#include<stdio.h> #include<conio.h> #include<string.h> #include<ctype.h> typedef struct { char num[10]; int top; } stack; typedef struct { char symbolname,lead[10]; int leadcount; stack s; } lead; lead it[10]; void push(int no,char a) { it[no].s.num[it[no].s.top++]=a; } char pop(int no) { int a; if(it[no].s.top==0) return -1; it[no].s.top=it[no].s.top-1; a=it[no].s.num[it[no].s.top]; return a; } int check(int no,char a) { int m; for(m=0;m<it[no].leadcount;m++) if(it[no].lead[m]==a) return 1; return 0; } void main() { char LHS[10],RHS[10][10]; char nonterminal[20]={""},terminal[25]={""},a; int n,i,j,k,l,m,flag; int nontercount=0,tercount=0,noncount=0,length=0; clrscr();

printf("\n\n\t\t input\n"); printf("\t\t*************\n"); printf("\t enter the no of production:"); scanf("%d",&n); printf("\n enter the production in the following form:"); printf("\n "); printf("\n LHS\tRHS\n"); for(i=0;i<n;i++) { fflush(stdin); scanf("%c\t",&LHS[i]); scanf("%s",&RHS[i]); } printf("\nenter the nonterminal of a string:"); scanf("%s",nonterminal); printf("\n enter the terminal of a string:"); scanf("%s",terminal); nontercount=strlen(nonterminal); tercount=strlen(terminal); for(i=0;i<nontercount;i++) { it[noncount].symbolname=nonterminal[i]; it[noncount].leadcount=0; for(j=0;j<n;j++) if(nonterminal[i]==LHS[j]) { length=strlen(RHS[j]); for(k=0;k<nontercount;k++) if(RHS[j][0]==nonterminal[k]) push(noncount,RHS[j][0]); for(k=0;k<length;k++) { for(l=0;l<tercount;l++) if(RHS[j][k]==terminal[l]) { if(k==1) { if(toascii(RHS[j][k-1]>=65)&&toascii(RHS[j][k-1]<=91)) { flag=check(noncount,RHS[j][k]); if(flag==0) it[noncount].lead[it[noncount].leadcount++]=RHS[j][k]; }} { flag=check(noncount,RHS[j][k]); if(flag==0)

it[noncount].lead[it[noncount].leadcount++]=RHS[j][k]; }}}} noncount++; } for(i=noncount-1;i>=0;i--) { if(it[i].s.top!=0) { a=pop(i); for(j=0;j<noncount;j++) { if(it[j].symbolname==a) for(k=0;k<it[j].leadcount;k++) { flag=check(i,it[j].lead[k]); if(flag==0) it[i].lead[it[i].leadcount++]=it[j].lead[k]; }}}} printf("\n\t\t output\n"); for(i=0;i<noncount;i++) { printf("\n\n\t leading(%c)={",it[i].symbolname); for(j=0;j<it[i].leadcount;j++) printf("%c",it[i].lead[j]); printf("}\n"); }} Output: Enter the no of production:3 Enter the production in the following form: LHS RHS S CC C cC C d Enter the nonterminal of a string:SC Enter the terminal of a string:cd OUTPUT leading(S)={cd} leading(C)={cd}

EX: NO: 5

OPERATOR PRECEDENCE OPERATORS

AIM:
To write a c program to perform the operator precedence operating parsing concept.

ALGORITHM:
STEP 1: Start the program. STEP 2: Include header files STEP 3: Declare the variable, whatever need in program. STEP 4: Declare the value for the variable STEP 5: Define function. STEP 6: Get precedence table. STEP 7: Get the string table check. STEP 8: Display the result STEP 9: Stop the program.

PROGRAM: OPERATOR PRECEDENCE PARSING #include <stdio.h>

#include <conio.h> #include<string.h> #include<math.h> #include<process.h> #include<ctype.h> typedef struct { char num[25]; int top; }stack; stack s; void push(char a) { s.num[s.top++]=a; } char pop() { int a; if(s.top==0) return -1; s.top=s.top-1; a=s.num[s.top]; s.num[s.top]='\0'; return(a); } char copy() { if(s.top==0) return -1; return(s.num[s.top-1]); } char *substring(char *st,int start) { char *sub= ; int i=0; while(st[start]!=0) sub[i++]=st[start++]; sub[i]='\0'; return sub; } void main() { char terminals[10],nonterminals[10]; char tercount,*sub= ; char op[10][10]={ }; char ip[10];

char topstack; int i,j,row,col; clrscr(); printf(\n Enter nonterminal as a string:); scanf(%s,nonterminals); printf(\n Enter terminal as a string:); scanf(%s,terminals); strcat(terminals,$); tercount=strlen(terminals); printf(\n enter the precedence table:); printf(\n<\-yields precedence); printf(\n>\-takes precedence); printf(\n=-same precedence); printf(\n|%s,terminals); for(i=0;i<tercount;i++) { printf(\n %c,terminals[i]); fflush(stdin); gets(op[i]); } printf(\n Enternthe string:); scanf(%s,ip); strcat(ip,$); push('$'); i=0; printf(\tstack\tipstring\taction); while(i<strlen(ip)) { topstack=copy(); row=col-1; for(j=0;j<tercount;j++) { if(terminals[j]==topstack) row=j; if(terminals[j]==ip[i]) col=j; } if(row=-1 || col==-1) { printf(\n ERROR); exit(0); }if(op[row][col]=='<' || op[row][col]=='=') { push(op[row][col]); push(ip[i++]); sub=substring(ip,i);

printf(\n\t%s\t%s\tSHIFT,s.num,sub); } if(op[row][col]=='>') { while(pop()!='<') sub=substring(ip,i); if(s.top==1 && ip[i]=='$') { printf(\n\t%s\t%s\tACCEPT,s.num,sub); i++; } else printf(\n \t%s\t%s\tREDUCE,s.num,sub); if(op[row][col]==' ') { printf(\n ERROR); }}} getch(); } OUTPUT: Enter the nonterminals:ETF Enter the terminals:+*()i Enter precedence table: '<'-yields precedence '>'-takes precedence '='-same precedence |+*()i$ +><<><> *>><><> (<<<=< )>> > > i>> > > $<<< < Enter string:i+1 stack input string Action $<i +i$ SHIFT $ +i$ REDUCE $<+ i$ SHIFT $<+<i $ SHIFT $<+ $ REDUCE $ $ ACCEPT

EX: NO: 6

CODE OPTIMIZATION

AIM:
To write a program to perform the code optimization concept.

ALGORITHM:
STEP 1: Start the program. STEP 2: Include all the header files, whatever need in program. STEP 3: Declare the variable and function. STEP 4: Declare the value of a variable. STEP 5: Get the optimizer address code. STEP 6: Get input string to be checked. STEP 7: Unoptimized input block. STEP 8: Optimize the three address code. STEP 9: Algebric Expression. STEP10: Display the algebric expression. STEP 11: Stop the program.

PROGRAM: CODE OPTIMIZATION #include<stdio.h> #include<conio.h>

#include<ctype.h> void main() { char a[25][25]; int p,q,r,o,l; char u; int ch,i=1,k; int c,j,count=0,kk; FILE *fi,fo; char ope1='*',ope2='+',ope3='/',ope4='-'; clrscr(); printf("\n OUTPUT"); printf("\n Enter the three address code:"); printf("\n Enter CONTROL+Z to complete the input:"); fi=fopen("infile","w"); while((c=getchar())!=EOF) fputc(c,fi); fclose(fi); printf("\n UNOPTIMIZED INPUT BLOCK:"); fi=fopen("infile","r"); while((c=getc(fi))!=EOF) { k=1; while(c!=';' && c!=EOF) { a[i][k]=c; printf("%c",a[i][k]); k++;c=fgetc(fi); } printf("\n"); i++; } count=i; fclose(fi); i=1; printf("\n OPTIMIZED THREE ADDRESS CODE:"); while(i<count) { if(strcmp(a[i][4],ope1)==0 && strcmp(a[i][5],ope1)==0) { ch=1; goto c1; } else if(isdigit(a[i][3]) && isdigit(a[i][5])) { ch=2; goto c1; }

else if(strcmp(a[i][5],'0')==0 || strcmp(a[i][5],',')==0) { ch=3; goto c1; } else { printf("\n\n ERRORNEOUS INPUT"); } c1: switch(ch) { case 1: printf("\nTYPE:REDUCTION IN STRENGTH"); printf("\t"); if(strcmp(a[i][6],',')==0) { for(j=1;j<=4;j++) printf("%c",a[i][j]); printf("%c",a[i][3]); printf(";"); } break; case 2: printf("\n\nTYPE:CONSTANT FOLDING:"); printf("\t"); p=a[i][3]; q=a[i][5]; p=q-48; if(strcmp(a[i][4],ope1)==0) r=p*q; else if(strcmp(a[i][4],ope2)==0) r=p+q; else if(strcmp(a[i][4],ope3)==0) r=p/q; else if(strcmp(a[i][4],ope4)==0) r=p-q; for(j=1;j<=2;j++) printf("%c",a[i][j]); printf("%d",r); printf(";"); break; case 3: printf("\n\n TYPE:ALGEBRAIC EXPRESSION REDUCTION"); printf("\t"); if((strcmp(a[i][4],ope2)==0) || (strcmp(a[i][4],ope4)==0) && (strcmp(a[i] [5],'0')==0) || (strcmp(a[i][3],'0')==0))

{ for(j=1;j<=3;j++) printf("%c",a[i][j]); printf(";"); } else printf("\n SORRY CAN'T OPTIMIZE"); break; }i++; } getch(); } OUTPUT: Enter the three address code: Enter CONTROL+Z to complete the input:a=a**b^Z UNOPTIMIZED INPUT BLOCK:a=a**b OPTIMIZED THREE ADDRESS CODE: TYPE:REDUCTION IN STRENGTH

EX: NO: 7

SAMPLE LEX PROGRAM

AIM:
To write a UNIX program for sample lexical program.

ALGORITHM:
STEP 1: Start the program. STEP 2: Include header files. STEP 3: Initiate all values. STEP 4: Get sentence from user STEP 5: Count number of character until end of line. STEP 6: Count number of lines. STEP 7: Count number of character used word and line. STEP 8: Stop the program.

PROGRAM: SAMPLE LEX PROGRAM

%{ int cc=0,wc=0,lc=0; %} %% [^\t\n]+ {wc++;cc+=yyleng;} \n {cc++;lc++;} [] cc++; %% int main(int argc,char **argv) { if(argc>1) { FILE *f; f=fopen(argv[1],"r"); if(!f) { printf("could not open%s",argv[1]); exit(0); } yyin=f; } + yylex(); printf("%d\n%d\n",cc,wc,lc); return 0; } int yywrap() {return 1; } INPUT: #include<stdio.h> void main() { int a,b,c; printf("enter the a "); scanf("%d%d",&a,&b); c=a+b; getch(); } OUTPUT: $ lex word.l

$ cc lex.yy.c -ll $ ./a.out seven.c 101 9

EX: NO: 8 CONVERTSION OF INFIX TO POSTFIX EXPRESSION

AIM:
To write a unix program to perform infix to postfix expression.

ALGORITHM:
STEP 1: Start the program. STEP 2: Get the expression and function. STEP 3: Check the expression with y containing operators. STEP 4: Then convert infix to postfix by shifting the operator. STEP 5: Define value whatever using in variable. STEP 6: print postfix expression. STEP 7: Stop the program. .

PROGRAM: CONVERSION OF INFIX TO POSTFIX INFIX TO POSTFIX EXPRESSION:

%{ #include<stdlib.h> #include<stdio.h> #include "y.tab.h" %} %% [0-9]+ {yylval.no=atoi(yytext); return(DIGIT); } [a-zA-Z][a-zA-Z0-9]* { strcpy(yylval.str,yytext); return(ID); } "+" {return(PLUS);} "-" {return(MINUS);} "*" {return(MUL);} "/" {return(DIV);} "^" {return(EXPO);} "(" {return(OPEN);} ")" {return(CLOSE);} "\n" {return 0;} [lt] ; [] ; %% PROGRAM FOR POSTFIX OPERATION: %{ #include<stdio.h> %} %union { int no; char str[10]; } %token <no> DIGIT %token <str> ID %left PLUS MINUS %left MUL DIV %right EXPO %left OPEN CLOSE %% STMT: EXPR {printf("\n");} EXPR: EXPR PLUS EXPR {printf("+");}

| EXPR MINUS EXPR {printf("-");} | EXPR MUL EXPR {printf("*");} | EXPR DIV EXPR {printf("/");} | EXPR EXPO EXPR {printf("^");} | OPEN EXPR CLOSE | DIGIT {printf("%d",yylval.no);} | ID {printf("%s",yylval.str);} ; %% int main(void) { printf("\n"); yyparse(); printf("\n"); return 0; } OUTPUT: $ lex postfix.l $ yacc -d postfix.y -ly $ cc lex.yy.c y.tab.c -ll -ly lex.yy.c: y.tab.c: $ ./a.out (a+b) ab+

EX: NO: 9

IMPLEMENTION OF LEXICAL ANALYSER USING LEX

AND YACC

AIM:
To write a unix program to implement lexical analysis using LEX and YACC

ALGORITHM:
STEP 1: Start the program. STEP 2: Include all preprocessor directories and keywords. STEP 3: Include all various types of operators and identifiers. STEP 4: To check whether various types of keyword operators and Identifier for input program. STEP 5: Stop the program. .

PROGRAM: IMPLEMENTATION OF LEXICAL ANALYSER USING LEX & YACC

%% #.* {printf("\n %s is a PREPROCESSOR DIRECTIVE",yytext);} int | float | char | double | while | for | do | if | break | continue | void | switch | case | long | struct | scanf | printf | const | typedef | return | else | goto {printf("\n\t %s is a KEYWORD",yytext);} \< | \> | \<= | \>= | \== | \!= {printf("\n\t %s is RELATIONAL OPERATOR",yytext); } \= {printf("\n\t %s is ASSIGNMENT OPERATOR",yytext);} \+ | \- | \* | \/ | \% {printf("\n\t %s is ARITHMETIC OPERATOR",yytext);} \".*\" {printf("\n\t %s is a string",yytext);} [0-9]+ {printf("\n\t %s is a NUMBER",yytext);} [a-Za-Z][a zA Z0 9]* {printf("\n\t %s is a IDENTIFIER",yytext);} \{ {printf("\n BLOCK BEGINS");} \} {printf("\n BLOCK ENDS");} \/\*.*\*\/ printf("\n %s is COMMENT",yytext); [\t]; %% int main(int argc,char **argv)

{ if(argc>1) { FILE *f; f=fopen(argv[1],"r"); if(!f) { printf("could not open %s\n",argv[1]); exit(0); } yyin=f; } yylex(); } INPUT: #include<stdio.h> void main() { int a,b,c; printf("enter the a "); scanf("%d%d",&a,&b); c=a+b; getch(); } OUTPUT: $ lex lexical.l $ cc lex.yy.c ll $ ./a.out seven.c #include<stdio.h> is a PREPROCESSOR DIRECTIVE void is a KEYWORD main is a IDENTIFIER BLOCKS BEGINS int is a KEYWORD a,b,c are IDENTIFIER printf is a KEYWORD scanf is a KEYWORD BLOCK ENDS

EX: NO: 10

IMPLEMENTATION OF SIMPLE CALCULATOR USING LEX and YACC

AIM:
To write a unix program to implement simple calculating using LEX and YACC

ALGORITHM:
STEP 1: Start the program. STEP 2: Include the number of operators and logical function and Operator. STEP 3: Include perform calculation for given input. STEP 4: Then convert infix to postfix by shifting the operator. STEP 5: Display the result. STEP 7: Stop the program. .

PROGRAM: IMPLEMENTATION OF SIMPLE CALCULATOR USING LEX AND YACC %{

double memvar; %} %union { double dval; } %token <dval> NUMBER %token <dval> MEM %token LOG SINE nLOG COS TAN %left ' ' '+' %left '*' '/' %right '^' %left LOG SINE nLOG COS TAN %nonassoc UMINUS %type <dval> expression %% start: statement '\n' | start statement '\n' ; statement: MEM '=' expression {memvar=$3;} | expression {printf("Answer-%g\n",$1);} ; expresion: expression '+' expression{$$=$1+$3;} | expression '-' expression {$$ -$i-$3;} | expression '*' expression {$$ =$i*$3;} | expression '/' expression { if($3==0) yyerror("Divide by zero"); else $$_$1/$3: } | expression '^' expression {$$-pow($1,$3);} : expression: '-' expression%prec UMINUS {$$=-$2;} |'(' expression ')' {$$=$2;} | LOG expression {$$=log($2)/log(10);} | nLOG expression {$$=log($2);} | SINE expression {$$=sin($2*3.141592654/180);} | COS expression {$$-cos($2*3.141592654/180);} | TAN expression {$$-tan($2*3.141592654/180);} | NUMBER {$$=$1;} | MEM {$$=memvar;} ; %% main()

{ printf("ENTER THE EXPRESSION:"); yyparse(); { int yyerror(char *error) { printf("%s\n",error); } IMPLEMENTATION OF LEX USING YACC %{ #include "y.tab.h" #include<math.h> %} %% ([0-9]+|([0-9}+) ([eE][-+]?[0-9]+)?) {yylval.dval=atof(yytext); return NUMBER;} log | LOG {return LOG;} ln {return nLOG;} sin | SIN {return SINE;} cos | COS {return COS; } tan | TAN {return MEM;} [ \t ]; \$ {return o;} \n | return yytext[0]; %% OUTPUT: $ lex calci.l $ yacc -d calci.y $ ./a.out sin 90 1