Hi Victor,
Here's a base principle on which SableCC is built: The role of the parse
tree (CST) is to organize the /scanned tokens/ into their grammatical
tree structure. The abstract tree (when using CST->AST transformations)
encodes an abstract organization of these tokens, which is usually a
simpler organization appropriate for semantic processing.
What I am trying to say is: it's not the role of the CST/AST to encode
evolutions of the code towards the target language. So, it makes no
sense for SableCC to create tokens out of nothing. For one thing, what
would be the text and location of such tokens, in the input stream? How
can a left-parenthesis token have no text and no location in the input
stream?
My recommendation, for adding parentheses, is to do it in the back-end:
public class CodeGenerator extends DepthFirstAdapter {
private String expStr;
...
public void caseAAddExp(AAddExp node) {
// get left expression string
node.getLeftExp.apply(this);
String leftStr = this.expStr;
// get right expression string
node.getRightExp.apply(this);
String rightStr = this.expStr;
// compute and store expression string
this.expStr = "(" + leftStr + " + " + rightStr + ")";
}
...
}
As you can see, there is no need to add parenthesis tokens in the
CST/AST to get them in the output.
Have fun!
Etienne
On 2011-07-30 02:28, [email protected] wrote:
Hello,
if a special method in my parser is executed (called ACompareExpression), i want
to force the parser to generate a parse tree with parentesis before and after
this node. i got some type errors for:
[...] so i want to
add parentesis in the parsetree. ( but not force in the sourcecode )
--
Etienne M. Gagnon, Ph.D.
SableCC: http://sablecc.org
_______________________________________________
SableCC-Discussion mailing list
[email protected]
http://lists.sablecc.org/listinfo/sablecc-discussion