[antlr-interest] RE: Treeparser question
Jiho Han
jhan at InfinityInfo.com
Tue Jul 25 14:44:25 PDT 2006
I figured out that it's referring to itself. Makes sense now.
Another issue is - although non-critical - that I have duplicate
semantic actions for multiple rules and I don't know how to combine
them:
compExpr : #(OP_EQ IDENTIFIER literal) {
AST op = compExpr_AST_in;
AST id =
compExpr_AST_in.getFirstChild();
AST value = id.getNextSibling();
AddEntityQueryClause(op, id, value);
}
| #(OP_GT IDENTIFIER literal) {
AST op = compExpr_AST_in;
AST id =
compExpr_AST_in.getFirstChild();
AST value = id.getNextSibling();
AddEntityQueryClause(op, id, value);
}
| #(STARTSWITH IDENTIFIER literal) {
AST op = compExpr_AST_in;
AST id =
compExpr_AST_in.getFirstChild();
AST value = id.getNextSibling();
AddEntityQueryClause(op, id, value);
} ;
With the rule like above, as you can see, all of the semantic actions
are identical. Is there a way to combine them?
Also, if you take a look, I am having to use the generated code's
variable (hack) to get at the nodes. I tried labeling them as:
#(op:OP_EQ id:IDENTIFIER value:literal) but then it complained for the
alternatives (for redeclaring them). Then I added an init section that
declares them
{
AST op, id, value;
}
And tried #(op=OP_EQ id=IDENTIFIER value=literal) but it croaked again.
What am I doing wrong?
Thanks
> _____________________________________________
> From: Jiho Han
> Sent: Tuesday, July 25, 2006 1:42 PM
> To: antlr-interest at antlr.org
> Subject: Treeparser question
>
> In this example from a tutorial,
>
> class ExpressionParser extends Parser;
> options { buildAST=true; }
>
> expr : sumExpr SEMI!;
> sumExpr : prodExpr ((PLUS^|MINUS^) prodExpr)* ;
> prodExpr : powExpr ((MUL^|DIV^|MOD^) powExpr)* ;
> powExpr : atom (POW^ atom)? ;
> atom : INT ;
>
> class ExpressionLexer extends Lexer;
>
> PLUS : '+' ;
> MINUS : '-' ;
> MUL : '*' ;
> DIV : '/' ;
> MOD : '%' ;
> POW : '^' ;
> SEMI : ';' ;
> protected DIGIT : '0'..'9' ;
> INT : (DIGIT)+ ;
>
> {import java.lang.Math;}
> class ExpressionTreeWalker extends TreeParser;
>
> expr returns [double r]
> { double a,b; r=0; }
>
> : #(PLUS a=expr b=expr) { r=a+b; }
> | #(MINUS a=expr b=expr) { r=a-b; }
> | #(MUL a=expr b=expr) { r=a*b; }
> | #(DIV a=expr b=expr) { r=a/b; }
> | #(MOD a=expr b=expr) { r=a%b; }
> | #(POW a=expr b=expr) { r=Math.pow(a,b); }
> | i:INT { r=(double)Integer.parseInt(i.getText()); }
> ;
>
> Does expr reference in #(PLUS a=expr b=expr) in the treeparser refer
> to itself or the one in ExpressionParser?
> Basically, I am having a hard time going from a parser to a tree
> parser in my own project.
> Thanks
>
> Jiho Han
> Senior Software Engineer
> Infinity Info Systems
> The Sales Technology Experts
> Tel: 212.563.4400 x216
> Fax: 212.760.0540
> jhan at infinityinfo.com
> www.infinityinfo.com <http://www.infinityinfo.com/>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060725/b5c10e87/attachment.html
More information about the antlr-interest
mailing list