tags:

views:

156

answers:

2

I recently created an ANTLR3 parser rule

options : foo bar;

which didn't compile and it took me some time to discover that options was a reserved word (AntlrWorks indicated an error but not why). Is there a list of reserved words in ANTLR and are there best practices in naming rules (which might help avoid this)?

+2  A: 

The reserved words of ANTLR v3 are:

    Keyword  |  Description
    ---------+--------------------------------------------------------
    scope    |  Dynamically-scoped attribute
    fragment |  lexer rule is a helper rule, not real token for parser
    lexer    |  grammar type
    tree     |  grammar type
    parser   |  grammar type
    grammar  |  grammar header
    returns  |  rule return value(s)
    throws   |  rule throws exception(s)
    catch    |  catch rule exceptions
    finally  |  do this no matter what
    options  |  grammar or rule options
    tokens   |  can add tokens with this; usually imaginary tokens
    import   |  import grammar(s)

See: http://www.antlr.org/wiki/display/ANTLR3/ANTLR+Cheat+Sheet (at the end of the page)

Don't know of a "naming convention" w.r.t. rules (other than they should start with a lower case, which is not convention, of course...).

Bart Kiers
Thanks! I wondered if I shoulde use pseudonamespaced names such as init_a, init_b, ... to avoid clashes if possible.
peter.murray.rust
No problem Peter.
Bart Kiers
A: 

Using the reserved words of your target language can also cause problems.

I spent a day trying to figure out why my grammar wouldn't work only to realize it was getting messed up by the "enum" rule in my grammar I had named "enum".

This may or may not be an issue depending on what the target language is. I was using the default Java target, and it certainly was, so if that's the case you should check the list of reserved keywords for your target language as well.

hollaburoo
Could you expand the explanation please?
peter.murray.rust