[antlr-interest] lexical rule problem
David-Sarah Hopwood
david-sarah at jacaranda.org
Tue Aug 25 11:30:31 PDT 2009
Safiye Celik wrote:
> Hi,
> I have to write a lexical rule such as
>
> a: b c b
> b: D | E
>
> and I want 'b' tokens in rule 'a' to be the same. That's, when the first 'b2
> in 'a' is D, then the last 'b' should be D. The same is true for E.
>
> To provide this, do I have to write two alternatives for 'a' (that is, a: D
> c D | E c E) or is there an easier way to make ANTLR to provide this
> condition?
I think "a: D c D | E c E;" is probably the easiest way.
An alternative is
a: x=b c y=b { $x != null && $y != null && $x.text.equals($y.text) }?;
b: D | E;
but that is hardly simpler. (Also note that it is only equivalent when
D and E are fixed tokens, and it might have worse error recovery
behaviour.)
> I have to use explicit version (righthandside) of rule 'a' in
> many rules, so it would be good if I have a chance to write 'a' in one
> alternative.
Why do you need to use the RHS of rule 'a' rather than just saying "a"?
--
David-Sarah Hopwood ⚥ http://davidsarah.livejournal.com
More information about the antlr-interest
mailing list