ANTLR™ (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It’s widely used to build languages, tools, and frameworks. From a grammar, ANTLR generates a parser that can build and walk parse trees.
# Download $ cd /usr/local/lib $ curl -O https://www.antlr.org/download/antlr-4.7.2-complete.jar
# Add antlr-4.7.2-complete.jar to your CLASSPATH # Create aliases for the ANTLR Tool, and TestRig $ vim ~/.bashrc export CLASSPATH=".:/usr/local/lib/antlr-4.7.2-complete.jar:$CLASSPATH" alias antlr4='java -Xmx500M -cp "/usr/local/lib/antlr-4.7.2-complete.jar:$CLASSPATH" org.antlr.v4.Tool' alias grun='java -Xmx500M -cp "/usr/local/lib/antlr-4.7.2-complete.jar:$CLASSPATH" org.antlr.v4.gui.TestRig'
$ source ~/.bashrc
编写源文件
1 2 3 4 5 6
$ vim Benedict.g4 // Define a grammar called Benedict grammar Benedict; r : 'hello' Name ; // match keyword hello followed by an identifier Name : [a-z]+ ; // match lower-case identifiers WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
/** * Enter a parse tree produced by the {@code Colon} * labeled alternative in {@link benedictParser#say}. * @param ctx the parse tree */ voidenterColon(benedictParser.ColonContext ctx); /** * Exit a parse tree produced by the {@code Colon} * labeled alternative in {@link benedictParser#say}. * @param ctx the parse tree */ voidexitColon(benedictParser.ColonContext ctx);