[stringtemplate-interest] Strange tail-recursive behaviour
VAUCHER Laurent
VAUCHER at fermat.fr
Wed Oct 12 06:52:39 PDT 2005
Hi,
I'm currently trying StringTemplate (v2.2 downloaded yesterday oct 11)
and trying a kind of tail-recursive template, I stumbled upon the
following behaviour.
I have a template group file that reads :
--- "mytest/main.stg" ----------------------
group main;
/* Root template */
root(place) ::= <<
<namestail(place)>
>>
/* Tail template */
namestail(x) ::= <<
<if(x)>
++++
TRUE:<x>
TRUE:<x>
first(PLACE)=<first(x)>
rest(PLACE)=<rest(x)>
TAIL CALL:
<namestail(x=rest(x))>
<else>
-----
FALSE:<x>
<endif>
>>
---------------------------------------------
and a simple driver class
----- "Main.java" ---------------------------
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;
import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(new SimpleDateFormat().format(new Date()));
try {
FileReader reader = new FileReader("src/mytest/main.stg");
StringTemplateGroup stg = new StringTemplateGroup(reader,
AngleBracketTemplateLexer.class);
StringTemplate firstST = stg.getInstanceOf("root");
firstST.setAttribute("place", "A");
firstST.setAttribute("place", "B");
System.out.println("Generated [" + firstST.toString() + "]");
} catch (FileNotFoundException eFNFE) {
eFNFE.printStackTrace(System.err);
}
}
}
--------------------------------------------
And the output is :
------------- output -----------------------
12/10/05 15:37
Generated [
++++
TRUE:AB
TRUE:AB
first(PLACE)=A
rest(PLACE)=B
TAIL CALL:
++++
TRUE:B
TRUE:
first(PLACE)=
rest(PLACE)=
TAIL CALL:
-----
FALSE:
]
--------------------------------------------
What I find annoying is the inner call to namestail : the value of x
changes from a line to the next. The first "TRUE:<x>" yields "TRUE:b",
the second "TRUE:" as if the value had been swallowed by the first <x>.
As you can see, the outer invocation of namestail does not seem
afflicted...
Is there something I am missing (dynamic scoping can be tricky, I
know) ?
Laurent.
More information about the stringtemplate-interest
mailing list