ANTLR是否在locals子句中允许多个变量定义? [英] Does ANTLR allow multiple variable definitions in the locals clause?

查看:49
本文介绍了ANTLR是否在locals子句中允许多个变量定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在解析器语法中,我想在 locals子句.

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

一个简化的示例如下:

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++;
                } ;

这给出了错误:

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

如果我像这样交换locals子句中的行

If I swap the lines in the locals clause like this

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

它给出了错误:

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

显然,ANTLR仅识别locals子句中的第一个局部变量定义.

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

有没有办法在locals下定义多个局部变量?

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

推荐答案

是的,但是它们像参数列表和returns子句一样以逗号分隔.

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
]

这篇关于ANTLR是否在locals子句中允许多个变量定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆