- %{
- #include <stdio.h>
- int flag = 0;
- int yylex();
- void yyerror(const char *s);
- %}
- %token NUMBER
- %left '+' '-'
- %left '*' '/' '%'
- %left '(' ')'
- %%
- ArithmeticExpression: E{
- printf("\nResult=%d\n", $$);
- return 0;
- };
- 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;}
- ;
- %%
- void main()
- {
- printf("\nEnter Any Arithmetic Expression which can have operations Addition, Subtraction, Multiplication, Division, Modulus and Round brackets:\n");
- yyparse();
- if (flag == 0)
- printf("\nEntered arithmetic expression is Valid\n\n");
- }
- void yyerror(const char *s)
- {
- printf("\nEntered arithmetic expression is Invalid\n\n");
- flag = 1;
- }
- %{
- #include<stdio.h>
- #include "y.tab.h"
- extern int yylval;
- %}
- %%
- [0-9]+ {
- yylval=atoi(yytext);
- return NUMBER;
- }
- [\t] ;
- [\n] return 0;
- . return yytext[0];
- %%
- int yywrap()
- {
- return 1;
- }
[text] q5
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new:
File Description
- q5
- Paste Code
- 28 Mar-2024
- 1.03 Kb
You can Share it: