[antlr-interest] Fwd: Simple question, Hard answer?

Bill Mayfield antlrinterest at gmail.com
Wed Sep 3 05:28:52 PDT 2008


The grammar below (derived from the one suggested by Liehann Loots)
seems to be working. It doesn't do exactly what I originally wanted
(match chaos, followed by pattern, followed by chaos) but it seems to
be working to solve my problem...

grammar test;

start	:	either*;

either	:	pattern | .;

pattern	:	T1 T2 T2 T1;

T1	:	'o';
T2	: 	'k';

----------------------------------------------------------------------------------------------------------------


Thomas: This one doesn't work however


grammar test;

start   :       chaos* pattern* chaos*;

pattern :       T1 T2 T2 T1;

chaos   :       T1 | T2;

T1      :       'o';
T2      :       'k';

It seems to be recognizing only "chaos*" and ignores the "pattern*
chaos*" part. I guess it is logical to keep matching the first loop as
long as possible. But if I change that start rule to:

start   :       chaos+ pattern+ chaos+;

Which states that there should be some chaos, some patterns and again
some chaos, I get an error stating that "The following alternatives
could never be matched: 2" which I don't understand. Or would it stay
in the first chaos+ because the parser keeps believing it sees chaos..


Damn.. it's hard to communicate this by email.


Thanks so far for your input Thomas, Matt, Robert & Liehann! I've
learned something out of this...

Kind regards,
Bill




On Wed, Sep 3, 2008 at 2:00 PM, Thomas Brandon <tbrandonau at gmail.com> wrote:
> On Wed, Sep 3, 2008 at 7:36 PM, Bill Mayfield <antlrinterest at gmail.com> wrote:
>> Okay... but what if this isn't a lexer problem? Suppose I my input is
>> only made up of 'o' and 'k'
>>
>> oooookooookkooo -> 1 match
>> oookkoooookkoook -> 2 matches
>>
>> what would my grammar look like?
>>
>> grammar test;
>>
>> start   :       chaos* pattern* chaos*;
>>
>> pattern :       T1 T2 T2 T1;
>>
>> chaos   :       T1 | T2;
>>
>> T1      :       'o';
>> T2      :       'k';
>>
>>
>> This one compains that it can match multiple alternatives.
> Posting the actual warning given is generally a good idea.
>> I don't understand since I read it like: match any iteration of T1 or T2
>> tokens followed by T1 T2 T2 T1 followed by any iteration of T1 or T2
>> tokens....
>>
> Yes, but assuming you want the T1 T2 T2 T1 to match as a pattern
> rather than a sequence of chaos's would lead to problems in other
> cases so ANTLR is warning you either could match and thus it will
> disable one of the alternates. I think it will automatically choose
> pattern rather than chaos in which case you can ignore the warning as
> it is doing what you want.
>
> Tom.
>>
>> Thanks again :o)
>>
>>
>> Kind regards,
>> Bill
>>
>


More information about the antlr-interest mailing list