[antlr-interest] antlr way of controlling generated XML (previously: Token type as string)

Adrian Herscu bmf1972 at fastmail.fm
Sat May 5 07:18:53 PDT 2007


Hi all,

After few days of digging through the antlr framework and krugle for 
code fragments I still cannot guess the proper way of doing a relatively 
simple thing: serialize the AST in XML format, where for each of the 
serialized AST nodes, an attribute containing the token *name* is 
generated as an XML attribute besides the other attributes.
Further, I will need to get the textual position of each AST node too.

I am trying now to wrap the CommonAST instance supplied by the parser 
and replace the serializeXml methods with mine -- however, it does quite 
work and I am investigating it right now...

Another approach I thought about is to supply my own ASTFactory and my 
own AST type. MyAST would be able to serialize as XML all the 
information in need and currently is buried somewhere in the Parser 
instance or in the CommonAST instace...

Please help,
Adrian.



Miguel Ping wrote:
> edit: Sorry for the 2x reply, I just need to lose the habit of not 
> replying to the mailing list
> 
> Err, what version of antlr are you using? I'm guessing v2, I never used 
> it. I use v3. In v3 there is no special switch or something.
> 
> Anyway, I guess there should be a place somewhere where the token <> int 
> is defined. Try peeking inside the .txt or the .smap. I don't know the 
> v2 generation procedure, but it should be similar to v3. If you run 
> outta luck, just ask in the forum, I guess some v2 user should help you out.
> 
> On 5/3/07, *Adrian Herscu* 
> <bmf1972 at fastmail.fm 
> <mailto:bmf1972 at fastmail.fm>> wrote:
> 
>     Hi Miguel, thanks for your fast reply.
> 
>     I looked into the generated files directory and only file types see
>     there are expanded*.g, *.smap, *.txt and of course *.java.
>     Is there some switch to generate them?
>     Adrian.
> 
> 
>     Miguel Ping wrote:
>      > Hi,
>      >
>      > There is a file with the grammar name and .tokens extension
>      > (MyGrammar.tokens) that contains correspondence between numbers and
>      > strings. This file is created by antlr generator. Take a look, it
>      > should suit your needs.
>      >
>      > On 5/3/07, Adrian Herscu
>      > <bmf1972 at fastmail.fm
>     <mailto:bmf1972 at fastmail.fm>> wrote:
>      >> Hi all,
>      >>
>      >> I would like to write an XSL template to transform my AST.
>      >> I tried to serialize the AST as XML and I am getting something like:
>      >>
>      >> <antlr.CommonAST text="compilation-unit" type="4">
>      >>    <antlr.CommonAST text="package" type="15">
>      >>      ...
>      >>    </antlr.CommonAST>
>      >> </antlr.CommonAST>
>      >>
>      >> In order to apply an XSL transformation using matching
>     templates, I am
>      >> transforming the above into:
>      >>
>      >> <compilation-unit>
>      >>    <package>
>      >>      ...
>      >>    </package>
>      >> </compilation-unit>
>      >>
>      >> then I can bind XSL templates such as match="/compilation-unit",
>      >> match="package", etc.
>      >>
>      >> The problem is with AST nodes that have text content which cannot
>      >> function as XML tags - for example:
>      >>
>      >> <CommonAST text="123" type="14" />
>      >>
>      >> In this case <123 /> is an illegal XML tag.
>      >> type="14" means that there is some token assigned the identifier 14.
>      >>
>      >> If there would be some easy way to change the token type
>     identifiers to
>      >> strings or to add a string field associated with the token type
>     then it
>      >> would solve my problem really quick.
>      >>
>      >> Is there such a thing? A better approach?
>      >>
>      >> Adrian.
>      >>
>      >>
>      >
> 
> 



More information about the antlr-interest mailing list