Questions tagged [abstract-syntax-tree]
Abstract syntax trees (ASTs) represent the recursive structure of a formal document (program source code).
2,738
questions
0
votes
0
answers
5
views
way to perform ast manipulation on webpack output code with a plugin (patch webpack_require)
Goal
I'm trying to personalize the webpack output to add some information when requiring a module like if the module is currently loading or already loaded (webpack_require).
Currently i'm patching ...
0
votes
0
answers
17
views
Eclipse annotate internal AST
I need to annotate/comment the AST from an eclipse plug-in and keep those annotations/metadata even if the code changes.
The background is:
I want to save the AST-based location of certain code ...
1
vote
0
answers
28
views
How to get the function result from ast.Call object?
I need to test the python dictionary in the python module without running the file. So I've decided to parse it with the ast.parse. I've almost figure out how to build the original dictionary except I ...
0
votes
0
answers
20
views
Can asynchronous module definitions be used with abstract syntax trees on v8 engine to read third party dependencies? [duplicate]
I understand eval string-to-function is impossible to use on the browsers' application programming interfaces.
I have been exploring the require.js repositories, and they either use eval or AST
...
0
votes
0
answers
24
views
how can I assert minified javascript is equivalent to source
I'm writing a webapp and I import a lot of minified JS libraries
Assuming they all minified using terser which is used by js-minify
How can I assert (or score) the source (which I have) is equal to ...
0
votes
0
answers
16
views
How to match the AST tree by the pattern I want?
I'm practice using clang-query to find the AST I want.
The code below:
while or for loop{
a = b + c;
d = e - f;
foo(a);
if (cond) {
foo(d);
}
}
However, I even cannot ...
0
votes
1
answer
50
views
Can asynchronous module definitions be used with abstract syntax trees on v8 engine to read third party dependencies? This is for Cloudflare Workers
I understand eval string-to-function is impossible to use on the browsers' application programming interfaces, but there must be another strategy to use third party dependencies without node.js on v8 ...
1
vote
1
answer
16
views
How to access FlowNode in typescript ast api?
I try to resolve foo and bar from nodes variable.
After I checked at ts-ast-viewer you can see in the picture that typescript can know about foo and bar from nodes node, under FlowNode section. (node -...
0
votes
0
answers
20
views
dom tree to abstract syntax tree in javascript [closed]
const domTree = ['div', { class: 'container' }, [
['div', { class: 'row' }, [
['div', { class: 'col' }, [
['img', { src: 'path/to/file.png', alt: 'picture' }],
['br'...
1
vote
1
answer
39
views
How to find ast nodes and its sub nodes in Python
I am trying to traverse through ast nodes in Python to fetch value node's Name ids for all Assign Nodes. Ast.walk() method gives me nodes randomly.
Assumption : Only for Assign nodes
for node in ast....
1
vote
1
answer
18
views
Static analyse and extract the difference between two versions of same Python Library
I'm doing a research project on detecting breaking changes from Python library upgrades. One of the steps is to extract the difference between two major versions of the same Python library by using ...
0
votes
1
answer
40
views
Where is the "invalid decimal literal" in my (valid) dict code string? [closed]
In python I have:
parser.add_argument('-d', help='provide data', type=str)
args = parser.parse_args()
if args.d:
try:
map_of_devices = ast.literal_eval(args.d)
except SyntaxError as e:...
-2
votes
5
answers
51
views
How to get list of strings from list-like string that includes nan?
Here is toy-example, I've string like this:
import numpy as np
z = str([np.nan, "ab", "abc"])
Printed it looks like "[nan, 'ab', 'abc']" but I've to process z = str([np....
0
votes
0
answers
12
views
Is there a way to easily compare 2 AST structures disregarding position?
Is there a way to easily compare 2 ast's on babel?
Consider a program like this as the source file:
// example.ts
import { foo } from "bar";
describe("Some test", () => {
let ...
-1
votes
4
answers
112
views
How to fetch line numbers for all 'if' 'else' and 'elif' positions in a python file
Example: Suppose, we have a .py file containing the below code snippet. How do we read and extract the positions of if-elif-else
If fisrtconditon:#line 1
If sub-condition:#line2
print(line ...
0
votes
0
answers
11
views
Custom Injections in Neovim Treesitter with Tagged Template Literals
I'm trying to write a custom injection for Neovim's Treesitter that will highlight tagged template literals as SQL code.
The code in question looks like this:
import sql from "postgres"
...
0
votes
0
answers
4
views
How to get the information of DeclRefExpr from the basic block in Clang:CFG?
How to get the information of DeclRefExpr from the basic block in Clang:CFG?
This is code i have coded
class CFGPrinter : public MatchFinder::MatchCallback {
public:
virtual void run(const ...
0
votes
0
answers
36
views
If Javascript code gets executed line by line then, how AST (Abstract syntax tree) works?
Javascript is an interpreted language which executes code line by line. On the other hand, AST (Abstract Syntax Tree) inside the Javascript engine, created the data structure of the whole code and ...
2
votes
1
answer
21
views
Adding visit function to AST visitor prevents other methods from firing
Using the ast module, I run into a weird issue:
Code
import ast
from _ast import Try
code = """
def my_add(a, b):
try:
return a + b
except TypeError:
return a + ...
1
vote
1
answer
46
views
How can I retrieve function names and attributes from Python code with AST?
Given a piece of code like this:
aaa = bbb()
ccc.ddd().eee(fff(foo))
ggg(bar)
hhh().iii
I would like to get something like this:
['bbb', 'ccc.ddd.eee', 'fff', 'ggg', 'hhh.iii']
I'm aware of the ...
0
votes
1
answer
14
views
Get AST nodes typescript name from SpreadOperator RuleListener Identifier node argument
I would like to get the node.argument properties Typescript types name, and check if it's Foo.
Let's say the Foo type is defined like this:
type Foo = {};
This is my eslint rule definition so far.
...
0
votes
0
answers
32
views
Typescript AST: how to distinguish `extends` and `in` clauses in a TypeParameterDeclaration node
I am parsing a typescript source file using a library that provides the typescript Abstract Syntax Tree as processed by the typescript compiler.
In order to verify the correctness I am rebuilding the ...
0
votes
0
answers
26
views
AST - parsing code of R programming language
Is there any good parser for R programming Language from which the abstract syntax tree can be derived? I noticed that there is the lobstr library from which the ast() function accepts expression as ...
0
votes
1
answer
25
views
Why use ast syntax tree modification instead of regex replacement?
javascript can use ast to convert js code into syntax tree, then change the data according to the syntax tree, and then use tools to convert ast syntax tree into js file, and finally write to the file....
1
vote
1
answer
47
views
How do you robustly generate code, given a description of its exact behavior in another language?
I have been reverse engineering a specific black box equation that is part of a system I do not own (do not worry, it's white-hat), in which you can only measure the inputs (a large set of integers) ...
0
votes
0
answers
9
views
Prevent screen.findByX without an await with eslint no-restricted-syntax?
This medium article shows me how to prevent an await preceding a property:
"no-restricted-syntax": [
"error",
{
"message": "promise.then is unnecessary when ...
1
vote
1
answer
46
views
I am not able to get dictionary keys
**I made an api request in which the content iconverted into dictionary but after nested key item all the other content were in string which I wanted to in dictionary to make it easy to find mint ...
3
votes
1
answer
61
views
Use go ast to get surrounding function name from position
What's the best way to get the surrounding function name from a file position via the source code alone (no runtime checks)?
For example say I have some code:
func MyFunc() {
doSomething() // ...
2
votes
1
answer
85
views
Why is this Groovy AST transformation within a Grails app only being applied during class reloading?
In an attempt to develop a global AST transformation to add a new method to an Enum class within my app, I've set up this example project using Grails 5.
https://github.com/davebrown1975/...
0
votes
0
answers
61
views
Generating AST for BigQuery using ZetaSQL
I am new to ZetaSQL and trying to generate an AST for BigQuery (sample query eg: select cols from dataset.table where condition;) using ZetaSQL.
I have added below dependancy in my pom.xml
<...
0
votes
0
answers
23
views
typescript API factory.updateSourceFile, how to set the "pos" and "end" members of new statements (and their sub-nodes)?
I'm updating a sourceFile in a transform -
export function transformBefore(
absTsconfigPath: string
): ts.TransformerFactory<ts.SourceFile> | ts.CustomTransformerFactory {
return (ctx) => ...
0
votes
1
answer
45
views
Replacing print loops with list appends in python
I want to algorithmically change loops with print statements to list appends so that I can return them. There is no limit to the number of nested loops nor if there are if statements. For example, I ...
0
votes
0
answers
18
views
Clair does not parse header files correctly for pointers in arguments
I am parsing a header file using Rascal ClaiR:
iFile = toLocation("file://D:/test.h");
ast = parseCpp(iFile);
iprint(ast);
The header file test.h looks as follows:
#ifndef TEST_H
#define ...
0
votes
1
answer
24
views
Find which line a variable was defined on after importing file
I can use the ast module in Python to look for ast.Assign nodes or FunctionDef nodes and look for the name of the variable being assigned to. But my issue is code like this
try:
from ...
0
votes
1
answer
61
views
How many BNF-like grammar specifications does Python have?
There are at least 3 grammar-like specifications within the Python 3.9.1 reference documents 'library-3.9.1.pdf' and 'reference-3.9.1.pdf' (aka https://docs.python.org/3.9/library/ast.html and https://...
-2
votes
1
answer
46
views
How to get an abstract syntax tree in python?
I have a grammar defined. For sake of example expr = a | expr *expr.
I want to get an ast.
Is it possible to do this in python?
I have looked at ast module in python, but I believe it works only for ...
3
votes
2
answers
138
views
ANTLR4: Re-visiting parse rules after the whole ast is visited
I am currently implementing generic functions for my own language, but I got stuck and currently have the following problem:
Generic functions can get called from another source file (another parser ...
0
votes
1
answer
55
views
Python string eval failing on empty items in list [closed]
import ast
s = "[0,1,,,2]"
d = ast.literal_eval(s)
I have a string with blank items in a list that I need convert to a list. Is there any library that can handle lists like this? The ...
-2
votes
1
answer
68
views
How are these parse tree generated?
I need assistance regarding generating the parse tree and the syntax tree. My version of the solutions are the images below but I don't know if they are correct or way off.
Given the Grammar: S-->...
1
vote
1
answer
27
views
How can I match universal reference arguments to a member function using Clang AST matchers?
I'm trying to match arguments passed to a templated member function invocation using clang-query as a precursor to writing a clang-tidy check. Whilst I can get non-templated member functions to match, ...
1
vote
0
answers
30
views
Getting AST of some C code programmatically
What I'm doing?
I'm building a C code analyzer. to do some checks at the compile time. and if passed, compile it.
What do I want?
for my goal, I should lex and parse user source code. maybe in the ...
0
votes
0
answers
27
views
How to parse multiple files using clang python bindings?
Python script:
def GetTranslationUnit(source_file_path):
compilation_database_path = r'G:/UnrealEngine425'
index = cindex.Index.create(1)
# Step 1: load the compilation database
compdb ...
1
vote
0
answers
31
views
data extraction from AST python file
code = """selection = ExtraTreesRegressor()
selection.fit(X,y)"""
ext = ast.parse(code)
dump = ast.dump(ext)
After running the above lines of code ...
1
vote
1
answer
46
views
How to use subItem level using Expression?
I have the next models:
public class MaterialCatalog {
public int MaterialCatalogID { get; set; }
public int AccountID { get; set; }
public string MaterialName { get; set; }
public ICollection&...
0
votes
0
answers
26
views
Eslint custom rule - Is it possible to find a variable value defined in ember component?
For example, I want to restrict a specific class name usage across my product.
So, I had blocked that class name usage from .hbs files as template lint rules.
But, that classnames are being used as ...
0
votes
0
answers
25
views
AST: check if two nodes are connected
I am parsing some code as below. I get a list of ast.node. Now, I want to know which nodes are linked to which node. Is it possible to do that?
Example in below code: 1st assign is not connected (f = ...
0
votes
1
answer
28
views
Data extraction from a python syntax using AST(Abstract Syntax Tree) module
import sklearn as sk
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.model_selection import train_test_split
selection = ExtraTreesRegressor()
selection.fit(X,y)
X_train, X_test, y_train,...
0
votes
1
answer
30
views
Evaluating prefix expressions
I have this long code for evaluating prefix evaluation. They give me a grammar for the prefix expressions which as the following EXPR := OP EXPR EXPR | CONSTANT, where OP is one of +, -, *, /, %.
All ...
0
votes
1
answer
43
views
Clang AST: Evaluate the expression
Suppose we have a c++ code as below
int main()
{
int val = 4 + 5;
}
For above code, corresponding AST will look like as below with the command clang++ -cc1 -ast-dump ConsoleApplication1.cpp
...
0
votes
0
answers
33
views
Delete subtree in AST after testing that it exists in ANTLRv4
I have an AST from an ANTLR grammar "Mygrammar" and i want to delete the node (with its children) corresponding to the rule "myrule" after testing that "myrule" exists in ...