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

Gavin Lambert antlr at mirality.co.nz
Fri Dec 12 02:37:26 PST 2008


At 03:17 12/12/2008, Sven Van Echelpoel wrote:
 >According to the book you can derive imaginary nodes from real
 >tokens to take over attributes (such as line number) from the
 >latter. The syntax for this is :
 >
 >foo : b='bar' -> ^( BAR[ $b ] )
 >    ;
[...]
 >In the first one, createImaginaryNode, if you pass one argument 
to
 >it, which I'm assuming to be the case, a type from a text is 
created.
 >In the second one, createRewriteNodeFromElement, a type from a 
token
 >is created with the same number of arguments.
 >
 >Although I have not dug any further, I can tell from examining 
the
 >generated parser code that in an AST rewrite, the
 >createImaginaryNode is used, resulting in nothing (the cast to
 >pANTLR3_UINT8 probably and luckily makes it point to a zero,
 >immediately terminating the string).
 >
 >It can be made to work by passing the second argument (the 
text),
 >even though I have no use for that.

This is just a guess, but I suspect the reason for this is the 
lack of the ability to have overloaded methods in C :)

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.

(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.



More information about the antlr-interest mailing list