-
Notifications
You must be signed in to change notification settings - Fork 0
/
example2_new.c
46 lines (46 loc) · 1.24 KB
/
example2_new.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
#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(char *value) {
return ifwhile (*value) {
char ctemp = 0;
unsigned int length = 0;
success = while (isalpha(*value)) {
value++;
length++;
}{
ctemp = *value;
*value = '\0';
value -= length;
process_string(value);
value += length;
*value = ctemp;
} else ifwhile (isdigit(*value)) {
value++;
length++;
}{
ctemp = *value;
*value = '\0';
value -= length;
process_number(value);
value += length;
*value = ctemp;
} else {
// Unexpected data, skip
value++;
}
length = 0;
}
}
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;
}