76

Count C keywords in a file

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. #define BSEARCH 1
  7.  
  8. typedef struct {
  9.     char *word;
  10.     unsigned count;
  11. } keyword;
  12.  
  13. keyword keytab[] = {
  14.     "auto", 0,
  15.     "break", 0,
  16.     "case", 0,
  17.     "char", 0,
  18.     "const", 0,
  19.     "continue", 0,
  20.     "default", 0,
  21.     "do", 0,
  22.     "double", 0,
  23.     "else", 0,
  24.     "enum", 0,
  25.     "extern", 0,
  26.     "float", 0,
  27.     "for", 0,
  28.     "goto", 0,
  29.     "if", 0,
  30.     "int", 0,
  31.     "long", 0,
  32.     "register", 0,
  33.     "return", 0,
  34.     "short", 0,
  35.     "signed", 0,
  36.     "sizeof", 0,
  37.     "static", 0,
  38.     "struct", 0,
  39.     "switch", 0,
  40.     "typedef", 0,
  41.     "union", 0,
  42.     "unsigned", 0,
  43.     "void", 0,
  44.     "volatile", 0,
  45.     "while", 0
  46. };
  47.  
  48. #define NKEYS (sizeof (keytab)/sizeof (keyword))
  49.  
  50. int fgetword( FILE *in, char *word, int mlen ) {
  51.     char *w = word;
  52.     int c;
  53.  
  54.     while ( (c = getc( in )) != EOF && !isalpha( c ) ) {
  55.         /* skip comments */
  56.         /* skip strings */
  57.         if ( c == '\"' )
  58.             while ( (c = getc( in )) != EOF && c != '\"' )
  59.                 ;
  60.     }
  61.     if ( c == EOF )
  62.         return EOF;
  63.  
  64.     *w++ = c;
  65.  
  66.     while ( --mlen > 0 ) {
  67.         c = getc( in );
  68.         if ( !isalpha( c ) ) {
  69.             ungetc( c, in );
  70.             break;
  71.         }
  72.         *w++ = c;
  73.     }
  74.     *w = '\0';
  75. #if defined(DEBUG)
  76.     printf( "%s\n", word );
  77. #endif
  78.     return 0;
  79. }
  80.  
  81. #if !BSEARCH
  82. keyword *ksearch( char *w, keyword *tab, int siz ) {
  83.     keyword *l = tab;
  84.     keyword *r = tab+siz;
  85.     keyword *i;
  86.  
  87.     int cond;
  88.  
  89.     while ( l < r ) {
  90.         i = l + (r-l) / 2;
  91.         cond = strcmp( w, i->word );
  92.         if ( cond < 0 )
  93.             r = i;
  94.         else if ( cond > 0 )
  95.             l = i + 1;
  96.         else
  97.             return i;
  98.     }
  99.     return 0;
  100. }
  101. #endif
  102.  
  103. #if BSEARCH
  104. int keycmp( const char *s, const keyword *k ) {
  105.     return strcmp( s, k->word );
  106. }
  107. #endif
  108.  
  109. int main() {
  110.     char buf[ 128 ];
  111.     keyword *p;
  112.  
  113.     while ( fgetword( stdin, buf, sizeof (buf) ) != EOF )
  114. #if !BSEARCH
  115.         if ( (p = ksearch( buf, keytab, NKEYS)) != 0 )
  116. #else
  117.         if ( (p = bsearch( buf, keytab, NKEYS, sizeof( keyword ), keycmp )) != 0 )
  118. #endif
  119.             p->count++;
  120.  
  121.     for ( p = keytab; p < keytab+NKEYS; p++ )
  122.         if ( p->count > 0 )
  123.             printf( "%d\t%s\n", p->count, p->word );
  124.  
  125.     exit( 0);
  126. }
$ gcc -o ckw ckw.c
$ ./ckw < ckw.c
1	break
6	char
2	const
3	else
1	for
12	if
7	int
5	return
4	sizeof
1	struct
1	typedef
1	unsigned
4	while

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].