Enter your email address & Win:

System Software Lab Programs ( Lex and Yaac Programs ) 5a

// June 12th, 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

………………………………………………………………………………………………………………………….

5) a. Program to evaluate an arithmetic expression involving operators +, -, * and /.

5a.l

%{
#include “y.tab.h”
extern int yylval;
%}

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

————————————————-

5a.y
/* 5a.y */
%{
#include<stdio.h>
%}
%token NUM
%left ‘-”+’
%left ‘*”/’

%%
start: expr    {printf(“\nValid expression.\nValue: %d”,$1);}

expr: expr’+'expr   {$$=$1+$3;}
|expr’-'expr    {$$=$1-$3;}
|expr’*'expr    {$$=$1*$3;}
|expr’/'expr     {if($3==0)
yyerror(“Divide by zero”);
else
$$=$1/$3;
}
|’(‘ expr ‘)’  {$$=$2;}
|NUM
;
%%

main()
{
printf(“\nEnter an expression: “);
yyparse();
return(0);
}

int yyerror(char *msg)
{
printf(“\n%s”,msg);
printf(“\nInvalid expression”);
exit(0);
}


If you enjoyed this post, make sure you subscribe to my RSS feed!

Related posts:

  1. System Software Lab Programs ( Lex and Yaac Programs ) 4a
  2. System Software Lab Programs ( Lex and Yaac Programs ) 6a
  3. System Software Lab Programs ( Lex and Yaac Programs ) 5b
  4. System Software Lab Programs ( Lex and Yaac Programs )2a.l
  5. System Software Lab Programs ( Lex and Yaac Programs )1b
blog comments powered by Disqus
Get Adobe Flash playerPlugin by wpburn.com wordpress themes