Sharecodepoint App Download Qrcode Sharecodepoint Android App Banner
Your Ads Here Sharecodepoint Youtube Channel



Sharecodepoint popular queries

Saturday, 23 February 2019

Program to evaluate an arithmetic expression involving operating +, -, * and /. | Compiler Design Lab Programs | Yacc Programs


Yacc Part :

%token NUMBER ID NL
%left ‘+’ ‘-‘
%left ‘*’ ‘/’
%%
stmt : exp NL { printf(“Value = %d\n”,$1); exit(0);}
 ;
exp : exp ‘+’ exp { $$=$1+$3; }
 | exp ‘-‘ exp { $$=$1-$3; }
 | exp ‘*’ exp { $$=$1*$3; }
 | exp ‘/’ exp { if($3==0)
 {
printf(“Cannot divide by 0”);
exit(0);
 }
 else
 $$=$1/$3;
}
 | ‘(‘ exp ‘)’ { $$=$2; }
 | ID { $$=$1; }
 | NUMBER { $$=$1; }
 ;
%%
int yyerror(char *msg)
{
 printf(“Invalid Expression\n”);
 exit(0);
}
main ()
{
 printf(“Enter the expression\n”);
 yyparse();

Lex Part :

%{
 #include “y.tab.h”
 extern int yylval;
%}
%%
[0-9]+ { yylval=atoi(yytext); return NUMBER; }
\n { return NL ;}
. { return yytext[0]; }
%%

0 comments:

Post a Comment

 

Version 23.2.3.4 - 2016 - 2019 - © Sharecodepoint | Made in India. | Copyright Content