[antlr-interest] Q: Peek at token without consumeing

Gustaf Johansson gustaf.j at gmail.com
Sun May 22 12:56:04 PDT 2011


Hi,

This does not seem to work.

I believe the problem is that "( . '}' ) =>" looks forward one token,
when what i need is to look backwards.
Can this be acomplished in a similar manner?

BR G

On Sun, May 22, 2011 at 8:52 PM, Ben Corne <ben.corne at gmail.com> wrote:
> I think you can make use of rule precedence for this:
> end
>   : ( '}' ) => /* this won't consume */
>   | ( . '}' ) => /* checks 1 token/char and won't consume */
>   | ';'
>   ;
> 2011/5/22 Gustaf Johansson <gustaf.j at gmail.com>
>>
>> Hi,
>>
>> I have a tricky problem that i cant seem to get right.
>>
>> I have a static semantics rule which says " ';' is optional if
>> preceding statement ends with '}' or the statement is last in list"
>> Basically meaning either have LA(-1) == '}' or LA(1) == '}' or we
>> should expect a ';'.
>>
>> This rule is used in many places throughout the grammar so a generic
>> approach would be best.
>>
>> I would like this case to produce good error messages since missing
>> ';' is quite common.
>> Can i use a "standard" rule and mark it somehow to not consume a token
>> if it shouldnt (like the below example)?
>>
>> end:
>>        : ';'
>>        | '}' { doNotConsume() }
>>        | ( LA(-1) == '}' ) { doNotConsume() } ;
>>
>> BR Gustaf
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe:
>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>


More information about the antlr-interest mailing list