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
?
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
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With