Advertisement
Guest User

Calc.g4

a guest
Aug 30th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. grammar Calc;
  2. eval returns[value]
  3. : SPACE? expr SPACE? {$value = $expr.value;}
  4. ;
  5. expr returns[value]
  6. : me1=multExpr {$value = $me1.value;}
  7. (SPACE? '+' SPACE? me2=multExpr {$value += $me2.value;}
  8. |SPACE? '-' SPACE? me2=multExpr {$value -= $me2.value;})*
  9. ;
  10. multExpr returns[value]
  11. : a1=atom {$value = $a1.value;}
  12. (SPACE? '*' SPACE? a2=atom {$value *= $a2.value;}
  13. |SPACE? '/' SPACE? a2=atom {$value /= $a2.value;})*
  14. ;
  15. atom returns[value]
  16. : INT {$value = Number.parseInt($INT.text);}
  17. | '(' SPACE? expr SPACE? ')' {$value = $expr.value;}
  18. ;
  19. SPACE: [ \t]+;
  20. INT: [0-9]+;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement