Created
September 30, 2010 13:05
-
-
Save tristan/604540 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tree grammar SimpleCalcWalker; | |
options { | |
language=JavaScript; | |
tokenVocab=SimpleCalc; | |
ASTLabelType=CommonTree; | |
} | |
eval returns [var result] | |
: e=math EOF { result = e; } | |
; | |
math returns [var result] | |
: ^('+' op1=math op2=math) { result = op1 + op2; } | |
| ^('-' op1=math op2=math) { result = op1 - op2; } | |
| ^('*' op1=math op2=math) { result = op1 * op2; } | |
| ^('/' op1=math op2=math) { result = op1 / op2; } | |
| ^('%' op1=math op2=math) { result = op1 \% op2; } | |
| ^(NEGATION op1=math) { result = -op1; } | |
| FLOAT { result = parseFloat($FLOAT.text); } | |
| INT { result = parseInt($INT.text); } | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment