Skip to content

Instantly share code, notes, and snippets.

@RubenCordeiro
Created April 7, 2014 17:52
Show Gist options
  • Save RubenCordeiro/10025295 to your computer and use it in GitHub Desktop.
Save RubenCordeiro/10025295 to your computer and use it in GitHub Desktop.
grammar LabeledExpr;
prog: stat+ ;
stat: expr NEWLINE # printExpr
| ID '=' expr NEWLINE # assign
| NEWLINE # blank
;
expr: expr op=('*'|'/') expr # MulDiv
| expr op=('+'|'-') expr # AddSub
| INT # int
| ID # id
| '(' expr ')' # parens
;
MUL : '*' ; // assigns token name to '*' used above in grammar
DIV : '/' ;
ADD : '+' ;
SUB : '-' ;
ID : [a-zA-Z]+ ; // match identifiers
INT : [0-9]+ ; // match integers
NEWLINE:'\r'? '\n' ; // return newlines to parser (is end-statement signal)
WS : [ \t]+ -> skip ; // toss out whitespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment