dotgnu-general
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [DotGNU]Treecc / Bison - List rule best pattern


From: Rhys Weatherley
Subject: Re: [DotGNU]Treecc / Bison - List rule best pattern
Date: Sat, 31 Jan 2004 08:35:34 +1000
User-agent: KMail/1.4.3

> If i make the AST by hand tipically i create a vector in a class called
> FunctionList and an AddFunction() method that pushes the new Function into
> it.
> In treecc i dont know how to do it.
> Please someone tell me a simple (keep in mind that the goal is simplicity)
> pattern to implement this in my parser ?

There are two ways.  The simplest is a binary node that has a list on the left 
and the current list member on the right.  You'll create this like any other 
binary node.  e.g.

opt_function_list
    : function_list { $$ = $1; }
    | { $$ = 0; }
    ;

function_list
    : abstract_function { $$ = $1; }
    | function_list abstract_function { $$ = function_list_create($1, $2); }
    ;

Then you simply do a depth-first recursion on the left-hand expression and 
process the nodes on the way back to get them in order.  We use this in 
Portable.NET for argument lists.

The drawback with this approach is that the left-hand recursion can get very 
deep if there are lots of list members.  In Portable.NET, we avoid this using 
a slightly smarter list structure which is right-recursive.  A number of 
helper functions take care of appending.

The node type "ILNode_List", which is declared in "pnet/codegen/cg_nodes.tc" 
and the helper functions are declared in "pnet/codegen/cg_stmt.tc".  Look for 
"ILNode_List_Add" and "ILNode_ListIter" for the iterator.

I hope this helps.

Cheers,

Rhys.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]