[antlr-interest] Inline StringTemplates - sample?

Steve Bennett stevagewp at gmail.com
Sun Nov 18 15:50:35 PST 2007


Hi all,
  I'm trying to find a simple "hello world" version of a grammar that
uses inline stringtemplates, with associated test rig. The book is
frustratingly brief on inline stringtemplates, and doesn't give any
java code - only for stringtemplates defined with an external file,
and using a tree grammar.

Here's my grammar:
---
grammar st1;
options {output=template;}

start	:	text;

CHARS	:	('a'..'z' | ' ' | ('\r'? '\'n'))+;
text	:	CHARS -> template(x={$CHARS.text}) "[<x>]";
---

and here's my test rig:
---
import org.antlr.runtime.*;

public class Main {
    public static void main(String[] args) throws Exception {
        ANTLRInputStream input = new ANTLRInputStream(System.in);
        st1Lexer lexer = new st1Lexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        st1Parser parser = new st1Parser(tokens);
        parser.start();
    }
}
---

Obviously I need to add something (currently it outputs nothing), but
what? I do have stringtemplate-3.0.jar on the CLASSPATH.

Also, question 2: what is the correct way to express a rule outputting
a stringtemplate that is composed of some literal text and its
children's stringtemplates. For example, a "bold" rule needs to output
<b>, followed by whatever its children output, followed by </b>. The
examples mostly use child.$text which obviously isn't right, as it's
not the child's raw text that I want to output, but their transformed
text...

Thanks very much for any help,
Steve


More information about the antlr-interest mailing list