ANTLR简介

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

准备语法文件Simple.g4

grammar Simple;

calc: expr EOF;

expr
:BR_OPEN expr BR_CLOSE
|expr TIMES expr
|expr DIV expr
|expr PLUS expr
|expr MINUS expr
|number
;

number: NUMBER;

PLUS:   'plus'  | '+';
MINUS:  'minus' | '-';
TIMES:  'times' | '*';
DIV:    'div'   | '/';
![](https://img2020.cnblogs.com/blog/803166/202102/803166-20210227232628613-1602253693.jpg)


NUMBER: '-'? [0-9]+;
BR_OPEN: '(';
BR_CLOSE: ')';

WS: [ \t\r\n]+ -> skip;

生成CSharp代码

命令行执行
java -jar antlr-4.9.1-complete.jar -Dlanguage=CSharp Simple.g4

将生成文件添加至CSharp Project

CSharp Project添加ANTLR runtime引用

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详细的玩法再给大家分享。

Reference

C# target for ANTLR 4
Antlr is Awesome

版权声明:本文为JayWist原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/JayWist/p/14457878.html