labeling a closure?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Ian_Cardenas
Posted On:   Wednesday, September 12, 2001 12:36 PM

Hi, I'm working on a parser for a language with Pascal-like variable declarations a, b : int; I want to build a tree like (VARDECL int a b) that will then be walked to generate the symbol table. I started with a rule like variableDeclaration : identifier ( COMMA identifier )* COLON type SEMI and then tried to annotate it with the tree notation as: variableDeclaration! : id:identifier idrest:( COMMA! identifier )* COLON! t:type SEMI! { #variableDeclaration = #([VARDECL,"VARDECL"], #t, #id, #idrest); } but that didn't work for #idrest. Looking at the generated code, the AST built in the loo   More>>

Hi,

I'm working on a parser for a language with Pascal-like variable declarations

			
a, b : int;


I want to build a tree like (VARDECL int a b) that will then be walked to generate the symbol table. I started with a rule like
			
variableDeclaration
: identifier ( COMMA identifier )* COLON type SEMI


and then tried to annotate it with the tree notation as:
			
variableDeclaration!
: id:identifier idrest:( COMMA! identifier )* COLON! t:type SEMI!
{
#variableDeclaration = #([VARDECL,"VARDECL"], #t, #id, #idrest);
}


but that didn't work for #idrest. Looking at the generated code, the AST built in the loop wasn't being saved anywhere after specifying the ! after the rule name.


So I ended up with:

			
variableDeclaration!
: id:identifier
idrest:
(
COMMA! identifier
{ astFactory.addASTChild(currentAST, returnAST); }
)*
{ #idrest = currentAST.root; }
COLON! t:type SEMI!
{
#variableDeclaration = #([VARDECL,"VARDECL"], #t, #id, #idrest);
}


My question is: is there a better way to do this? It seems odd that the only thing the idrest label does is label the loop and declare an AST named idrest_AST. Did I miss some tree building feature?    <<Less

Re: labeling a closure?

Posted By:   Monty_Zukowski  
Posted On:   Monday, September 17, 2001 07:03 AM

Labels on closures and subrules currently don't do anything. They were supposed to, but it never got implemented. They really should issue a warning at present.


Let me explain why the following will work. Note that I got rid of idrest entirely and turned off tree building for t:type.


variableDeclaration!
: id:identifier ( COMMA! identifier )* COLON! t:type! SEMI!
{
#variableDeclaration = #([VARDECL,"VARDECL"], #t, #id);
}

The tree being built is a flat list. In your first attempt at tree building you had a tree that was (a b int). Note that this means that node b's nextSibling() is int. If you then try to rebuild it using a tree constructor like mine above, then your tree will have an infinite loop. When you construct trees as above, you aren't just adding individual nodes, you are adding entire trees. Adding #id adds (a b int), where int is the pointer to #t which is earlier in the tree. So turn off the automatic tree building for t and you're ok.


In general you want to turn off tree building for elements that you are rearranging later.

About | Sitemap | Contact