-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample3_traditional.c
51 lines (51 loc) · 1.4 KB
/
example3_traditional.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
#include <ctype.h> // isdigit isalpha
#include <stdbool.h> // bool
#include <stdio.h> // printf
#include <string.h> // strlen
void process_string(char *value) { printf("String processed: %s\n", value); }
void process_number(char *value) { printf("Number processed: %s\n", value); }
bool lex(const char *value) {
bool success = false;
while (*value) {
const unsigned int temp_length = 42;
char temp[temp_length];
char *tptr = temp;
unsigned int length = 0;
if (isalpha(*value)) {
while (isalpha(*value)) {
*tptr = *value;
tptr++;
value++;
length++;
}
*tptr = '\0';
tptr -= length;
process_string(tptr);
continue;
} else if (isdigit(*value)) {
while (isdigit(*value)) {
*tptr = *value;
tptr++;
value++;
length++;
}
*tptr = '\0';
tptr -= length;
process_number(tptr);
continue;
}
// Unexpected data, skip
value++;
success = true;
}
return success;
}
int main() {
char value[] = "1337*cat+42-dog_1984/pony";
if (lex(value)) {
printf("Lex result: %s\n", "success!");
} else {
printf("Lex result: %s\n", "failure!");
}
return 0;
}