English 中文(简体)
Problems with implicit "and" in query grammar build using Antlr
原标题:
  • 时间:2009-11-15 17:43:01
  •  标签:
  • antlr

I have been building a google-like query syntax parser in ANTLR (C#).

I am finished except for one thing, which I have struggled with for a long time and failed to solve.

If the user enters "word1 word2 word3", I want the parser to treat this the same as "word1 and word2 and word3". So in effect the "and" is optional/implicit between terms.

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 cant find the right syntax to make it work.

Here is a snippet of my current grammar that works with an explicit "and":

expression
    : andexpression (OR_OP^ andexpression)*
    ;

andexpression
options {
backtrack=true;
}
    : atom (AND_OP^ atom)*
    ;

Ideally, I would like 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 string like "sheep dog fish".

If anybody has any tips on how to make my "and" optional, it would be much appreciated.

最佳回答

Answer was supplied by somebody on the ANTLR mailing list. Works like a charm. Re-posting here for those interested.

expression
    : l=andexpression (OR_OP^ r=andexpression)*
    ;

andexpression
    : atom (andop^ atom)*
    ;

andop
    : AND_OP -> AND_OP
    | -> AND_OP
    ;
问题回答

Best way is to do this:

andexpression : atom (AND_OP^ atom) ;

backtrack option is unnecessary too.

Terence





相关问题
ANTLR parser hanging at proxy.handshake call

I am attempting to get a basic ECMAScript parser working, and found a complete ANTLR grammar for ECMAScript 3, which appears to compile ok and produces the appropriate Lexer/Parser/Walker Java files. (...

Will ANTLR Help? Different Suggestion?

Before I dive into ANTLR (because it is apparently not for the faint of heart), I just want to make sure I have made the right decision regarding its usage. I want to create a grammar that will parse ...

How to use ANTLR to parse xml document

can anybody tell how to use ANTLR tool(in java) to create our own grammar for xml documents and how to parse those documents using ANTLR tool(in java)?

JavaCC Problem - Generated code doesn t find all parse errors

Just started with JavaCC. But I have a strange behaviour with it. I want to verify input int the form of tokens (letters and numbers) wich are concatenated with signs (+, -, /) and wich can contain ...

How to generate introductory recognizer using ANTLR3C?

The Definitive ANTLR Guide starts with a simple recognizer. Using grammar verbatim to target C-runtime fails because %s means something to ANTLR: $ cat T.g grammar T; options { language = ...

What s the matter with this Grammar?

grammar Test; IDHEAD: ( a .. z | A .. Z | _ ); IDTAIL: (IDHEAD | 0 .. 9 ); ID: (IDHEAD IDTAIL*); fragment TYPE: ( text | number | bool ); define: define ID as TYPE; The problem ...

热门标签