如何在antlr4中找到仅重写的方法

huangapple go评论72阅读模式
英文:

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=&#39;utf-8&#39;)
    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           &#39;@Override&#39;;

In JavaParser.g4,

methodDeclaration
    : OVERRIDEN typeTypeOrVoid IDENTIFIER formalParameters (&#39;[&#39; &#39;]&#39;)*
      (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 &#39;void&#39; expecting {&lt;EOF&gt;, &#39;abstract&#39;, &#39;boolean&#39;, &#39;byte&#39;, &#39;char&#39;, &#39;class&#39;, &#39;double&#39;, &#39;enum&#39;, &#39;final&#39;, &#39;float&#39;, &#39;int&#39;, &#39;interface&#39;, &#39;long&#39;, &#39;native&#39;, &#39;private&#39;, &#39;protected&#39;, &#39;public&#39;, &#39;short&#39;, &#39;static&#39;, &#39;strictfp&#39;, &#39;synchronized&#39;, &#39;transient&#39;, &#39;volatile&#39;, &#39;@Override&#39;, &#39;{&#39;, &#39;}&#39;, &#39;;&#39;, &#39;&lt;&#39;, &#39;@&#39;, IDENTIFIER}
line 74:23 mismatched input &#39;(&#39; expecting &#39;;&#39;

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).

huangapple
  • 本文由 发表于 2020年1月6日 16:18:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定