Skipping AST nodes in a treewalker
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Arnar_Birgisson
Posted On:   Monday, September 22, 2003 11:21 AM

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 use   More>>

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

   <<Less
About | Sitemap | Contact