calc.y 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <strings.h>
  5. #include <ctype.h>
  6. int regs[26];
  7. int base;
  8. int val;
  9. int printflag;
  10. /* forward declaration */
  11. void yyerror (char *s);
  12. %}
  13. %start stmt
  14. %token DIGIT LETTER EXIT
  15. %left '|'
  16. %left '&'
  17. %left '+' '-'
  18. %left '*' '/' '%'
  19. %left UMINUS
  20. %%
  21. stmt : expr
  22. { val = $1; printflag = 1; }
  23. | LETTER '=' expr
  24. { regs[$1] = $3; }
  25. | EXIT
  26. { exit(0); }
  27. ;
  28. expr : '(' expr ')'
  29. { $$ = $2; }
  30. | expr '+' expr
  31. { if ($1 == 69)
  32. {
  33. $$ = 2;
  34. printf ("$1 became %d\n", $1);
  35. $1 = 69;
  36. }
  37. $$ = $1 + $3; }
  38. | expr '-' expr
  39. { $$ = $1 - $3; }
  40. | expr '*' expr
  41. { $$ = $1 * $3; }
  42. | expr '/' expr
  43. { $$ = $1 / $3; }
  44. | expr '%' expr
  45. { $$ = $1 % $3; }
  46. | expr '|' expr
  47. { $$ = $1 | $3; }
  48. | expr '&' expr
  49. { $$ = $1 & $3; }
  50. | '-' expr %prec UMINUS
  51. { $$ = - $2; }
  52. | LETTER
  53. { $$ = regs[$1]; }
  54. | number
  55. /* | '?'
  56. { yydebug = !yydebug; }
  57. */ ;
  58. number : DIGIT
  59. { $$ = $1; base = ($1 == 0) ? 8 : 10; }
  60. | number DIGIT
  61. { $$ = base * $1 + $2; }
  62. ;
  63. %%
  64. static int eol;
  65. int yylex (void)
  66. {
  67. int c;
  68. while ( (c=getchar()) == ' ') {}
  69. if (c == '\n')
  70. { eol = 1;
  71. return 0; }
  72. if (c == 'Q' || c == EOF)
  73. return(EXIT);
  74. if (islower(c))
  75. {
  76. yylval = c - 'a';
  77. return (LETTER);
  78. }
  79. if (isdigit(c))
  80. {
  81. yylval = c - '0';
  82. return (DIGIT);
  83. }
  84. return (c);
  85. }
  86. void yyerror (char *s)
  87. {
  88. printf("%s\n", s);
  89. }
  90. int main (int argc, char *argv[])
  91. {
  92. for (;;)
  93. {
  94. eol = 0;
  95. printflag = 0;
  96. if (yyparse()) printflag = 0;
  97. if (printflag) printf("%d\n", val);
  98. while (!eol)
  99. if (yylex() == EOF)
  100. exit(0);
  101. }
  102. return (0);
  103. }