Can I get a list of rule name/value pairs from a ParserInterpreter?

20 views
Skip to first unread message

Andre Francois

unread,
Aug 23, 2016, 10:46:39 AM8/23/16
to antlr-discussion
I see on https://github.com/antlr/antlr4/blob/master/doc/interpreters.md

public static ParseTree parse(String fileName, String combinedGrammarFileName,
                              String startRule) throws IOException {
    final Grammar g = Grammar.load(combinedGrammarFileName);
    LexerInterpreter lexEngine = g.createLexerInterpreter(new ANTLRFileStream(fileName));
    CommonTokenStream tokens = new CommonTokenStream(lexEngine);
    ParserInterpreter parser = g.createParserInterpreter(tokens);
    ParseTree t = parser.parse(g.getRule(startRule).index);
    System.out.println("parse tree: "+t.toStringTree(parser));
    return t;
}

but how can I use the tree to get a list of rule names and values?

Ian Utley

unread,
Aug 25, 2016, 6:28:27 AM8/25/16
to antlr-discussion
What do you mean by get a list of rule names and values? Is it that you want to debug the parse? If so you could use 
    org.antlr.v4.gui.Trees.inspect(t,parser) to give you a visual representation of the parse tree.

Andre Francois

unread,
Aug 25, 2016, 10:29:53 AM8/25/16
to antlr-discussion
Thanks.  What I did instead was create a Listener with a enterEveryRule override which printed the rule name and value using the context. I then used the ParseTreeWalker to walk the tree.
Reply all
Reply to author
Forward
0 new messages