System Software Lab Programs ( Lex and Yaac Programs ) 4a
// June 11th, 2009 // Educational, Engineering, Unix and System Software Lab // Written by sandeephegde
-->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 exppression\n”);
exit(0);
}
int main()
{
printf(“enter an expression:”);
yyparse();
printf(“valid expression”);
return 0;
}
If you enjoyed this post, make sure you subscribe to my RSS feed!
Related posts:



