===== calc.y ===== %{ #include int flag = 0; %} %union { int ival; } %token NUMBER %type E %left '+' '-' %left '*' '/' '%' %% ArithmeticExpression: E { printf("\nResult = %d\n", $1); } ; E: E '+' E { $$ = $1 + $3; } | E '-' E { $$ = $1 - $3; } | E '*' E { $$ = $1 * $3; } | E '/' E { $$ = $1 / $3; } | E '%' E { $$ = $1 % $3; } | '(' E ')' { $$ = $2; } | NUMBER { $$ = $1; } ; %% int main() { printf("\n Enter any arithmetic expression \n"); yyparse(); if(flag == 0) printf("\nEntered arithmetic expression is Valid\n"); return 0; } void yyerror(char *s) { printf("\nEntered arithmetic expression is Invalid\n"); flag = 1; } ===== lex.l ===== %{ #include "y.tab.h" #include %} %% [0-9]+ { yylval.ival = atoi(yytext); return NUMBER; } [ \t]+ ; \n { return 0; } . { return yytext[0]; } %% int yywrap() { return 1; } ===== run ===== yacc -d calc.y lex lex.l gcc lex.yy.c y.tab.c -o calc ./calc