Java 如何使用ANTLR为Python中的每个函数获取ast
我已经有了Python语法。我想为Python中定义的每个函数生成一个单独的AST。这是我的密码:Java 如何使用ANTLR为Python中的每个函数获取ast,java,python,antlr4,abstract-syntax-tree,Java,Python,Antlr4,Abstract Syntax Tree,我已经有了Python语法。我想为Python中定义的每个函数生成一个单独的AST。这是我的密码: public class TestGrammar extends Python3BaseListener { public static void main(String[] args) throws IOException { PlagiarismPercentage obj = new PlagiarismPercentage(); String source = obj
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;
}
}
public类TestGrammar扩展了Python3BaseListener{
公共静态void main(字符串[]args)引发IOException{
剽窃保护obj=新剽窃保护();
字符串源=obj
.readFile(“C:\\Users\\Paridhi\\quickSort.py”);
AntlInputStream inputCharStream=新的AntlInputStream(新的StringReader(源));
TokenSource lexer=新的Python3Lexer(inputCharStream);
//Python3Lexer lexer=新的Python3Lexer(CharStreams.fromString(source));
Python3Parser=newpython3parser(newcommontokenstream(lexer));
ParseTreeWalker.DEFAULT.walk(新的Python3BaseListener(){
AstPrinter AstPrinter=新建AstPrinter();
@凌驾
public void enterFuncdef(Python3Parser.FuncdefContext ctx){
System.out.printf(“NAME=%s\n”,ctx.NAME().getText());
//System.out.println(ctx.toString());
TestGrammar=新的TestGrammar();
StringBuilder str=grammar.explore(ctx,false,0,new StringBuilder(“”);
System.out.println(“-------------------------------”;
系统输出打印项次(str);
System.out.println(“-------------------------------”;
}
},parser.single_input());
}
私有StringBuilder浏览(RuleContext ctx、布尔详细、int缩进、StringBuilder str){
布尔值toBeIgnored=!verbose&&ctx.getChildCount()==1&&ctx.getChild(0)ParserRuleContext实例;
如果(!toBeIgnored){
字符串ruleName=Python3Parser.ruleNames[ctx.getRuleIndex()];
对于(int i=0;i
下面给出了我在文件quicksort中进行快速排序的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)
def快速排序(列表):
快速排序器(列表,0,列(列表)-1)
def quickSortHelper(列表、第一个、最后一个):
如果首先在main
方法中实例化java.util.List
,然后在enterFuncdef
方法中向其添加项。谢谢Bart!还有一件事。。如果我添加语句funccall:NAME参数;在Python3语法和enterFunccall函数的重写中,我写了ctx.Name().getText(),在给出上面的Python输入时,它并没有给我对语句quickSortHelper(alist,0,len(alist)-1的funccall,而是给了我一个简单的例子,我怎样才能使它成为funccall?不客气。我不明白。我建议提出一个新问题。在更改语法时,还要确保重新生成lexer和parser类。