[antlr-interest] Why BANG after rule id ?

Graham Wideman gwlist at grahamwideman.com
Tue Oct 6 15:51:46 PDT 2009


Hi Kaleb and Kirby,

Thanks for taking a look at this. Like you both, I did not find this documented anywhere.

The grammar fragment that I showed as an example (rule range) was from the ANTLR v3 grammar, and is itself a v3 grammar, and ANTLR 3 processes it with no complaint.

Kaleb quotes the relevant part of the v3 grammar that shows the "!" being accepted.  But if you look at that rule, what does it do when it recognizes the "!" ?  The way I read it, it does nothing, or am I missing something that happens automatically or by some other means?

I inspected the code that ANTLR3 generates for the rule "rule", and I see where the '!' is recognized, and where it's matched (consumed) but the code seems to do nothing with that info.

I too am unsure whether this grammar is the actual spec that ANTLR 3 uses, so I ran ANTLR 3 on the ANTLR v3 grammar with "range!" changed to "range", and this seemed to make no difference in code generated for the range rule, aside from changes in variable numbers (which thus gives a lot of diff results, so easy to overlook something).

Anyhow, it looks to me like a '!' in this position is recognized but ignored -- either a vestige of a no-longer active feature, or an anticipation of a future feature?

-- Graham

=============================
rule
scope {
  String name;
}
  : DOC_COMMENT?
    ( modifier=('protected'|'public'|'private'|'fragment') )?
    id {$rule::name = $id.text;}
    '!'?
    ( arg=ARG_ACTION )?
    ( 'returns' rt=ARG_ACTION  )?
    throwsSpec? optionsSpec? ruleScopeSpec? ruleAction*
   ':' altList ';'
    exceptionGroup?
    -> ^( RULE id {modifier!=null?adaptor.create(modifier):null} 
          ^(ARG[$arg] $arg)?
          ^('returns' $rt)?
          throwsSpec? optionsSpec? ruleScopeSpec? ruleAction*
          altList
          exceptionGroup?
          EOR["EOR"]
        )
;



More information about the antlr-interest mailing list