[antlr-interest] How to do "not" in a syntactic predicate?

Naveen Chawla naveen.chwl at googlemail.com
Fri Oct 16 04:39:30 PDT 2009


So, does anybody have a way of doing "Take *a* IF not followed by *b* (both
syntactic constructs)" ?

i.e.
q: (a !b)=> a;                        //("!" or "not" doesn't exist in
ANTLR)

I have unsuccessfully tried the following:

q: (a b?)=> {mark = input.mark();} a {bFoundYet=false; i=input.index();} b?
{if(input.index()>i){bFoundYet=true;input.rewind(mark);} }
{bFoundYet==false}?=> ;       //"mark" and "bFound" are my own variables,
already declared

Obviously something is wrong here and there may be a much, much simpler way.
Or I'm nearly right.

2009/10/15 Naveen Chawla <naveen.chwl at googlemail.com>

> Yes, if you put "sentence: q b;" above q then it accepts "asdqwe", even
> though the *a* in q is followed by *b*. And in fact, on my side, accepts *a*
> regardless of what it's followed by. So the predicate seems to have no
> effect, on my side.
>
> I haven't yet been able to reproduce the problem itself in simpler form,
> and I agree about trying to find other ways, although unable so far. A
> working workaround of "not followed by" would fix the whole problem though.
>
> Jim posted in 2007 a PEG-style "followed by" solution using "foo: a
> {input.mark();} b {input.rewind();}".. which "takes *a* and *b* and then
> rewinds as if it it has just taken *a* "  so I was wondering if there was a
> solution for "not followed by" using mark(), rewind() and maybe reset()
> which "takes *a* and *b* and then (something or another?)..." ?
>   2009/10/14 Indhu Bharathi <indhu.b at s7software.com>
>
>>  Here is the complete grammar:
>>
>>
>>
>> grammar Test;
>>
>>
>>
>> q             :               a ((b)=>NOWAY | /*nothing*/);
>>
>>
>>
>> a              :               'asd';
>>
>> b             :               'qwe';
>>
>>
>>
>> fragment NOWAY:          ;
>>
>>
>>
>>
>>
>> ‘q’ will accept “asd” but not “asdqwe”. Am I missing something?
>>
>>
>>
>>
>>
>> BTW, if you can post a simplified example of your exact problem, you might
>> get more elegant solutions. This is only a hack for the specific problem you
>> posted. Maybe your original problem can be solved in a more elegant way.
>> Cases that require hacks of this kind are very rare and arises only while
>> parsing some very obscure languages.
>>
>>
>>
>> Cheers, Indhu
>>
>>
>>
>>
>>
>> *From:* Naveen Chawla [mailto:naveen.chwl at googlemail.com]
>> *Sent:* Wednesday, October 14, 2009 11:38 PM
>> *To:* Indhu Bharathi
>> *Cc:* Jim Idle; antlr-interest at antlr.org
>>
>> *Subject:* Re: [antlr-interest] How to do "not" in a syntactic predicate?
>>
>>
>>
>> Yikes, that doesn't seem to work, it seems to give a normal positive
>> predicate for me. (Or something). But not "if a not followed by b" => a.
>>
>>
>>
>> ?
>>
>> 2009/10/14 Indhu Bharathi <indhu.b at s7software.com>
>>
>> The other say I replied from my ipod and was not able to test it with
>> ANTLR. I checked it now and the code doesn’t work for me too. However you
>> can try a variant:
>>
>>
>>
>> q             :               a ((b)=>NOWAY | /*nothing*/)
>>
>>                 ;
>>
>>
>>
>> fragment NOWAY
>>
>>                 :               ;
>>
>>
>>
>> This is tested J But note that this is only a hack and use it only when
>> there is no other alternative.
>>
>>
>>
>> Going by your “one of my "smaller" rules is "consuming" something that
>> should belong to a "larger" rule” description, it looks like you can
>> solve that problem by turning off greedy. Grep for ‘greedy’ in the book or
>> wiki.
>>
>>
>>
>> Cheers, Indhu
>>
>>
>>
>>
>>
>> *From:* antlr-interest-bounces at antlr.org [mailto:
>> antlr-interest-bounces at antlr.org] *On Behalf Of *Naveen Chawla
>> *Sent:* Wednesday, October 14, 2009 3:50 PM
>> *To:* Jim Idle
>> *Cc:* antlr-interest at antlr.org
>> *Subject:* Re: [antlr-interest] How to do "not" in a syntactic predicate?
>>
>>
>>
>> Jim, being new to predicates I realised soon after this that you were
>> talking about token lookaheads. And yes I do need a syntactic lookahead (but
>> thas is negative). In beginner-speak, one of my "smaller" rules is
>> "consuming" something that should belong to a "larger" rule. This would be
>> correct if that particular something (e.g. "a") was *not* followed by a
>> syntactic construct conforming to "b". Hence (a !b)=>a (for the smaller
>> rule) seems the simplest solution to this to me. Is Indhu's version correct
>> for doing this trick? It doesn't seem to work for me (but I might be doing
>> something else wrong). If not correct, what is the correct way? My target is
>> Java.
>>
>> 2009/10/5 Jim Idle <jimi at temporal-wave.com>
>>
>> Use a semantic predicate rather than syntactic. You possibly need a bated
>> predicate here too:
>>
>>
>>
>> { input.LA(1) == A && input.LA(2) != B}?=>
>>
>>
>>
>> However, if you need that kind of syntactic predicate, then I suggest you
>> may be approaching your problem incorrectly.
>>
>>
>>
>> Jim
>>
>>
>>
>> *From:* antlr-interest-bounces at antlr.org [mailto:
>> antlr-interest-bounces at antlr.org] *On Behalf Of *Naveen Chawla
>> *Sent:* Monday, October 05, 2009 7:02 AM
>>
>>
>> *To:* antlr-interest at antlr.org
>> *Subject:* [antlr-interest] How to do "not" in a syntactic predicate?
>>
>>
>>
>> If I do
>>
>>
>>
>> (a ~b)=> a
>>
>>
>>
>> meaning "take this alternative if you encounter an a when not followed by
>> b"
>>
>>
>>
>> I get a syntax error: unexpected token b
>>
>> Is it the right syntax to use '~'?
>>
>>
>>
>> N
>>
>>
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe:
>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>
>>
>>
>>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20091016/beb3141c/attachment.html 


More information about the antlr-interest mailing list