开发者

Is there a list of reserved words in ANTLR grammars?

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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜