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?)
Abstract Syntax Tree is a kind of tree representation of the abstract syntactic structure of source code written in a programming language. Each node of the tree denotes a construct occurring in the source code.
In computer science, an abstract syntax tree (AST), or just syntax tree, is a tree representation of the abstract syntactic structure of text (often source code) written in a formal language. Each node of the tree denotes a construct occurring in the text.
Typically, you would split the work into a tokenizer which splits the input stream representing the expression into a list of tokens, and a parser which takes the list of tokens and constructs a parse tree\ast from it. The first column is the actual text value. The second represents the token type.
An Abstract Syntax Tree, or AST, is a tree representation of the source code of a computer program that conveys the structure of the source code. Each node in the tree represents a construct occurring in the source code.
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.
I would recommend you to start with exploring CRefactoring and its subclasses (e.g. ExtractFunctionRefactoring).
There are many problems that CDT refactoring framework tries to address:
I am pretty sure that even if you don't need all those features, these two classes should be a good starting point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With