英文:
How to find only overridden methods in antlr4
问题
I'm trying to find all overridden methods in java files using antlr4 with python runtime. I have taken the grammar from https://github.com/antlr/grammars-v4/tree/master/java. I'm able to find the methods using the below code successfully.
istream = FileStream(repo_path, encoding='utf-8')
lexer = JavaLexer(istream)
stream = CommonTokenStream(lexer)
parser = JavaParser(stream)
tree = parser.compilationUnit()
walker = ParseTreeWalker()
walker.walk(PatternListener(), tree)
Inside Listener,
def enterMethodDeclaration(self, ctx):
method_name = ctx.IDENTIFIER().getText()
method_defition = ctx.methodBody().getText()
Now I want to track only overridden methods, not all the methods. so, I made below changes.
In JavaLexer.g4,
OVERRIDEN '@Override';
In JavaParser.g4,
methodDeclaration
: OVERRIDEN typeTypeOrVoid IDENTIFIER formalParameters ('[' ']')*
(THROWS qualifiedNameList)?
methodBody
;
to restrict methodDeclaration to only overriden method, and I have re-generated all lexer and parser again. But this time I'm getting many mismatch issues while parsing, like below.
line 71:15 extraneous input 'void' expecting {<EOF>, 'abstract', 'boolean', 'byte', 'char', 'class', 'double', 'enum', 'final', 'float', 'int', 'interface', 'long', 'native', 'private', 'protected', 'public', 'short', 'static', 'strictfp', 'synchronized', 'transient', 'volatile', '@Override', '{', '}', ';', '<', '@', IDENTIFIER}
line 74:23 mismatched input '(' expecting ';'
Can anyone please highlight the mistake which I'm doing here, is there any other way to implement this?. Any suggestions or discussions are appreciated. Thank you in advance:).
英文:
I'm trying to find all overridden methods in java files using antlr4 with python runtime. I have taken the grammar from https://github.com/antlr/grammars-v4/tree/master/java. I'm able to find the methods using the below code successfully.
istream = FileStream(repo_path, encoding='utf-8')
lexer = JavaLexer(istream)
stream = CommonTokenStream(lexer)
parser = JavaParser(stream)
tree = parser.compilationUnit()
walker = ParseTreeWalker()
walker.walk(PatternListener(), tree)
Inside Listener,
def enterMethodDeclaration(self, ctx):
method_name = ctx.IDENTIFIER().getText()
method_defition = ctx.methodBody().getText()
Now I want to track only overridden methods, not all the methods. so, I made below changes.
In JavaLexer.g4,
OVERRIDEN '@Override';
In JavaParser.g4,
methodDeclaration
: OVERRIDEN typeTypeOrVoid IDENTIFIER formalParameters ('[' ']')*
(THROWS qualifiedNameList)?
methodBody
;
to restrict methodDeclaration to only overriden method, and I have re-generated all lexer and parser again. But this time I'm getting many mismatch issues while parsing, like below.
line 71:15 extraneous input 'void' expecting {<EOF>, 'abstract', 'boolean', 'byte', 'char', 'class', 'double', 'enum', 'final', 'float', 'int', 'interface', 'long', 'native', 'private', 'protected', 'public', 'short', 'static', 'strictfp', 'synchronized', 'transient', 'volatile', '@Override', '{', '}', ';', '<', '@', IDENTIFIER}
line 74:23 mismatched input '(' expecting ';'
Can anyone please highlight the mistake which I'm doing here, is there any other way to implement this?. Any suggestions or discussions are appreciated. Thank you in advance:).
答案1
得分: 1
你已经更改了语法,以便所有方法都必须有@Override
注释。但似乎你的方法中至少有一个没有这样的注释。因此,你的Java文件不再符合你的语法,会导致语法错误。
如果你不想更改哪些文件在语法上是有效的,而只想更改哪些方法由你的监听器处理,那么你应该更改你的监听器,而不是你的语法。具体来说,你应该检查你的methodDeclaration
所属的classBodyDeclaration
(即methodDeclaration
的父级)是否包含名为@Override
的注释(如果没有,那就什么都不做)。
英文:
You've changed the grammar, so that all methods must have an @Override
annotation. It looks like at least one of your methods doesn't have such an annotation though. Therefore your Java file no longer fits your grammar and you get a syntax error.
If you don't want to change, which files are syntactically valid, but only want to change which methods are handled by your listener, then you should change your listener - not your grammar. Specifically, you should check whether the classBodyDeclaration
that your methodDeclaration
belongs to (i.e. the methodDeclaration
's grand parent) contains an annotation named @Override
(and then just not do anything if it doesn't).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论