[antlr-interest] Problems with Tree Grammars outputting Templates in C#

Maik Schmidt contact at maik-schmidt.de
Thu Aug 25 08:15:13 PDT 2011


Hi!

I am working through the "Language Implementation Patterns" book and I am
translating all examples to C#. Unfortunately, I cannot get StringTemplate
output to work.

Attached you'll find a grammar named Cymbol.g3 and a tree parser named
Gen.g3. I have downloaded them from the publisher's web site and only had to
change a few minor thing to make them compliant with C#.

VisualStudio 2010 turns the g3 files into grammars and lexers nicely, but
unfortunately I cannot compile them. The problem seems to appear whenever an
expression such as "d+=" is used as in the following rule:

compilationUnit:   ( d+=classDeclaration | d+=methodDeclaration |
d+=varDeclaration )+ -> file(defs={$d})

For such rules I get errors like the following:

Error 6 Argument 1: cannot convert from 'Antlr3.ST.StringTemplate' to
'Antlr.Runtime.Tree.CommonTree' c:\users\mschmidt2\documents\visual studio
2010\Projects\StringTemplateTest\StringTemplateTest\obj\x86\Debug\Gen.cs

The corresponding code snippet looks as follows:

case 1:
DebugEnterAlt(1);
// Gen.g3:13:11: d+= classDeclaration
{
DebugLocation(13, 12);
PushFollow(Follow._classDeclaration_in_compilationUnit74);
d=classDeclaration();
PopFollow();
if (state.failed) return retval;
if (list_d==null) list_d=new List<StringTemplate>();
list_d.Add(d.Template);

The problem seems to be the initial definition of list_d which is

List<CommonTree> list_d = null;

What am I doing wrong?

Cheers,
Maik
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Cymbol.g3
Type: application/octet-stream
Size: 4536 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20110825/2d01979d/attachment.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Gen.g3
Type: application/octet-stream
Size: 3141 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20110825/2d01979d/attachment-0001.obj 


More information about the antlr-interest mailing list