[antlr-interest] C# antlr 3.3.1 and ST: Wallowing in templates, would like some direction

Peter Crowther peter.crowther at melandra.com
Fri Feb 25 03:23:01 PST 2011


My goal is to take an expression of the form "NAMESPACE a=123, b=456", parse
it into an AST ^(NAMESPACE ^(= a 123) ^(= b 456)) and then unparse that back
to the original text.  I know - not very inspiring yet, but I'll be hacking
at the AST in the middle.  This is a part of a larger project.

The parser's fine.  I'm getting stuck on the use of templates in the
unparser.  I'm reasonably sure I've got the wrong end of at least one stick,
and would appreciate any pointers of "start reading here, you idiot".  If
someone was willing to take 5 minutes to change my example into something
sensible, I'd *really* appreciate it.  I've not yet found a useful tutorial
on using template group files and ANTLR parsers together, and suspect that's
the issue.

File fragments below.  With the tree given above, this produces "NAMESPACE
;" - in other words, nothing is produced for the list at all.  I have two
concerns about my use of templates: 1) the repeated expression and ".st" use
in the grammar seems horribly repetitive and horribly ugly - neither are
common features of antlr or ST -  and 2) I seem to be having issues with
multi-valued attributes.

-- mdxForHealthRenderer.g3 fragment --

options {
    language=CSharp3;
    ASTLabelType=CommonTree;
    output=template;
    tokenVocab = mdxForHealth;
}

namespace_part        : ^(NAMESPACE namespace_list) ->
namespace_part(namespace_list={$namespace_list.st});

namespace_list        : namespace_definition+ ->
namespace_list(namespace_definitions={$namespace_definition.st});

namespace_definition    : ^(EQ namespace_identifier OID) ->
namespace_definition(namespace_identifier={$namespace_identifier.text},
oid={$OID.text});

namespace_identifier    : unquoted_identifier;

... unquoted_identifier.text and OID.text are properly set and appear in
templates, so I think their definitions are fine...

-- end fragment --

-- msas.stg --
group msas;

namespace_part(namespace_list) ::= "NAMESPACE <namespace_list>;"

namespace_list(namespace_definitions) ::= "<namespace_definitions;
separator=','>"

namespace_definition(namespace_identifier, oid) ::= "<namespace_identifier>
= <oid>"
-- end msas.stg --

-- Call sequence (fragment) --
            CommonTree root = (CommonTree)ret.Tree;

            // Unparse as well, to prove it can be done
            string templateFileName = "msas.stg";
            StringTemplateGroup templates = new
StringTemplateGroup(File.OpenText(templateFileName));
            CommonTreeNodeStream nodes = new CommonTreeNodeStream(root);
            nodes.TokenStream = tokens;
            mdxForHealthRenderer unparser = new mdxForHealthRenderer(nodes);
            unparser.TemplateGroup = templates;
            StringTemplate code = unparser.root().Template;
            if (null == code)
                MessageBox.Show("null returned from unparser");
            else
                MessageBox.Show(code.ToString());
-- End call sequence --

Comments, suggestions, and ways of handing me the right end of the stick
would be very welcome.  This really can't be as hard as I've made it for
myself!

Cheers,

- Peter


More information about the antlr-interest mailing list