abstract-syntax-tree

AST from c code with preprocessor directives

喜夏-厌秋 提交于 2019-12-05 15:24:44
How can I build an AST (Abstract Syntax Tree) from gcc C code in order to make some transformations, like below, and reproduce(generate) the code to C syntax again after that? if(condition_1){ //lines of code 1 } #ifdef expression_1 else if(condition_2){ //lines of code 2 } #endif into bool test = condition_1; if(teste){ //lines of code 1 } #ifdef expression_1 if(!(test) && condition_2){ //lines of code 2 } #endif GCC itself will build ASTs, but not before expanding the preprocessor directives. So the preprocessor conditionals are gone. Reinstalling them after you have done the transformations

Eclipse Abstract Syntax Tree Diff

痴心易碎 提交于 2019-12-05 12:59:13
Given the following code in Eclipse: import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.CompilationUnit; public class Question { public static void main(String[] args) { String source = "class Bob {}"; ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(source.toCharArray()); CompilationUnit result = (CompilationUnit) parser.createAST(null); String source2 = "class Bob {public void MyMethod(){}}"; ASTParser parser2 = ASTParser.newParser(AST.JLS3); parser2.setSource(source2.toCharArray()); CompilationUnit result2 =

Method name from line number

送分小仙女□ 提交于 2019-12-05 12:39:00
Given a line number of a particular class source code (Java/C#) - is there an easy way to get the name of the method it falls within? (If it falls within one) (Presumably using an Abstract Syntax Tree) (This would be useful in limiting the output of checkstyle to just the method touched). I'm assuming you'd have to use an Abstract Syntax Tree to do Line#->MethodName. (Java-specific) If the class file was compiled with debug info then the line number table will contain a mapping of code<->line number. I don't think there's a built-in API for getting at this at runtime though I'm sure you can

How to merge two ASTs?

一曲冷凌霜 提交于 2019-12-05 11:19:34
I'm trying to implement a tool for merging different versions of some source code. Given two versions of the same source code, the idea would be to parse them, generate the respective Abstract Source Trees (AST), and finally merge them into a single output source keeping grammatical consistency - the lexer and parser are those of question ANTLR: How to skip multiline comments . I know there is class ParserRuleReturnScope that helps... but getStop() and getStart() always return null :-( Here is a snippet that illustrates how I modified my perser to get rules printed: parser grammar

Insert a node into an abstract syntax tree

笑着哭i 提交于 2019-12-05 10:30:13
The ast module's documentation explains how to replace a node in the AST using the NodeTransformer class, but does not explain how to insert a new node into the tree. For example, given this module: import foo import bar class Baz(object): def spam(self): pass I would like to add another import, and set a class variable on Baz . How can I create and insert these nodes into the AST? Python ASTs are essentially composed of nested lists, so new nodes can be inserted into these lists once they have been constructed. First, get the AST that is to be changed: >>> root = ast.parse(open('test.py')

Java static metaprogramming

↘锁芯ラ 提交于 2019-12-05 09:40:13
I'd like to implement annotation processor that will generate new class based on existing "prototype" class. import java.util.List @MyAnnotation class MySuperClassPrototype { static MySuperClassPrototype createInstance() { return new MySuperClassPrototype(); } } As a result of code below. The following new source file (compilation unit) will be generated: import java.util.List class MySuperClass { static MySuperClass createInstance() { return new MySuperClass(); } public void specialAddedMethod() { /*...*/ } } I'd like to copy all top-level import statements and static members and not static

When to use an abstract or concrete syntax tree?

会有一股神秘感。 提交于 2019-12-05 09:39:27
I've been doing research on compilers. The lexer seems to be very straight forward: Take a "sentence" and break it up into words (or tokens). To ensure correct grammar a parser is needed. The parser generally takes the tokens and builds a tree that results in a root node (words into sentences, paragraphs, pages, etc...). From this question it would seem a parser would build an AST. The AST only contains what is necessary to execute the code, so things like parentheses would be unnecessary since operator precedence is built into an AST. An AST is probably all a compiler needs. But what about

How to create AST parser which allows syntax errors?

岁酱吖の 提交于 2019-12-05 07:18:30
First, what to read about parsing and building AST? How to create parser for a language (like SQL) that will build an AST and allow syntax errors? For example, for "3+4*5": + / \ 3 * / \ 4 5 And for "3+4*+" with syntax error, parser would guess that the user meant: + / \ 3 * / \ 4 + / \ ? ? Where to start? SQL: SELECT_________________ / \ \ . FROM JOIN / \ | / \ a city_name people address ON | =______________ / \ .____ . / \ / \ p address_id a id Ira Baxter The standard answer to the question of how to build parsers (that build ASTs), is to read the standard texts on compiling. Aho and Ullman

How do I generate an AST from a string of C++ using Clang?

心已入冬 提交于 2019-12-05 01:40:51
I am trying to use Clang to manipulate C++ source-code, but I am having trouble discovering the API. I would like to take a string of C++ source-code and generate an AST from it; something like: auto myAst = clang::parse("auto x = 1 + 1;"); Is there a minimal working example of this? You can try the next code: std::unique_ptr<ASTUnit> AST(tooling::buildASTFromCode("auto x = 1 + 1;")); TranslationUnitDecl *DC = AST->getASTContext().getTranslationUnitDecl(); if (DC) { llvm::errs() << "---------dump begin----------\n"; DC->dump(); llvm::errs() << "---------dump end----------\n"; } 来源: https:/

How do I get clang to dump the AST without color?

狂风中的少年 提交于 2019-12-05 01:01:58
Using clang-check to dump a source code's AST, can be done with the following command: $ clang-check -ast-dump file.c -- However, the output of this command will appear colorful within the terminal. When I direct the output to a file, I'm stuck with all of the color escape codes: $ clang-check -ast-dump file.c -- > out.txt example: [0;1;32mTranslationUnitDecl[0m[0;33m 0x227c5c0[0m <[0;33m<invalid sloc>[0m> [0;33m<invalid sloc>[0m [0;34m|-[0m[0;1;32mTypedefDecl[0m[0;33m 0x227cac0[0m <[0;33m<invalid sloc>[0m> [0;33m<invalid sloc>[0m implicit[0;1;36m __int128_t[0m [0;32m'__int128'[0m [0;34m|-[0m