Advertisement
sid11k

bison file

Nov 25th, 2020 (edited)
145
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. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "polyn.h"
  6. #include "symbtab.h"
  7.  
  8. extern int yylex();
  9. extern int yyparse();
  10. void yyerror(const char* s);
  11. %}
  12.  
  13.  
  14. %token T_INT
  15. %token T_IDENT T_POLYN
  16. %token T_PLUS T_MINUS T_MULTIPLY T_DIVIDE T_LROUND T_RROUND T_EQUAL
  17. %token T_NEWLINE T_QUIT
  18. %left T_PLUS T_MINUS
  19. %left T_MULTIPLY T_DIVIDE
  20.  
  21. %start input
  22.  
  23. %union {
  24. int intval;
  25. char *chstr;
  26. struct Polyn *p;
  27. }
  28.  
  29. %type <intval> expression T_INT
  30. %type <chstr> assign T_IDENT
  31. %type <p> poln T_POLYN
  32.  
  33. %%
  34.  
  35. input:
  36. %empty
  37. | input line
  38. ;
  39.  
  40. line:
  41. T_NEWLINE
  42. | expression T_NEWLINE { printf("\tResult: %i\n", $1); }
  43. | assign T_NEWLINE ;
  44. | T_QUIT T_NEWLINE { exit(0); }
  45. ;
  46.  
  47. assign:
  48. T_IDENT T_EQUAL poln {store($1, $3); }
  49.  
  50. ;
  51.  
  52. poln:
  53. T_POLYN ;
  54. | poln T_PLUS poln ;
  55. | poln T_MINUS poln ;
  56. ;
  57.  
  58. expression:
  59. T_INT { $$ = $1; }
  60. | expression T_PLUS expression { $$ = $1 + $3; }
  61. | expression T_MINUS expression { $$ = $1 - $3; }
  62. | expression T_MULTIPLY expression { $$ = $1 * $3; }
  63. | expression T_DIVIDE expression { $$ = $1 / $3; }
  64. | T_LROUND expression T_RROUND { $$ = $2; }
  65. ;
  66.  
  67. %%
  68.  
  69. int main() {
  70. while (yyparse());
  71. return 0;
  72. }
  73.  
  74. void yyerror(const char* s) {
  75. fprintf(stderr, "Parse error: %s\n", s);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement