Syntax sugar in Sablecc-3

View: New views
2 Messages — Rating Filter:   Alert me  

Syntax sugar in Sablecc-3

by Dehua Zhang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



Hi,

I have the following grammar,

Productions
cst_exp { -> exp } =
   {minus} cst_exp minus term {-> New exp.minus(cst_exp.exp, term.exp) }
|  {term} term {-> term.exp};

term {-> exp } =
   {number} number {-> New exp.number(number)};

Abstract Syntax Tree
exp =
   {minus} [l]:exp [r]:exp
|  {number} number;

I would like to add the unary minus operation as a syntax sugar, which means I only modify the Productions without modifying the AST.
What I am thinking about is when transforming the uminus operation production, I transform it to the minus node in AST, is it possible in sablecc3?

Thanks,
--
Dehua (Andy) Zhang
Sable Research Group, McGill University
Montréal, Québec, Canada
http://www.cs.mcgill.ca/~dzhang25





_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion

Re: Syntax sugar in Sablecc-3

by Etienne M. Gagnon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Dehua,

The CST to AST transformation syntax of SableCC 3 does not allow for creating (and duplicating) tree nodes (it would be difficult to create a 'zero' number token). So, generally speaking, SableCC 3 would not allow you to transform:
 {unary_minus} minus_token exp
into
 {minus} [l]:exp [r]:exp
There are two solutions for you, though:
  1. Do the transformation after parsing, using a single pass over the AST with a DepthFirstAdapter. You call node.replaceBy(...) to replace the unary expression with an equivalent binary minus expression node.
  2. Change the AST syntax to accept a binary minus with a possibly null left expression (where semantics equate null with zero).
As an example of solution 2, the AST would look as:
 {minus} [l]:exp? [r]:exp  // notice the added "?"
 Then, in the CST to AST transformation, you create a binary minus expression with a null left expression:
 ... New exp.minus(Null, ...) ...
This second solution requires changes to the semantic part of your compiler, though. So, if you really want pure syntactic sugar, solution 1 is the one you want.

Have fun!

Etienne

Dehua Zhang wrote:
I have the following grammar,
...
Abstract Syntax Tree
exp =
   {minus} [l]:exp [r]:exp
|  {number} number;

I would like to add the unary minus operation as a syntax sugar, which means I only modify the Productions without modifying the AST.
What I am thinking about is when transforming the uminus operation production, I transform it to the minus node in AST, is it possible in sablecc3?
  

-- 
Etienne M. Gagnon, Ph.D.
SableCC:                                            http://sablecc.org
SableVM:                                            http://sablevm.org


_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion

signature.asc (257 bytes) Download Attachment