How do I broadcast debug events to multiple listeners?
Use the org.antlr.runtime.DebugEventHub class. Imagine that you want to build parse trees as in How can I build parse trees not ASTs? but you also want to pass the events to ANTLRWorks for interactive debugging. You need to use a hub that broadcasts events to the parse tree builder as well as the socket proxy for ANTLRWorks. Here is the altered test rig.
public class Test { public static void main(String[] args) throws Exception { // create the lexer attached to stdin ANTLRInputStream input = new ANTLRInputStream(System.in); PLexer lexer = new PLexer(input); // create the buffer of tokens between the lexer and parser CommonTokenStream tokens = new CommonTokenStream(lexer); // create a debug event listener that builds parse trees ParseTreeBuilder builder = new ParseTreeBuilder("prog"); // create a debug socket proxy to ANTLRWorks DebugEventSocketProxy AW = new DebugEventSocketProxy("P.g"); AW.handshake(); // create a hub that knows how to broadcast to the parse tree builder // and the ANTLRWorks proxy DebugEventHub hub = new DebugEventHub(builder, AW); // create the parser attached to the token buffer // and tell it to fire events to the hub PParser parser = new PParser(tokens, hub); // launch the parser starting at rule prog parser.prog(); // print the resulting parse tree System.out.println(builder.getTree().toStringTree()); } }
Note that you will have to send in the entire input before events will be fired. This is the nature of the token stream that will suck up all the tokens before doing a parsing. It is during a parsing that events are fired. Make sure that you run ANTLR with the -debug commandline option.