Try:
expression : andexpression (opand^ andexpression)* ; opand : OP_ AND -> OP_AND | ->OP_AND ; Which will be fine so long as your and expressions are not ambiguous without the AND keyword. I suggest defaulting operators is always fraught with danger though as no one can remember if it means OR or AND and some people will never actually know ;-) Jim From: [email protected] [mailto:[email protected]] On Behalf Of Allen Jones Sent: Monday, November 16, 2009 1:36 PM To: [email protected] Subject: [antlr-interest] Problems with implicit "and" in query grammar build using Antlr Hi all. I have been building a google-like query syntax parser in ANTLR (C#). This is the first work I have done with ANTLR, or any parser generator, so it has been quite an experience. I want the user to be able to use "and" and "or" to combine search terms e,g. "(apple or microsoft) and tablet and office". But I also want the user to be able to enter a simple set of words and implicitly treat it as though they are joined by "and" e.g. "apple tablet office windows" would be treated as "apple and tablet and office and windows" So far, I have been unable to get a syntax that correctly handles implicit "ands" between sets of words. Here is a snippet of my current grammar that works properly with an explicit "and": expression : andexpression (OR_OP^ andexpression)* ; andexpression options { backtrack=true; } : atom (AND_OP^ atom)* ; The problem is that I need the "and" to form the root of the AST subtree, which doesn't go well if the "and" isn't actually there. Even with lookahead/backtrack I can't find the right syntax to make it work. In wanting the AND_OP to be optional, I had to move away from the tree construction operators because the following would not work: andexpression : atom (AND_OP?^ atom)* ; Ideally, I thought I needed to make andexpression look like this: andexpression options { backtrack=true; } : l=atom (AND_OP? r=atom)* -> ^(AND_OP $l $r?)+ ; But I get RewriteEmptyStreamException parsing strings where words are not joined by 'and', for example "sheep dog fish". If anybody has any tips on how to make my "and" optional, it would be much appreciated. Regards Allen Jones -- You received this message because you are subscribed to the Google Groups "il-antlr-interest" group. To post to this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=.
List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
