5A) Program to evaluate an arithmetic expression involving operators +, -, * and /. [VTU SS&OS]

vtu ss & os 5a
Code:
LEX:
YACC:

Explanation:
This program will evaluate an arithmetic expression. See how,

Lex:
%{
#include<stdlib.h>
#include "y.tab.h"
extern int yylval;
%}

Declaration section. Include y.tab.h for making lexer read the input symbol so that it can send tokens to Yacc parser. stdlib.h for the function atoi().
extern int yylval is needed for evaluating the expression and storing results.

%%
[0-9]+   { yylval=atoi(yytext);  return NUM;  }
[\t] ;
\n return 0;
. return yytext[0];
%%
yywrap()
{}

Definition section.

  • if any numbers are found in the input, convert the text equivalent of that numbers to integer using atoi() function. and return NUM token to parser. store the number value in yylval so that parser can access this for printing.
  • [\t] will be ignored.
  • \n, newline will be considered as end of arithmatic expression. at this time you need to stop parsing and you should now print the result. so result 0 is written.
  • any other character in the input, '.' will be returned as it is.
yywrap() function is responsible for checking whether there are any more inputs. If no, then yywrap() tells program to end.


Yacc:
%{
#include <stdio.h>
#include<stdlib.h>
%}
%token  NUM 
%left   '+' '-'
%left   '*' '/'

declaration section. include all header files required.
define tokens which are getting returned from lexer.
assign precedence and associativity of operators. first defined ones will have least preference. left means left associativity, and right means right associativity.

%%
expr : e { printf(" Result : %d\n",$1); return 0;};
e : e '+' e {$$=$1+$3;}
|   e '-' e {$$=$1-$3;}
|   e '*' e {$$=$1*$3;}
|   e '/' e {$$=$1/$3;}
|   '('e')' {$$=$2;}
| NUM       {$$=$1;};
%%

This is grammar to evaluate the expression.

  • expr is the grammar defined as e. if e is valid, result is printed on screen. result is yylval and in this case, parameter 1. so we display contents of $1 and terminate.
  • e is defined as multiple sub grammars. but input scan is done according to precedence defined in the declaration section after tokens.
  • e+e: here its addition. so $$ is updated with $1+$3. (because $1 is 'e' here. $2 is '+' symbol. $3 is 'e' again, we need to add 'e' and 'e'. so $1+$3.).
  • same way, e-e, e/e, e*e are evaluated as said above.
  • (e): here opening parentheses is $1, e is $2, closing parentheses  is $3. so $$=$2.
  • If NUM is the returned token from lexer, then we assign the yylval value to $$. remember yylval is the evaluated answer in lex part.
  • So after all this processing, if e is valid, $1 contains evaluated answer, we simply print it.


main()
{
printf(" Type the Expression & Press Enter key\n");  
yyparse();
printf(" Valid Expression \n");
}
yyerror()

printf(" Invalid Expresion!!!!\n");  
exit(0);  
}

main function. ask for expression. if calculated normally, display "valid expression".
Otherwise, yyerror() will be executed displaying "Invalid expression".
remember we are displaying the evaluated answer in the grammer itself, when 'e' is valid.

He is a simple passionate tech freak who himself is an engineering student at Canara Engineering college. He likes App Development, Web designing, Blogging, Youtubing, Debugging and also is a CodeGeek!

Sharing is sexy!

Related Articles

Share your views about this article!