[antlr-interest] AST rewrite infinite loop

John B. Brodie jbb at acm.org
Wed Oct 14 13:38:23 PDT 2009


Greetings!

On Wed, 2009-10-14 at 16:15 -0400, Bill Andersen wrote:
> FYI, all.  The problem described does NOT occur without rewrite = true
> 
Isn't rewrite = true for Tree grammars only? -- your rule below looks
like a Parser rule to me - altho can't really tell for sure...

> On Oct 14, 2009, at 11:20 , Bill Andersen wrote:
> 
> > Hi folks
> >
> > I have this production
> >
> > assertion
> > 	: sentence -> ^(ASSERTION NAME["foo"] sentence)
> > 	;

Appended below is a COMPLETE Parsing example using just this rule.

And it WORKS just fine.

Could you please modify this complete example so that it will
demonstrate the error you are seeing. And then maybe someone will be
better able to figure out what is the cause.

Thank You
   -jbb

> >
> > in a tree grammar with rewrite = true.  This is meant to tack a name
> > onto the results of parsing a 'sentence'.  Thing is when I parse a
> > sample input I get an infinite tree that looks like this
> >
> > (ASSERTION foo
> > 	(ASSERTION foo
> > 		(ASSERTION foo
> > 			(ASSERTION foo
> > 				(ASSERTION foo
> > 					....
> >
> > Thing is that the sentence production does not call assertion,
> > otherwise I could understand why my rewrite might get itself reparsed
> > or something.
> >
> > Any idea what's happening here and how to stop it?

//-----begin cut here ----- cut here ----- cut here -----

grammar Test;

options {
    output = AST;
    ASTLabelType = CommonTree;
}

tokens {
   ASSERTION;
   NAME;
}

@members {
    private static final String [] x = new String[]{
       "a sentence goes here"
    };

    public static void main(String [] args) {
        for( int i = 0; i < x.length; ++i ) {
            try {
                System.out.println("about to parse:`"+x[i]+"`");
                TestLexer lexer = new TestLexer(new
ANTLRStringStream(x[i]));
                CommonTokenStream tokens = new CommonTokenStream(lexer);

                TestParser parser = new TestParser(tokens);
                TestParser.start_return p_result = parser.start();

                CommonTree ast = p_result.tree;
                if( ast == null ) {
                   System.out.println("resultant tree: is NULL");
                } else {
                   System.out.println("resultant tree: " +
ast.toStringTree());
                }
                System.out.println();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
}

start : assertion EOF!;
         
assertion : sentence -> ^(ASSERTION NAME["foo"] sentence) ;

sentence : 'a sentence goes here' ;

WS : ( ' ' | '\t' | '\r' | '\n' )+ { $channel=HIDDEN; } ;

// ----- end cut here ----- cut here ----- cut here -----




More information about the antlr-interest mailing list