我如何在python中为java代码编写一个非常简单的解析器?

时间:2023-01-27 11:45:14

I'm trying to write a very barebones parser for java code in python.

我正在尝试在python中为java代码编写一个非常准确的解析器。

Basically what I'd need are two methods: handle_keyword(keyword) and handle_operator(operator) which would get automatically called when keywords and operators are encountered.

基本上我需要的是两个方法:handle_keyword(keyword)和handle_operator(operator),它们会在遇到关键字和运算符时自动调用。

I'd also like a third method called handle_other(text) which would handle any substring in the file which was not a keyword or operator.

我还想要一个名为handle_other(text)的第三种方法,它可以处理文件中不是关键字或运算符的任何子字符串。

text, keyword, and operator in the above methods would all be strings

上述方法中的text,keyword和operator都是字符串

I'd like to know if Python has some sort of built in for something like this, or if there's any parsing algorithms/techniques that anyone can recommend. I've looked at python's HTML parser but I'm not sure I can use similar methods for java code. If there's already an existing library that would be even better/

我想知道Python是否有类似内置的内置,或者是否有任何人可以推荐的解析算法/技术。我看过python的HTML解析器,但我不确定我是否可以使用类似的java代码方法。如果现有的库已经更好/

1 个解决方案

#1


3  

Just use ANTLR with the Java grammar and specify Python as the target language.

只需将ANTLR与Java语法一起使用,并将Python指定为目标语言。

#1


3  

Just use ANTLR with the Java grammar and specify Python as the target language.

只需将ANTLR与Java语法一起使用,并将Python指定为目标语言。