You are on page 1of 26

COMPLIER DESIGN

LAB MANUAL

III-B.TECH II-SEM

JNTUH-R18

Faculty In-charge:
M.SADALAXMI, Asst.Prof.

DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

COMPLIER DESIGN LAB MANUAL

LIST OF EXPERIMENTS:

Compiler Design Experiments

1. Write a LEX Program to scan reserved word & Identifiers of C


Language

2. Implement Predictive parsing algorithm

3. Write a C program to generate three address codes.

4. Implement SLR (1) Parsing algorithm

5. Design LALR bottom up parser for the given language

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 1


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 2


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 3


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 4


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

COMPLIER DESIGN LAB

Course Objectives:

1. To provide hands-on experience on web technologies


2. To develop client-server application using web technologies
3. To introduce server-side programming with Java servlets and JSP
4. To understand the various phases in the design of a compiler.
5. To understand the design of top-down and bottom-up parsers.
6. To understand syntax directed translation schemes.
7. To introduce lex and yacc tools.

Course Outcomes:

1. Design and develop interactive and dynamic web applications using HTML, CSS, JavaScript and
XML
2. Apply client-server principles to develop scalable and enterprise web applications.
3. Ability to design, develops, and implements a compiler for any language.
4. Able to use lex and yacc tools for developing a scanner and a parser.
5. Able to design and implement LL and LR parsers.

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 5


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

Course Plan:

Experiment List of Experiments Course Objective Course Outcome


1 Write a LEX Program to To understand the various Design and develop
phases in the design of a interactive and dynamic web
scan reserved word &
compiler, applications using HTML,
Identifiers of C Language CSS, JavaScript and XML

2 Implement Predictive To provide hands-on Apply client-server


experience on web principles to develop
parsing algorithm
technologies, scalable and enterprise
To develop client-server web applications.
application using web
technologies

3 Write a C program to To understand syntax Ability to design, develops,


and implements a compiler
generate three address directed translation schemes,
To introduce server-side for any language.
codes. programming with Java
servlets and JSP

4 Implement SLR(1) To introduce lex and yacc Able to use lex and yacc
tools. tools for developing a
Parsing algorithm
scanner and a parser.

5 Design LALR bottom up To understand the design Able to design and


of top-down and bottom- implement LL and LR
parser for the given up parsers.
parsers
language

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 6


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

GENERAL INSTRUCTIONS

1. Students are instructed to come to laboratory on time. Late comers are not entertained in the
lab.

2. Students should be punctual to the lab. If not, the conducted experiments will not be repeated.

3. Students are expected to come prepared at home with the experiments which are going to be
performed.

4. Students are instructed to display their identity cards before entering into the lab.

5. Students are instructed not to bring mobile phones to the lab.

6. Any damage/loss of system parts like keyboard, mouse during the lab session, it is
student’s responsibility and penalty or fine will be collected from the student.

7. Students should update the records and lab observation books session wise. Before leaving
the lab the student should get his lab observation book signed by the faculty.

8. Students should submit the lab records by the next lab to the concerned faculty members in the
staffroom for their correction and return.

9. Students should not move around the lab during the lab session.

10. If any emergency arises, the student should take the permission from faculty member
concerned in written format.

11. The faculty members may suspend any student from the lab session on disciplinary grounds.

Never copy the output from other students.

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 7


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

1). AIM: - Write a LEX Program to scan reserved word & Identifiers of C
Language

#include<string.h>
#include<ctype.h>
#include<stdio.h>
void keyword(char str[10])
{
if(strcmp("for",str)==0||strcmp("while",str)==0||
strcmp("do",str)==0||strcmp("int",str)==0|| strcmp("float",str)==0|| strcmp("char",str)==0||
strcmp("double",str)==0|| strcmp("static",str)==0|| strcmp("switch",str)==0||
strcmp("case",str)==0)

printf("\n%s is a keyword",str);
else
printf("\n%s is an identifier",str);
}
int main()
{
FILE *f1,*f2,*f3;
char c,str[10];
int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0;
printf("\n enter the c program\n");
f1=fopen("input","w");
while((c=getchar())!=EOF)

putc(c,f1);
fclose(f1);
f1=fopen("input","r");
f2=fopen("identifier","w");
f3=fopen("specialchar","w");
while((c=getc(f1))!=EOF)
{
if(isdigit(c))
{
tokenvalue=c-'0';
c=getc(f1);
while(isdigit(c))
{
tokenvalue*=10+c-'0';
c=getc(f1);
}
num[i++]=tokenvalue;
ungetc(c,f1);
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 8
COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

}
else if(isalpha(c))
{
putc(c,f2);
c=getc(f1);
while(isdigit(c)||isalpha(c)||c=='_'||c=='$')
{
putc(c,f2);
c=getc(f1);
}
putc(' ',f2);
ungetc(c,f1);
}
else if(c==' '||c=='\t')
printf("");
else if(c=='\n')
lineno++;
else
putc(c,f3);
}
fclose(f2);
fclose(f3);
fclose(f1);
printf("\n The no %s in the program are ");
for(j=0;j<i;j++)
printf("%d",num[j]);
printf("\n");
f2=fopen("identifier ","r");
k=0;
printf("the keywords and identifiers are");
while((c=getc(f2))!=EOF)
{
if(c!=' ')
str[k++]=c;
else
{
str[k]='\0';
keyword(str);
k=0;
}
}
fclose(f2);
f3=fopen("specialchar","r");
printf("\n special characters are");
while((c=getc(f3))!=EOF)
printf("%c",c);
printf("\n");
fclose(f3);

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 9


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

printf("total no of lines are :%d",lineno);


return 0;
}

INPUT:

enter the c program


int main()
{
int a,b,c;
printf(“enter a,b values”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“c=%d”,c);
getch();
}

OUTPUT:
int is a keyword
main is an identifier
int is a keyword
a is an identifier
b is an identifier
c is an identifier
printf is an identifier
enter is an identifier
a is an identifier
b is an identifier
values is an identifier
scanf is an identifier
d is an identifier
a is an identifier
b is an identifier
c is an identifier
a is an identifier
b is an identifier
printf is an identifier
c is an identifier
d is an identifier
c is an identifier
getch is an identifier
special characters are(){,,;(",");("%",&,&);=+;("=%",);();}
total no of lines are :9

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 10


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

2). AIM: - Implement Predictive parsing algorithm


#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define SIZE 128
#define NONE -1
#define EOS '\0'
#define NUM 257
#define KEYWORD 258
#define ID 259
#define DONE 260
#define MAX 999
char lexemes[MAX];
char buffer[SIZE];
int lastchar=-1;
int lastentry=0;
int tokenval=DONE;
int lineno=1;
int lookahead;
struct entry
{
char *lexptr;
int token;
}symtable[100];
struct entry
keywords[]={
{"if",KEYWORD},
{"else",KEYWORD},
{"for",KEYWORD},
{"int",KEYWORD},
{"float",KEYWORD},
{"double",KEYWORD},
{"char",KEYWORD},
{"struct",KEYWORD},
{"return",KEYWORD},0,0};

void Error_Message(char *m)


{
fprintf(stderr,"line %d,%s\n",lineno,m);
exit(1);

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 11


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

}
int look_up(char s[])
{
int k;
for(k=lastentry;k>0;k--)
if(strcmp(symtable[k].lexptr,s)==0)
return k;
return 0;
}
int insert(char s[],int tok)
{
int len;
len=strlen(s);
if(lastentry+1>=MAX)
Error_Message("Symbol Table is full");
if(lastchar+len+1>=MAX)
Error_Message("Lexmes array is full");
lastentry=lastentry+1;
symtable[lastentry].token=tok;
symtable[lastentry].lexptr=&lexemes[lastchar+1];
lastchar=lastchar+len+1;
strcpy(symtable[lastentry].lexptr,s);
return lastentry;
}

/*void initialize()
{
struct entry *ptr;
for(ptr=keywords; prt->token;prt+1)
insert(prt->lexptr,ptr->token);
} */
int lexer()
{
int t;
int val,i=0;
while(1)
{
t=getchar();
if(t=='\0' ||t=='\t');
else if(t=='\n')
lineno=lineno+1;
else if(isdigit(t))
{
ungetc(t,stdin);
scanf("%d",&tokenval);
return NUM;
}
else if(isalpha(t))

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 12


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

{
while(isalnum(t))
{
buffer[i]=t;
t=getchar();
i++;
if(i>=SIZE)
Error_Message("compiler error");
}
buffer[i]=EOS;
if(t!=EOF)
ungetc(t,stdin);
val=look_up(buffer);
if(val==0)
val=insert(buffer,ID);
tokenval=val;
return symtable[val].token;
}
else if(t==EOF)
return DONE;
else
{
tokenval=NONE;
return t;
}
}
}

void Match(int t)
{
if(lookahead==t)
lookahead=lexer();
else
Error_Message("syntax error");

}
void display(int t,int tval)
{
if(t=='+'||t=='-'||t=='*'||t=='/')
printf("\n arithmetic operator:%c",t);
else if(t==NUM)
printf("\n Number: %d",tval);
else if(t==ID)
printf("\n identifier :%s",symtable[tval].lexptr);
else
printf("\n Token %d tokenval %d",t,tokenval);
}

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 13


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

void F()
{
void E();
switch(lookahead)
{
case '(':Match('(');
E();
Match(')');
break;
case NUM:display(NUM,tokenval);
Match(NUM);
break;
case ID:display(ID,tokenval);
Match(ID);
break;
default:Error_Message("syntax error");
}
}
void T()
{
int t;
F();
while(1)
{
switch(lookahead)
{
case '*':t=lookahead;
Match(lookahead);
F();
display(t,NONE);
continue;
case '/':t=lookahead;
Match(lookahead);
display(t,NONE);
continue;
default:return;
}
}
}
void E()
{
int t;
T();
while(1)
{
switch(lookahead)
{
case '+':t=lookahead;

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 14


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

Match(lookahead);
T();
display(t,NONE);
continue;
case '-':t=lookahead;
Match(lookahead);
T();
display(t,NONE);
continue;
default:return;
}
}
}
void parser()
{
lookahead=lexer();
while(lookahead!=DONE)
{
E();
Match(';');
}

}
void main()
{
char ans[10];
printf ("\n program for recursive decent parsing");
printf ("\n Enter the expression");
printf("And place; at the end \n");
printf("press Ctrl-Z to terminate \n");
parser();
}

output :

program for recursive decent parsing


Enter the expression And place; at the end
press Ctrl-Z to terminate

(a+b);

identifier :a
identifier :b
arithmetic operator:+

c+d*e;

identifier :c

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 15


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

identifier :d
identifier :e
arithmetic operator:*
arithmetic operator:+
*f;
line 5,syntax error

3). AIM: - Write a C program to generate three address codes.


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int label[20];
int no=0;
int main()
{
FILE *fp1,*fp2;
char fname[10],op[10],ch;
char operand1[8],operand2[8],result[8];
int i=0,j=0;
printf("\n Enter filename of the intermediate code");
scanf("%s",&fname);
fp1=fopen(fname,"r");
fp2=fopen("target.txt","w");
if(fp1==NULL || fp2==NULL)
{
printf("\n Error opening the file");
exit(0);
}
while(!feof(fp1))
{
fprintf(fp2,"\n");
fscanf(fp1,"%s",op);
i++;
if(check_label(i))
fprintf(fp2,"\nlabel#%d",i);
if(strcmp(op,"print")==0)
{
fscanf(fp1,"%s",result);
fprintf(fp2,"\n\t OUT %s",result);
}
if(strcmp(op,"goto")==0)
{
fscanf(fp1,"%s%s",operand1,operand2);
fprintf(fp2,"\n\t JMP %s,label#%s",operand1,operand2);
label[no++]=atoi(operand2);
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 16
COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

}
if(strcmp(op,"[]=")==0)
{
fscanf(fp1,"%s%s%s",operand1,operand2,result);
fprintf(fp2,"\n\t STORE %s[%s],%s",operand1,operand2,
result);
}
if(strcmp(op,"uminus")==0)
{
fscanf(fp1,"%s%s",operand1,result);
fprintf(fp2,"\n\t LOAD -%s,R1",operand1);
fprintf(fp2,"\n\t STORE R1,%s",result);
}
switch(op[0])
{
case '*': fscanf(fp1,"%s%s%s",operand1, operand2,
result);
fprintf(fp2,"\n\t LOAD",operand1);
fprintf(fp2,"\n\t LOAD %s,R1",operand2);
fprintf(fp2,"\n\t MUL R1,R0");
fprintf(fp2,"\n\t STORE R0,%s",result);
break;
case '+': fscanf(fp1,"%s%s%s",operand1,operand2,
result);
fprintf(fp2,"\n\tLOAD %s,R0",operand1);
fprintf(fp2,"\n\t LOAD %s,R1",operand2);
fprintf(fp2,"\n\t ADD R1,R0");
fprintf(fp2,"\n\t STORE R0,%s",result);
break;
case '-': fscanf(fp1,"%s%s%s",operand1,operand2,
result);
fprintf(fp2,"\n\t LOAD %s,R0",operand1);
fprintf(fp2,"\n\t LOAD %s,R1",operand2);
fprintf(fp2,"\n\t SUB R1,R0");
fprintf(fp2,"\n\t STORE R0,%s",result);
break;
case '/': fscanf(fp1,"%s%s%s",operand1,operand2,
result);
fprintf(fp2,"\n\t LOAD %s,R0",operand1);
fprintf(fp2,"\n\t LOAD %s,R1",operand2);
fprintf(fp2,"\n\t DIV R1,R0");
fprintf(fp2,"\n\t STORE R0,%s",result);
break;
case '%': fscanf(fp1,"%s%s%s",operand1,operand2,
result);
fprintf(fp2,"\n\t LOAD %s,R0",operand1);
fprintf(fp2,"\n\t LOAD %s,R1",operand2);
fprintf(fp2,"\n\t DIV R1,R0");

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 17


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

fprintf(fp2,"\n\t STORE R0,%s",result);


break;
case '=': fscanf(fp1,"%s %s",operand1,result);
fprintf(fp2,"\n\t STORE %s %s",operand1,result);
break;
case '>': j++;
fscanf(fp1,"%s %s %s",operand1,operand2,result);
fprintf(fp2,"\n\t LOAD %s,R0",operand1);
fprintf(fp2,"\n\t JGT %s,label#%s",operand2,
result);
label[no++]=atoi(result);
break;
case '<': fscanf(fp1,"%s%s%s",operand1,operand2,
result);
fprintf(fp2,"\n\t LOAD %s,R0",operand1);
fprintf(fp2,"\n\t JLT %s,label#%d",operand2,
result);
label[no++]=atoi(result);
break;
}
}
fclose(fp2);
fclose(fp1);
fp2=fopen("target.txt","r");
if(fp2==NULL)
{
printf("Error opening the file\n");
exit(0);
}
do
{
ch=fgetc(fp2);
printf("%c",ch);
}while(ch!=EOF);
fclose(fp1);
return 0;
}
int check_label(int k)
{
int i;
for(i=0;i<no;i++)
{
if(k==label[i])
return 1;
}
return 0;
}

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 18


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

/* create input file mc.txt as follows */

=t1 2
[]=a 0 1
[]=a 1 2
[]=a 2 3 *t1 6 t2
+a[2] t2 t3
-a[2] t1 t2
/t3 t2 t2
uminus t2 t2
print t2
goto t2 t3
=t3 99
uminus 25 t2
*t2 t3 t3
uminus t1 t1
+t1 t3 t4
print t4
OUTPUT :
Enter filename of the intermediate code: mc.txt
STORE t1,2
STORE a[0],1
STORE a[1],2
STORE a[2],3

LOAD t1,R0
LOAD 6,R1
ADD R1,R0
STORE R0,t3

LOAD a[2],R0
LOAD t2,R1
ADD R1,R0
STORE R0,t3

LOAD a[t2],R0
LOAD t1,R1
SUB R1,R0
STORE R0,t2

LOAD t3,R0
LOAD t2,R1
DIV R1,R0
STORE R0,t2

LOAD t2,R1
STORE R1,t2

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 19


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

LOAD t2,R0
JGT 5,label#11

Label#11: OUT t2
JMP t2,label#13
Label#13: STORE t3,99
LOAD 25,R1
STORE R1,t2

LOAD t2,R0
LOAD t3,R1
MUL R1,R0
STORE R0,t3

LOAD t1,R1
STORE R1,t1

LOAD t1,R0
LOAD t3,R1
ADD R1,R0
STORE R0,t4
OUT t4

4). AIM: - Implement SLR (1) parsing algorithm


%{
int COMMENT=0;
%}
id [a-z][a-z0-9]*
%%
#.* {printf("\n%s is a PREPROCESSOR DIRECTIVE",yytext);}
int|double|char {printf("\n\t%s is a KEYWORD",yytext);}
if|then|endif {printf("\n\t%s is a KEYWORD",yytext);}
else {printf("\n\t%s is a KEYWORD",yytext);}
"/*" {COMMENT=1;}
"*/" {COMMENT=0;}
{id}\( {if(!COMMENT)printf("\n\nFUNCTION\n\t%s",yytext);}
{id}(\[[0-9]*\])? {if(!COMMENT) printf("\n\tidentifier\t%s",yytext);}
\{ {if(!COMMENT) printf("\n BLOCK BEGINS");ECHO; }
\} {if(!COMMENT)printf("\n BLOCK ends");ECHO; }
\".*\" {if(!COMMENT)printf("\n\t %s is a STRING",yytext);}
[+\-]?[0-9]+ {if(!COMMENT)printf("\n\t%s is a NUMBER",yytext);}
\( {if(!COMMENT)printf("\n\t");ECHO;printf("\t open parenthesis\n");}

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 20


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

\) {if(!COMMENT)printf("\n\t");ECHO;printf("\t closed parenthesis");}


\; {if(!COMMENT)printf("\n\t");ECHO;printf("\t delim semicolon");}
\= {if(!COMMENT)printf("\n\t%s is an ASSIGNMENT OPERATOR",yytext);}
\<|\> {printf("\n\t %s is relational operator",yytext);}
"+"|"-"|"*"|"/" {printf("\n %s is an operator\n",yytext);}
"\n" ;
%%
main(int argc ,char **argv)
{
if (argc > 1)
yyin = fopen(argv[1],"r");
else
yyin = stdin;
yylex ();
printf ("\n");
}
int yywrap()
{
return 0;
}

First Create the following c file for intput


[kvs@localhost lexprograms]$ vi sri.c
void main()
{
int a,b;
printf("enter a,b values");
scanf("%d%d",&a,&b);
c=a+b;
printf("result=%d",c);
getch();
}

Compile the program lex2.l as follows

[kvs@localhost lexprograms]$ vi lex2.l


[kvs@localhost lexprograms]$ lex lex2.l
[kvs@localhost lexprograms]$ cc lex.yy.c -ll
[kvs@localhost lexprograms]$ ./a.out sri.c

Output :
identifier void

FUNCTION
main(
) closed parenthesis
BLOCK BEGINS{

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 21


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

int is a KEYWORD
identifier a,
identifier b
; semicolon

FUNCTION
printf(
"enter a,b values" is a STRING
) closed parenthesis
; semicolon

FUNCTION
scanf(
"%d%d" is a STRING,&
identifier a,&
identifier b
) closed parenthesis
; semicolon
identifier c
= is an ASSIGNMENT OPERATOR
identifier a
+ is an operator

identifier b
; semicolon

FUNCTION
printf(
"result=%d" is a STRING,
identifier c
) closed parenthesis
; semicolon

FUNCTION
getch(
) closed parenthesis
; semicolon

5). AIM: - Design LALR bottom up parser for the given language

$ vi sri4.l /* create sri4.l lex program */


KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 22
COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

%{
#include"y.tab.h"
#include<stdio.h>
void yyerror(char *);
%}

%%
(([0-9]+)|([0-9]*\.[0-9]+)) {
yylval.dval=atof(yytext);
return NUM;
}
[+-/*\n,~()] {
return (*yytext);
}
sin return SIN;
cos return COS;
tan return TAN;
sqrt return SQRT;
exit exit(0);
[ \t] /*ignore*/
%%

int yywrap(void)
{
return 1;
}
/* now compile the above lex program as follows
$ lex sri4.l
/* now create yacc specification (program) as follows */

$ vi sri4.y
%{
#include<stdio.h>
#include<math.h>
void yyerror(char *);
%}

%union
{
double dval;
}
%token <dval> NUM
%token SIN COS TAN SQRT
%right '~'
%left '+' '-'
%left '*' '/'
%type <dval> expression

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 23


COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

%%
program: program statement'\n'
|
;
statement:
expression { printf("%lf\n", $1); }
;
expression:
NUM
| expression '+' expression {$$ = $1 + $3;}
| expression '-' expression {$$ = $1 - $3;}
| expression '*' expression {$$ = $1 * $3;}
| expression '/' expression {$$ = $1 / $3;}
| '~' expression {$$ = -(1) * $2;}
| SQRT'('expression')' {$$ = sqrt( $3 );}
| SIN'('expression')' {$$ = sin ($3*3.142/180);}
| COS'('expression')' {$$ = cos ($3*3.142/180);}
| TAN'('expression')' {$$ = tan ($3*3.142/180);}
;
%%

main()
{
yyparse();
}

void yyerror(char *s)


{
fprintf(stderr,"%s\n",s);
}

/* now compile the yacc program sri4.y as */

$ yacc -d sri4.y
/* linking lex program and yacc specification as given below */

$ cc lex.yy.c y.tab.c -ll -lm


$ ./a.out

OUTPUT :
3+2
5.000000
4+5+6
15.000000
5*9-3
42.000000
sin(30)
0.500059
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 24
COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING

cos(45)
0.707035

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 25

You might also like