abstract-syntax-tree

Python: error parsing strings from text file with Ast module

ぐ巨炮叔叔 提交于 2019-12-02 21:28:10
问题 I have a text file (.txt) with the following two strings as so (this is a snippet of a larger file where all strings have the same format): ["('a', '1')", "('b', '2')"] ["('c', '3')", "('d', '4')"] I am using the below code to parse the strings in the text file so that I can access each element for data analysis. The code works for the string if I declare it as a variable, such as: string = ["('c', '3')", "('d', '4')"] But the code does not work for the text file: import ast text_file = open(

Python ast to dot graph

随声附和 提交于 2019-12-02 21:10:52
I'm analyzing the AST generated by python code for "fun and profit", and I would like to have something more graphical than "ast.dump" to actually see the AST generated. In theory is already a tree, so it shouldn't be too hard to create a graph, but I don't understand how I could do it. ast.walk seems to walk with a BFS strategy, and the visitX methods I can't really see the parent or I don't seem to find a way to create a graph... It seems like the only way is to write my own DFS walk function, is does it make sense? If you look at ast.NodeVisitor, it's a fairly trivial class. You can either

Generating an Abstract Syntax Tree for java source code using ANTLR

痴心易碎 提交于 2019-12-02 21:09:41
How Can I Generate an AST from java src code Using ANTLR? any help? The setps to generate java src AST using antlr4 are: Install antlr4 you can use this link to do that. After installation download the JAVA grammar from here . Now generate Java8Lexer and Java8Parser using the command: antlr4 -visitor Java8.g4 This will generate several files such as Java8BaseListener.java Java8BaseVisitor.java Java8Lexer.java Java8Lexer.tokens Java8Listener.java Java8Parser.java Java8.tokens Java8Visitor.java Use this code to generate AST: import java.io.File; import java.io.IOException; import java.nio

How can we get the Syntax Tree of TypeScript?

◇◆丶佛笑我妖孽 提交于 2019-12-02 20:33:14
Is there a process on getting a syntax tree of a compiler. We had been assigned on a project that needs to access typescript's syntax tree (which is opensource so we could see the whole compiler's code). But we don't know how to get it. I've been reading some articles in the Internet but I can't really find a user-friendly article or which is written in lehman's term. I believe some mentioned that the first step we need to do is to find the parsing step. But after that we had no idea what to do next. Sorry for the noob question. :) The TypeScript compiler API is really quite easy to use. To

Representing an Abstract Syntax Tree in C

三世轮回 提交于 2019-12-02 20:07:50
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 of node(constant, binary operator, assignment, etc). Members of the struct are accessed using a set of

JavaCC dump method to print AST

倾然丶 夕夏残阳落幕 提交于 2019-12-02 18:52:28
问题 I am using JavaCC to print an AST in a particular format. I need it to be like this : LetNode( Identier(X), ExprNode( PlusNode( IntegerLiteral(8), IntegerLiteral(2) ))) but I am getting: Start(LetNode(Identifier(x)(ExprNode(IntegerLiteral(5)(PlusNode(IntegerLiteral(5)())) I am using the dump method to print this: public void dump(String prefix) { System.out.print(toString(prefix)); System.out.print("("); if (children != null) { for (int i = 0; i < children.length; ++i) { SimpleNode n =

Developing Abstract Syntax Tree

一笑奈何 提交于 2019-12-02 17:19:55
I've scoured the internet looking for some newbie information on developing a C# Abstract Syntax Trees but I can only find information for people already 'in-the-know'. I am a line-of-business application developer so topics like these are a bit over my head, but this is for my own education so I'm willing to spend the time and learn whatever concepts are necessary. Generally, I'd like to learn about the techniques behind developing an abstract representation of code from a code string. More specifically, I'd like to be able to use this AST to do C# syntax highlighting. (I realize that syntax

Adding line information to my AST in OCaml

不问归期 提交于 2019-12-02 08:14:55
问题 I am creating a compiler in OCaml where the grammar is as follow: type expr = | Cons of const | Var of string | List of ( expr list ) | Sum of ( expr * expr ) | Less_than of ( expr * expr ) | Conditional of ( expr * expr * expr ) | Array_literal of ( expr ) | Array_read of ( expr * expr ) The node of an AST looks like this: type 'a astNode = { data: 'a; metadata: Metadata; } and the Metadata module looks like this: module Metadata = struct type loc = Lexing.position type loc_range = loc * loc

Eclipse AST not changing files which are not opened in eclipse

别来无恙 提交于 2019-12-02 08:06:12
问题 I am trying to modify source code using eclipse plugin, JDT and AST (Abstract Syntax Tree). I can read all Java files and make operation on all those file, But when i am saving those changes (Edits) in to files using TextEdit edits = rewriter.rewriteAST(); // apply the text edits to the compilation unit edits.apply(document); iCompilationUnit.getBuffer().setContents(document.get()); It only make changes in file those are open in eclipse in unsaved mode. Rest of files are not affected. Find my

How to convert AST to JDT Java model

天大地大妈咪最大 提交于 2019-12-02 05:20:12
问题 I am writing unit tests for my plugin that makes use of IType and IMethod interfaces from JDT. To write unit tests I would need to instantiate such interfaces. Answer to this question shows how to create AST model, but I don't know how to convert it into Java model? My code looks like this: String source = "package com.test\n" + "\n" + "import com.test.something;" + "\n" + "public class Class{\n" + "int sum(int a, int b)\n" + "}\n"; ASTParser parser = ASTParser.newParser(AST.JLS4); parser