[stringtemplate-interest] ST/ANTLR Language Translation, Optional Attributes

Alan Larkin alan.j.larkin at ucd.ie
Tue Jan 16 04:29:25 PST 2007


Im new to this so please be patient and have a go at reading/answering
this Q ...

I am experimenting with StringTemplate and ANTLR for language
translation a la Terrence's article
(http://www.codegeneration.net/tiki-read_article.php?articleId=77).

I have a question about how to handle stuff like the following production (from Wiggs' C++ Grammar):

...
("typedef")=>
  (
    ("typedef" class_specifier)=>
    "typedef" class_decl_or_def[fs] (init_declarator_list)? SEMICOLON
  |
    ("typedef" "enum")=>
    "typedef" enum_specifier (init_declarator_list)? SEMICOLON
  |
    (declaration_specifiers function_declarator[0] SEMICOLON)=>
    declaration
  |
    declaration
)
...

Should I create a single StringTemplate called typedef which takes all
the possible arguments, like this:

/* There is no translation required for typedefs */
typedef(class_decl_or_def, init_declarator_list, enum_specifier,
declaration) ::= <<
  "typedef " <class_decl_or_def> <enum_specifier> <init_declarator_list; separator=", "> ";"
>>

and write the production as:

...
("typedef")=>
  { code = template("typedef") }
  (
    ("typedef" class_specifier)=>
    "typedef" a=class_decl_or_def[fs] (b=init_declarator_list)? SEMICOLON
    {
      code.setAttribute("class_decl_or_def", a);
      code.setAttribute("init_declarator_list", b);
    }			}
  |
    ("typedef" "enum")=>
    "typedef" a=enum_specifier (b=init_declarator_list)? SEMICOLON
    {
      code.setAttribute("enum_specifier", a);
      code.setAttribute("init_declarator_list", b);
    }
  |
    (declaration_specifiers function_declarator[0] SEMICOLON)=>
    a=declaration
    {
      code.setAttribute("declaration", a);
    }
  |
    a=declaration
    {
      code.setAttribute("declaration", a);
    }
)
...

?

This doesnt capture the fact that only certain combinations of
arguments/attributes will occur.

Or should I write different templates for each type of typedef?

Or is there some other way that is appropriate?

Thanks for your help.


More information about the stringtemplate-interest mailing list