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

Ben Dotte ben.dotte at gmail.com
Wed Jun 18 14:35:42 PDT 2008


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