[antlr-interest] Syntactic predicate or not...

Ric Klaren ric.klaren at gmail.com
Tue Jan 25 10:10:02 PST 2005


On Tue, 25 Jan 2005 18:40:18 +0100, Tomasz Bluszcz <moviem at web.de> wrote:
> The difference between LegalName and LegalNameEx is that the LegalNameEx
> containst the special character '.'  and the first char must not be a
> letter.
> My problem is to write the right LEGALNAME_OR_LEGALNAMEEX_OR_INT rule... I
> think it is completely wrong..

Something like this?

LEGALNAME_OR_LEGALNAMEEX { bool sawdot = false; }:
  LETTER (LETTER|DIGIT|DOT { sawdot = true; } )*
  { if( sawdot) { $setType(LEGALNAMEEX); } else { $setType(LEGALNAME); }
  }
| DOT ( DOT | LETTER | DIGIT )* { $setType(LEGALNAMEEX); }
;

General strategy: assume it's the one thing when you encounter
something that makes it the other thing patch the return value.

I'm missing the backslash from your predicate in the LEGALNAMEEX rule
you provided so not sure if this is 100% what you want or something
got lost in pruning for posting to the list.

The INT rule can be non protected with this if I'm not mistaken.

Hmmm you say that LEGALNAMEEX cannot start with a letter... Then this
would be better (sawndigit = sawnondigit):

LEGALNAME_OR_LEGALNAMEEX { bool sawnd = false; }:
  LETTER (LETTER|DIGIT)* { $setType(LEGALNAME); }
| (DOT { sawndigit = true; } | DIGIT )
  ( DOT { sawndigit = true; }  | LETTER { sawndigit = true; } | DIGIT )*
  { if ( sawndigit ) { $setType(INT); } else { $setType(LEGALNAMEEX);} }
;

Add LEGALNAME/LEGALNAMEEX/INT to the tokens section where needed.

Excuse the crappy layout and C++-isms if present. (I did not test the
rules in antlr but I think they should be ok)

> Is it necessary to use a syntactic pradicate?

Don't think so in this case.

Cheers,

Ric


More information about the antlr-interest mailing list