On May 5, 5:22 am, AC <u...@[EMAIL PROTECTED]
> wrote:
> Raul Brito wrote:
> > My goal is to create a regular expression that matches an unpadded
> > number with 3 digits maximum. For example, 298 or 12 or 1 would all
> > match this regular expression.
>
> > For example, shouldn't something like this work?
>
> > (["1"-"9"]["0"-"9"]["0"-"9"]|["1"-"9"]["0"-"9"]|["1"-"9"])
>
> > It works for an unpadded number with 2 digits, that is (["1"-"9"]
> > ["0"-"9"]|["1"-"9"]) , but with 3 or digits it always fails.
>
> I don't understand, this seems to work. Maybe try to reduce it to a
> small test grammar you can post here. For example, the following works.
>
> Hope this helps.
>
> PARSER_BEGIN(UnpaddedDigitsTestParser)
>
> im****t java.io.*;
>
> public class UnpaddedDigitsTestParser {
> public static void main(String... ignore) {
> int successes = 0, failures = 0;
> new UnpaddedDigitsOrigParser((Reader)null); // prepare for ReInit
> for (String input : positiveCases) {
> UnpaddedDigitsOrigParser.ReInit(new StringReader(input));
> try {
> UnpaddedDigitsOrigParser.start();
> ++successes;
> } catch (TokenMgrError e) {
> System.out.println("failed to accept "+input+": "+e);
> ++failures;
> } catch (ParseException e) {
> System.out.println("failed to accept "+input+": "+e);
> ++failures;
> }
> }
> for (String input : negativeCases) {
> UnpaddedDigitsOrigParser.ReInit(new StringReader(input));
> try {
> UnpaddedDigitsOrigParser.start();
> System.out.println("failed to reject "+input);
> ++failures;
> } catch (TokenMgrError e) {
> ++successes;
> } catch (ParseException e) {
> ++successes;
> }
> }
> System.out.println(successes+" successes, "+failures+" failures");
> }
> static String[] positiveCases = {
> "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
> "11", "22", "33", "44", "55", "66", "77", "88", "99", "100",
> "111", "222", "333", "444", "555", "666", "777", "888", "999"
> };
> static String[] negativeCases = {
> "00", "01", "02", "03", "04", "05", "06", "07", "08", "09",
> "001", "002", "003", "004", "005", "006", "007", "008", "009",
> "011", "022", "033", "044", "055", "066", "077", "088", "099",
> "1000", "1234"
> };}
>
> PARSER_END(UnpaddedDigitsTestParser)
>
> void start() : {} {
> <unpaddedDigits> <EOF>
>
> }
>
> TOKEN : {
> <unpaddedDigits:
> (["1"-"9"] ["0"-"9"] ["0"-"9"] |
> ["1"-"9"] ["0"-"9"] |
> ["1"-"9"]) | "0">
>
> }
Hi AC and all,
Thanks for the response, but I've realised what is my problem.. and
the question is what to do when a number matches two different tokens?
For example, I have a number of type unpadded 3, and a number of type
padded 2:
TOKEN :
{
|
< PADDED_2: ["0"-"9"]["0"-"9"] >
|
< UNPADDED_3: (["1"-"9"]["0"-"9"]["0"-"9"]|["1"-"9"]["0"-"9"]|
["1"-"9"]) >
}
>From what I've understood from the do***entation, using LOOKAHEAD is
the way to go, but i don't seem to have it working. For instance, I
have this:
void Input() :
{}
{
LOOKAHEAD(2)
<UNPADDED_3> <UNPADDED_3> <EOF>
|
<PADDED_2> <EOF>
}
but when my input is "23 23", it always matches PADDED_2, because it
appears first in the definition of tokens. And using the LOOKAHEAD
doesn't seem to help because the first tokens in Input() are
different, although it matches to the same number..
Any solution/tip for my problem here?
Thanks!


|