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

Jim Idle jimi at temporal-wave.com
Fri Feb 25 09:14:28 PST 2011


Each rule in your tree parser must set the return template correctly and
each rule that calls a rule should pick up that rule's template and use it
when creating its own template. Otherwise you create the template and it
just goes away if you don't reference it. The ANTLR book would give you a
big jump start on this, but don't forget to look at the generated Java
code as this often shows you why you are not getting what you expect to
get.

ruleDefinition
	:	^(RULE

            statements
        )

        {
            // Just pass on up the generated statement code
            //
            $st = $statements.st;

        }

    |   RULE    // No statements, no st
	;



Jim



> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Peter Crowther
> Sent: Friday, February 25, 2011 3:23 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] C# antlr 3.3.1 and ST: Wallowing in
> templates, would like some direction
>
> 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
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address


More information about the antlr-interest mailing list