Advertisement
DrBoat

Untitled

Jan 29th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. grammar Calculator;
  2.  
  3. !e [returns float a]:
  4. t ea {a = t.a + ea.a;}
  5. ;
  6. ea [returns float a]:
  7. PLUS t ea {a = t.a + ea.a;}
  8. | MINUS t ea {a = ea.a - t.a;}
  9. | {a = 0;}
  10. ;
  11. t [returns float a]
  12. : d ta {a = d.a * ta.a;}
  13. ;
  14. ta [returns float a]
  15. : MUL d ta {a = d.a * ta.a;}
  16. | DIV d ta {a = ta.a / d.a;}
  17. | {a = 1;}
  18. ;
  19. d [returns float a]
  20. : f pa {a = (float)Math.pow(f.a, pa.a);}
  21. ;
  22. pa [returns float a]
  23. : POWER f pa {a = (float)Math.pow(f.a, pa.a);}
  24. | {a = 1;}
  25. ;
  26. f [returns float a]
  27. : NUMBER {a = Integer.parseInt(NUMBER.value);}
  28. | LB e RB {a = e.a;}
  29. ;
  30.  
  31. PLUS
  32. = '+'
  33. ;
  34.  
  35. MUL
  36. = '*'
  37. ;
  38.  
  39. MINUS
  40. = '-'
  41. ;
  42.  
  43. DIV
  44. = '/'
  45. ;
  46.  
  47. NUMBER
  48. : '([1-9][0-9]*)|(0)'
  49. ;
  50.  
  51. POWER
  52. = '**'
  53. ;
  54.  
  55. LB
  56. = '('
  57. ;
  58.  
  59. RB
  60. = ')'
  61. ;
  62.  
  63. WS -> '[ \t\r\n]'
  64. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement