2013-05-20 26 views
4

在解析器语法中我想在 locals子句中定义几个变量。ANTLR是否允许在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 [ 
    long counter = 0; 
    Map<String, String> bodyContent = new HashMap<String, String>(); 
] 

locals子句中的线它给出了错误:

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

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

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

回答

7

是的,但它们是逗号分隔的,如参数列表和returns子句。

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

这就是解决方法,谢谢! – DCX

相关问题