Untested but should work, assuming you want something that works after
the fact.
W.
SOME_STRING : YourStringRule
{
String wholeStr;
int strLen, maxStrLen = 20;
wholeStr = getText().toString();
strLen = wholeStr.length;
if ( strLen > maxStrLen ) {
System.out.println( strLen + " > " + maxStrLen + ".
Truncating...");
setText(wholeStr.substring(maxStrLen)); // text is now
truncated
}
};
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Gavin Lambert
Sent: Friday, September 12, 2008 8:01 AM
To: Olya Krachina; [EMAIL PROTECTED]
Subject: Re: [antlr-interest] set max number of characters in a string
literal
At 15:48 12/09/2008, Olya Krachina wrote:
>I am working on a lexer and i was wondering how i could set a
max
>limit on the number of characters that make up a string literal,
>i.e. it is valid when there are n (let's say n = 20) or less
chars.
>I tried setting lookahead to 20 (options k = 20) but it did not
>have any effect. I am using antlr 2.7.
If you need to parse exactly 20 chars and stop dead (eg. for
fixed-width data formats), you'll need to spell it out explicitly
-- eg. repeat CHAR 20 times. (ANTLR only supports cardinalities
for zero, one, or many.)
Of course you can be a bit more clever about it, eg. making a rule
that contains five CHARs and then use that rule four times, etc.
If your input language isn't actually ambiguous, though, and you
just want to do this for validation purposes, then your best bet
is to just successfully match however many characters happen to
appear (even if more than 20), and use a semantic action in the
parser or tree parser to validate the length after the fact.
Generally speaking the lexer should be built to be as tolerant as
it possibly can -- wait until parsing time to detect and report
errors.
List: http://www.antlr.org:8080/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org:8080/mailman/options/antlr-interest/your-email-addr
ess
List: http://www.antlr.org:8080/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org:8080/mailman/options/antlr-interest/your-email-address
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---