[antlr-interest] [C target] [3.1.1] Deriving imaginary nodes from real tokens

Sven Van Echelpoel sven.van.echelpoel at empolis.com
Fri Dec 12 02:49:00 PST 2008


[...]
> This is just a guess, but I suspect the reason for this is the 
> lack of the ability to have overloaded methods in C :)
> 
Yes
> In Java, you can have one method that accepts two different types 
> of parameters, so the template code for the version that passes in 
> a string and the version that passes in a token can be identical, 
> leaving it up to the Java compiler to figure out which one you 
> meant to call.
> 
> In C, however, there's no overloading so this is not 
> possible.  The template will have to generate calls to 
> differently-named functions depending on whether you are passing 
> in a string or a token -- and unfortunately, since this is 
> target-specific code, there's no (reliable) way for ANTLR to 
> detect the type of a single parameter.  So the only option left 
> open to it is to settle for detecting whether you're passing one 
> parameter or two.

Indeed it does, but the single argument case can never be a text, it is
always a token, at least if I understand this correctly. There are in
fact two templates in AST.stg. There is one that does the right thing
when a single argument is passed, while the other (which is
unfortunately the one that gets invoked in my case) does not.

> 
> (A native C++ target could remove this limitation again, of 
> course.)
> 
> The workaround would be to change BAR[$b] to BAR[$b, $b.text], I 
> think.
> 
Or, the one I picked BAR[$b, "" ], as I have no use for the text.

Sven



More information about the antlr-interest mailing list