在Java中建立halstead指标 [英] building halstead metrics in java

查看:72
本文介绍了在Java中建立halstead指标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Java中构建一个接口程序来分析任何Java文件并获取Halstead度量结果,但是我在解析输入文件时遇到了问题,我了解了很多有关Antlr和JavaParsing项目的信息,但实际上无法了解我们如何使用它. 我的问题是:

I'm trying to build an interface program in java to analyzing any java file and get the Halstead metrics result but i face a problem in parsing the input file, I read a lot about Antlr and JavaParsing projects but really can't understand how we can use it. My questions is:

1- Java编译器中是否有任何默认命令行来解析任何字符串,并在此输入字符串行中找到任何操作数或运算符. 2-如果答案是否定的,那么对于发送有关使用Javaparsing或Antlr库的小例子的任何帮助都可以帮忙

1- is there any default command line in java compiler parsing any string and find any operand or operator in this input string line. 2 - if the answer is no can any one help in sending small example about using Javaparsing or Antlr libraries

您一如既往的支持表示赞赏 最好的问候

Your usual support appreciated Best Regards

推荐答案

我不知道执行此操作的任何命令行解析器.

I'm not aware of any command line parser that does this.

ANTLR并不简单,但是您要做的是从Java语法创建一个解析器.这就是我们生成语法的方式(您可以在此处下载语法- https://github.com/antlr/grammars-v4/tree/master/java ):

ANTLR isn't simple but what you want to do is create a parser from a Java Grammar. This is how we generate our grammar (you can download the grammar here - https://github.com/antlr/grammars-v4/tree/master/java) :

公共类GenerateVisitor {

public class GenerateVisitor {

  public static void main(String[] args) {
      String[] arg0 = { "-visitor", "D:/grammars/antlr/Java.g4", "-package", "com.virtualmachinery.jhawk.antlr" };
      org.antlr.v4.Tool.main(arg0);
  }  }

完成此操作后,您将发现有6个生成的类和两个文件.

When you have done this you will find you have 6 generated classes and two files.

很显然,我无法在5分钟内教您如何使用ANTLR,但是您要做的是创建一个访问器,该访问器扩展了已创建的JavaVisitor类.然后,您可以覆盖访问这些方法的方法,然后分析传入的上下文.然后,可以使用switch语句分离出作为运算符的标记和作为操作数的标记.

Obviously I can't teach you how to use ANTLR in 5 minutes but what you want to do is create a visitor that extends the JavaVisitor class that has been created. You can then override the methods that visit the methods and then analyse the context that is passed in. You can then separate out the tokens that are operators and the tokens that are operands using a switch statement.

在我们的代码中,我们这样做:

In our code we do something like this:

public static void handleTokens(ParseTree ctx)
        List<Token> tokens = getTokens(ctx);
        int type;
        for (Token each: tokens) {
            type = getHalsteadType(type);
         }
}

所有类型都在JavaParser类中定义为常量.关于哪些令牌可以忽略,哪些是运算符以及哪些是运算符,存在一些争议-您可以在此处找到有关如何做出决定的信息-

The types are all defined as constants in the JavaParser class. There is some debate about which tokens can be ignored, which are operators and which are operators - you can find some information on how to make that decision here -

www.virtualmachinery.com/sidebar2.htm

这篇关于在Java中建立halstead指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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