Does ANTLR allow multiple variable definitions in the locals clause?

可紊 提交于 2019-12-12 11:28:06

问题


in a Parser Grammar I'd like to define several variables in the locals clause.

A simplified example looks like this:

body
locals [
    Map<String, String> bodyContent = new HashMap<String, String>();
    long counter = 0;
]
            :   BODY_CAPTION NEWLINE line ;
line        :   key='MyKey' TAB value='MyValue' NEWLINE
                {
                    $body::bodyContent.put($key.text, $value.text);
                    $body::counter++;
                } ;

This gives the error:

unknown attribute 'counter' for rule 'body' in '$body::counter'

If I swap the lines in the locals clause like this

locals [
    long counter = 0;
    Map<String, String> bodyContent = new HashMap<String, String>();
]

it gives the error:

unknown attribute 'bodyContent' for rule 'body' in '$body::bodyContent'

Apparently, ANTLR recognizes only the first local variable definition in the locals clause.

Is there a way, to define multiple local variables under locals?


回答1:


Yes, but they are comma separated like the parameter list and returns clause.

locals [
    Map<String, String> bodyContent = new HashMap<String, String>(),
    long counter = 0
]


来源:https://stackoverflow.com/questions/16644200/does-antlr-allow-multiple-variable-definitions-in-the-locals-clause

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!