abstract-syntax-tree

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

时光总嘲笑我的痴心妄想 提交于 2019-12-31 22:21:11
问题 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;

changing ** operator to power function using parsing?

北战南征 提交于 2019-12-31 03:23:28
问题 My requirement is to change ** operator to power function For example 1.Input -"B**2" Output - power(B,2) 2."B**2&&T**2*X" Output - power(B,2) I have wrote following regular expression to address that problem rx=r"([a-zA-Z0-9]+)\*\*([a-zA-Z0-9()]+)" result = regex.sub(rx, r"power(\1,\2)", expression, 0, regex.IGNORECASE | regex.MULTILINE) But above code successfully converting expression similar to the example 1 and example 2, but failed to convert expression like (a+1)**2 or ((a+b)*c)**2 . I

ast.literal_eval for variables in python?

倖福魔咒の 提交于 2019-12-30 07:16:06
问题 Suppose I have a file example.py : import example VVV = 2 DictionaryNameB = { 'a' : VVV, 'bb' : 'SomethingB', 'c' : False, 'ccc' : None, 'dddd' : 'true', 'eeeee' : 0.123456, 'f' : 2, 'h' : [1,2,3] } I wrote a function that uses ast.literal_eval() : def getDicFromFile(self, dic_name): with open( 'example.py' ) as f: file_data = f.read() match = re.findall('%s[^{]+\{[^\}]+\}' % dic_name, file_data, re.MULTILINE)[0] # print(match) dicObject = ast.literal_eval(match[len(dic_name)+3:]) return

Easy way to convert python source code to an AST with comments intact

爷,独闯天下 提交于 2019-12-30 06:18:09
问题 I have done fair bit of searching around how to capture python ASTs with comments preserved. The suggested way includes using ast and tokenize libraries to get the job done. I have had fair bit of success in utilizing these libraries as per my requirement but I feel there has to be a better way. This thought stems from the fact that lib2to3 converts python2 code to python3 code with comments preserved. Also the process is stated to be Source-Code-in-Python2 -> AST -> Source-Code-in-Python3

Typed abstract syntax tree with function application

烂漫一生 提交于 2019-12-30 04:56:09
问题 I am trying to write a typed abstract syntax tree datatype that can represent function application. So far I have type Expr<'a> = | Constant of 'a | Application of Expr<'b -> 'a> * Expr<'b> // error: The type parameter 'b' is not defined I don't think there is a way in F# to write something like 'for all b' on that last line - am I approaching this problem wrongly? 回答1: In general, the F# type system is not expressive enough to (directly) define a typed abstract syntax tree as the one in your

Tracing Python expression evaluation step by step

五迷三道 提交于 2019-12-28 15:09:10
问题 I'm trying to write Python expression evaluation visualizer, that will show how Python expressions are evaluated step by step (for education purposes). Philip Guo's Python Tutor is great, but it evaluates Python program line by line, and I found that students sometimes do not understand how one-line expressions like sorted([4, 2, 3, 1] + [5, 6])[1] == 2 are evaluated and I'd like to visualize this process. (It seems that nobody did it yet — at least I found nothing.) The ideal solution will

Java Tree parser output for ANTLR

。_饼干妹妹 提交于 2019-12-28 12:43:19
问题 I've found a sample template in the ANTLR website, its the Javatreeparser.g, which the site says could produce the AST that I need, but since I'm new to ANTLR, how do I make it show? What I've done so far is placing the grammar file together with my existing java grammar. But I have no idea on how to use and output the AST that I need from the file. How do I do it? 回答1: I've found a sample template in the ANTLR website, its the Javatreeparser.g, which the site says could produce the AST that

Parsing an equation with sub-formulas in python

霸气de小男生 提交于 2019-12-25 10:22:40
问题 I'm trying to develop an equation parser using a compiler approach in Python. The main issue that I encounter is that it is more likely that I don't have all the variables and need therefore to look for sub-formulas. Let's show an example that is worth a thousand words ;) I have four variables whom I know the values: vx, vy, vz and c: list_know_var = ['vx', 'vy', 'vz', 'c'] and I want to compute the Mach number (M) defined as equation = 'M = V / c' I already know the c variable but I don't

Parsing an equation with sub-formulas in python

亡梦爱人 提交于 2019-12-25 10:21:33
问题 I'm trying to develop an equation parser using a compiler approach in Python. The main issue that I encounter is that it is more likely that I don't have all the variables and need therefore to look for sub-formulas. Let's show an example that is worth a thousand words ;) I have four variables whom I know the values: vx, vy, vz and c: list_know_var = ['vx', 'vy', 'vz', 'c'] and I want to compute the Mach number (M) defined as equation = 'M = V / c' I already know the c variable but I don't

Serialize arithmetic expression-tree

泪湿孤枕 提交于 2019-12-25 08:17:06
问题 I'm doing a simple task for investigation purposes. The problem is as follows: Create an arithmetic expression with variables. Build an AST for the expression. Send it to the server (using Sockets) Calculate an result of the server side and return the results. Now I can to build a tree. This method doing it: private readonly Stack<Expression> expressionStack = new Stack<Expression>(); private readonly Stack<Symbol> operatorStack = new Stack<Symbol>(); private readonly List<string> parameters