|
View:
New views
2 Messages
—
Rating Filter:
Alert me
|
|
|
Syntax sugar in Sablecc-3Hi, 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
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:
{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:
-- 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 |
| Free Forum Powered by Nabble | Forum Help |