Friday, April 12, 2019

lex yacc program to add and subtract two numbers


lex.l file
%{
#include<stdio.h>
#include "y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {yylval= atoi(yytext);return num;}
[+-] {return yytext[0];}
[ \t]+ ;
\n {return 0;}
%%


yacc.y file
%{
     #include<stdio.h>
%}
%token num
%%
sta: exp { printf("%d",$1);}
exp: num '+' num {$$=$1 + $3;}
    | num '-' num {$$=$1 - $3;}
     ;
%%
main()
{
   yyparse();
}
yyerror(char *s)
{
  printf("errror:%s",s);
}

ScreenShot:




No comments:

Post a Comment