Does ANTLR provide multiple variable definitions in a locals clause?

in Parser grammar I would like to define several variables in locals .

A simplified example is as follows:

 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 an error:

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

If I change the lines in the locals clause as follows

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

he gives an error:

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

It appears that ANTLR only recognizes the first definition of a local variable in the locals clause.

Is there a way to define multiple local variables in locals ?

+4
source share
1 answer

Yes, but they are separated by a comma, as a list of parameters, and returns .

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

Source: https://habr.com/ru/post/1481710/


All Articles