Hi AA,
[0-9]{1,4} is just syntactic sugar for the following:
[0-9] ([0-9] ([0-9] ([0-9])?)?)?
ANTLR doesn’t support the earlier syntax, but either of these forms is rarely needed and generally not recommended. Whenever possible, it’s preferable to place your multiplicity validation in a separate semantic analysis phase that is performed after the parsing is complete. For ANTLR 4, this would be written as a listener or visitor.
Thanks,
Sam
--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "antlr-discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/antlr-discussion/mAIRHaU8B0o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to antlr-discussi...@googlegroups.com.
Hi AA,
When parsing, you generally want to move as many constraints from your grammar to a semantic analyzer as you can, as long as it doesn’t result in your parser producing unusable trees. This strategy results in much more meaningful error messages being reported to users. In some cases, such as Unicode escape sequences within strings, you may actually need to use multiplicity constraints. For example, the following grammar fragment uses modes to pass escape sequences within strings to the parser as individual tokens. The multiplicity constraint in the UnicodeEscape rule is required to ensure that \u00523 is passed as two tokens: UnicodeEscape “\u0052” and Text “3”.
DoubleQuote : '"' -> pushMode(StringMode);
mode StringMode;
StringMode_DoubleQuote : DoubleQuote -> type(DoubleQuote), popMode;
Text : ~[\\"]+;
UnicodeEscape : '\\u' HexDigit HexDigit HexDigit HexDigit;
InvalidUnicodeEscape : '\\u' (HexDigit (HexDigit HexDigit?)?)?;
Escape : '\\' [rnt];
InvalidEscape : '\\' (. | EOF);