40 lines
868 B
C
40 lines
868 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static const char *keywords[] = {
|
|
"auto",
|
|
"break",
|
|
"case",
|
|
"char",
|
|
/* . */
|
|
/* . */
|
|
/* . */
|
|
"while"
|
|
};
|
|
|
|
#define NUM_KW sizeof(keywords) / sizeof(char *)
|
|
|
|
int kw_compare( const void *p1, const void *p2 )
|
|
{
|
|
const char *p1c = (const char *) p1;
|
|
const char **p2c = (const char **) p2;
|
|
return( strcmp( p1c, *p2c ) );
|
|
}
|
|
|
|
int keyword_lookup( const char *name )
|
|
{
|
|
const char **key;
|
|
key = (char const **) bsearch( name, keywords, NUM_KW,
|
|
sizeof( char * ), kw_compare );
|
|
if( key == NULL ) return( -1 );
|
|
return key - keywords;
|
|
}
|
|
|
|
void main()
|
|
{
|
|
printf( "%d\n", keyword_lookup( "case" ) );
|
|
printf( "%d\n", keyword_lookup( "crigger" ) );
|
|
printf( "%d\n", keyword_lookup( "auto" ) );
|
|
}
|