Skip to content

Instantly share code, notes, and snippets.

@panlw
Created August 29, 2013 02:06
Show Gist options
  • Save panlw/6373547 to your computer and use it in GitHub Desktop.
Save panlw/6373547 to your computer and use it in GitHub Desktop.
Antlr v4 Sample - MyLanguage.g4
grammar MyLanguage;
// Rules
code : line+ ;
line : expr NEWLINE
| NEWLINE
;
expr : '(' expr ')' # group
| expr ' & ' expr # and
| expr ' | ' expr # or
| lh=(var | fnCnt | fnSum) op=(EQ | NE | LE | LT | GE | GT) rh=(var | N) # compare
;
fnSum : 'SUM(' var ')' # fnSum1
| 'SUM(' vlist ')' # fnSum2
;
fnCnt : 'CNT(' var ')' # fnCnt1
| 'CNT(' vlist ')' # fnCnt2
;
var : W ; // Variable
vlist : (N | range) (',' (N | range))* // value list
range : N '-' N ; // value range
// Lexers
EQ : '=' ;
NE : '^=' ;
LE : '<=' ;
LT : '<' ;
GE : '>=' ;
GT : '>' ;
W : [a-zA-Z_][a-zA-Z0-9_]+ ; // Word
N : [0-9]+ ; // Number
NEWLINE : '\r'? '\n' ; // Newline
WS : [ \t]+ -> channel(HIDDEN) ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment