abstract-syntax-tree

Representing an Abstract Syntax Tree in C

情到浓时终转凉″ 提交于 2019-12-03 05:35:46
问题 I'm implementing a compiler for a simple toy language in C. I have a working scanner and parser, and a reasonable background on the conceptual function/construction of an AST. My question is related to the specific way to represent an AST in C. I've come across three styles pretty frequently in different texts/resources online: One struct per type of node. This has a base node "class"(struct) that is the first field in all the child structs. The base node contains an enum that stores the type

Generate .pyc from Python AST?

孤街醉人 提交于 2019-12-03 05:34:53
How would I generate a .pyc file from a Python AST such that I could import the file from Python? I've used compile to create a code object, then written the co_code attribute to a file, but when I try to import the file from Python, I get an ImportError: Bad magic number in output.pyc . exupero The solution can be adapted from the py_compile module: import marshal import py_compile import time import ast codeobject = compile(ast.parse('print "Hello World"'), '<string>', 'exec') with open('output.pyc', 'wb') as fc: fc.write('\0\0\0\0') py_compile.wr_long(fc, long(time.time())) marshal.dump

How to convert source code to a xml based representation of the ast?

陌路散爱 提交于 2019-12-03 03:33:05
i wanna get a xml representation of the ast of java and c code. 3 months ago, i asked this question yet but the solutions weren't comfortable for me srcml seems to be a good solution for this problem but it does not support line numbers and columns but i need that feature. about elsa : cite: "There is ongoing effort to export the Elsa AST as an XML document; we expect to be able to advertise this in the next public release." dms ... didn't understand that. especially for java, there is javaml which supports line numbers. but the sourceforge page doesn't list any files. question: there's

Building own C# compiler using ANTLR: Compilation Unit

佐手、 提交于 2019-12-03 02:59:54
// Create a scanner that reads from the input stream passed to us CSLexer lexer = new CSLexer(new ANTLRFileStream(f)); tokens.TokenSource = lexer; // Create a parser that reads from the scanner CSParser parser = new CSParser(tokens); // start parsing at the compilationUnit rule CSParser.compilation_unit_return x = parser.compilation_unit(); object ast = x.Tree; What can I do with the x which is of compilation_unit_return type, to extract its root, its classes, its methods etc? Do I have to extract its Adaptor out? How do I do that? Note that the compilation_unit_return is defined as such in my

Clang : What does AST (abstract syntax tree) look like?

自古美人都是妖i 提交于 2019-12-03 02:51:14
Hi I am new in Compiler development, and am wondering how AST look like. I have a small section of code, and I use Clang for generating the AST. I don't get much information out of it. From the looks of it, the Syntax tree is exactly the same as the source, except for one struct that is added to almost any sample I test with. Source: class A { public: int *a, *b, *c; int i; void sum() { a = new int[5]; b = new int[5]; c = new int[5]; for (i = 0; i < 5; i++) { a[i] = i; b[i] = i; } for (i = 0; i < 5; i++) { c[i] = a[i] + b[i]; } delete[] a; delete[] b; delete[] c; } }; class B : public A { };

how to view clang AST?

元气小坏坏 提交于 2019-12-03 01:10:25
问题 I am trying to get hold on Clang. So, I would like to view the AST generated by Clang after parsing the given program. Is it possible to dump AST in .dot or .viz format? Is there any tool out there? 回答1: Clang supports showing the AST with Graphviz's dotty -- you can grab the temporary .dot file generated (name is printed out) to get the graph source. clang -cc1 -ast-view your_file.c You can also print to the command line with: clang -cc1 -ast-dump your_file.c or: clang -cc1 -ast-print your

Access the Abstract Syntax Tree of V8 Engine

自闭症网瘾萝莉.ら 提交于 2019-12-03 00:21:22
Is it possible to access the AST of the v8 engine, for a given JavaScript code? I'm working on a JavaScript Static Analyzer using V8 engine. This is pretty old but maybe the answer helps someone stumbling upon this. The answer is yes, assuming you are willing to modify V8 and compile your own version of it. If so, then in compiler.cc you find a spot where MakeCode is called throughout MakeFunctionInfo which transforms the AST that is stored in the passed in CompilationInfo object into native code. You need to write a class that inherits from AstVisitor then you can inspect the AST by inserting

A parser for regular expressions in PHP?

可紊 提交于 2019-12-02 23:56:19
I need to parse regular expressions into their components in PHP. I have no problem creating the regular expressions or executing them, but I want to display information about the regular expression (e.g. list the capture groups, attach repetition characters to their targets, ...). The overall project is a plugin for WordPress that gives info about the rewrite rules, which are regexes with substitution patterns, and can be cryptic to understand. I have written a simple implementation myself, which seems to handle the simple regexes I throw at it and convert them to syntax trees. Before I

Pretty Printing AST with Minimal Parentheses

梦想的初衷 提交于 2019-12-02 23:49:32
I'm implementing a pretty-printer for a JavaScript AST and I wanted to ask if someone is aware of a "proper" algorithm to automatically parenthesize expressions with minimal parentheses based on operator precedence and associativity . I haven't found any useful material on the google. What seems obvious is that an operator whose parent has a higher precedence should be parenthesized, e.g.: (x + y) * z // x + y has lower precedence However, there are also some operators which are not associative, in which case parentheses are still are needed, e.g.: x - (y - z) // both operators have the same

Using the Eclipse AST

▼魔方 西西 提交于 2019-12-02 22:54:24
I have recently come into the need of modifying some Java code (adding methods, changing the signatures of some fields and removing methods) and I think that all of this can be accomplished though the use of the Eclipse SDK's AST. I know from some research how to parse in a source file but I don't know how to do the things mentioned above. Does anyone know a good tutorial or could someone give me a brief explanation on how to resolve these issues? Thanks a lot, ExtremeCoder Edit: I have now started to look more into the JCodeModel and I think this could be much easier to use but I do not know