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

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

问题描述

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

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

这给出了错误:

'$body::counter' 中规则 '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>();
]

它给出了错误:

'$body::bodyContent' 中规则 '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天全站免登陆