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
………………………………………………………………………………………………………………………….
4) a. Program to recognize a valid arithmetic expression that uses operators+, -, * and /.
4a.l
%{
#include “y.tab.h”
extern int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext); return NUM; }
[ t] ;
n return 0;
. return yytext[0];
%%
4a.y
%{
#include<stdio.h>
%}
%%
%token NUM
%left ‘+’ ‘-‘
%left ‘+’ ‘-‘
%nonassoc UMINUS
%%
e:e’+’e |
e’-‘e |
e’*’e |
e’/’e {if($3==0) {printf(“n DIVIDE BY ZERO ERROR”); exit(0);}} |
‘(‘e’)’ |
‘-‘e %prec UMINUS |
NUM
;
%%
yyerror()
{
printf(“invalid exppressionn”);
exit(0);
}
int main()
{
printf(“enter an expression:”);
yyparse();
printf(“valid expression”);
return 0;
}
Beeneantony says
good