Skip to content

Instantly share code, notes, and snippets.

@tristan
Created September 30, 2010 13:01
Show Gist options
  • Save tristan/604531 to your computer and use it in GitHub Desktop.
Save tristan/604531 to your computer and use it in GitHub Desktop.
grammar SimpleCalc;
options {
language=JavaScript;
output=AST;
ASTLabelType=CommonTree;
}
tokens {
NEGATION;
}
evaluate
: math EOF
;
math
: term (('+' | '-')^ term)*
;
term
: unary (('*' | '/' | '%')^ unary)*
;
unary
: ('+'! | negation^)* factor
;
negation
: '-' -> NEGATION
;
factor
: FLOAT
| INT
| IDENT
| '('! math ')'!
IDENT: ('a'..'z' | 'A'..'Z')('a'..'z' | 'A'..'Z' | '0'..'9' | '_')*;
FLOAT: ('0'..'9'+ '.' '0'..'9'+) | ('0'..'9' '.' '0'..'9'+ 'E' ('+' | '-')? '0'..'9'+);
INT: '0'..'9'+ ;
WS: ( ' ' | '\t' | '\r' | '\n')+ {$channel=HIDDEN;} ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment