进行词法分析时,ANTLR 可以返回代码行吗? [英] Can ANTLR return Lines of Code when lexing?

查看:22
本文介绍了进行词法分析时,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++;} 放在之后(或之前)规则.

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 总是以 ';' 结尾.在词法分析期间,您将无法区分 ';' 之后的 ';' 和 2 ';'s 在 for(int i = 0; i < n; i++) { ... } 语句中(不会是 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 可以返回代码行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆