Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ANTLR allow multiple variable definitions in the locals clause?

Tags:

antlr4

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?

like image 899
DCX Avatar asked Mar 23 '23 21:03

DCX


1 Answers

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
]
like image 127
Sam Harwell Avatar answered Apr 26 '23 19:04

Sam Harwell