[antlr-interest] Re: Skipping AST nodes in tree-parsing

lgcraymer lgc at mail1.jpl.nasa.gov
Mon Sep 22 13:50:18 PDT 2003


Arnar--

Annotation in tree parsers is a bit broken, but I think that the 
reason that ! had no effect on your code was that you probably had 
tree construction turned off and so exactly the same code would be 
generated.  With tree construction turned on, this tree pass would 
have produced a syntax tree without the block_stmt subtrees.  ! 
affects construction only, not recognition. In fact, ! in ANTLR 2 
means "construct, but do not add to the tree being built".

--Loring


--- In antlr-interest at yahoogroups.com, "Arnar Birgisson" <arnarb at o...> 
wrote:
> Hello..
> 
> Thanks for the lightning fast response :o)
> 
> This works, I have:
> 
> funcdef
>   : #("function" ID arg_list (block_stmt)=> block:.)
>   ;
> 
> This ensures that the atom is of the correct token.
> 
> One thought this leads to: what meaning does ! have in tree parsers?
> Excpecting to match a token and create the AST for it but skip
> descending
> into it, I first tried
> 
> funcdef
>   : #("function" ID arg_list block_stmt!)
>   ;
> 
> which was accepted by ANTLR but generated the exact same code as it 
did
> without the !.
> 
> Arnar
> 
> > -----Original Message-----
> > From: mzukowski at y... [mailto:mzukowski at y...] 
> > Sent: 22. september 2003 18:35
> > To: antlr-interest at yahoogroups.com
> > Subject: RE: [antlr-interest] Skipping AST nodes in tree-parsing
> > 
> > 
> > funcdef
> >   : #("function" ID arg_list .)
> >   ;
> > 
> > '.' is a wild card and just matches one node but does not 
> > decend into the
> > tree, just like ID above does not descend into a tree and 
> > just matches one
> > node of type ID.
> > 
> > Monty
> > 
> > -----Original Message-----
> > From: Arnar Birgisson [mailto:arnarb at o...] 
> > Sent: Monday, September 22, 2003 11:30 AM
> > To: antlr-interest at yahoogroups.com
> > Subject: [antlr-interest] Skipping AST nodes in tree-parsing
> > 
> > 
> > Hello there.. 
> > 
> > Is there a way in a treeparser to skip the walking/parsing of 
> > a tree node? I
> > want to store a reference to the AST instead. Two examples: 
> > 
> > This is in my parser: 
> > 
> > funcdef
> >   : "function"^ ID LPAREN! arg_list RPAREN! block_stmt
> >   ;
> > 
> > and in the tree parser: 
> > funcdef
> >   : #("function" ID arg_list block_stmt)
> >   ;
> > 
> > Now, instead of parsing ID, arg_list and then block_stmt 
> > nodes, I only want
> > to parse the ID and arg_list and skip over block_stmt, 
> > instead I will store
> > a reference to the node in a hashtable indexed on the given ID. 
> > 
> > Later, when the function is called in the user-program, the 
> > tree-parser rule
> > for a function call will look up the AST for the function 
> > definition by its
> > name and manually call appropriate rule for walkin the AST, 
> > after installing
> > the given paremeters into the symbol table. 
> > 
> > Another example of this is rewriting for loops to while loops: 
> > 
> > for (stmt1; expr; stmt2) stmt3; 
> > 
> > becomes 
> > 
> > stmt1; while (expr) { stmt3; stmt2; } 
> > 
> > This involves the reordering of the atoms, i.e. this 
> > translation scheme is
> > not simple by the definiton of the Dragonbook. 
> > 
> > How does one do this kind of translation in a tree walker? 
> > Perhaps someone
> > could point out a simpler solution, but if one could skip 
> > over nodes as
> > described above and manually call the parsing method for them 
> > at a later
> > time, this becomes trivial. 
> > 
> > Arnar
> > 
> > 
> >  
> > 
> > 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/ 
> > 
> >


 

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




More information about the antlr-interest mailing list