[antlr-interest] What tool[s] do you use when debugging ANTLR grammars?

mzukowski at yci.com mzukowski at yci.com
Fri Jun 21 07:42:42 PDT 2002


You've got some excellent repies so far.  

I would add version control to the iterative process.  I have an RCS call in
my makefile for every time I run antlr.Tool.  I enter a little log message
and am completely safe about restructuring grammars, etc., because I can
always return to a known good state.

I also use the -trace, -traceParser, -traceLexer, and -traceTreeParser (or
is it -traceTreeWalker?  I can't remember)  Here's my trace methods from the
GCC translator, which indent nicely and translate token numbers to names:

<<trace methods>>=
        int traceDepth = 0;
        public void reportError(ParserException ex) {
	  try {
	    System.err.println("ANTLR Parsing Error: "+ex + " token name:" +
tokenNames[LA(1)]);
	    ex.printStackTrace(System.err);
	  }
	  catch (IOException e) {
	      //if we can't print out there's not much else we can do
	  }
        }
        public void reportError(String s) {
	    System.err.println("ANTLR Parsing Error from String: " + s);
        }
        public void reportWarning(String s) {
            System.err.println("ANTLR Parsing Warning from String: " + s);
        }
        public void match(int t) throws MismatchedTokenException {
          boolean debugging = false;
          
          if ( debugging ) {
           for (int x=0; x<traceDepth; x++) System.out.print(" ");
           try {
	    System.out.println("Match("+tokenNames[t]+") with LA(1)="+
		tokenNames[LA(1)] + ((guessing>0)?" [guessing "+ guessing +
"]":""));
    	   }
	   catch (IOException e) {
	       //if we can't print out there's not much else we can do
	   }
    
          }
          try {
	    if ( LA(1)!=t ) {
		if ( debugging ){
		    for (int x=0; x<traceDepth; x++) System.out.print(" ");
		    System.out.println("token mismatch: "+tokenNames[LA(1)]
				+ "!="+tokenNames[t]);
		}
		throw new MismatchedTokenException(tokenNames, LT(1), t,
false);
            } else {
		// mark token as consumed -- fetch next token deferred until
LA/LT
		consume();
            }
	  }
	  catch (IOException e) {
		//if we can't print out there's not much else we can do
	  }
    
        }
        public void traceIn(String rname) {
          traceDepth += 1;
          for (int x=0; x<traceDepth; x++) System.out.print(" ");
          try {
            System.out.println("> "+rname+"; LA(1)==("+
tokenNames[LT(1).getType()] 
		+ ") " + LT(1).getText() + " [guessing "+ guessing + "]");
	  }
	  catch (IOException e) {
	      //if we can't print out there's not much else we can do
	  }
        }
        public void traceOut(String rname) {
          for (int x=0; x<traceDepth; x++) System.out.print(" ");
          try {
            System.out.println("< "+rname+"; LA(1)==("+
tokenNames[LT(1).getType()] 
		+ ") "+LT(1).getText() + " [guessing "+ guessing + "]");
	  }
	  catch (IOException e) {
	      //if we can't print out there's not much else we can do
	  }
          traceDepth -= 1;
        }

I've never tried Loring's approach with the synpreds, but it sounds
promising.  Also get in the habit of reading the generated code.  It's
readable and understandable (except maybe for the tree building stuff) and
will get you to the point where instead of asking "Why doesn't this work?"
you can ask "I wrote this but antlr generates this, why?"

Most of my tips are on www.codetransform.com/tips.html.

Monty


> -----Original Message-----
> From: micheal_jor [mailto:open.zone at virgin.net]
> Sent: Thursday, June 20, 2002 7:23 PM
> To: antlr-interest at yahoogroups.com
> Subject: [antlr-interest] What tool[s] do you use when debugging ANTLR
> grammars?
> 
> 
> Following on from my grammar debugging issues and recent ParseView 
> post, I'd like to know what tools others rely on for debugging 
> grammars.
> 
> I've re-written my grammar many times already due to a number of 
> seemingly intractable warnings/errors. I am just about to rewrite 
> again but this time I am starting off by designing an heterogenous 
> AST structure and working my way back from that to the grammar.
> 
> So what tools do you use for grammar debugging and, what tips can you 
> offer an ANTLR newbie on grammar creation and debugging?
> 
> Micheal
> 
> 
> 
>  
> 
> Your use of Yahoo! Groups is subject to 
http://docs.yahoo.com/info/terms/ 


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list