[antlr-interest] Begins-with/Ends-with ambiguity

Ben Dotte ben.dotte at gmail.com
Thu Jun 19 08:25:48 PDT 2008


I think I got this solved now by defining a wildcard rule in the lexer
like this:

TEXT_WITH_WILDCARD
	:	(('*' TEXT)|(TEXT '*')) ;

Then I just handle the difference between begins-with and ends-with in
the AST tree walker. I assume this works because ANTLR is interpreting
the entire thing as a single token (if that's the right word), rather
than trying to match to a parser rule that ignores spaces.

On Wed, Jun 18, 2008 at 4:35 PM, Ben Dotte <ben.dotte at gmail.com> wrote:
> Hi,
>
> I've been developing a search grammar with ANTLR 3 and I'm stuck
> trying to figure out how to resolve an ambiguity between begins-with
> and ends-with searches. I am a novice at this, so please forgive my
> ignorance if the answer is obvious :)
>
> The idea is if the user enters "a *e", it should be interpreted as
> "contains 'a' and ends with 'e'". Right now, that expression is
> incorrectly interpreted as "starts with 'a' and contains 'e'". So it
> is assigning the "*" to the "a" instead of the "e".
>
> This is the parser rule that is supposed to sort these things out:
>
> wildcardSearch
>        :       '*' term                        -> ^(ENDSWITH term)
>        |       (term '*')=> term '*'   -> ^(BEGINSWITH term)
>        |       term
>        ;
>
> The lexer is setup to ignore whitespace, but I guess it does become
> significant here, so I'm unsure if I should not ignore whitespace and
> somehow account for it in the rules, or if there is some syntactic
> predicate I haven't thought of that can resolve this for me.
>
> Any suggestions would be much appreciated.
>
> Thanks,
> Ben
>


More information about the antlr-interest mailing list