yacc program to implement arithmetic operators

    Lex program %{ #include<stdio.h> #include<stdlib.h> %} %% [0-9]+    {yylval=atoi(yytext);return (NUM);} “+”|”-“|”*”|”/”|”\n”    {return yytext[0];} %% Yacc program %{ #include<stdio.h> #include<ctype.h> int i=0; %} %token NUM %left ‘+”-‘ %left ‘*”/’ %% S:E ‘\n’    {printf(“result =%d”,(int)$$);} ; E:E’+’E    {$$=$1+$3;} |E’-‘E    {$$=$1-$3;} |E’*’E    {$$=$1*$3;} |E’/’E    {$$=$1/$3;} |NUM    {$$=$1;} ; %% #include “lex.yy.c” main() { yyparse(); } int …

Read more

yacc program to implement logical operators

Lex program %{ #include<stdio.h> #include<stdlib.h> %} %% (0|1)+    {yylval=atoi(yytext);return (NUM);} “&”|”|”|”!”|”x”|”\n”    {return yytext[0];} %% yacc program %{ #include<stdio.h> #include<ctype.h> int i=0; %} %token NUM %left ‘&”|”x’ %right ‘!’ %% S:E ‘\n’    {printf(“result =%d”,(int)$$);} ; E:E’&’E    {$$=$1&$3;} |E’|’E    {$$=$1|$3;} |’!’E    {$$=!$2;} |E’x’E    {$$=($1&(!$3))|((!$1)&$3);} |NUM    {$$=$1;} ; %% #include “lex.yy.c” main() { yyparse(); } int yywrap() {} …

Read more