[antlr-interest] Value passed to String based rule parameter has spaces inserted after commas.
Benjamin Niemann
pink at odahoda.de
Sat Jul 21 05:55:05 PDT 2007
Sam Ellis wrote:
> I've using ANTLR Version 3.0 (May 17, 2007). I have found that quoted
> strings passed to String-based rules in my grammar are having extra spaces
> inserted into the string when it appears in the generated parser code.
> Specifically, if the string in my grammar contains a comma, then a space
> is inserted after the comma in the resulting code. Here is an example:
>
> grammar Fruit;
>
> options {
> language=Java;
> }
>
> fruit[String allowedFruit]
> : NAME { System.out.println(allowedFruit);}
> ;
>
> bowl
> : fruit["apple,orange"]
> ;
>
> NAME : ('a' .. 'z')+
> ;
>
> The code that is generated contains:
>
> // /Users/samellis/Fruit.g:12:7: ( fruit[\"apple,orange\"] )
> // /Users/samellis/Fruit.g:12:7: fruit[\"apple,orange\"]
> ...
> fruit("apple, orange");
>
> So you can see that a space has appeared in the generated code (not in the
> comments though). Bug in ANTLR perhaps?
Not a bug, but a limitation. The contents of [...] is a comma seperated list
of code fragments of the target language. ANTLR does not know anything
about the structure of the target language, it just splits the list by
comma, replaces $ and % references and passes it to the code generation
template, which construct a method call suitable for the target language.
So what ANTLR sees is: a rule with two arguments '"apple' and 'orange"'.
This is passed to the template which generates something like
<rule>([list of arguments, seperated by ", "]);
Hence the additional space.
There not much ANTLR can do without knowing how strings, types,
comments, ... look like in the target language - that it would need a
complete grammar of each target language, which would make things way more
difficult to implement.
So finally: to work around this limitation, use something like that:
bowl
: { f = "apple,orange"; }
fruit[f]
;
--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
More information about the antlr-interest
mailing list