Blob


1 /* lex [-[dynvt]] [file] ... [file] */
3 /* Copyright 1976, Bell Telephone Laboratories, Inc.,
4 written by Eric Schmidt, August 27, 1976 */
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;
17 char *cname;
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 int eof;
36 int lgatflg;
37 int divflg;
38 int funcflag;
39 int pflag;
40 int chset; /* 1 = char set modified */
41 Biobuf *fin, *fother;
42 int fptr;
43 int *name;
44 int *left;
45 int *right;
46 int *parent;
47 uchar *nullstr;
48 int tptr;
49 uchar pushc[TOKENSIZE];
50 uchar slist[STARTSIZE];
51 uchar **def, **subs, *dchar;
52 uchar **sname, *stchar;
53 uchar *ccl;
54 uchar *ccptr;
55 uchar *dp, *sp;
56 int dptr;
57 uchar *bptr; /* store input position */
58 uchar *tmpstat;
59 int count;
60 int **foll;
61 int *nxtpos;
62 int *positions;
63 int *gotof;
64 int *nexts;
65 uchar *nchar;
66 int **state;
67 int *sfall; /* fallback state num */
68 uchar *cpackflg; /* true if state has been character packed */
69 int *atable;
70 int nptr;
71 uchar symbol[NCH];
72 uchar cindex[NCH];
73 int xstate;
74 int stnum;
75 uchar match[NCH];
76 uchar extra[NACTIONS];
77 uchar *pchar, *pcptr;
78 int pchlen = TOKENSIZE;
79 long rcount;
80 int *verify, *advance, *stoff;
81 int scon;
82 uchar *psave;
84 static void free1core(void);
85 static void free2core(void);
86 #ifdef DEBUG
87 static void free3core(void);
88 #endif
89 static void get1core(void);
90 static void get2core(void);
91 static void get3core(void);
93 void
94 main(int argc, char **argv)
95 {
96 int i;
98 cname = unsharp("#9/lib/lex.ncform");
100 ARGBEGIN {
101 # ifdef DEBUG
102 case 'd': debug++; break;
103 case 'y': yydebug = TRUE; break;
104 # endif
105 case 't': case 'T':
106 Binit(&fout, 1, OWRITE);
107 errorf= 2;
108 foutopen = 1;
109 break;
110 case 'v': case 'V':
111 report = 1;
112 break;
113 case 'n': case 'N':
114 report = 0;
115 break;
116 case '9':
117 nine = 1;
118 break;
119 default:
120 warning("Unknown option %c", ARGC());
121 } ARGEND
122 sargc = argc;
123 sargv = argv;
124 if (argc > 0){
125 fin = Bopen(argv[fptr++], OREAD);
126 if(fin == 0)
127 error ("Can't read input file %s",argv[0]);
128 sargc--;
129 sargv++;
131 else {
132 fin = myalloc(sizeof(Biobuf), 1);
133 if(fin == 0)
134 exits("core");
135 Binit(fin, 0, OREAD);
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 */
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);
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",cname);
183 while ( (i=Bgetc(fother)) != Beof)
184 Bputc(&fout, i);
186 Bterm(fother);
187 Bterm(&fout);
188 if(
189 # ifdef DEBUG
190 debug ||
191 # endif
192 report == 1)statistics();
193 Bterm(fin);
194 exits(0); /* success return code */
197 static void
198 get1core(void)
200 ccptr = ccl = myalloc(CCLSIZE,sizeof(*ccl));
201 pcptr = pchar = myalloc(pchlen, sizeof(*pchar));
202 def = myalloc(DEFSIZE,sizeof(*def));
203 subs = myalloc(DEFSIZE,sizeof(*subs));
204 dp = dchar = myalloc(DEFCHAR,sizeof(*dchar));
205 sname = myalloc(STARTSIZE,sizeof(*sname));
206 sp = stchar = myalloc(STARTCHAR,sizeof(*stchar));
207 if(ccl == 0 || def == 0 || subs == 0 || dchar == 0 || sname == 0 || stchar == 0)
208 error("Too little core to begin");
211 static void
212 free1core(void)
214 free(def);
215 free(subs);
216 free(dchar);
219 static void
220 get2core(void)
222 int i;
224 gotof = myalloc(nstates,sizeof(*gotof));
225 nexts = myalloc(ntrans,sizeof(*nexts));
226 nchar = myalloc(ntrans,sizeof(*nchar));
227 state = myalloc(nstates,sizeof(*state));
228 atable = myalloc(nstates,sizeof(*atable));
229 sfall = myalloc(nstates,sizeof(*sfall));
230 cpackflg = myalloc(nstates,sizeof(*cpackflg));
231 tmpstat = myalloc(tptr+1,sizeof(*tmpstat));
232 foll = myalloc(tptr+1,sizeof(*foll));
233 nxtpos = positions = myalloc(maxpos,sizeof(*positions));
234 if(tmpstat == 0 || foll == 0 || positions == 0 ||
235 gotof == 0 || nexts == 0 || nchar == 0 || state == 0 || atable == 0 || sfall == 0 || cpackflg == 0 )
236 error("Too little core for state generation");
237 for(i=0;i<=tptr;i++)foll[i] = 0;
240 static void
241 free2core(void)
243 free(positions);
244 free(tmpstat);
245 free(foll);
246 free(name);
247 free(left);
248 free(right);
249 free(parent);
250 free(nullstr);
251 free(state);
252 free(sname);
253 free(stchar);
254 free(ccl);
257 static void
258 get3core(void)
260 verify = myalloc(outsize,sizeof(*verify));
261 advance = myalloc(outsize,sizeof(*advance));
262 stoff = myalloc(stnum+2,sizeof(*stoff));
263 if(verify == 0 || advance == 0 || stoff == 0)
264 error("Too little core for final packing");
266 # ifdef DEBUG
267 static void
268 free3core(void){
269 free(advance);
270 free(verify);
271 free(stoff);
272 free(gotof);
273 free(nexts);
274 free(nchar);
275 free(atable);
276 free(sfall);
277 free(cpackflg);
279 # endif
280 void *
281 myalloc(int a, int b)
283 void *i;
284 i = calloc(a, b);
285 if(i==0)
286 warning("OOPS - calloc returns a 0");
287 return(i);
290 void
291 yyerror(char *s)
293 fprint(2, "line %d: %s\n", yyline, s);