问题
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