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

查看:96
本文介绍了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/Java

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)

此代码的解析树的一部分

推荐答案

我认为大概是这样的:

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天全站免登陆