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

Re: CST->AST (default)



Hi Etienne, all


On Fri, 4 Jul 2003, Etienne Bergeron wrote:

> 0341470.6582-100000@dino01.iro.umontreal.ca>
> MIME-Version: 1.0
> Content-Type: TEXT/PLAIN; charset=US-ASCII
> X-MailScanner: Found to be clean
> Sender: owner-sablecc-list@sable.mcgill.ca
> Precedence: bulk
> Content-Length: 1971
> Status: RO
> X-Status: 
> X-Keywords:
> X-UID: 522
> 
> 
> 	I would like to do a partial CST->AST transformation. For my 
> grammar, CST is fine except for expression.
> I would like to rewrite exp without having to write all node in AST.
> Why SableCC doesn't have a default CST->AST transformation?
> 
SableCC do have default CST->AST transformation. It still supports
SableCC2.x.x grammars. So there is a default transformation of
SableCC2.x.x grammars to SableCC3.0 grammar.

> Here's a simple example:
> 
> [...]
> Productions
> 
>     grammar           = exp_list ;              /**
>     exp_list          = exp_list_tail* exp ;     * Suppose I want CST as AST.
>     exp_list_tail     = exp comma ;              */
> 
>     exp               {-> exp} =  
>                         {plus}   exp plus factor    {-> New exp.mult( exp.exp, factor.exp ) }
>                       | {minus}  exp minus factor   {-> New exp.mult( exp.exp, factor.exp ) }
>                       | {factor} factor             {-> factor.exp }
>                       ;
> 
> 
>     factor            {-> exp} =
>                         {mult}   factor mult term   {-> New exp.mult( factor.exp, term.exp ) }
>                       | {div}    factor div term    {-> New exp.div( factor.exp, term.exp ) }
>                       | {term}   term               {-> term.exp }
>                       ;
> 
>     term              {-> exp} =
>                         {number}   number           {-> New exp.number(number) }
>                       | {exp}      l_par exp r_par  {-> exp.exp }
>                       ;
> 
> 
> Abstract Syntax Tree
> 
>     grammar       = exp_list;               /** 
>     exp_list      = exp_list_tail* exp;      * Why should I define these rules? 
>     exp_list_tail = exp comma ;              */
> 
>     exp = {plus}    [l]:exp  [r]:exp |
>           {minus}   [l]:exp  [r]:exp |
>           {div}     [l]:exp  [r]:exp |
>           {mult}    [l]:exp  [r]:exp |
>           {number}  number;
> 
>

When thinking about the readability of AST section, the problem of
automatic insertion of production is not so obvious.
 As you and other users(Jon Shapcott) suggest it, we have already think of
automatic addition of non-transformed productions in AST section.
In term of code effort, it is really easy, but the problem leads to the
user point of view.
In particular, when writing a visitor(inherited already existing one), a
user is supposed to look at AST section of the grammar and only this
one.
So if we add other productions internally, there will be a difference
between specified AST section and the real AST(this one SableCC will
used to generate visitor and do internal works). So to avoid this
confusion, we made the arbitrary decision to not introduce any
transformation to this section, unless the whole section is not
specified (like Sablecc2.x.x grammars).

>
>
>            ciao
>

 Cheers,

 kevin.