Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?)

like image 850
Erika Redmark Avatar asked Jan 07 '13 14:01

Erika Redmark


People also ask

How do you syntax an abstract tree?

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.

Is Abstract Syntax Tree and syntax tree same?

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.

How would you construct an Abstract Syntax Tree for arithmetic expressions?

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.

What is Abstract Syntax Tree in compiler design?

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.


2 Answers

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.

like image 72
Aykut Kllic Avatar answered Oct 07 '22 03:10

Aykut Kllic


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:

  1. Let the user preview the changes before actually committing them to the source code.
  2. Operate on unsaved file bug (e.g. restructure the code in the unsaved source editor)
  3. Honour the user code formatting settings in a newly generated code.
  4. 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.

like image 42
Eugene Avatar answered Oct 07 '22 04:10

Eugene