Debugging Tree Grammars in ANTLRWorks

The current version of ANTLRWorks does not support tree grammar debugging directly. However, there is a way to get it to work using remote debugging. The ANTLR FAQ for debugging has some information on setting up remote debugging but it does not show the remaining steps for tree debugging.

	 1 import java.io.*;
	 2 import org.antlr.runtime.*;
	 3 import org.antlr.runtime.tree.*;
	 4 import org.antlr.runtime.debug.*;
	 5 
	 6 /*
	 7 Sample file that shows how to connect to AntlrWorks
	 8 
	 9 Remember to change the following to suit your own project
	10  - XMLGrammarLexer
	11  - XMLGrammarParser
	12  - XMLTree
	13 
	14 */
	15 
	16 public class TreeTest {
	17 
	18   public static void main(String args[]) throws Exception {
	19       CharStream input = new ANTLRFileStream(args[0]);
	20       XMLGrammarLexer lex = new XMLGrammarLexer(input);
	21 
	22       // MODIFY: change document to the root rule 
	23       ParseTreeBuilder builder = new ParseTreeBuilder("document");
	24 
	25       // create a debug socket proxy to ANTLRWorks
	26       // MODIFY: change the name of the grammar file
	27       DebugEventSocketProxy AW = new DebugEventSocketProxy("/Users/vazexqi/Antlr/handGeneratedXMLGrammar/XMLTree.g");
	28       AW.handshake();
	29 
	30       DebugEventHub hub = new DebugEventHub(builder, AW);
	31 
	32       CommonTokenStream tokens = new CommonTokenStream(lex);
	33       XMLGrammarParser parser = new XMLGrammarParser(tokens);
	34       XMLGrammarParser.document_return root = parser.document();
	35 
	36       CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)root.tree);
	37       XMLTree walker = new XMLTree(nodes, hub);
	38       try {
	39           // MODIFY: change to the rule that you want to follow
	40           walker.document();
	41       } catch (RecognitionException e) {
	42           e.printStackTrace();
	43       }
	44 
	45   }
	46 }
	

The original file is available from here.

And when you generate the relevant files for your grammar, you must use the - debug option as such:

	
  java org.antlr.Tool XMLGrammar.g
  java org.antlr.Tool -debug XMLTree.g
	
Notice that you should not use the -debug option for the normal grammar file (the one that contains the parser/lexer). If you use the - debug option then it will try to debug the XMLGrammar.g file in addition to the XMLTree.g file and cause this error:
	
  error(10):  internal error: org.antlr.runtime.debug.DebugParser.reportError(DebugParser.java:88): java.net.BindException: Address already in use 
	

Compile the necessary java files and run the program using java TestTree someFile. Then in ANTLRWorks, select Debugger > Debug Remote....


comments powered by Disqus