Problem: Parser produces same values form $expressionItem.value and $expression.value
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Thursday, November 29, 2007 10:12 AM

Hi everybody. My name is Anders Norås and this is my first post to this forum. I'm am the founder of the Quaere project (http://quaere.codehaus.org). For those who are unfamiliar with my humble project, it is a Java based query API (Internal DSL) similar to Microsoft's LINQ project. We use ANTLR in our project to parse literal expression fragments when we build the expression trees for the queries. The literal expressions are combined with a number for framework generated nodes when the tree is built. We're very happy with the choice of ANTLR, but I have run into a small problem which I hope that you can help me out with. The query language allows users to apply indexers in their queries. E.g.    More>>

Hi everybody. My name is Anders Norås and this is my first post to this forum. I'm am the founder of the Quaere project (http://quaere.codehaus.org).
For those who are unfamiliar with my humble project, it is a Java based query API (Internal DSL) similar to Microsoft's LINQ project.

We use ANTLR in our project to parse literal expression fragments when we build the expression trees for the queries. The literal expressions are combined with a number for framework generated nodes when the tree is built.
We're very happy with the choice of ANTLR, but I have run into a small problem which I hope that you can help me out with.



The query language allows users to apply indexers in their queries. E.g.


			
String[] words = {"Albatross","Soup","Sixpence","Guitar"};

Iterable wordsStartingWithS =

from(s).in(words).

where(eq("s[0]","S").or(eq("s[0]","s")).

select("s");


My problem is that the s[0] fragment is parsed as:


			
new Indexer(

new Statement(

new Identifier("s")

),

new Statement(

new Identifier("s")

)

)


...while it should become:


			
new Indexer(

new Statement(

new Identifier("s")

),

new Statement(

new Constant(0,Integer.class)

)

)


Indexer is the expression type for an indexer expression with the Quaere expression tree.



The relevant ANTLR grammar can be found below:


			
expression returns [Expression value]

: expressionItem {$value = $expressionItem.value; } ('[' expression ']' { $value = new Indexer($expressionItem.value, $expression.value); })?

;



expressionItem returns [Expression value]

: left=conditionalExpression { $value = $left.value; } ( '?' middle=conditionalExpression ':' right=conditionalExpression { $value = new TernaryExpression($left.value, $middle.value, $right.value); })?

;


You can find the entire grammar at: http://svn.codehaus.org/quaere/trunk/Quaere/src/main/java/org/quaere/dsl/parser/Quaere.g


I suspect that there is an error in the grammar that I can't spot.



I hope that someone can help me out with this one, because it has been nagging me for some time.



Thanks,
Anders Norås

   <<Less
About | Sitemap | Contact