Advertisement
cd62131

Bison: orig.y

Jun 16th, 2017
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. %{
  2. #include <stdio.h>
  3. #include "orig.tab.h"
  4. #include "lex.yy.c"
  5. int yylex(void);
  6. void yyerror(const char *);
  7. extern FILE *yyin;
  8.  
  9. int memori = 0, memori_rsl = 0;
  10. %}
  11.  
  12. %token KAZU
  13. %token MPLUS MMINUS MPRINT MCLEAR
  14. %left '-' '+'
  15. %left '/' '*'
  16. %precedence VTOKEN
  17.  
  18. %%
  19.  
  20. source:
  21. /* empty */
  22. | source bun
  23. ;
  24.  
  25. bun:
  26. shiki ';' { printf("%d\n", $1); memori = $1; }
  27. | memori ';'
  28. ;
  29.  
  30. shiki:
  31. KAZU
  32. | tasu
  33. | hiku
  34. | waru
  35. | kakeru
  36. | '-' shiki %prec VTOKEN { $$ = -$2; }
  37. | '(' shiki ')' { $$ = $2; }
  38. ;
  39.  
  40. tasu:
  41. shiki '+' shiki { $$ = $1 + $3; }
  42. ;
  43.  
  44. hiku:
  45. shiki '-' shiki { $$ = $1 - $3; }
  46. ;
  47.  
  48. waru:
  49. shiki '/' shiki {
  50. if ($3) $$ = $1 / $3;
  51. else {
  52. $$ = 1;
  53. fprintf(stderr, "division by zero: \"%d / %d\"\n", $1, $3);
  54. }
  55. }
  56. ;
  57.  
  58. kakeru:
  59. shiki '*' shiki { $$ = $1 * $3; }
  60. ;
  61.  
  62. memori:
  63. MPLUS { memori_rsl += memori; }
  64. | MMINUS { memori_rsl -= memori; }
  65. | MPRINT { printf("memori_rsl = %d\n", memori_rsl); }
  66. | MCLEAR { memori = memori_rsl = 0; }
  67. ;
  68.  
  69. %%
  70.  
  71. void yyerror(const char *str) {
  72. fprintf(stderr, "error: %s\n", str);
  73. }
  74.  
  75. int main(int argc, char *argv[]) {
  76. if (argc != 2) return 1;
  77. yyin = fopen(argv[1], "r");
  78. return yyparse();
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement