<br />//   Yacc File (.y) //calculator %{ #include<stdio.h> #include<stdlib.h> void yyerror(char *s); %} %token NAME NUM %% statement: NAME'='expression |expression{printf("\n%d\n",$1);}; expression:expression'+'NUM{$$=$1+$3;} |expression'-'NUM{$$=$1-$3;} |expression'/'NUM{$$=$1/$3;} |expression'*'NUM{$$=$1*$3;} |NUM{$$=$1;}; %% int main() { while(yyparse()); } yyerror(char *s) { fprintf(stdout,"\n%s",s); } // Lex file (.l) %{ #include<stdio.h> #include<stdlib.h> #include"y.tab.h" extern int yylval; %} %% [0-9]+ {yylval=atoi(yytext);return NUM;} [ \t] {;} \n return 0; . return yytext[0]; %% /* To Compile : 1) yacc -d FileName.y 2) lex FileName.l 3) gcc lex.yy.c y.tab.c -ll 4) ./a.out */
Output :-
5+6
11