This post was published long ago, when I was a student and an amateur blogger. The links might be outdated and content may not be useful anymore. Please read this content keeping its age in mind.
Subject : System Software Laboratory
Branch : Information Science & Engineering
Semester : 6
University : VTU
………………………………………………………………………………………………………………………….
PART – A
………………………………………………………………………………………………………………………….
5) a. Program to evaluate an arithmetic expression involving operators +, -, * and /.
5a.l
%{
#include “y.tab.h”
extern int yylval;
%}
%%
[0-9]+ { yylval=atoi(yytext);return NUM;}
[ t];
n return 0;
. return yytext[0];
%%
5a.y
/* 5a.y /
%{
#include<stdio.h>
%}
%token NUM
%left ‘-”+’
%left ‘”/’
%%
start: expr {printf(“nValid expression.nValue: %d”,$1);}
expr: expr’+’expr {$$=$1+$3;}
|expr’-‘expr {$$=$1-$3;}
|expr’‘expr {$$=$1$3;}
|expr’/’expr {if($3==0)
yyerror(“Divide by zero”);
else
$$=$1/$3;
}
|'(‘ expr ‘)’ {$$=$2;}
|NUM
;
%%
main()
{
printf(“nEnter an expression: “);
yyparse();
return(0);
}
int yyerror(char *msg)
{
printf(“n%s”,msg);
printf(“nInvalid expression”);
exit(0);
}
Leave a ReplyCancel reply