views:

485

answers:

2

I am using ANTLRWorks to create ANTLR grammars. I have a valid grammar and the parser and lexer source files are generated as well. I have also tried debugging the generated code and the output is as expected in the debugger output.

But when I try to invoke the __Test__ class generated by the debugger nothing is coming up in the console. I have properly set up the classpath as I can successfully compile the __Test__.java with the same classpath.

What would be the problem? Is there any clear tutorial for writing and compiling a sample parser with antlr and antlrworks?

+1  A: 

What do you expect on the console to come up?

Have a look at this project. The ANTLRWorks generated parser is here. As you can see from the dependencies in the POM you need to make sure antlr is in the classpath. Then you use the parser as shown in this class.

final DriftLexer lexer = new DriftLexer(new ANTLRInputStream(inputStream));
final CommonTokenStream tokens = new CommonTokenStream(lexer);        
final DriftParser parser = new DriftParser(tokens);
parser.file();

That should be enough to get your stuff working as well.

tcurdt
A: 

ANTLRWorks generates test classes that create a socket connection back to ANTLRWorks, so they aren't usable from the console. You can edit the generated test class to not use the debug port (socket connection) option.

The line to edit is:

FormalSpecParser g = new FormalSpecParser(tokens, 49100, null);

You can change it to:

FormalSpecParser g = new FormalSpecParser(tokens, null);

which uses a debug listener object instead of a port, and the "null" means you're not giving it a debug listener, so debug output is ignored. You could write your own debug listener to print out messages to the console.

See the ANTLR documentation for more information: http://www.antlr.org/api/Java/namespaces.html

Everett