Java / Abstract Syntax Tree into XML representatio

2019-02-19 12:47发布

问题:

do you know of any tool which creates an AST from a Java program or class and creates an XML representation (Collection or single XML document) from the AST?

kind regards,
Johannes

回答1:

Not any tools directly, but http://www.antlr.org/ is the defacto tool for building ASTs from any general language. And there exists several grammar files for Java that you can repurpose for your own programs. So grab ANTLR, use the latest Java grammer, and write out the XML representation you want.



回答2:

Our DMS Software Reengineering Toolkit with its Java Front End can do this directly. You ask DMS to parse the file, and produce an XML dump using a command line switch ++XML.

See What would an AST (abstract syntax tree) for an object-oriented programming language look like?.

As a general rule, we don't recommend this, for several reasons:

  • XML output for real files is really enormous, and takes a lot of time to write and read

  • Most people do this because they believe with an XML representation that just a little bit of XSLT will get them what they want

  • If you intend to modify the code, once you have the XML you pretty much can't regenerate it.

  • The machinery that DMS provides (attribute grammars, symbol tables, flow analyses, pattern matching and source-to-source transformations, source regeneration from the AST, is what you really want, and you get access to it by using DMS after the parsing step without exporting the XML ever