Subversion Repositories planix.SVN

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
/* lex [-[dynvt]] [file] ... [file] */
2
 
3
/* Copyright 1976, Bell Telephone Laboratories, Inc.,
4
   written by Eric Schmidt, August 27, 1976   */
5
 
6
# include "ldefs.h"
7
Biobuf	fout;
8
int	foutopen;
9
int	errorf = 1;
10
int	sect = DEFSECTION;
11
int	prev = '\n';	/* previous input character */
12
int	pres = '\n';	/* present input character */
13
int	peek = '\n';	/* next input character */
14
uchar	*pushptr = pushc;
15
uchar	*slptr = slist;
16
 
17
char	*cname = "/sys/lib/lex/ncform";
18
 
19
int nine;
20
int ccount = 1;
21
int casecount = 1;
22
int aptr = 1;
23
int nstates = NSTATES, maxpos = MAXPOS;
24
int treesize = TREESIZE, ntrans = NTRANS;
25
int yytop;
26
int outsize = NOUTPUT;
27
int sptr = 1;
28
int report = 2;
29
int debug;		/* 1 = on */
30
int charc;
31
int sargc;
32
char **sargv;
33
uchar buf[520];
34
int yyline;		/* line number of file */
35
char *yyfile;		/* filename for error messages */
36
int eof;
37
int lgatflg;
38
int divflg;
39
int funcflag;
40
int pflag;
41
int chset;	/* 1 = char set modified */
42
Biobuf *fin = 0, *fother;
43
int fptr;
44
int *name;
45
int *left;
46
int *right;
47
int *parent;
48
uchar *nullstr;
49
uchar **ptr;
50
int tptr;
51
uchar pushc[TOKENSIZE];
52
uchar slist[STARTSIZE];
53
uchar **def, **subs, *dchar;
54
uchar **sname, *stchar;
55
uchar *ccl;
56
uchar *ccptr;
57
uchar *dp, *sp;
58
int dptr;
59
uchar *bptr;		/* store input position */
60
uchar *tmpstat;
61
int count;
62
int **foll;
63
int *nxtpos;
64
int *positions;
65
int *gotof;
66
int *nexts;
67
uchar *nchar;
68
int **state;
69
int *sfall;		/* fallback state num */
70
uchar *cpackflg;		/* true if state has been character packed */
71
int *atable;
72
int nptr;
73
uchar symbol[NCH];
74
uchar cindex[NCH];
75
int xstate;
76
int stnum;
77
uchar match[NCH];
78
uchar extra[NACTIONS];
79
uchar *pchar, *pcptr;
80
int pchlen = TOKENSIZE;
81
 long rcount;
82
int *verify, *advance, *stoff;
83
int scon;
84
uchar *psave;
85
 
86
static void	free1core(void);
87
static void	free2core(void);
88
static void	free3core(void);
89
static void	get1core(void);
90
static void	get2core(void);
91
static void	get3core(void);
92
 
93
void
94
main(int argc, char **argv)
95
{
96
	int i;
97
 
98
	ARGBEGIN {
99
# ifdef DEBUG
100
		case 'd': debug++; break;
101
		case 'y': yydebug = TRUE; break;
102
# endif
103
		case 't': case 'T':
104
			Binit(&fout, 1, OWRITE);
105
			errorf= 2;
106
			foutopen = 1;
107
			break;
108
		case 'v': case 'V':
109
			report = 1;
110
			break;
111
		case 'n': case 'N':
112
			report = 0;
113
			break;
114
		case '9':
115
			nine = 1;
116
			break;
117
		default:
118
			warning("Unknown option %c", ARGC());
119
	} ARGEND
120
	sargc = argc;
121
	sargv = argv;
122
	if (argc > 0){
123
		yyfile = argv[fptr++];
124
		fin = Bopen(yyfile, OREAD);
125
		if(fin == 0)
126
			error ("%s - can't open file: %r", yyfile);
127
		sargc--;
128
		sargv++;
129
	}
130
	else {
131
		yyfile = "/fd/0";
132
		fin = myalloc(sizeof(Biobuf), 1);
133
		if(fin == 0)
134
			exits("core");
135
		Binit(fin, 0, OREAD);
136
	}
137
	if(Bgetc(fin) == Beof)		/* no input */
138
		exits(0);
139
	Bseek(fin, 0, 0);
140
	gch();
141
		/* may be gotten: def, subs, sname, stchar, ccl, dchar */
142
	get1core();
143
		/* may be gotten: name, left, right, nullstr, parent, ptr */
144
	strcpy((char*)sp, "INITIAL");
145
	sname[0] = sp;
146
	sp += strlen("INITIAL") + 1;
147
	sname[1] = 0;
148
	if(yyparse()) exits("error");	/* error return code */
149
		/* may be disposed of: def, subs, dchar */
150
	free1core();
151
		/* may be gotten: tmpstat, foll, positions, gotof, nexts, nchar, state, atable, sfall, cpackflg */
152
	get2core();
153
	ptail();
154
	mkmatch();
155
# ifdef DEBUG
156
	if(debug) pccl();
157
# endif
158
	sect  = ENDSECTION;
159
	if(tptr>0)cfoll(tptr-1);
160
# ifdef DEBUG
161
	if(debug)pfoll();
162
# endif
163
	cgoto();
164
# ifdef DEBUG
165
	if(debug){
166
		print("Print %d states:\n",stnum+1);
167
		for(i=0;i<=stnum;i++)stprt(i);
168
		}
169
# endif
170
		/* may be disposed of: positions, tmpstat, foll, state, name, left, right, parent, ccl, stchar, sname */
171
		/* may be gotten: verify, advance, stoff */
172
	free2core();
173
	get3core();
174
	layout();
175
		/* may be disposed of: verify, advance, stoff, nexts, nchar,
176
			gotof, atable, ccpackflg, sfall */
177
# ifdef DEBUG
178
	free3core();
179
# endif
180
	fother = Bopen(cname,OREAD);
181
	if(fother == 0)
182
		error("Lex driver missing, file %s: %r",cname);
183
	while ( (i=Bgetc(fother)) != Beof)
184
		Bputc(&fout, i);
185
 
186
	Bterm(fother);
187
	Bterm(&fout);
188
	if(
189
# ifdef DEBUG
190
		debug   ||
191
# endif
192
			report == 1)statistics();
193
	if (fin)
194
		Bterm(fin);
195
	exits(0);	/* success return code */
196
}
197
 
198
static void
199
get1core(void)
200
{
201
	ccptr =	ccl = myalloc(CCLSIZE,sizeof(*ccl));
202
	pcptr = pchar = myalloc(pchlen, sizeof(*pchar));
203
	def = myalloc(DEFSIZE,sizeof(*def));
204
	subs = myalloc(DEFSIZE,sizeof(*subs));
205
	dp = dchar = myalloc(DEFCHAR,sizeof(*dchar));
206
	sname = myalloc(STARTSIZE,sizeof(*sname));
207
	sp = stchar = myalloc(STARTCHAR,sizeof(*stchar));
208
	if(ccl == 0 || def == 0 || subs == 0 || dchar == 0 || sname == 0 || stchar == 0)
209
		error("Too little core to begin");
210
}
211
 
212
static void
213
free1core(void)
214
{
215
	free(def);
216
	free(subs);
217
	free(dchar);
218
}
219
 
220
static void
221
get2core(void)
222
{
223
	int i;
224
 
225
	gotof = myalloc(nstates,sizeof(*gotof));
226
	nexts = myalloc(ntrans,sizeof(*nexts));
227
	nchar = myalloc(ntrans,sizeof(*nchar));
228
	state = myalloc(nstates,sizeof(*state));
229
	atable = myalloc(nstates,sizeof(*atable));
230
	sfall = myalloc(nstates,sizeof(*sfall));
231
	cpackflg = myalloc(nstates,sizeof(*cpackflg));
232
	tmpstat = myalloc(tptr+1,sizeof(*tmpstat));
233
	foll = myalloc(tptr+1,sizeof(*foll));
234
	nxtpos = positions = myalloc(maxpos,sizeof(*positions));
235
	if(tmpstat == 0 || foll == 0 || positions == 0 ||
236
		gotof == 0 || nexts == 0 || nchar == 0 || state == 0 || atable == 0 || sfall == 0 || cpackflg == 0 )
237
		error("Too little core for state generation");
238
	for(i=0;i<=tptr;i++)foll[i] = 0;
239
}
240
 
241
static void
242
free2core(void)
243
{
244
	free(positions);
245
	free(tmpstat);
246
	free(foll);
247
	free(name);
248
	free(left);
249
	free(right);
250
	free(parent);
251
	free(nullstr);
252
	free(ptr);
253
	free(state);
254
	free(sname);
255
	free(stchar);
256
	free(ccl);
257
}
258
 
259
static void
260
get3core(void)
261
{
262
	verify = myalloc(outsize,sizeof(*verify));
263
	advance = myalloc(outsize,sizeof(*advance));
264
	stoff = myalloc(stnum+2,sizeof(*stoff));
265
	if(verify == 0 || advance == 0 || stoff == 0)
266
		error("Too little core for final packing");
267
}
268
# ifdef DEBUG
269
static void
270
free3core(void){
271
	free(advance);
272
	free(verify);
273
	free(stoff);
274
	free(gotof);
275
	free(nexts);
276
	free(nchar);
277
	free(atable);
278
	free(sfall);
279
	free(cpackflg);
280
}
281
# endif
282
void *
283
myalloc(int a, int b)
284
{
285
	void *i;
286
	i = calloc(a, b);
287
	if(i==0)
288
		warning("OOPS - calloc returns a 0");
289
	return(i);
290
}
291
 
292
void
293
yyerror(char *s)
294
{
295
	fprint(2, "%s:%d %s\n", yyfile, yyline, s);
296
}