abstract-syntax-tree

Method name from line number

五迷三道 提交于 2019-12-07 10:46:45
问题 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. 回答1: (Java-specific) If the class file was compiled with debug info then the line number table will contain a mapping of code<-

Eclipse Abstract Syntax Tree Diff

萝らか妹 提交于 2019-12-07 10:23:31
问题 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 =

Getting the AST of a function

℡╲_俬逩灬. 提交于 2019-12-07 08:44:41
问题 Say I have a function: function hi() print('hi') end I'd like to be able to take this function as an argument and perform analysis on it, such as listing all the function calls in it, seeing all the variables it references, etc. Note that I'm not talking about analysing source code ; I don't have the source text of the function, I just have the function object, i.e. this is being done at runtime. The way I think you'd go about doing this is to get the AST of the function. But I don't know how

eclipse ASTNode to source code line number

帅比萌擦擦* 提交于 2019-12-07 08:23:42
问题 Given an ASTNode in eclipse, is there any way to get the corresponding source code line number? 回答1: You can get the line number of an ASTNode using the below code int lineNumber = compilationUnit.getLineNumber(node.getStartPosition()) - 1; the compilation unit can be obtained from the ASTParser using the below code ASTParser parser = ASTParser.newParser(AST.JLS3); // Parse the class as a compilation unit. parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(source); // give your

AST from c code with preprocessor directives

孤人 提交于 2019-12-07 07:03:25
问题 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 回答1: GCC itself will build ASTs, but not before expanding the preprocessor

Java static metaprogramming

我的未来我决定 提交于 2019-12-07 06:27:01
问题 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

Insert a node into an abstract syntax tree

倖福魔咒の 提交于 2019-12-07 04:41:00
问题 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? 回答1: Python ASTs are essentially composed of nested lists, so new nodes can be inserted into these lists once

When to use an abstract or concrete syntax tree?

馋奶兔 提交于 2019-12-07 04:11:47
问题 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

How to create AST parser which allows syntax errors?

不想你离开。 提交于 2019-12-07 03:18:15
问题 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 回答1: The standard answer to the question of

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

那年仲夏 提交于 2019-12-06 19:49:17
问题 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