标签: abstract-syntax-tree

如何推翻受歧视的工会

我正在尝试对受歧视的联合实施折叠。DU称为Expr,表示程序表达式,并且通常是递归的。我正在尝试编写一个折叠,以递归方式累积 Exprs 上的操作结果。下面是我尝试写的折叠。

let rec foldProceduralExpr (folder : 's -> Expr list -> 's) (state : 's) (expr : Expr) : 's =
    let children =
        match expr with
        | Series s -> s.SerExprs
        | Lambda l -> [l.LamBody; l.LamPre; l.LamPost]
        | Attempt a -> a.AttemptBody :: List.map (fun ab -> ab.ABBody) a.AttemptBranches
        | Let l -> l.LetBody :: List.concat (List.map (fun lb -> match lb with LetVariable (_, expr) -> [expr] | LetFunction (_, _, body, _, pre, post, …
Run Code Online (Sandbox Code Playgroud)

f# abstract-syntax-tree fold discriminated-union

3
推荐指数
1
解决办法
581
查看次数

ESLint“代码”与“空白”值可修复

规则定义为 fixable 时fixable应指定可以具有codewhitespace值的键:

fixable(string) 如果--fix命令行上的选项自动修复规则报告的问题,则为“代码”或“空格”

这些值之间有什么区别whitespace,具体是什么?

javascript testing abstract-syntax-tree static-code-analysis eslint

3
推荐指数
1
解决办法
276
查看次数

如何检索 python 代码的控制流图?

我想转储给定 python 代码的控制流图,类似于 gcc 编译器选项给出的选项:用于 c 代码的 -fdump-tree-cfg 。

我成功获得了Python代码的AST(抽象语法树),但是从AST阶段获得控制流图似乎相当复杂和麻烦。

有没有更简单的方法来直接检索Python代码的控制流图?有什么建议么?

哦顺便说一下我正在使用 python3.5

谢谢你们!

PS我真的不知道我在幕后使用什么样的解释器,据我所知它是CPython(不确定),我不认为它是PyPy(Rpython)。有什么建议我如何验证吗?

python abstract-syntax-tree control-flow-graph

3
推荐指数
1
解决办法
2631
查看次数

如何设置ply的yacc优先级

我需要使用 ply 从正则表达式创建 AST。例如,如果 RE 是 (a|b*)abc,我想将一个配对元组制作为 (':', (':', (':', ('|', 'a', ('* ', 'b')), 'a'), 'b'), 'c') <-- ':' 表示将字符串分成两部分。

这是我的代码。

tokens = (
    "SYMBOL",
    "LBRACKET",
    "RBRACKET",
    "STAR",
    "UNION"
)

t_ignore = ' \t\n'
t_SYMBOL = r'[a-zA-Z0-9]'
t_LBRACKET = r'\('
t_RBRACKET = r'\)'
t_STAR = r'\*'
t_UNION = r'\|'

def t_error(t):
    raise TypeError("Unknown text '%s'" % t.value)

def p_multiple(p) :
    '''string : SYMBOL SYMBOL
              | string SYMBOL
              | string string'''
    p[0] = (':', p[1], p[2])    

def p_union(p) :
    '''string …
Run Code Online (Sandbox Code Playgroud)

python regex yacc ply abstract-syntax-tree

3
推荐指数
1
解决办法
3625
查看次数

使用 TypeScript API 查找类型引用

我试图找到 a 的种类(类、接口、类型别名、枚举...)TypeReference

我有这个:

const anode = node as ts.TypeReferenceNode;
const symbol = this.typechecker.getSymbolAtLocation(anode.typeName) as ts.Symbol;
const type = this.typechecker.getTypeOfSymbolAtLocation(symbol, anode);
const decls = symbol.getDeclarations() as ts.Declaration[]; 
Run Code Online (Sandbox Code Playgroud)

但是调用getSymbolAtLocation返回undefined

anodeTypeReferenceNode根据 VSC 调试器是一个(种类 159):

在此处输入图片说明

ETypes对枚举引用的转义文本引用。

abstract-syntax-tree typescript

3
推荐指数
1
解决办法
516
查看次数

Java 的 AST 差异提取器

假设我有两个这样的源代码:

程序1:

public class MathUtils4M0
{

    public  int getMaxAdjacentSum( int[] numbers )
    {
        if (numbers == null || numbers.length < 2) {
            return 0;
        } else {
            int max = Integer.MIN_VALUE;
            for (int i = 0; i < numbers.length * 1; i++) {
                int temp = numbers[i] + numbers[i + 1];
                if (temp > max) {
                    max = temp;
                }
            }
            return max;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

程序2:

public class MathUtils4M92
{

    public  int getMaxAdjacentSum( int[] numbers )
    {
        if (numbers …
Run Code Online (Sandbox Code Playgroud)

java diff patch abstract-syntax-tree

3
推荐指数
1
解决办法
1104
查看次数

以点文件的形式生成 AST

我正在使用 ANTLR4 生成 java 源代码的 AST,但我不得不转向 ANTLR3,因为我没有得到太多帮助和文档,而且确实很难继续。我设法生成 AST,但不是以可视格式生成。然后我遇到了一个很棒的答案 ,我确实能够在 DOT 文件中生成 AST,但有一个小问题。

我的代码:

import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.DOTTreeGenerator;
import org.antlr.stringtemplate.StringTemplate;

class Main {

        public static void main(String[] args) throws Exception {
            parseFile("/home/satnam-sandhu/Workstation/ASTGenerator/resource/java/Blabla.java");
        }

    public static void parseFile(String f)throws Exception {

            JavaLexer lexer = new JavaLexer(new ANTLRFileStream(f));
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            JavaParser parser = new JavaParser(tokens);
            CommonTree tree = (CommonTree)parser.compilationUnit().getTree();
            DOTTreeGenerator gen = new DOTTreeGenerator();
            StringTemplate st = gen.toDOT(tree);
            System.out.println(st);

    }

}
Run Code Online (Sandbox Code Playgroud)

我正在使用 gradle,所以我通过以下方式构建项目:

gradle clean …
Run Code Online (Sandbox Code Playgroud)

java dot abstract-syntax-tree antlr3

3
推荐指数
1
解决办法
2337
查看次数

使用 AST 模块来改变和删除赋值/函数调用

例如,如果我想将大于更改为小于或等于,我已成功执行:

def visit_Gt(self, node):
    new_node = ast.GtE()
    return ast.copy_location(new_node, node)
Run Code Online (Sandbox Code Playgroud)

我如何访问/检测赋值操作(=)和函数调用()并简单地删除它们?我正在阅读 AST 文档,但找不到访问赋值或函数调用类然后不返回任何内容的方法。

我正在寻找的赋值操作的一个例子:

print("Start")
x = 5
print("End")
Run Code Online (Sandbox Code Playgroud)

变成:

print("Start")

print("End")
Run Code Online (Sandbox Code Playgroud)

我正在寻找删除函数调用的示例:

 print("Start")
 my_function_call(Args)
 print("End")
Run Code Online (Sandbox Code Playgroud)

成为

print("Start")

print("End")
Run Code Online (Sandbox Code Playgroud)

python abstract-syntax-tree mutation

3
推荐指数
1
解决办法
3511
查看次数

从 Powershell 脚本中提取 AST

给定一个 powershell 脚本(例如 $C=$a+$b; $d = $e*$f),我尝试访问脚本 AST 中的每个节点。

\n\n

目前,我\xe2\x80\x99已经尝试过:

\n\n
$code = {$C=$a+$b; $d = $e*$f}\n$astVisitor = [System.Management.Automation.Language.AstVisitor]\n$visit = $code.Ast.Visit($astVisitor)\n
Run Code Online (Sandbox Code Playgroud)\n\n

but I\xe2\x80\x99m running into the following error: Cannot find an overload for "Visit" and the argument count: "1".

\n\n

What\'s the right way to access the ast data structure and use the visit method correctly to loop through every node in the tree? I didn\'t find the documentation of the ast api very helpful.

\n\n

Thanks very …

powershell abstract-syntax-tree

3
推荐指数
1
解决办法
1094
查看次数

64 位移植 c++ - 跟踪基本类型、指针、算术的使用

我们正在使用 gcc/linux 从 32 位移植到 LP64。我正在寻找一种方法来跟踪在内存布局(long,Ptr)中更改的数据类型的使用情况。我对与这些类型的所有可能有问题的“交互”的位置感兴趣。

Typedef 需要取消引用到内置类型,如 ULONG 和 DWORD -> unsigned long 可能的问题可能是 iE:旧式强制转换、重新解释强制转换、算术、上溢/下溢、赋值、比较、常量、ptr 算术、sizeof、size_t 算术,隐含的对话......你就是这样。

因为这与正则表达式无关,而且代码库很大,所以我需要以某种方式使其自动化。我想我至少需要一个解析树/AST 以及一个符号表。我在各个阶段尝试了 GCC 的一些内部树转储,但仍然没有任何线索。我还启用了编译器必须提供的所有警告;-) 也许任何人都可以给我一个提示,在哪里寻找。(我已经看到了这个看起来很有前途的语义设计 Toolokit)

你是怎么做到的?尝试、测试、失败并处理错误?我会试试clang...

问候

c++ 64-bit porting types abstract-syntax-tree

3
推荐指数
1
解决办法
133
查看次数