简体   繁体   English

通过覆盖 enterFunctionDefinition 获取 C Antlr 中所有 function 名称的列表

[英]Fetching list of all the function names in C Antlr by overriding enterFunctionDefinition

I want to fetch all the function names in a.c file.我想在 a.c 文件中获取所有 function 名称。 Example foo.c示例 foo.c

#include <stdio.h>

void insert(MYDATATYPE* myptr)
{
  printf("insert");
  return;
}

void delete(MYDATATYPE* myptr)
{
  printf("delete");
  return;
}

I had overriden the enterFunctionDefinition method like this我已经像这样覆盖了 enterFunctionDefinition 方法

@Override
public void enterFunctionDefinition(@Nullable FunctionDefinitionContext ctx) {
if (ctx == null) {
    return;
}
final String context = ctx.start.getInputStream().getText(Interval.of(ctx.start.getStartIndex(), ctx.stop.getStopIndex()));
System.out.println(context);
}

The context that is printed is:打印的上下文是:

void insert(MYDATATYPE* myptr)
{
  printf("insert");
  return;
}

void delete(MYDATATYPE* myptr)
{
  printf("delete");
  return;
}

But I want to fetch only the name of the functions.但我只想获取函数的名称。 I tried something like this:我试过这样的事情:

@Override
public void enterFunctionDefinition(@Nullable FunctionDefinitionContext ctx) {
if (ctx == null) {
    return;
}
if (ctx.declarationSpecifiers() != null && ! 
 ctx.declarationSpecifiers().declarationSpecifier().isEmpty()) {

 List<DeclarationSpecifierContext> declartionSpecifierContextList = 
 ctx.declarationSpecifiers().declarationSpecifier(); /* This is giving me only the first functions context */

    declartionSpecifierContextList.forEach(declartionSpecifierContext -> {
       declartionSpecifierContext.children.forEach(childContext -> {
          if (childContext.getChild(0) instanceof TypedefNameContext) {
              System.out.println(childContext.getChild(0).getText()); /* This prints 
              the name of the first function which in this case is : insert */
          }
       });
    });

  }
}

But I am getting only the first function from ctx.declarationSpecifiers().但我只从 ctx.declarationSpecifiers() 得到第一个 function。 How do I get the declarationSpecifiers for all the functions?如何获取所有函数的声明说明符?

Assuming you're using an unmodified version of the C grammar from the ANTLR4 repo , This small listener produces both function names:假设您使用的是来自 ANTLR4 存储库的 C 语法的未修改版本,这个小型侦听器会生成两个 function 名称:

class FunctionNameListener extends CBaseListener {

    @Override
    public void enterFunctionDefinition(CParser.FunctionDefinitionContext ctx) {
        if (ctx.declarationSpecifiers() == null) return;

        for (CParser.DeclarationSpecifierContext child : ctx.declarationSpecifiers().declarationSpecifier()) {
            if (child.typeSpecifier() != null && child.typeSpecifier().typedefName() != null) {
                System.out.println(child.typeSpecifier().typedefName().Identifier());
                break;
            }
        }
    }
}

Tested with the following class:使用以下 class 测试:

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

public class Main {

    public static void main(String[] args) throws Exception {

        String source = "#include <stdio.h>\n" +
                "\n" +
                "void insert(MYDATATYPE* myptr)\n" +
                "{\n" +
                "  printf(\"insert\");\n" +
                "  return;\n" +
                "}\n" +
                "\n" +
                "void delete(MYDATATYPE* myptr)\n" +
                "{\n" +
                "  printf(\"delete\");\n" +
                "  return;\n" +
                "}";

        CLexer lexer = new CLexer(CharStreams.fromString(source));
        CParser parser = new CParser(new CommonTokenStream(lexer));

        ParseTreeWalker.DEFAULT.walk(new FunctionNameListener(), parser.compilationUnit());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM