[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Mapping lists during AST transformation
When there is a CST production that is list-valued, is it possible
to map it to a list of different type for the AST, depending on
context?
For example, I have:
Token
...
id = ... ;
Productions
...
id_list {-> id*} = ... ;
expr_list {-> expr*} = ... ;
expr {-> expr} =
{id} id {-> New expr.variable(id)} |
{id_list} id_list {-> New expr.id_list([id_list.id])} |
{tuple} expr_list {-> New expr.tuple{[expr_list.expr])} |
... ;
declarator {-> declarator} =
{id_list} id_list {-> New declarator.complex([id_list.id])} |
... ;
Abstract Syntax Tree
...
expr =
{variable} id |
{id_list} id* |
{tuple} expr* |
... ;
declarator =
{complex} identifier* |
... ;
Now I'd actually like id_list and tuple to be the same in the AST,
i.e. id_list would become an expr* instead of an id*. The problem is
that I need to assemble id_list as id* in Productions because it is
also used by declarator and I would get ambiguities otherwise.
What I'd actually like to write is the following:
Productions
...
expr {-> expr} =
...
{id_list} id_list
{-> New expr.tuple([New expr.variable(id_list.id)])} |
... ;
Abstract Syntax Tree
expr =
{variable} id |
{tuple} expr* |
... ;
In other words, in the id_list Production I'd like to map the id* to
an expr*, but the above doesn't compile.
Is there some way to do such a mapping of lists?
-- Niklas Matthies