[antlr-interest] Problems with StringTemplate within TreeGrammar

G R relationalalgebra at gmail.com
Mon Oct 29 05:58:14 PDT 2007


Hi,
I've got a problem trying to use ST with my tree grammar.
I'm trying to translate relational algebra into SQL. I got everything
working fine for most of all the RA operators, but I'm stuck with the
selection.

Here is the parser grammar for the selection :

// select = SELECT "[" condition "]" relation
// condition = logicalTerm {"OR" logicalTerm}
// logicalTerm = logicalFactor {"AND" logicalFactor}
// logicalFactor = ["NOT"] operand CompareOperator operand | "(" condition
")"
select    :    Select LeftBrack condition RightBrack relation
        ->^(Select condition relation);
condition
    :    logicalTerm (Or^ logicalTerm)*;
logicalTerm
    :    logicalFactor (And^ logicalFactor)*;
logicalFactor
    :    Not op1=operand CompareOperator op2=operand
        ->^(Not ^(CompareOperator $op1 $op2))
    |    op1=operand CompareOperator op2=operand
        ->^(CompareOperator $op1 $op2)
    |    LeftParent condition RightParent
        ->^(condition);
operand    :    AttributeName
    |    Numeric;

this gave me a beautiful AST with for each condition, the tokens "AND" or
"OR" as node, and logicalTerm or LogicalFactor as children.
So in my tree grammar, I've try this using StringTemplate as output option :


select    :    ^(Select condition relation)
                ->select(condition={$condition.st},
relation={$relation.st});
condition
    :    ^(Or? lt+=logicalTerm+)
                ->condition(logicalTerms={$lt});
logicalTerm
    :    ^(And? lf+=logicalFactor+)
                ->logicalTerm(logicalFactors={$lf});
logicalFactor
    :    ^(Not ^(CompareOperator leftOp=operand rightOp=operand))
                ->notLogicalFactor(leftOperand={$leftOp.text},
rightOperand={$rightOp.text}, operator={$CompareOperator.text})
    |    ^(CompareOperator operand operand)
                ->logicalFactor(leftOperand={$leftOp.text},
rightOperand={$rightOp.text}, operator={$CompareOperator.text})
    |    condition
                ->innerCondition(condition={$condition.st});
operand    :    AttributeName
    |    Numeric;

With the templates :

select(condition, relation) ::= "SELECT * FROM <relation> WHERE <condition>"
condition(logicalTerms) ::= <<"<logicalTerms; separator="\" OR \"">">>
logicalTerm(logicalFactors) ::= <<"<logicalFactors; separator="\" AND
\"">">>
notLogicalFactor(leftOperand, rightOperand, operator) ::= "<leftOperand> NOT
<operator> <rightOperand>"
logicalFactor(leftOperand, rightOperand, operator) ::= "<leftOperand>
<operator> <rightOperand>"
innerCondition(condition) ::= "(<condition>)"

Using theses grammars, when i try to translate the following RA query :
\u03c3 [att1>20 AND (att2 = foo OR att3=bar)] R1 (where \u03c3 representing
the Select token)

I've got the following error :
BR.recoverFromMismatchedToken
D:\Diplome\RATranslator\src\grammar\RATree.g: node from line 1:16 mismatched
tree node: AND expecting <DOWN>

I've check with ANTLRWorks, the parser grammar gave me the right AST as the
picture join to this mail.

Could anyone help me debugging this and find a solution ?
Thanks.

G.R
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071029/07ab014e/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: select.jpg
Type: image/jpeg
Size: 10492 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20071029/07ab014e/attachment-0001.jpg 


More information about the antlr-interest mailing list