home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / examples / calc / Parser.grm < prev   
Encoding:
Text File  |  1997-08-18  |  641 b   |  29 lines  |  [TEXT/R*ch]

  1. %token <int> INT
  2. %token PLUS MINUS TIMES DIV UMINUS
  3. %token LPAREN RPAREN
  4. %token EOL EOF
  5.  
  6. %left PLUS MINUS        /* lowest precedence */
  7. %left TIMES DIV         /* medium precedence */
  8. %nonassoc UMINUS        /* highest precedence */
  9.  
  10. %type <int> Expr
  11.  
  12. %start Main             /* the entry point */
  13. %type <int> Main
  14.  
  15. %%
  16.  
  17. Main:
  18.     Expr EOL                { $1 }
  19. ;
  20. Expr:
  21.     INT                     { $1 }
  22.   | LPAREN Expr RPAREN      { $2 }
  23.   | Expr PLUS Expr          { $1 + $3 }
  24.   | Expr MINUS Expr         { $1 - $3 }
  25.   | Expr TIMES Expr         { $1 * $3 }
  26.   | Expr DIV Expr           { $1 div $3 }
  27.   | UMINUS Expr             { ~ $2 }
  28. ;
  29.