forked from RealNeGate/Cuik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexgen.c
153 lines (126 loc) · 4.16 KB
/
lexgen.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
static uint8_t table[256][32];
static void emit_range(uint64_t min, uint64_t max, uint64_t old, uint64_t next) {
for (size_t i = min; i <= max; i++) {
table[i][old] = next;
}
}
static void emit_chars(const char* str, uint64_t old, uint64_t next) {
for (; *str; str++) {
table[(unsigned char) *str][old] = next;
}
}
static void emit_all_chars(const char* str, uint64_t old, uint64_t next) {
for (size_t i = 0; i < 256; i++) {
table[i][old] = next;
}
}
// new state
static int table_id_counter = 1;
static int ns(void) {
return table_id_counter++;
}
#define RANGE(old, new, ...) range_pattern(old, new, sizeof((const char*[]){ __VA_ARGS__ }) / sizeof(const char*), (const char*[]){ __VA_ARGS__ })
static uint64_t range_pattern(uint64_t old, uint64_t new, int c, const char* ranges[]) {
for (int i = 0; i < c; i++) {
unsigned char min = ranges[i][0], max = ranges[i][1];
if (max == 0) {
table[min][old] = new;
} else {
for (int j = min; j <= max; j++) {
table[j][old] = new;
}
}
}
return new;
}
#define CHARS(old, new, ...) chars_pattern(old, new, __VA_ARGS__)
static uint64_t chars_pattern(uint64_t old, uint64_t new, const char* str) {
emit_chars(str, old, new);
return new;
}
int main(int argc, char** argv) {
if (argc <= 1) {
fprintf(stderr, "requires output path!\n");
return 1;
}
int ident = ns();
RANGE(0, ident, "AZ", "az", "_", "$", "\x80\xFF", "\\");
RANGE(ident, ident, "AZ", "az", "_", "$", "09", "\x80\xFF", "\\");
int num = ns(), floats = ns(), suffix = ns();
{
RANGE(0, num, "09");
RANGE(num, num, "09");
CHARS(num, suffix, "bBxXuUiIlLfFdD");
RANGE(suffix, suffix, "AZ", "az", "09");
CHARS(suffix, floats, ".");
CHARS(num, floats, ".");
CHARS(floats, floats, "0123456789e+-");
CHARS(floats, suffix, "UILFDuilfd");
}
CHARS(0, ns(), "@?;:,(){}");
// *= /= %= != &= ^= ~=
int ops = CHARS(0, ns(), "*/%!=&^~");
int eq = CHARS(ops, ns(), "=");
// >>= <<= >= <= > <
int s1 = CHARS(0, ns(), "><");
int s2 = CHARS(s1, ns(), "><");
int s3 = CHARS(s2, ns(), "=");
CHARS(s1, eq, "=");
// - -= -> --
CHARS(CHARS(0, ns(), "-"), ns(), "=>-");
int hash = CHARS(0, ns(), "&");
CHARS(hash, ns(), "&");
CHARS(hash, eq, "=");
CHARS(CHARS(0, ns(), "+"), ns(), "+");
CHARS(CHARS(0, ns(), "#"), ns(), "#");
CHARS(CHARS(0, ns(), "["), ns(), "[");
CHARS(CHARS(0, ns(), "]"), ns(), "]");
int plus = CHARS(0, ns(), "+"), plus_end = ns();
CHARS(plus, plus_end, "+");
CHARS(plus, plus_end, "=");
int pipe = CHARS(0, ns(), "|"), pipe_end = ns();
CHARS(pipe, pipe_end, "|");
CHARS(pipe, pipe_end, "=");
int dot = CHARS(0, ns(), ".");
CHARS(dot, dot, ".");
int str = CHARS(0, ns(), "\'\"");
// string
int wide_str = CHARS(0, ns(), "L");
CHARS(wide_str, str, "\'\"");
// wide string
RANGE(
wide_str, ident,
"AZ", "az", "_", "$", "09", "\x80\xFF", "\\",
);
if (table_id_counter >= 32) {
fprintf(stderr, "Failed to generate DFA (too many states)\n");
return 1;
}
FILE* file = fopen(argv[1], "wb");
fprintf(file, "enum {\n");
fprintf(file, " DFA_IDENTIFIER = %d,\n", ident);
fprintf(file, " DFA_NUMBER0 = %d,\n", num);
fprintf(file, " DFA_STRING = %d,\n", str);
fprintf(file, " DFA_NUMBER1 = %d,\n", floats);
fprintf(file, " DFA_NUMBER2 = %d,\n", suffix);
fprintf(file, " DFA_IDENTIFIER_L = %d,\n", wide_str);
fprintf(file, "};\n");
fprintf(file, "static uint8_t dfa[256][32] = {\n");
for (int i = 0; i < 256; i++) {
fprintf(file, " { ");
for (int j = 0; j < 32; j++) {
if (j) fprintf(file, ", ");
fprintf(file, "0x%02x", table[i][j]);
}
fprintf(file, " },\n");
}
fprintf(file, "};\n");
fclose(file);
return 0;
}