1
votes

This is my image structure ANTLR 4 parsing tree gui:

enter image description here

This my listener rule enterDeclaration :

   public void enterDeclaration(ADTCParser.DeclarationContext ctx) { 

    TokenStream tokens = parser.getTokenStream();
    String initDeclarationList = tokens.getText(ctx.initDeclarationList());
   }

to get tokens from initDeclarationList such as :

String initDeclarationList = tokens.getText(ctx.initDeclarationList());
String parameterDeclaration = ???

And my questions is "How to get tokens from sub-rule "parameterDeclaration" from " structure based on the image above ?

BTW, im using grammar C in ANTLR 4 this is link for grammar : https://github.com/antlr/grammars-v4/blob/master/c/C.g4

1
Need to show the relevant parser rules.GRosenberg
im using grammar C EBNF in ANTLR 4Fachrian Luthfi Ramadhan

1 Answers

1
votes

Like this:

ADTCParser.ParameterDeclarationContext context = parser.declaration()
        .initDeclaratorList()
        .initDeclarator()
        .declarator()
        .directDeclarator()
        .parameterTypeList()
        .parameterList()
        .parameterDeclaration();

where parser.declaration() is your ADTCParser.DeclarationContext ctx parameter.