[antlr-interest] CSharp3 Target (ANTLR 3.2)

Kevin Carroll kcarroll at signmgmt.com
Tue Mar 23 06:51:07 PDT 2010


Andrew,

Thanks for your reply.  As I mentioned in my post, I am using Sam Harwell's port (http://markmail.org/message/secfbpchtbsev6pv).  I had to do some similar things as you to get the tree rewriting working (e.g., added overrides to TopDown, Bottomup and ApplyOnce; fixed one capitalization error in ASTTreeParser.stg - marked any rules called from outside the parser class "public").  With those changes, things are working, I believe, identically to the java implementation.  I was just hoping for an officially blessed version that perhaps the Actipro SyntaxEditor folks would support.

Thanks again,
Kevin Carroll

From: Andrew Bradnan [mailto:andrew.bradnan at gmail.com]
Sent: Monday, March 22, 2010 5:20 PM
To: Kevin Carroll
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] CSharp3 Target (ANTLR 3.2)


I can't answer the question about the official C# runtimes for 3.2.  I have gotten tree rewriting working in C#
however.
Here's how to get this working with a slightly modified antlr-3.2.jar and the latest CSharp2 runtime.  Specifically
I got the sample for pattern 19 (symtab/class) from Language Implementation Patterns working.  That should cover
all the filter=true; Downup() goodness.

1.) Download the latest CSharp2 runtime code and build it (I got a zip from http://fisheye2.atlassian.com/browse/antlr/runtime/CSharp2).
There's a Visual Studio 2008 project file that builds it without modification.
2.) You'll need to make a few modifications to the string templates.  The file to modify in the jar (antlr3-3.2.jar) is
org\antlr\codegen\templates\CSharp2\CSharp2.stg.  (To update...Unzip the jar, modify CSharp2.stg, and rezip sans
any compression).
Here are the updates to make.
a.) Change Index() to just Index (there are 3 occurances)
b.) Update the treeParser rule to use the TreeRewriter base class as shown below.  Replace the old version
(line 312) with this.

/** How to generate a tree parser; same as parser except the input
 *  stream is a different type.
 */
treeParser(grammar, name, scopes, tokens, tokenNames, globalAction, rules,
           numRules, bitsets, labelType={<ASTLabelType>}, ASTLabelType="object",
           superClass={<if(filterMode)><if(buildAST)>TreeRewriter<else>TreeFilter<endif><else>TreeParser<endif>},
           members={<actions.treeparser.members>},
           filterMode) ::= <<

3.)  Add the override methods for Topdown / Bottomup to YourGrammar.g.

[[To have this behave like the java runtime, the string template for rule would have to be modified to override
the two methods topdown/bottomup.  You have to explicitly "override" in C#.  topdown/bottomup also need to be all
lower case in CSharpN runtimes since they are not Lexer rules.]]

@members
{
 ...
    protected override void Topdown() { topdown(); }
    protected override void Bottomup() { bottomup(); }
}

4.)  Make sure you translate the java version of TreeAdapter class to explicitly override Create/DupNode/ErrorNode
(or you'll get mysterious casting exceptions if left as create/dupNode/errorNode).  Mine looks like this
 // test.cs
    public static Test.TreeAdaptor cymbalAdaptor = new Test.TreeAdaptor();
 public class TreeAdaptor : CommonTreeAdaptor
 {
        public override object Create(IToken token)
  {
   return new CymbolAST(token);
        }
        public override object DupNode(object t)
  {
            if (t == null)
                return null;
            return Create(((CymbolAST)t).Token);
        }
        public override object ErrorNode(ITokenStream input, IToken start, IToken stop, RecognitionException e)
        {
            CymbolErrorNode t = new CymbolErrorNode(input, start, stop, e);
            Console.WriteLine("returning error node '"+t+"' @index="+input.Index);
            return t;
        }
    }
Hopefully that helps.
Andrew
On Mon, Mar 15, 2010 at 9:34 AM, Kevin Carroll <kcarroll at signmgmt.com<mailto:kcarroll at signmgmt.com>> wrote:
What is the "official" status of C# port of ANTLR 3.2 (i.e., language=CSharp3)?  Several of the new features of 3.2 are utilized quite a bit in "Language Implementation Patterns".  Most importantly, tree rewriting.

I have been using Sam Harwell's stand-alone Windows port of ANTLR 3.2 with success (after a few tweaks to unify member name capitalization and a correction to the ApplyOnce method to bring it up-to-date with the Java version).  The only true problem is that I would really love to use my ANTLR-generated parser in conjunction with Actipro Software's SyntaxEditor, but they only currently support the CSharp2 target (understandably).

Thanks for any info,
Kevin Carroll

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address



--
/Andrew


More information about the antlr-interest mailing list