Home > Net >  How can I parse the code that is conforming to a grammar expressed using ANTLR4 and then generate XM
How can I parse the code that is conforming to a grammar expressed using ANTLR4 and then generate XM

Time:05-10

I have specified a grammar using ANTLR4 using VScode and the extension by Mike Lischke. I am wondering if there is a way to parse the code of the program that is conforming to the grammar and generate eventually some XML tags.

Xtext provides this solution by generating a .xtend file that contains the famous doGenerate method, in which we access to objects and then generate a new code.

CodePudding user response:

There’s not a “write this parse tree out as XML” functionality built into ANTLR.

It would not be too hard to write a listener that produced XML while traversing the parse tree. You’d have to make decisions about which property to include in you XML, as well as which to make attributes.

Probably, most people wanting to serialize to XML create an AST from the parse tree (parse trees can be rather verbose depending upon the grammar). With an AST you could even annotate it, and use a library to serialize the AST as XML (using something like JaxB, for instance)

  • Related