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

Arnar Birgisson arnarb at oddi.is
Mon Sep 22 11:30:02 PDT 2003


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/ 




More information about the antlr-interest mailing list