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 parse trees and also generates a listener interface (or visitor) that makes it easy to respond to the recognition of phrases of interest.
ANTLR(ANother Tool for Language Recognition)是一个强大的生成Parser的工具,用来读取,处理,执行或者翻译结构文本或二进制文件。ANTLR将从grammar生成一个parser,可以构建parse tree 并且生成一个监听接口(或者visitor),并使你更简单的感受到短语识别的乐趣。
本例简介本例将使用ANTLR生成CSharp parser代码,对减乘除数学表达式进行Parse并执行。源码直接下载吧,点这里。
准备环境1.ANTLR是一个JAVA程序,所以需要JAVA环境,本例使用的是java version "1.8.0_181"。
2.下载ANTLR https://www.antlr.org/download.html 本例使用Complete ANTLR 4.9.1 Java binaries jar
命令行执行
java -jar antlr-4.9.1-complete.jar -Dlanguage=CSharp Simple.g4
Install-Package Antlr4.Runtime.Standard
执行读取到的数学表达式 private static int visit(SimpleParser.ExprContext context) { if (context.number() != null) { //Just a number return int.Parse(context.number().GetText()); } else if (context.BR_CLOSE() != null) { //Expression between brackets return visit(context.expr(0)); } else if (context.TIMES() != null) { //Expression * expression return visit(context.expr(0)) * visit(context.expr(1)); } else if (context.DIV() != null) { //Expression / expression return visit(context.expr(0)) / visit(context.expr(1)); } else if (context.PLUS() != null) { //Expression + expression return visit(context.expr(0)) + visit(context.expr(1)); } else if (context.MINUS() != null) { //Expression - expression return visit(context.expr(0)) - visit(context.expr(1)); } else { throw new Exception(); } } 读入输入测试 var input = "2 * 3"; AntlrInputStream inputStream = new AntlrInputStream(input); SimpleLexer lexer = new SimpleLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); SimpleParser parser = new SimpleParser(commonTokenStream); var result = visit(parser.expr()); Console.WriteLine($"input:{input},output:{result}"); 尾声本例简单介绍在CSharp中如何使用ANTLR生成一个Parser,感受了一波短语识别的乐趣,You feel me? 后续ANTLR详细的玩法再给大家分享。
ReferenceC# target for ANTLR 4
Antlr is Awesome