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

Re: Mapping lists during AST transformation



Hi Niklas,

Interesting problem.  You say that you would get conflicts by
duplicating id_list as follows?

  // Note: You must inline expr_id_list in every alternative that uses
  // expr.  In other words:
  //    {some_alt} ... exp ...
  // becomes
  //    {some_alt}        ... exp ... |
  //    {some_other_alt}  ... expr_id_list ...;

  Tokens
    ...
    id = ... ;

  Productions
    ...
    decl_id_list {-> id*} =
      ... ;

    expr_id_list {-> exp*} =
      ... ;

    expr_list {-> expr*} = ... ;

    expr {-> expr} =
      {id}      id           {-> New expr.variable(id)} |
      {tuple}   expr_list    {-> New expr.tuple{[expr_list.expr])} |
      ... ;

    declarator {-> declarator} =
      {id_list} decl_id_list {-> New declarator.complex([id_list.id])} |
      ... ;

  Abstract Syntax Tree
    ...
    expr =
      {variable}  id    |
      {tuple}     expr* |
      ... ;

    declarator =
      {complex}   id*    |
      ... ;

If this doesn't work, it is probably because the "context" can only be
determined by the "right" context (i.e. by tokens that follow id_list).
 If this is the case, then there is no nice solution other than changing
the language because the parser won't be able to chose between
transforming or not an id into an expr in a token sequence such as follows:

 [left context] ... id [*] comma id comma id [right context]

(where [*] shows the parse decision point).

The easiest, in this case, is to change the language such that the left
context implies whether you're expecting an expression or a declarator.


Note 1: Your grammar seems strange to me.  Why would an expression be
made of many expressions?  (Why is "expr = ...| id_list|... " in the
first place?)

Note 2: I am sure that you realize that it is quite difficult to debug a
grammar from just little fragments of it...


Hoping this helps,

Etienne

Niklas Matthies wrote:
> 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?
> ...

-- 
Etienne M. Gagnon, Ph.D.            http://www.info2.uqam.ca/~egagnon/
SableVM:                                       http://www.sablevm.org/
SableCC:                                       http://www.sablecc.org/

Attachment: signature.asc
Description: OpenPGP digital signature