home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume24 / hp / hp.gram.y < prev    next >
Text File  |  1991-03-13  |  2KB  |  146 lines

  1. /* yacc grammar for hp reverse polish calculator */
  2.  
  3. %{
  4. #include "y.tab.h"
  5.  
  6. #include "hp.h"
  7.  
  8. int i;
  9. extern int sound();
  10. extern double ctod(), pow(), fmod();
  11. %}
  12.  
  13. %start expression
  14. %term DOT DIGIT BIGP LITTLEP PLUS MINUS STAR SLASH PERCENT POW
  15. %term AND OR NOT EQ LT GT LE GE BIGD LITTLED QUIT
  16.  
  17. %%
  18. expression : constant_or_command_list
  19.     |
  20.     ;
  21.  
  22. constant_or_command_list : constant_or_command_list constant_or_command
  23.     | constant_or_command
  24.     ;
  25.  
  26. constant_or_command : constant
  27.     | command
  28.     ;
  29.  
  30. constant : DOT    { push(ctod(line, & scanptr)); scanptr--; }
  31.     | DIGIT    { push(ctod(line, & scanptr)); scanptr--; }
  32.     ;
  33.  
  34. command : LITTLEP    { if (sound(1)) printf("%lf\n", stack[sp]); }
  35.  
  36.     | BIGP    {
  37.             for (i = 1; i < sp; i++)
  38.                 printf("%lf\n", stack[i]);
  39.         }
  40.  
  41.     | PLUS    {
  42.             if (sound(2)) {
  43.                 stack[sp - 1] += stack[sp];
  44.                 sp--;
  45.             }
  46.         }
  47.  
  48.     | MINUS    {
  49.             if (sound(2)) {
  50.                 stack[sp - 1] -= stack[sp];
  51.                 sp--;
  52.             }
  53.         }
  54.  
  55.     | STAR    {
  56.             if (sound(2)) {
  57.                 stack[sp - 1] *= stack[sp];
  58.                 sp--;
  59.             }
  60.         }
  61.  
  62.     | SLASH    {
  63.             if (sound(2)) {
  64.                 stack[sp - 1] /= stack[sp];
  65.                 sp--;
  66.             }
  67.         }
  68.  
  69.     | PERCENT    {
  70.                 if (sound(2)) {
  71.                     stack[sp - 1] = fmod(stack[sp - 1], stack[sp]);
  72.                     sp--;
  73.                 }
  74.             }
  75.  
  76.     | POW    {
  77.             if (sound(2)) {
  78.                 stack[sp - 1] = pow(stack[sp - 1], stack[sp]);
  79.                 sp--;
  80.             }
  81.         }
  82.  
  83.     | LITTLED    { if (sound(1)) sp--; }
  84.  
  85.     | BIGD        { sp = 0; }
  86.  
  87.     | LT    {
  88.             if (sound(2)) {
  89.                 if (stack[sp - 1] < stack[sp])
  90.                     stack[sp - 1] = 1.0;
  91.                 else
  92.                     stack[sp - 1] = 0.0;
  93.                 sp--;
  94.             }
  95.         }
  96.  
  97.     | EQ    {
  98.             if (sound(2)) {
  99.                 if (stack[sp - 1] == stack[sp])
  100.                     stack[sp - 1] = 1.0;
  101.                 else
  102.                     stack[sp - 1] = 0;
  103.                 sp--;
  104.             }
  105.         }
  106.  
  107.     | GT    {
  108.             if (sound(2)) {
  109.                 if (stack[sp - 1] > stack[sp])
  110.                     stack[sp - 1] = 1.0;
  111.                 else
  112.                     stack[sp - 1] = 0.0;
  113.                 sp--;
  114.             }
  115.         }
  116.  
  117.     | AND    {
  118.             if (sound(2)) {
  119.                 if (stack[sp - 1] != 0.0 && stack[sp] != 0.0)
  120.                     stack[sp - 1] = 1.0;
  121.                 else
  122.                     stack[sp - 1] = 0.0;
  123.                 sp--;
  124.             }
  125.         }
  126.  
  127.     | OR    {
  128.             if (sound(2)) {
  129.                 if (stack[sp - 1] != 0.0 || stack[sp] != 0.0)
  130.                     stack[sp - 1] = 1.0;
  131.                 else
  132.                     stack[sp - 1] = 0.0;
  133.                 sp--;
  134.             }
  135.         }
  136.  
  137.     | NOT    {
  138.             if (sound(1)) if (stack[sp] != 0.0)
  139.                     stack[sp] = 0.0;
  140.                 else
  141.                     stack[sp] = 1.0;
  142.         }
  143.  
  144.     | QUIT    { exit (0); }
  145.     ;
  146.