在解析器语法中,我想在locals
条款。
一个简化的示例如下所示:
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
中的行像这样的条款locals [
long counter = 0;
Map<String, String> bodyContent = new HashMap<String, String>();
]
它给出了错误:
unknown attribute 'bodyContent' for rule 'body' in '$body::bodyContent'
显然,ANTLR 只识别
locals
中的第一个局部变量定义。条款。有没有办法,在
locals
下定义多个局部变量?
最佳答案
是的,但它们像参数列表和 returns
一样用逗号分隔条款。
locals [
Map<String, String> bodyContent = new HashMap<String, String>(),
long counter = 0
]
关于antlr4 - ANTLR 是否允许在 locals 子句中定义多个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16644200/