R – list of reserved words in ANTLR grammars

antlr

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)?

Best Answer

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: https://web.archive.org/web/20120314155217/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. parser rules (other than they should start with a lower case, which is not convention, of course...).

Related Topic