2012-07-15 07:32:04 +08:00
|
|
|
grammar T;
|
2012-07-22 07:27:00 +08:00
|
|
|
s : stat ;
|
|
|
|
stat: expr[0] NEWLINE
|
|
|
|
| ID '=' expr[0] NEWLINE
|
|
|
|
| NEWLINE
|
|
|
|
;
|
2012-06-08 08:31:18 +08:00
|
|
|
|
2012-07-22 07:27:00 +08:00
|
|
|
expr[int _p]
|
|
|
|
: ( INT
|
|
|
|
| ID
|
|
|
|
| '(' expr[0] ')'
|
|
|
|
)
|
|
|
|
( {5 >= $_p}? ('*'|'/') expr[6]
|
|
|
|
| {4 >= $_p}? ('+'|'-') expr[5]
|
|
|
|
)*
|
|
|
|
;
|
2012-06-18 07:56:26 +08:00
|
|
|
|
2012-07-22 07:27:00 +08:00
|
|
|
/*
|
|
|
|
expr: expr ('*'|'/') expr # MulDiv
|
|
|
|
| expr ('+'|'-') 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
|