Using CDT's Abstract Syntax Tree API to generate/write C code?

I have been able to use CDT's AST API for parsing source code successfully. My question involves the reverse: How can I build an C AST programmatically, and then tell it to write itself to a file? I have seen the class ASTWriter (but that is internal) and can't find any tutorials or documentation on building the actual AST.

I have found a paper that goes over the idea of what I want to do: Generating Rewritable Abstract Syntax Trees which makes it seem like generating code would be easy if I could construct the tree and say 'write yourself'. Is this possible in CDT and how might I get started (preferably without deprecated/internal methods?)


I would recommend you to start with exploring CRefactoring and its subclasses (eg ExtractFunctionRefactoring).

There are many problems that CDT refactoring framework tries to address:

  • Let the user preview the changes before actually committing them to the source code.
  • Operate on unsaved file bug (eg restructure the code in the unsaved source editor)
  • Honour the user code formatting settings in a newly generated code.
  • Undoable transactions spanning several source files.
  • I am pretty sure that even if you don't need all those features, these two classes should be a good starting point.


    What you need is using an ASTWriter:

    ASTWriter writer = new ASTWriter()
    String code = writer.write(myAST);
    

    Then you can dump the string to a file which is in the context of eclipse resources plugin.

    链接地址: http://www.djcxy.com/p/11692.html

    上一篇: 验证语言环境中的十进制数字

    下一篇: 使用CDT的抽象语法树API来生成/编写C代码?