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()
{}
int yyerror()
{}

Sample output

1&0

result=0

1|1

result=1

!1

result=0

1×0

result=1

Leave a Reply

Your email address will not be published. Required fields are marked *