本文介绍了词法分析时,ANTLR可以返回代码行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ANTLR使用完整的Java语法分析大量代码.由于ANTLR需要打开所有源文件并对其进行扫描,所以我想知道它是否还可以返回代码行.

I am trying use ANTLR to analyse a large set of code using full Java grammar. Since ANTLR needs to open all the source files and scan them, I am wondering if it can also return lines of code.

我检查了Lexer和Parser的API,看来它们没有返回LoC.一点点地掌握语法规则来获得LoC容易吗?完整的Java规则很复杂,我真的不想弄乱它的很大一部分.

I checked API for Lexer and Parser, it seems they do not return LoC. Is it easy to instrument the grammar rule a bit to get LoC? The full Java rule is complicated, I don't really want to mess a large part of it.

推荐答案

如果您已有ANTLR语法,并且想在解析过程中计算某些内容,则可以执行以下操作:

If you have an existing ANTLR grammar, and want to count certain things during parsing, you could do something like this:

grammar ExistingGrammar;

// ...

@parser::members {
  public int loc = 0;
}

// ...

someParserRule
 : SomeLexerRule someOtherParserRule {loc++;}
 ;

// ...

因此,每当操作者遇到someParserRule时,都可以通过将{loc++;}放置在规则之后(或之前)来将loc增加1.

So, whenever your oparser encounters a someParserRule, you increase the loc by one by placing {loc++;} after (or before) the rule.

因此,无论您对代码行的定义是什么,只需将{loc++;}放在规则中以增加计数器.注意不要增加两次:

So, whatever your definition of a line of code is, simply place {loc++;} in the rule to increase the counter. Be careful not to increase it twice:

statement
 : someParserRule {loc++;}
 | // ...
 ;

someParserRule
 : SomeLexerRule someOtherParserRule {loc++;}
 ;

编辑

我刚刚注意到,在您的问题的标题中,您询问是否可以在词汇化过程中完成此操作.那是不可能的.假设LoC总是以';'结尾.在词法分析过程中,您将无法在例如赋值后的';'和单个for(int i = 0; i < n; i++) { ... }语句中的两个';'之间进行区分,例如,赋值是单个LoC.设为2 LoC.

EDIT

I just noticed that in the title of your question you asked if this can be done during lexing. That won't be possible. Let's say a LoC would always end with a ';'. During lexing, you wouldn't be able to make a distinction between a ';' after, say, an assignment (which is a single LoC), and the 2 ';'s inside a for(int i = 0; i < n; i++) { ... } statement (which wouldn't be 2 LoC).

这篇关于词法分析时,ANTLR可以返回代码行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 23:49