Working with Null

37 views
Skip to first unread message

Budi Kurniawan

unread,
Sep 30, 2012, 11:05:15 PM9/30/12
to sab...@googlegroups.com
Hi Everyone,
I'm new to SableCC but find it exciting already.

I'm using versions 3.2 and 3.6 on both the Mac (JDK 1.6) and Win7 (JDK 1.7).

I replaced this production (which I copied from http://sablecc.sourceforge.net/documentation/cst-to-ast.html):

exp = 
          {plus}   exp plus factor  {-> New exp.plus(exp, factor.exp)  }
        | {minus}  exp minus factor {-> New exp.minus(exp, factor.exp) }
        | {factor} factor           {-> factor.exp}
    ;

with

 exp = {plus} exp  plus factor  {-> New exp.plus(exp, factor.exp)} |
         {minus} exp minus factor {-> New exp.minus(exp, Null)}      | // Null as 2nd argument
             {factor} factor      {-> factor.exp };

and I'm now getting this error message when trying to generate a parser:

java.lang.RuntimeException: [36,50] expecting #PExp# when #Null# was found.


Any pointer would be much appreciated.

Thanks,
budi

Etienne Gagnon

unread,
Oct 1, 2012, 7:31:05 AM10/1/12
to sab...@googlegroups.com
Hi Budi,

Null is only accepted when the related AST element allows for it. In
your case, the AST grammar is:

exp = ... | {minus} [l]:exp [r]:exp | ...

The minus alternative requires two non-Null elements. If you want it to
allow for Null elements, you must change it to:

exp = ... | {minus} [l]:exp [r]:exp? | ...

I highly recommend that you update your SableCC version to the latest
stable version (3.6).

Have fun!

Etienne

Etienne Gagnon, Ph.D.
http://sablecc.org

On 2012-09-30 23:05, Budi Kurniawan wrote:
> [...]
Reply all
Reply to author
Forward
0 new messages