-
Notifications
You must be signed in to change notification settings - Fork 0
/
toysql_lexer6.c
104 lines (66 loc) · 2.06 KB
/
toysql_lexer6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* toysql_lexer.c */
/* The beginnings of a lexer for "toy SQL". */
/* This code is released to the public domain. */
/* "Share and enjoy...." ;) */
/* Note - use one-char lookahead. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <stdbool.h>
/* Token */
typedef struct {
char *toktype ;
char value[20];
} Token;
/* Helper function to get the type of a char. */
/* What we mean by "type" here is the "type" that */
/* the char can be used to create. */
/* For example: letters, digits, underscores can be used */
/* to create keywords or column names. */
/* digits can be used to create integers. */
/* Single chars are operators of some kind. */
/* Double chars are also operators. */
int type(char c)
{
/* NOTE - column names can have letters, digits or */
/* underscores. We separate the digit test out so that */
/* in order for a char to fit the column-name pattern, */
/* the test will simply be - */
/* while ( (type == 1) || (type == 2) ) */
if ( ( isalpha(c) ) || ( c == '_' ) ) return 1;
else if ( ( isdigit(c) ) ) return 2;
else if ( c == (',') ) return 3;
else if ( c == (';') ) return 4;
else if ( c == ('"') ) return 5;
else if ( c == ('=') ) return 6;
else if ( c == ('<') ) return 7;
else if ( c == ('>') ) return 8;
else if ( c == ('\0') ) return 9;
else return 10;
}
char *test(char *str)
{
int i=0;
char tok[20];
int type;
}
/* NOTE - need an array of tokens. */
void tokenise(char *str)
{
int i=0;
char c;
while ( c != '\0')
{
c = str[i];
printf("c: %c \n", c);
i++;
} /* *str != '\0' */
}
int main()
{
char *test = "select foo" ;
tokenise(test);
return 0;
}