ANTLR 4 - 树模式匹配 [英] ANTLR 4 - Tree pattern matching

查看:19
本文介绍了ANTLR 4 - 树模式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解 ANTLR 4 中的解析树匹配,所以为此我有以下 java 代码:

I am trying to understand parse tree matching in ANTLR 4, so for that I have the following java code:

package sampleCodes;

public class fruits {
  public static void main(String[] args){
    int a = 10;
    System.out.println(a);
  }
}

我正在使用 ANTLR 4 创建此代码的解析树.现在,我想使用树模式匹配函数来查找int a = 10;".GitHub 上有一个文档:https://github.com/antlr/antlr4/blob/master/doc/tree-matching.md 通过一个例子来解释这个(类似这样):

I am using ANTLR 4 to create a parse tree of this code. Now, I want to use tree pattern matching function to find "int a = 10;". There is a doc on GitHub: https://github.com/antlr/antlr4/blob/master/doc/tree-matching.md which explains this(something like this) by an example:

ParseTree t = ...; // assume t is a statement
ParseTreePattern p = parser.compileParseTreePattern("<ID> = <expr>;", MyParser.RULE_statement);
ParseTreeMatch m = p.match(t);
if ( m.succeeded() ) {...}

通过阅读本文档和其他一些资源,我了解到:

From reading through this doc and few other resources, what I understood was that in:

ParseTreePattern p = parser.compileParseTreePattern("<ID> = <expr>;", MyParser.RULE_statement);

作为第二个参数传递的规则必须能够正确解析作为第一个参数提供的模式.现在我使用的语法是这里给出的java:https://github.com/antlr/grammars-v4/tree/master/爪哇

The rule to be passed as second argument must be able to correctly parse the pattern provided as first argument. Now the grammar I am using is of java given here: https://github.com/antlr/grammars-v4/tree/master/java

JavaLexer.g4,JavaParser.g4

JavaLexer.g4, JavaParser.g4

我无法从上述 GitHub 文档中获得有关如何构建模式字符串及其相应规则的太多信息.所以我尝试了几种组合来获得匹配,但它们似乎都不起作用.例如:

I cannot get much info on how to structure your pattern string and its corresponding rule from the above GitHub doc. So I have tried few combinations to get the match, but none of them seems to work.For example:

ParseTreePattern p = parser.compileParseTreePattern("<variableDeclaratorId> = <variableInitializer>", parser.RULE_variableDeclarator);
ParseTreeMatch m = p.match(tree);
System.out.println(m);

这给出:

匹配失败;找到 0 个标签

Match failed; found 0 labels

我知道我的字符串模式肯定做错了.任何人都可以帮我解释这个模式匹配函数,并告诉我在这种情况下应该使用什么正确的参数.此外,提供指向一些有用资源的链接将非常有帮助,我可以在其中了解更多信息并处理复杂模式.(我在 ANTLR4 参考中找不到)

I know i am certainly doing something wrong in my string pattern. Can anyone please help me with explaining this pattern matching function, and tell what should be the correct arguments to be used in this case. Also, it will will be really helpful to provide links to some useful resources where I can learn more about this and work on complex patterns.(I could not find it in ANTLR4 reference)

此代码解析树的一部分

推荐答案

我想你想要的在 结合 XPath 和树模式匹配.

大概是这样的:

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.pattern.ParseTreeMatch;
import org.antlr.v4.runtime.tree.pattern.ParseTreePattern;

import java.util.List;

public class Main {

  public static void main(String[] args) {

    String source = "package sampleCodes;\n" +
            "\n" +
            "public class fruits {\n" +
            "\n" +
            "  static { int q = 42; }\n" +
            "\n" +
            "  public static void main(String[] args){\n" +
            "    int a = 10;\n" +
            "    System.out.println(a);\n" +
            "  }\n" +
            "}\n";

    JavaLexer lexer = new JavaLexer(CharStreams.fromString(source));
    JavaParser parser = new JavaParser(new CommonTokenStream(lexer));
    ParseTree tree = parser.compilationUnit();

    ParseTreePattern p = parser.compileParseTreePattern("<IDENTIFIER> = <expression>", JavaParser.RULE_variableDeclarator);
    List<ParseTreeMatch> matches = p.findAll(tree, "//variableDeclarator");

    for (ParseTreeMatch match : matches) {
      System.out.println("\nMATCH:");
      System.out.printf(" - IDENTIFIER: %s\n", match.get("IDENTIFIER").getText());
      System.out.printf(" - expression: %s\n", match.get("expression").getText());
    }
  }
}

导致以下输出:

MATCH:
 - IDENTIFIER: q
 - expression: 42

MATCH:
 - IDENTIFIER: a
 - expression: 10

这篇关于ANTLR 4 - 树模式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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