gpt4 book ai didi

java - 如何使用 ANTLR 获取 Python 中每个函数的 ast

转载 作者:行者123 更新时间:2023-12-02 11:29:38 24 4
gpt4 key购买 nike

我已经有了 Python 语法。我想为 Python 中定义的每个函数生成一个单独的 AST。这是我的代码:

public class TestGrammar extends Python3BaseListener {

public static void main(String[] args) throws IOException {
PlagiarismPercentage obj = new PlagiarismPercentage();

String source = obj
.readFile("C:\\Users\\Paridhi\\quickSort.py");
ANTLRInputStream inputCharStream = new ANTLRInputStream(new StringReader(source));
TokenSource lexer = new Python3Lexer(inputCharStream);
// Python3Lexer lexer = new Python3Lexer(CharStreams.fromString(source));
Python3Parser parser = new Python3Parser(new CommonTokenStream(lexer));

ParseTreeWalker.DEFAULT.walk(new Python3BaseListener() {

AstPrinter astPrinter = new AstPrinter();

@Override
public void enterFuncdef(Python3Parser.FuncdefContext ctx) {
System.out.printf("NAME=%s\n", ctx.NAME().getText());
// System.out.println(ctx.toString());
TestGrammar grammar = new TestGrammar();
StringBuilder str = grammar.explore(ctx, false, 0, new StringBuilder(""));
System.out.println("----------------------------");
System.out.println(str);
System.out.println("----------------------------");
}
}, parser.single_input());
}

private StringBuilder explore(RuleContext ctx, boolean verbose, int indentation, StringBuilder str) {
boolean toBeIgnored = !verbose && ctx.getChildCount() == 1 && ctx.getChild(0) instanceof ParserRuleContext;
if (!toBeIgnored) {
String ruleName = Python3Parser.ruleNames[ctx.getRuleIndex()];

for (int i = 0; i < indentation; i++) {
System.out.print(" ");

}

System.out.println(ruleName + " " + ctx.getText());
if (indentation != 0) {
str.append(ruleName + " ");
}

}
for (int i = 0; i < ctx.getChildCount(); i++) {
ParseTree element = ctx.getChild(i);
if (element instanceof RuleContext) {
explore((RuleContext) element, verbose, indentation + (toBeIgnored ? 0 : 1), str);
}
}
return str;
}

}

我在文件快速排序中进行快速排序的Python代码如下:

def quickSort(alist):
quickSortHelper(alist,0,len(alist)-1)

def quickSortHelper(alist,first,last):
if first<last:
splitpoint = partition(alist,first,last)
quickSortHelper(alist,first,splitpoint-1)
quickSortHelper(alist,splitpoint+1,last)

def partition(alist,first,last):
pivotvalue = alist[first]
leftmark = first+1
rightmark = last

done = False
while not done:
while leftmark <= rightmark and alist[leftmark] <= pivotvalue:
leftmark = leftmark + 1
while alist[rightmark] >= pivotvalue and rightmark >= leftmark:
rightmark = rightmark -1
if rightmark < leftmark:
done = True
else:
temp = alist[leftmark]
alist[leftmark] = alist[rightmark]
alist[rightmark] = temp
temp = alist[first]
alist[first] = alist[rightmark]
alist[rightmark] = temp


return rightmark


alist = [54,26,93,17,77,31,44,55,20]
quickSort(alist)
print(alist)

现在,树遍历器刚刚输出第一个快速排序函数的详细信息,但是,我想要 python 代码中所有函数定义的详细信息。我可以在这里做类似 java 中的 getAllMethods() 的事情吗?

最佳答案

您在 main 方法中实例化一个 java.util.List,然后在 enterFuncdef 方法中向其中添加项目。

关于java - 如何使用 ANTLR 获取 Python 中每个函数的 ast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49374199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com