File: keybld.c

package info (click to toggle)
a56 1.3%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 352 kB
  • sloc: ansic: 3,471; yacc: 1,888; makefile: 120; awk: 4
file content (231 lines) | stat: -rwxr-xr-x 4,524 bytes parent folder | download | duplicates (3)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright (C) 1990-1994 Quinn C. Jensen
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  The author makes no representations
 * about the suitability of this software for any purpose.  It is
 * provided "as is" without express or implied warranty.
 *
 */
static char *Copyright = "Copyright (C) 1990-1994 Quinn C. Jensen";

/*
 * keybld - builds a finite-state parser for the given keyword list
 *
 */

#include <stdio.h>
#include "a56.h"

char buf[1024];

main()
{
	int line = 0;

	while(gets(buf)) {
		char *bp = buf;
		line++;
		while(*bp != '\t' && *bp != ' ') bp++;
		*bp++ = '\0';
		while(*bp == '\t' || *bp == ' ') bp++;
		if(strcmp(buf, ".code") == 0) {
			printf("%s\n", bp);
		} else if(add_tok(buf, bp) == -1) {
			fprintf(stderr, "input line %d: ambiguous\n", line);
		}
	}

	dump_machine();
	return 0;
}

#define MAX_CHAR 'z'

#define TRANSITIONS (MAX_CHAR + 1)

struct state {
	int number;
	char *seen;
	struct trans {
		char action;
		void *arg;
	} trans[TRANSITIONS];
	struct state *next;
} empty_state, *stop = NULL, *scur = NULL, *new_state();
int n_states = 0;

/* actions */
#define NOMATCH 0	/* argument is nothing */
#define GOTO 1		/* argument is target state */
#define TERMINAL 2	/* argument is which user action to perform */

struct user_action {
	char *action;
	struct user_action *next;
} *utop = NULL, *ucur = NULL;
int n_user_actions = 0;

add_tok(tok, actions)
char *tok, *actions;
{
	struct state *scur;
	struct user_action *unew = (struct user_action *)alloc(sizeof *unew);
	unew->action = strsave(actions);
	unew->next = NULL;
	if(ucur)
		ucur->next = unew;
	ucur = unew;
	if(utop == NULL)
		utop = unew;

	if(stop == NULL)
		new_state(NULL);

	if(follow(*tok, tok + 1, stop) == -1)
		return -1;

	n_user_actions++;
	return 0;
}

follow(c, tp, sp)
char c;
char *tp;
struct state *sp;
{
	struct trans *arcp, *arcup;
	
	if(c >= 'a' && c <= 'z') {
		c -= 'a' - 'A';
	}
	arcp = sp->trans + c;

	if(c >= 'A' && c <= 'Z') {
		arcup = sp->trans + c + 'a' - 'A';
	} else {
		arcup = arcp;
	}

	if(c == '\0') {
		if(arcp->action == TERMINAL) {
			return -1;
		} else {
			arcp->action = arcup->action = TERMINAL;
			arcp->arg = arcup->arg = (void *)n_user_actions;
			return 0;
		}
	} else {
		if(arcp->action == GOTO) {
			return follow(*tp, tp + 1, arcp->arg);
		} else {
			struct state *new = new_state(tp);
			arcp->action = arcup->action = GOTO;
			arcp->arg = arcup->arg = (void *)new;
			return follow(*tp, tp + 1, new);
		}
	}
}

struct state *new_state(tp)
char *tp;
{
	struct state *snew = (struct state *)alloc(sizeof *snew);
	char tmp[1024];

	*snew = empty_state;

	snew->number = n_states++;

	snew->next = NULL;

	if(scur)
		scur->next = snew;
	scur = snew;

	if(stop == NULL)
		stop = snew;

	if(tp) {
		strncpy(tmp, buf, tp - buf);
		tmp[tp - buf] = '\0';
	} else
		strcpy(tmp, "<nothing>");

	snew->seen = strsave(tmp);

	return snew;
}

dump_machine()
{
	struct state *sp;
	struct user_action *up;
	int n, a;

	printf("/* state machine generated by keybld */\n");
	printf("/* states=%d actions=%d */\n", n_states, n_user_actions);
	printf("/* %d bytes required for transition table storage */\n",
		sizeof(short) * TRANSITIONS * n_states);

	printf("short transitions[%d][%d] = {\n", n_states, TRANSITIONS);
	for(n = 0, sp = stop; sp; sp = sp->next, n++) {
		printf("	/* state %d: \"%s\" */\n", n, sp->seen);
		printf("	{");
		for(a = 0; a < TRANSITIONS; a++) {
			struct trans *tp = sp->trans + a;
			switch(tp->action) {
			case GOTO:
				printf("%d", ((struct state *)tp->arg)->number);
				break;
			case TERMINAL:
				printf("%d", -(int)tp->arg - 1);
				break;
			case NOMATCH:
				printf("0");
				break;
			}
			printf(",%s", a % 20 == 19 ? "\n\t\t" : "");
		};
		printf("},\n");
	}

	printf("};\n");

	printf("\
\n\
kparse(kp)\n\
char *kp;\n\
{\n\
	int state = 0;\n\
\n\
	for(;;) {\n\
		short transition = transitions[state][*kp];\n");

printf("\
		if(transition == 0) {\n\
			return 0;\n\
		} else if(transition > 0) {\n\
			kp++;\n\
			state = transition;\n\
		} else {\n\
			switch(-transition) {\n");

	for(n = 1, up = utop; up; up = up->next, n++) {
		printf("\
			case %d:\n\
				%s;\n\
				break;\n", n, up->action);
	}

	printf("\
			}\n\
			return transition;\n\
		}\n\
	}\n\
}\n");

}