Created
March 30, 2013 18:46
-
-
Save mattmcd/5277890 to your computer and use it in GitHub Desktop.
ANTLR4 simple expression grammar. Note that left recursion is now allowed and operator precedence is just order of definition.
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 Expr; | |
// Need to call recursive rule expr from non-recursive rule | |
r : expr+ ; | |
// ANTLR4 : Left recursion! | |
// Operator precedence matches order of definition | |
expr : '-' expr // Unary minus | |
| expr ('*' | '/' ) expr | |
| expr ('+' | '-' ) expr | |
| '(' expr ')' | |
| INT | |
| ID | |
; | |
INT : [0-9]+ ; | |
ID : [a-z]+ ; | |
WS : [ \t\r\n]+ -> skip; |
Thank you a lot, I couldn't deal with right order of operations for a long time, and now after I run into your example, it finally works as I expected!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Testing: