[antlr-interest] Fwd: Mapping ANTLR grammar to XML

Ketan Maheshwari ketancmaheshwari at gmail.com
Fri Feb 19 08:13:43 PST 2010


forwarding my reply to the list that I forgot when replying earlier ...

---------- Forwarded message ----------
From: Ketan Maheshwari <ketancmaheshwari at gmail.com>
Date: Fri, Feb 19, 2010 at 3:44 PM
Subject: Re: [antlr-interest] Mapping ANTLR grammar to XML
To: johnpool at ision.nl


John

Thanks for your reply.

By target language if you mean the language I want to write the translator
in; it is java.

So, I want to read a program written in the Domain Language I wrote the
grammar of, and somehow walk the tree using the grammar and place tokens in
xml tags corresponding to the nodes it belongs to.

For instance the following assignment statement :

in = ["t1 hyde"%patient:"mr hyde", "t1 argan"%patient:"mr argan"]

is equivalent to the following xml:

<source name="in" type="String">
         <array>
             <item><tag name="patient" value="Mr Hyde" />T1 Hyde</item>
             <item><tag name="patient" value="Mr Argan" />T1 Argan</item>
         </array>
</source>

The tagnames and elementnames are not important as such but the fact that
they map as per the tree structure and the process is automated.


On Fri, Feb 19, 2010 at 2:43 PM, <johnpool at ision.nl> wrote:

> If I understand you correctly, you want to map your AST CommonTree
> structure onto an equivalent XML structure. You do not mention what you
> target language is: for C# you might do something like this:
>
>    XmlTextWriter xw = new XmlTextWriter ();
>
>    ...
>
>    private void CreateXmlTree (XmlTextWriter xw, CommonTree tree)
>    {
>      if (tree.ChildCount == 0)
>        xw.WriteString (tree.Text);
>      else
>      {
>        xw.WriteStartElement (tree.Text);
>
>        foreach (CommonTree c in tree.Children) CreateXmlTree (xw, c);
>
>        xw.WriteEndElement ();
>      }
>    }
>
>
> Regards,
>
> John Pool
>
> > Hello Friends
> >
> > I have an ANTLR grammar for my Domain language. While testing real
> > programs
> > against this grammar I could see the GUI generates a tree and places the
> > language tokens at an appropriate leaf node of that tree.
> > Now my next task is to build an xml equivalent of each program: so to
> > build
> > a translator that can translate this program into an xml. From the tree
> > structure it strikes very similar to xml and I believe the process is not
> > very tedious.
> >
> > Could some one guide me to any tools or guidelines available on this
> > direction? Has anyone done some similar thing in the past?
> >
> > Many Thanks in Advance.
> > --
> > Regards
> > Ketan
> >
> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > Unsubscribe:
> > http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> >
>
>
>


-- 
Regards
Ketan



-- 
Regards
Ketan


More information about the antlr-interest mailing list