Advertisement
AnindyaBiswas

Exp.yacc

May 25th, 2023
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1.  
  2.  
  3. %{
  4. //YACC with LEX
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. int yylex(void);
  8. void yyerror(char*);
  9. %}
  10.  
  11.  
  12. %start S
  13. %token L R NUM
  14. %left LOGICAL_AND LOGICAL_OR LOGICAL_NOT LOGICAL_EXOR
  15. %left REL_EQ REL_GE REL_LT REL_GT REL_LE
  16. %left BIT_AND BIT_OR BIT_EXOR
  17. %left SHIFT_LEFT SHIFT_RIGHT
  18.  
  19. %%
  20.  
  21. S : E { $$ = $1; printf("\n Result: %d", $$); }
  22.  
  23.  
  24. E : E LOGICAL_AND E { $$ = $1&&$3; printf("\n %d", $$);}
  25. | E LOGICAL_OR E { $$ = $1||$3; printf("\n %d", $$);}
  26. | LOGICAL_NOT E { $$ = ~$2; printf("\n %d", $$);}
  27. | E LOGICAL_EXOR E { $$ = $1^$3; printf("\n %d", $$);}
  28.  
  29. | E BIT_AND E { $$ = $1&$3; printf("\n %d", $$);}
  30. | E BIT_OR E { $$ = $1|$3; printf("\n %d", $$);}
  31. | E BIT_EXOR E { $$ = $1^$3; printf("\n %d", $$);}
  32.  
  33. | E REL_EQ E { $$ = $1==$3; printf("\n %d", $$);}
  34. | E REL_GE E { $$ = $1>=$3; printf("\n %d", $$);}
  35. | E REL_LE E { $$ = $1<=$3; printf("\n %d", $$);}
  36. | E REL_GT E { $$ = $1>$3; printf("\n %d", $$);}
  37. | E REL_LT E { $$ = $1<$3; printf("\n %d", $$);}
  38.  
  39. | E SHIFT_LEFT E { $$ = $1<<$3; printf("\n %d", $$);}
  40. | E SHIFT_RIGHT E { $$ = $1>>$3; printf("\n %d", $$);}
  41.  
  42.  
  43. | L E R {$$ = $2;}
  44. | NUM {$$ = $1;}
  45.  
  46.  
  47. %%
  48.  
  49. int main()
  50. {
  51. return (yyparse());
  52. return 0;
  53. }
  54.  
  55. void yyerror(char *S)
  56. {
  57. fprintf(stderr, "%s\n", S);
  58. }
  59.  
  60. yywrap()
  61. {
  62. return 1;
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement