[text] q5

Viewer

  1. %{
  2. #include <stdio.h>
  3. int flag = 0;
  4. int yylex();
  5. void yyerror(const char *s);
  6. %}
  7.  
  8. %token NUMBER
  9. %left '+' '-'
  10. %left '*' '/' '%'
  11. %left '(' ')'
  12.  
  13. %%
  14.  
  15. ArithmeticExpression: E{
  16.     printf("\nResult=%d\n", $$);
  17.     return 0;
  18. };
  19. E: E '+' E {$$ = $1 + $3;}
  20.  | E '-' E {$$ = $1 - $3;}
  21.  | E '*' E {$$ = $1 * $3;}
  22.  | E '/' E {$$ = $1 / $3;}
  23.  | E '%' E {$$ = $1 % $3;}
  24.  | '(' E ')' {$$ = $2;}
  25.  | NUMBER {$$ = $1;}
  26.  ;
  27.  
  28. %%
  29.  
  30. void main()
  31. {
  32.     printf("\nEnter Any Arithmetic Expression which can have operations Addition, Subtraction, Multiplication, Division, Modulus and Round brackets:\n");
  33.     yyparse();
  34.     if (flag == 0)
  35.         printf("\nEntered arithmetic expression is Valid\n\n");
  36. }
  37.  
  38. void yyerror(const char *s)
  39. {
  40.     printf("\nEntered arithmetic expression is Invalid\n\n");
  41.     flag = 1;
  42. }
  43.  
  44.  
  45.  
  46.  
  47.  
  48. %{
  49. #include<stdio.h>
  50. #include "y.tab.h"
  51. extern int yylval;
  52. %}
  53.  
  54. %%
  55. [0-9]+ {
  56.     yylval=atoi(yytext);
  57.     return NUMBER;
  58. }
  59. [\t] ;
  60. [\n] return 0;
  61. . return yytext[0];
  62. %%
  63.  
  64. int yywrap()
  65. {
  66.     return 1;
  67. }

Editor

You can edit this paste and save as new: