使用ANTLR4解析公式 [英] Parse a formula using ANTLR4

查看:1021
本文介绍了使用ANTLR4解析公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ANTLR4将数学公式解析为LaTeX的子集。例如,它应解析(a + 4)/(b * 10) \ frac {a + 4} {b\cdot 10 }

I am trying to parse a mathematical formula to a subset of LaTeX using ANTLR4. For example it should parse (a+4)/(b*10) to \frac{a+4}{b\cdot 10}.

我的简单语法创建了这样一棵树:

My simple grammar creates a tree like this:

现在我正在尝试实现解析树监听器在遍历树时以某种方式构造LaTeX字符串。在这里,我失败了,因为要构造一个像 \frac {} {} 这样的字符串,它必须以递归方式构建。然而,解析树walker会一个接一个地访问一个树节点(据我所知,以广度优先的方式)。

Now I am trying to implement parse tree listeners to somehow construct the LaTeX String while the tree is traversed. Here, I am failing because to construct a String like \frac{}{} it has to be built recursively. The parse tree walker, however, visits one tree node after the other (in a breadth-first way as far as I can tell).

我读过有关解析的内容树访客可能是我需要的。但我无法找到一些示例如何应用这些访问者。

I've read about parse tree visitors that might be what I need. But I wasn't able to find some examples how these visitors are applied.

您能举例说明在这种特殊情况下如何使用解析树监听器/访问者吗?您是否认为我在解析器中使用ANTLR的方法首先有意义?

Could you provide an example how parse tree listeners/visitors can be used in this particular case? Do you think my approach to use ANTLR for the parser makes sense in the first place?

推荐答案

您可以创建一个解析树助行器通过实现 ParseTreeVisitor 接口。为了便于使用,您可以在编译语法时指定Antlr以生成基本访问者(在Antlrworks中,运行 - >生成识别器 - >下一步 - >生成访问者 - >下一步 - >完成)。基本访问者将被称为 MyGrammarBaseVisitor 。请注意,访问者具有泛型类型T,每个访问方法都应该返回。我建议使用 Void 进行手动操作,或者 String ,以便在代码生成过程中使用。

You can create a parse tree walker by implementing the ParseTreeVisitor interface. For ease of use, you can specify Antlr to generate a base visitor when compiling the grammar (in Antlrworks, Run->Generate Recognizer->Next->Generate Visitor->Next->Finish). The base visitor will be called MyGrammarBaseVisitor. Note that the visitor has a generic type T, which every single visit method should return. I recommend using Void for manual manipulation or String for ease of use during code generation.

在扩展基本访问者之后(我假设我们在处理 String ),您需要覆盖访问方法。这些方法以您拥有的语法规则命名。这些方法中的每一个都将收到 ParserContext ctx 参数,您可以使用该参数访问子规则和/或获取终端值。例如,你可以这样做:

After you extend the base visitor (I'll assume here we're dealing with String), you need to override the visit methods. These methods are named after the grammar rules you have. Each of these methods will receive a ParserContext ctx parameter, which you use to visit child rules and/or get terminal values. For example, you could do this:

class MyVisitor extends MyGrammarBaseVisitor<String> {
    @Override
    public String visitMultiplicative(MyGrammarParser.MultiplicativeContext ctx) {
        if (ctx.opMult().getText().equals("/")) return "\\frac{" + visit(ctx.expr(0)) + "}{" + visit(ctx.expr(1)) + "}";
        else return visit(ctx.expr(0)) + "\\cdot " + visit(ctx.expr(1));
    }
    // visit methods for other rules...
}

我假设您的乘法规则看起来像 multiplicative:expr opMult expr; opMult:'*'| '/'; 您可以在中找到更多信息。最终的Antlr 4参考。您还可以在 Antlr文档中找到更多信息和示例。

I'm assuming your multiplicative rule looks like multiplicative: expr opMult expr; opMult: '*' | '/'; You can find more information in The Definitive Antlr 4 Reference. You may also find more information and examples in the Antlr documentation.

这篇关于使用ANTLR4解析公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆