找到函数名称并计算其LOC [英] finding a function name and counting its LOC

查看:133
本文介绍了找到函数名称并计算其LOC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以你知道蝙蝠,这是一个项目,我被分配。我不是在代码中寻找答案,而是更多的方向。

So you know off the bat, this is a project I've been assigned. I'm not looking for an answer in code, but more a direction.

我被告知要做的是通过一个文件并计算实际的代码行,同时记录函数名和单独的代码行功能。我遇到的问题是确定一种方式,当从文件读取以确定该行是否是一个函数的开始。

What I've been told to do is go through a file and count the actual lines of code while at the same time recording the function names and individual lines of code for the functions. The problem I am having is determining a way when reading from the file to determine if the line is the start of a function.

到目前为止,我只能想到也许具有数据类型(int,double,char等)的字符串数组,在行中搜索,然后搜索括号,然后搜索缺少分号(所以我知道它不只是声明的功能)。

So far, I can only think of maybe having a string array of data types (int, double, char, etc), search for that in the line and then search for the parenthesis, and then search for the absence of the semicolon (so i know it isn't just the declaration of the function).

所以我的问题是,这是我应该怎么走这个,还是有其他方法,你会推荐?

So my question is, is this how I should go about this, or are there other methods in which you would recommend?

我将要计数的代码将在C ++中。

The code in which I will be counting will be in C++.

推荐答案


  1. 使用正则表达式。这和你想的很相似。查找看起来像函数定义的线。这是相当快的,但可以在许多方面错误。

  1. Use regular expressions. This is fairly similar to what you're thinking of. Look for lines that look like function definitions. This is fairly quick to do, but can go wrong in many ways.

char *s = "int main() {"

不是函数定义,但肯定看起来像一个。

is not a function definition, but sure looks like one.

char
* /* eh? */
s
(
int /* comment? // */ a
)
// hello, world /* of confusion
{

是一个函数定义,但看起来不像一个。

is a function definition, but doesn't look like one.

好:写得快,即使面对语法错误也可以工作;坏的:可以容易地失火在看起来像(或不看起来像)正常情况的东西。

Good: quick to write, can work even in the face of syntax errors; bad: can easily misfire on things that look like (or fail to look like) the "normal" case.

变体:首先运行代码,例如GNU indent 。

Variant: First run the code through, e.g., GNU indent. This will take care of some (but not all) of the misfires.

使用正确的词法分析器和解析器。这是一个更彻底的方法,但你可以重新使用开源词典/解析(例如,从gcc)。

Use a proper lexer and parser. This is a much more thorough approach, but you may be able to re-use an open source lexer/parsed (e.g., from gcc).

好:将100 %准确(永远不会失火)。 Bad:一个缺少的分号和它发现错误。

Good: Will be 100% accurate (will never misfire). Bad: One missing semicolon and it spews errors.

看看你的编译器是否有一些调试输出可能有帮助。这是(2)的一个变体,但使用你的编译器的词法分析器/解析器而不是你自己的。

See if your compiler has some debug output that might help. This is a variant of (2), but using your compiler's lexer/parser instead of your own.

这篇关于找到函数名称并计算其LOC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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