Getting the name of the currently executing rule

288 views
Skip to first unread message

Dov Rosenberg

unread,
Jul 22, 2015, 10:02:07 PM7/22/15
to antlr-discussion
how do I get the name of the currently executing rule while using a listener? I found the enterEveryRule() but I dont see anything that provides the name of the rule that is executing like the -trace option on the TestRig

Thanks in advance

Dov Rosenberg

Norman Dunbar

unread,
Jul 23, 2015, 7:12:51 AM7/23/15
to antlr-di...@googlegroups.com, Dov Rosenberg
Hi Dov,

This works:

In the java file for the listener:

public class tns2toadListener extends tnsnamesBaseListener
{
tnsnamesParser parser; // The Parser we are listening for.
String[] ruleNames; // The parser's list of rule names.
...

In the constructor for the listener, we need the parser as a parameter:

public tns2toadListener(tnsnamesParser parser, ....)
{
this.parser = parser;
this.ruleNames = parser.getRuleNames();
...

In the EveryRule listener method, we do this:

@Override
public void enterEveryRule(ParserRuleContext ctx)
{
String Rule = this.ruleNames[ctx.getRuleIndex()];
System.out.println("Rule = " + Rule);
...

The Rule string should contain the rule that is currently executing.

In the java file for the app itself, we have this:

ANTLRInputStream input = new ANTLRInputStream(iStream);
tnsnamesLexer lexer = new tnsnamesLexer(input);

CommonTokenStream tokens = new CommonTokenStream(lexer);
tnsnamesParser parser = new tnsnamesParser(tokens);
ParseTree tree = parser.tnsnames();

ParseTreeWalker tnsWalker = new ParseTreeWalker();

// Create my listener and pass in the parser etc.
tns2toadListener tnsListener = new tns2toadListener(parser, ...);
tnsWalker.walk(tnsListener, tree);


When running the finished app, I get something similar to the following:

Rule = tnsnames
Rule = alias_list
Rule = alias
Rule = description
Rule = address
Rule = protocol_info
Rule = tcp_protocol
Rule = tcp_params
Rule = tcp_parameter
Rule = tcp_tcp
Rule = tcp_parameter
Rule = tcp_host
Rule = host
Rule = tcp_parameter
Rule = tcp_port
Rule = port
Rule = connect_data
Rule = cd_params
Rule = cd_parameter
Rule = cd_server
Rule = cd_parameter
Rule = cd_service_name

and so on, and on and on ... ;-)

HTH

Cheers,
Norm.
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.

Terence Parr

unread,
Jul 23, 2015, 12:35:09 PM7/23/15
to antlr-di...@googlegroups.com, Dov Rosenberg
off top of my head, ask parser or interp for current ATN state. Ask the state for rule index it’s embedded in. then ask parser for name for that index.
T
--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dov Rosenberg

unread,
Jul 23, 2015, 1:06:42 PM7/23/15
to antlr-discussion, dovrose...@gmail.com
I did as Norman suggested and that worked great!!
Reply all
Reply to author
Forward
0 new messages