$ cd /usr/local/lib $ sudo curl -O https://www.antlr.org/download/antlr-4.7.2-complete.jar // 设置环境变量 $ vim .bash_profile $ export CLASSPATH=".:/usr/local/lib/antlr-4.7.2-complete.jar:$CLASSPATH" $ alias antlr4='java -jar /usr/local/lib/antlr-4.7.2-complete.jar' $ alias grun='java org.antlr.v4.gui.TestRig' $ wq! // 保存 $ source .bash_profile // 测试,以下则成功: $ antlr4 ANTLR Parser Generator Version 4.7.2 -o ___ specify output directory where all output is generated -lib ___ specify location of grammars, tokens files -atn generate rule augmented transition network diagrams -encoding ___ specify grammar file encoding; e.g., euc-jp -message-format ___ specify output style for messages in antlr, gnu, vs2005 -long-messages show exception details when available for errors and warnings -listener generate parse tree listener (default) -no-listener don't generate parse tree listener -visitor generate parse tree visitor -no-visitor don't generate parse tree visitor (default) -package ___ specify a package/namespace for the generated code -depend generate file dependencies -D<option>=value set/override a grammar-level option -Werror treat warnings as errors -XdbgST launch StringTemplate visualizer on generated code -XdbgSTWait wait for STViz to close before continuing -Xforce-atn use the ATN simulator for all predictions -Xlog dump lots of logging info to antlr-timestamp.log -Xexact-output-dir all output goes into -o dir regardless of paths/package
第一个测试程序:
$ mkdir test $ cd test/ $ vim Hello.g4 输入: // Define a grammar called Hello grammar Hello; r : 'hello' ID ; // match keyword hello followed by an identifier ID : [a-z]+ ; // match lower-case identifiers WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines $ wq! // 保存 $ antlr4 Hello.g4 $ ls Hello.g4 HelloBaseListener.java HelloLexer.tokens Hello.interp HelloLexer.interp HelloListener.java Hello.tokens HelloLexer.java HelloParser.java $ javac Hello*.java $ ls Hello.g4 HelloLexer.java Hello.interp HelloLexer.tokens Hello.tokens HelloListener.class HelloBaseListener.class HelloListener.java HelloBaseListener.java HelloParser$RContext.class HelloLexer.class HelloParser.class HelloLexer.interp HelloParser.java $ grun Hello r -gui // 按回车 hello test // 输入这个,再按回车 ^D // 按ctrl+D 就出现gui图形界面了