How can to walk the Ast tree to get every parameter and its type

67 views
Skip to first unread message

fera...@gmail.com

unread,
Feb 10, 2019, 2:43:25 PM2/10/19
to antlr-di...@googlegroups.com

If I have  a function like this 

int addition (int a, int b)
{
  int r;
  r=a+b;
  return r;
}

written in c++

I need to get the following information
return type  int ;   function name  addition ; parameters a, b ; parameters type: int, int

after generating the lexer listener and visitor  classes from the CPP14.g4 grammar file using antlr4 and IntelliJ IDEA Community Edition 2018.3.4 x64 (programming language java)
I was able to get return type and the name of the function like this


public class MethodCallListener extends CPP14BaseListener {
CPP14Parser parser;

public MethodCallListener (CPP14Parser parser) {
this.parser = parser;
}
@Override
public void enterFunctiondefinition(CPP14Parser.FunctiondefinitionContext ctx)
{

String name = ctx.declarator().start.getText();
String returntype = ctx.declspecifierseq().getText();
System.out.println("Return type: "+  returntype + "  ,Function name: " + name(;
}

Is there any way to get parameters and their  types ?
or here I have to use visitor and how can I do that ?
Any help will be appreciated!

Is their any idea how to get function calls using https://github.com/antlr/grammars-v4/tree/master/cpp  CPP14.g4 in the same way above?
there were for fuction declaraton but I could not catch the function calls any idea?



Reply all
Reply to author
Forward
0 new messages