4 * High efficiency lexical state parser
6 * Copyright (C)2011-2014 Andy Green <andy@warmcat.com>
10 * Usage: gcc minilex.c -o minilex && ./minilex > lextable.h
12 * Run it twice to test parsing on the generated table on stderr
19 #include "lextable-strings.h"
25 * 0x00 - 0x07, then terminal as given in 2nd byte
27 * no match: go fwd 3 byte, match: jump fwd by amt in +1/+2 bytes
29 * no match: die, match go fwd 1 byte
32 unsigned char lextable[] = {
47 struct state state[1000];
50 #define FAIL_CHAR 0x08
52 int lextable_decode(int pos, char c)
56 if (lextable[pos] & (1 << 7)) { /* 1-byte, fail on mismatch */
57 if ((lextable[pos] & 0x7f) != c)
61 if (lextable[pos] == FAIL_CHAR)
64 } else { /* b7 = 0, end or 3-byte */
65 if (lextable[pos] < FAIL_CHAR) /* terminal marker */
68 if (lextable[pos] == c) /* goto */
69 return pos + (lextable[pos + 1]) +
70 (lextable[pos + 2] << 8);
90 while (n < sizeof(set) / sizeof(set[0])) {
96 if (set[n][0] == '\0') {
104 for (y = 0; y < state[walk].count; y++)
105 if (state[walk].c[y] == set[n][m]) {
106 /* exists -- go forward */
107 walk = state[walk].state[y];
115 /* something we didn't see before */
117 state[walk].c[state[walk].count] = set[n][m];
119 state[walk].state[state[walk].count] = next;
126 state[walk].c[0] = n++;
127 state[walk].state[0] = 0; /* terminal marker */
128 state[walk].count = 1;
132 for (n = 0; n < next; n++) {
133 state[n].bytepos = walk;
134 walk += (2 * state[n].count);
137 /* compute everyone's position first */
141 for (n = 0; n < next; n++) {
143 state[n].real_pos = pos;
145 for (m = 0; m < state[n].count; m++) {
147 if (state[n].state[m] == 0)
148 pos += 2; /* terminal marker */
149 else { /* c is a character */
150 if ((state[state[n].state[m]].bytepos -
155 if (m == state[n].count - 1)
165 for (n = 0; n < next; n++) {
166 for (m = 0; m < state[n].count; m++) {
169 fprintf(stdout, "/* pos %04x: %3d */ ",
170 state[n].real_pos, n);
172 fprintf(stdout, " ");
175 saw = state[n].state[m];
177 if (saw == 0) { // c is a terminal then
180 fprintf(stderr, "terminal too big\n");
184 fprintf(stdout, " 0x%02X, 0x%02X "
186 "/* - terminal marker %2d - */,\n",
187 y >> 8, y & 0xff, y & 0x7f);
193 /* c is a character */
196 if (prev < 32 || prev > 126)
200 if ((state[saw].bytepos - walk) == 2) {
201 fprintf(stdout, " 0x%02X /* '%c' -> */,\n",
208 j = state[saw].real_pos - pos;
212 "Jump > 64K bytes ahead (%d to %d)\n",
213 state[n].real_pos, state[saw].real_pos);
216 fprintf(stdout, " 0x%02X /* '%c' */, 0x%02X, 0x%02X "
217 "/* (to 0x%04X state %3d) */,\n",
220 state[saw].real_pos, saw);
223 if (m == state[n].count - 1) {
225 " 0x%02X, /* fail */\n",
234 fprintf(stdout, "/* total size %d bytes */\n", pos);
237 * Try to parse every legal input string
240 for (n = 0; n < sizeof(set) / sizeof(set[0]); n++) {
245 if (set[n][0] == '\0')
248 fprintf(stderr, " trying '%s'\n", set[n]);
251 walk = lextable_decode(walk, set[n][m]);
253 fprintf(stderr, "failed\n");
257 if (lextable[walk] < FAIL_CHAR) {
258 y = (lextable[walk] << 8) + lextable[walk + 1];
265 fprintf(stderr, "decode failed %d\n", y);
270 fprintf(stderr, "All decode OK\n");