Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. grammar Calc
  2. addSub
  3.  
  4. addSub return [v : int]
  5. : mulDiv addSub2(<mulDiv.v>)
  6. <$v = addSub2.v;>
  7. ;
  8.  
  9. addSub2 (left : int) return [v : int]
  10. : ADD mulDiv <int next = left + mulDiv.v;>
  11. addSub2(next) <$v = addSub2.v;>
  12. | SUB mulDiv <int next = left - mulDiv.v;>
  13. addSub2(next) <$v = addSub2.v;>
  14. | <$v = left;>
  15. ;
  16.  
  17. mulDiv return [v : int]
  18. : scope mulDiv2(<scope.v>) <$v = mulDiv2.v;>
  19. ;
  20.  
  21. mulDiv2 (left : int) return [v : int]
  22. : MUL scope mulDiv2(<left * scope.v>) <$v = mulDiv2.v;>
  23. | DIV scope mulDiv2(<left / scope.v>) <$v = mulDiv2.v;>
  24. | <$v = left;>
  25. ;
  26.  
  27. factorial return [v : int]
  28. : OP addSub CP <$v = addSub.v;>
  29. | NUM <$v = Integer.valueOf(NUM);>
  30. ;
  31.  
  32. maybe return [v : char]
  33. : FAC <$v = '!';>
  34. | "" <$v = 'N';>
  35. ;
  36.  
  37.  
  38. scope return [v : int]
  39. : OP addSub CP maybe <if(maybe.v == 'N')$v = addSub.v; else $v = addSub.v * 40;>
  40. | SUB scope <$v = -scope.v;>
  41. | NUM maybe <if(maybe.v == 'N')$v = Integer.valueOf(NUM); else $v = Integer.valueOf(NUM) * 40;>
  42. ;
  43.  
  44. ADD : "+";
  45. SUB : "-";
  46. MUL : "*";
  47. FAC : "!";
  48. DIV : "/";
  49. OP : "(";
  50. CP : ")";
  51. NUM $ "([1-9][0-9]*)|(0)";
  52. skip -> "[ \n\r\t]+";
  53. EMP : "";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement