Code:
/* lex.c */
/* This code is released to the public domain. */
/* "Share and enjoy......" :) */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
/* Array of our keywords in string form. */
char *kw_strings[10] =
{
"select", "from", "where", "and", "or", "not", "in", "is", "null"
} ;
/* Search function to search the array of keywords. */
int search(char *arr[], int dim, char *str)
{
int i;
int found_match;
for (i=0; i<dim; i++)
{
if ( !strcmp(arr[i], str ) )
{
found_match = 1;
break;
}
else
found_match = 0;
} /* For */
return found_match;
} /* search */
void print_token(char *str)
{
char token[80];
char *toktype;
int i=0;
char c;
while (*str != '\0')
{
c = str[i];
/* Keyword or identifier */
if (isalpha(c) || c == '_')
{
while (isalnum(c) || c == '_')
{
token[i] = c;
str++;
i++;
}
if (search(kw_strings, 10, token) == 1 )
toktype = "Keyword";
else
toktype "Identifier" ;
printf("%s %s\n", toktype, token);
}
else if (ispunct(c))
{
toktype = "operator";
printf("%s %s\n", toktype, token);
str++;
i++;
}
else if (isspace(c))
{
str++;
i++;
}
str++;
i++;
} // while
} // print_token
int main()
{
char *mystr = "select var1 from mytable ; " ;
print_token(mystr);
return 0;
}
Code:
C:\Users\stahta01\devel\open_source_code\no_version_control\Test\testsql\main.c:64:17: warning: statement with no effect [-Wunused-value]
toktype "Identifier" ;
^
C:\Users\stahta01\devel\open_source_code\no_version_control\Test\testsql\main.c:64:25: error: expected ';' before string constant
toktype "Identifier" ;
Tim S.