Created
August 29, 2013 02:06
-
-
Save panlw/6373547 to your computer and use it in GitHub Desktop.
Antlr v4 Sample - MyLanguage.g4
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
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