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)
55 if (lextable[pos] & (1 << 7)) { /* 1-byte, fail on mismatch */
56 if ((lextable[pos] & 0x7f) != c)
60 if (lextable[pos] == FAIL_CHAR)
63 } else { /* b7 = 0, end or 3-byte */
64 if (lextable[pos] < FAIL_CHAR) /* terminal marker */
67 if (lextable[pos] == c) /* goto */
68 return pos + (lextable[pos + 1]) +
69 (lextable[pos + 2] << 8);
89 while (n < sizeof(set) / sizeof(set[0])) {
95 if (set[n][0] == '\0') {
103 for (y = 0; y < state[walk].count; y++)
104 if (state[walk].c[y] == set[n][m]) {
105 /* exists -- go forward */
106 walk = state[walk].state[y];
114 /* something we didn't see before */
116 state[walk].c[state[walk].count] = set[n][m];
118 state[walk].state[state[walk].count] = next;
125 state[walk].c[0] = n++;
126 state[walk].state[0] = 0; /* terminal marker */
127 state[walk].count = 1;
131 for (n = 0; n < next; n++) {
132 state[n].bytepos = walk;
133 walk += (2 * state[n].count);
136 /* compute everyone's position first */
140 for (n = 0; n < next; n++) {
142 state[n].real_pos = pos;
144 for (m = 0; m < state[n].count; m++) {
146 if (state[n].state[m] == 0)
147 pos += 2; /* terminal marker */
148 else { /* c is a character */
149 if ((state[state[n].state[m]].bytepos -
154 if (m == state[n].count - 1)
164 for (n = 0; n < next; n++) {
165 for (m = 0; m < state[n].count; m++) {
168 fprintf(stdout, "/* pos %04x: %3d */ ",
169 state[n].real_pos, n);
171 fprintf(stdout, " ");
174 saw = state[n].state[m];
176 if (saw == 0) { // c is a terminal then
179 fprintf(stderr, "terminal too big\n");
183 fprintf(stdout, " 0x%02X, 0x%02X "
185 "/* - terminal marker %2d - */,\n",
186 y >> 8, y & 0xff, y & 0x7f);
192 /* c is a character */
195 if (prev < 32 || prev > 126)
199 if ((state[saw].bytepos - walk) == 2) {
200 fprintf(stdout, " 0x%02X /* '%c' -> */,\n",
207 j = state[saw].real_pos - pos;
211 "Jump > 64K bytes ahead (%d to %d)\n",
212 state[n].real_pos, state[saw].real_pos);
215 fprintf(stdout, " 0x%02X /* '%c' */, 0x%02X, 0x%02X "
216 "/* (to 0x%04X state %3d) */,\n",
219 state[saw].real_pos, saw);
222 if (m == state[n].count - 1) {
224 " 0x%02X, /* fail */\n",
233 fprintf(stdout, "/* total size %d bytes */\n", pos);
236 * Try to parse every legal input string
239 for (n = 0; n < sizeof(set) / sizeof(set[0]); n++) {
244 if (set[n][0] == '\0')
247 fprintf(stderr, " trying '%s'\n", set[n]);
250 walk = lextable_decode(walk, set[n][m]);
252 fprintf(stderr, "failed\n");
256 if (lextable[walk] < FAIL_CHAR) {
257 y = (lextable[walk] << 8) + lextable[walk + 1];
264 fprintf(stderr, "decode failed %d\n", y);
269 fprintf(stderr, "All decode OK\n");