CCOUNT readability metrics tool for C programs available
Linuxmisc » Unix Programmer
A simple measurement tool for C programs called 'ccount' (written
by Joerg Lawrenz) is available for anonymous ftp
from i41s10.ira.uka.de [129.13.13.110]
in /pub/ccount.tar.Z (100 kB)
It will also appear on comp.sources.misc in a few days.
The README file follows below
Lutz
Lutz Prechelt (email: prech...@ira.uka.de) | Whenever you
Institut fuer Programmstrukturen und Datenorganisation | complicate things,
Universitaet Karlsruhe; 76128 Karlsruhe; Germany | they get
(Voice: ++49/721/608-4068, FAX: ++49/721/694092) | less simple.
README file for 'ccount' analysis tools
===========================================
'ccount' is a set of simple tools for the analysis of the syntactic
readability of C source programs.
The attributes computed by the ccount tools are things such as
- length of modules/functions/blocks/ifs/whiles/switches,
- number of control structures
- number of operators in expressions,
- nesting depth of blocks etc.
ccount consists of the kernel analysis tool 'ccount', which performs
syntactic analysis of C programs WITHOUT running the preprocessor,
a simple front end 'ccounter' to simplify the use of 'ccount',
and a number of statistical analysis tools that generate reports
from the tons of data produced by running 'ccount' on a lot of C
source files.
These statistics include
- averages
- medians
- 25%, 75%, 90%, 95%, 98%, and 99% quantiles
- sums
- fractions
of various of the attributes (and combinations of them) over a set of
analyzed source files.
Environment:
------------
'ccount' is implemented as one C program (using a LEX scanner and
a YACC parser) and a set of scripts (Bourne-Shell and Perl).
To compile and run ccount you only need a C compiler, sh, and perl.
Installation:
-------------
To compile 'ccount' just type 'make'.
This should generate a single executable 'ccount'.
Put this executable and the scripts 'statty*' and 'ccounter' into
any directory that is in your $PATH. That's all.
Manifest:
----------
The ccount package consists of the following files:
README (this file)
README.1 Description of 'ccount' and 'ccounter'
README.2 Description of statistical tools 'statty*'
Makefile Makefile
ccount Executable 'ccount' command [must be generated by make]
ccount.l Source file of LEX scanner
ccount.y Source file of YACC parser
ccounter Script to simplify use of 'ccount'
lex.yy.c C code generated by LEX from ccount.l
statty "Generate all reports"-script
stattyB Blocks report script
stattyCSE Control structure and expression report script
stattyF Function report script
stattyM Module report script
y.tab.c C code generated by YACC from ccount.y
y.tab.h C header file generated by YACC from ccount.y
I'm interested in this, but I can't seem to ftp to it. The address is unknown.
Is it shown correctly here?
Dale
dhgilli...@yvax.byu.edu
Unless I am doing something radically wrong I found it to be rather
useless. It couldnt parse ANY of my code. Not even a simple
15 liner. Hopefully later releases will be better....
-----------------------------------------------------------------------
John Cavanaugh
cavan...@ecn.purdue.edu Purdue University
-----------------------------------------------------------------------
----------------------- No fancy quote today ------------------------
-----------------------------------------------------------------------
I have a version of ccount but I'm not sure where it came from.
I do use it though, even though its results are sometimes slightly
inaccurate. The program is quite short so the code is given below:-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*
Program to count number of lines in C source file.
*/
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#define MAXLINE (100)
#define BLANK (0)
#define CODE (1)
#define COMMENT (2)
main(argc, argv)
int argc;
char **argv;
{
FILE *fp;
char c;
char line[MAXLINE];
int i, j, k, flen;
int lineType,
inComment;
long nCodeTotal,
nBlankTotal,
nCommentTotal;
long nCode,
nBlank,
nComment,
nTotal;
/* ---------------------------------------------------------------- */
if (argc < 2)
{
fprintf(stderr, "Usage: %s filename [filename...]\n", argv[0]);
exit(0);
}
flen = strlen(argv[1]);
for (i = 2; i < argc; i++)
if (strlen(argv[i]) > flen)
flen = strlen(argv[i]);
for (i = 8; i < flen; i++)
printf (" ");
printf("%s %6s %6s %6s %6s\n",
"File Name", "Source", "Comments", "Blank", "Total");
for (i = 0; i < flen; i++)
printf (" ");
for (i = 0; i < 4; i++)
printf (" ");
printf("%s %6s %% %6s %% %6s %% %6s\n\n",
"", "Lines", "Lines", "Lines", "Lines");
nCodeTotal = nBlankTotal = nCommentTotal = 0;
for (i = 1; i < argc; i++)
{
nCode = nBlank = nComment = 0;
fp = fopen(argv[i], "r");
if (fp != NULL)
{
while (fgets(line, MAXLINE, fp) != NULL)
{
/* Is there a start of comment in the line */
lineType = BLANK;
j = 0;
while (c = line[j++])
{
if (isgraph(c))
lineType = CODE;
if (c == '/' && line[j] == '*')
{
/* Start of comment */
lineType = COMMENT;
/* Consume the rest of the line */
inComment = 1;
nComment++;
while (c = line[j++])
{
if (c == '*' && line[j] == '/')
{
inComment = 0;
break;
}
}
if (inComment)
{
/*
* Keep consuming lines until the end of comment
* marker is found.
*/
while (fgets(line, MAXLINE, fp) != NULL)
{
j = 0;
while (c = line[j++])
{
if (c == '*' && line[j] == '/')
{
inComment = 0;
break;
}
}
if (inComment)
{
nComment++;
}
else
{
break;
}
}
}
break;
}
}
if (lineType == CODE)
{
nCode++;
}
else if (lineType == BLANK)
{
nBlank++;
}
}
fclose(fp);
}
else
{
if (errno > 0)
perror("Opening file");
}
nCodeTotal += nCode;
nBlankTotal += nBlank;
nCommentTotal += nComment;
nTotal = nCode + nBlank + nComment;
if (nTotal > 0)
{
printf (" %s", argv[i]);
for (k = strlen(argv[i]); k < flen; k++)
printf (" ");
printf(" %6d %3d %6d %3d %6d %3d %6d\n",
nCode, nCode * 100 / nTotal,
nComment, nComment * 100 / nTotal,
nBlank, nBlank * 100 / nTotal, nTotal);
}
}
printf("\n -----------------------------------------\n\n");
printf(" %12s %6s %6s %6s %6s\n", "", "Source", "Comments", "Blank", "Total");
printf(" %12s %6s %% %6s %% %6s %% %6s\n\n", "", "Lines", "Lines", "Lines", "Lines");
--argc;
nTotal = nCodeTotal + nBlankTotal + nCommentTotal;
printf(" %12s %6d %6d %6d %6d\n",
"Totals", nCodeTotal, nCommentTotal, nBlankTotal, nTotal);
printf(" %12s %6d %3d %6d %3d %6d %3d %6d\n",
"Averages", nCodeTotal / argc, nCodeTotal * 100 / nTotal,
nCommentTotal / argc, nCommentTotal * 100 / nTotal,
nBlankTotal / argc, nBlankTotal * 100 / nTotal, nTotal / argc);
printf("\n %12s %d\n", "Number of files", argc);
/* ------------------------------------------------------------------------ */
--
Andrew Peter Marlow
APM Software Ltd
email: and...@apmsl.demon.co.uk