[antlr-interest] Why BANG after rule id ?

Kirby Bohling kirby.bohling at gmail.com
Tue Oct 6 14:57:45 PDT 2009


On Tue, Oct 6, 2009 at 4:18 PM, Kaleb Pederson <kaleb.pederson at gmail.com> wrote:
> On Tuesday 06 October 2009 03:15:05 am Graham Wideman wrote:
>> Hi folks:
>>
>> I see in the ANTLR grammar that you can place a BANG (exclamation point) after the initial rule id in a rule. Indeed it's used in the ANTLR v3 grammar itself:
>>
>> ----------------------------
>> range!
>>   :  c1=CHAR_LITERAL RANGE c2=CHAR_LITERAL elementOptions?
>>     -> ^(CHAR_RANGE[$c1,".."] $c1 $c2 elementOptions?)
>> ;
>> ----------------------------
>>
>> Could someone illuminate what this ! does?
>
> The exclamation mark is placed on a node when that node should not be present in the AST when output=AST for a grammar.
>
> See:
>
> http://jnb.ociweb.com/jnb/jnbJun2008.html#CreatingASTs
> http://www.antlr.org/wiki/display/ANTLR3/Tree+construction
>

Kaleb,

Neither of your example sites talk about what he is discussing.  It
isn't applied to a node, it is applied to a rule
definition/declaration, not to a rule invoked as part of the
production of a rule.  I checked every '!' in those links and none of
them mention are used like that, or as far as I can tell have a usage
similarly.  I don't know much about ANTLRv2, but I thought the ANTLRv3
wasn't self hosting, so the grammar for ANTLRv3 was written in
ANTLRv2.  Or at least that was the case during the early parts of
ANTLRv3.  Graham, you might try researching ANTLRv2 and see what it
does.

I'm guessing that this is mostly likely what you are looking for,
assuming that I'm correct and the grammar you are looking at is really
an ANTLRv2 grammar:
http://www.antlr2.org/doc/lexer.html#Manipulating_Token_Text_and_Objects

Kirby


More information about the antlr-interest mailing list