[antlr-interest] Stuck with parser & tree grammar
G R
relationalalgebra at gmail.com
Fri Dec 7 04:04:25 PST 2007
Hi all!
I'm still trying to translate relational algebra into SQL.
I used to some problems with my grammar, so I decide to change it into a
LL(1) grammar.
So, a relational algebra query can be describe like this :
raExpr
: raQuery^
| RelationName^;
raQuery
: project^
| select^
| rename^
| binaryExpr^;
binaryExpr
: r1=relation (BinaryOp r2=relation
| LeftBrack a1=AttributeName RightBrack Join r2=relation LeftBrack
a2=AttributeName RightBrack)
->{$BinaryOp!=null}?^(BinaryOp $r1 $r2)
->^(Join ^(Left $r1 $a1) ^(Right $r2 $a2));
relation
: LeftParent raQuery RightParent
->^(raQuery)
| RelationName
->^(RelationName);
As you can see, I'm trying to make possible input like :
relationName1 UNION relationName2
Or
relationName1[AttributeName1] JOIN relationName2[AttributeName2]
Using this grammar, this is ok : my AST looks good. Using previous inputs, I
got the following AST :
(UNION relationName1 relationName2)
Or
(Join (Left relationName1, AttributeName1) (Right relationName2,
AttributeName2))
The problem is in the tree grammar. I don't know how to deal with the rule
binaryExpr.
I've try this :
binaryExpr
: ^(BinaryOp r1=relation r2=relation)
-> {BinaryOp==Union}? Union(left={$r1.st}, right={$r2.st})
-> {BinaryOp==Intersect}? Intersect(left={$r1.st}, right={$r2.st})
-> {BinaryOp==Difference}? Difference(left={$r1.st}, right={$r2.st})
-> {BinaryOp==Divide}? Divide(left={$r1.st}, right={$r2.st})
| ^(Join ^(Left r1=relation a1=AttributeName) ^(Right r2=relation
a2=AttributeName))
-> join(leftRel={$lr.st}, leftAtt={$la.text}, leftAlias={$lr.alias},
rightRel={$rr.st}, rightAtt={$ra.text},
rightAlias={$rr.alias});
But, ANTLR is expecting a REWRITE instead of the optional operator ( | )
just after the Divide.
So, my question is : how can I generate my walker using the previous parser
grammar, or What should I do for this to work ?
Thanks for any help.
G.R.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071207/d601cb81/attachment.html
More information about the antlr-interest
mailing list