[antlr-interest] nullAST

Ric Klaren klaren at cs.utwente.nl
Thu May 13 15:24:06 PDT 2004


On Thu, May 13, 2004 at 09:22:00PM -0000, Chris Black wrote:
> I've been looking at the examples from:
> http://wwwhome.cs.utwente.nl/~klaren/antlr/treebuilding.txt
> 
> and notice syntax like:
> r1_manual:!	{ ## = nullAST; }
> 		e1:x { ## = #(nullAST,##,#e1); } // add x as a sibling
> 		e2:y { ## = #(nullAST,##,#e2); } // add y as a sibling
> 		e3:z { ## = #(nullAST,##,#e3); } // add z as a sibling
> 
> but when I try to use nullAST, it fails. I am also unable to do:
> dataStanza:! FIELD DELIM dt:FIELD! NEWLINE
> 	a:recordLine { ## = #a; } 
>         (b:recordLine { ##->addChild(#b); } )+ 
> 	{ 
> 		if(dt.getText().equals("Foo")) 
> 			## = #([STANZA, dt.getText()], ##);
> 	}
> 	;
> 
> Because AST->addChild(AST) doesn't compile correctly.
> (I get: Exception in thread "main" java.lang.Error: Unresolved
> compilation problem: Invalid expression as statement)

Hmm the treebuilding.txt might be a bit out of date for the latests
ASTfactory changes in java, the -> are C++-isms you appear to use java so
use a '.' in stead of '->'. nullAST should work I think just look at a
snippet of generated code at the start of a rule method there should be the
right incantations for nullAST. (and examples for addChild should also be
available)

> I don't see a way other than building up the (foo)+ parts of the tree
> during match (since it is not just one token), and then having a
> conditional at the end of the rule that either returns a null tree or
> a slight modification of the tree that has been constructed so far.

Note that the rule above never returns a null tree (only selectively adds a
STANZA node to the top of the rule. To return a null tree you should use 
## = nullAST (and yes ## is a shorthand for #rule btw :) ))

> Maybe I should just construct a full tree and skip over it in the tree
> parser, but I thought this might be better.

The more structure you add to the AST the easier it is to deal with it
later. But it depends on what you want to do...

I'd recomend adding a toStringList or another pretty printer of the
generated AST to the end of the rule actions. (just dump out ## this gives
you a bit more of a feel for what's happening and/or read the generated
code, some of the astfactory stuff is depedending on what you're used to a
bit counterintuitive)

Cheers,

Ric
-- 
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893755  ----
-----+++++*****************************************************+++++++++-------
 Why don't we just invite them to dinner and massacre them all when they're
  drunk? You heard the man. There's seven hundred thousand of them. Ah? ..
           So it'd have to be something simple with pasta, then.
                 From: Interesting Times by Terry Pratchet



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

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



More information about the antlr-interest mailing list