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 uintptr *right;
46 int *parent;
47 uchar *nullstr;
48 uchar **ptr;
49 int tptr;
50 uchar pushc[TOKENSIZE];
51 uchar slist[STARTSIZE];
52 uchar **def, **subs, *dchar;
53 uchar **sname, *stchar;
54 uchar *ccl;
55 uchar *ccptr;
56 uchar *dp, *sp;
57 int dptr;
58 uchar *bptr; /* store input position */
59 uchar *tmpstat;
60 int count;
61 int **foll;
62 int *nxtpos;
63 int *positions;
64 int *gotof;
65 int *nexts;
66 uchar *nchar;
67 int **state;
68 int *sfall; /* fallback state num */
69 uchar *cpackflg; /* true if state has been character packed */
70 int *atable;
71 int nptr;
72 uchar symbol[NCH];
73 uchar cindex[NCH];
74 int xstate;
75 int stnum;
76 uchar match[NCH];
77 uchar extra[NACTIONS];
78 uchar *pchar, *pcptr;
79 int pchlen = TOKENSIZE;
80 long rcount;
81 int *verify, *advance, *stoff;
82 int scon;
83 uchar *psave;
85 static void free1core(void);
86 static void free2core(void);
87 #ifdef DEBUG
88 static void free3core(void);
89 #endif
90 static void get1core(void);
91 static void get2core(void);
92 static void get3core(void);
94 void
95 main(int argc, char **argv)
96 {
97 int i;
99 cname = unsharp("#9/lib/lex.ncform");
101 ARGBEGIN {
102 # ifdef DEBUG
103 case 'd': debug++; break;
104 case 'y': yydebug = TRUE; break;
105 # endif
106 case 't': case 'T':
107 Binit(&fout, 1, OWRITE);
108 errorf= 2;
109 foutopen = 1;
110 break;
111 case 'v': case 'V':
112 report = 1;
113 break;
114 case 'n': case 'N':
115 report = 0;
116 break;
117 case '9':
118 nine = 1;
119 break;
120 default:
121 warning("Unknown option %c", ARGC());
122 } ARGEND
123 sargc = argc;
124 sargv = argv;
125 if (argc > 0){
126 fin = Bopen(argv[fptr++], OREAD);
127 if(fin == 0)
128 error ("Can't read input file %s",argv[0]);
129 sargc--;
130 sargv++;
132 else {
133 fin = myalloc(sizeof(Biobuf), 1);
134 if(fin == 0)
135 exits("core");
136 Binit(fin, 0, OREAD);
138 if(Bgetc(fin) == Beof) /* no input */
139 exits(0);
140 Bseek(fin, 0, 0);
141 gch();
142 /* may be gotten: def, subs, sname, stchar, ccl, dchar */
143 get1core();
144 /* may be gotten: name, left, right, nullstr, parent */
145 strcpy((char*)sp, "INITIAL");
146 sname[0] = sp;
147 sp += strlen("INITIAL") + 1;
148 sname[1] = 0;
149 if(yyparse()) exits("error"); /* error return code */
150 /* may be disposed of: def, subs, dchar */
151 free1core();
152 /* may be gotten: tmpstat, foll, positions, gotof, nexts, nchar, state, atable, sfall, cpackflg */
153 get2core();
154 ptail();
155 mkmatch();
156 # ifdef DEBUG
157 if(debug) pccl();
158 # endif
159 sect = ENDSECTION;
160 if(tptr>0)cfoll(tptr-1);
161 # ifdef DEBUG
162 if(debug)pfoll();
163 # endif
164 cgoto();
165 # ifdef DEBUG
166 if(debug){
167 print("Print %d states:\n",stnum+1);
168 for(i=0;i<=stnum;i++)stprt(i);
170 # endif
171 /* may be disposed of: positions, tmpstat, foll, state, name, left, right, parent, ccl, stchar, sname */
172 /* may be gotten: verify, advance, stoff */
173 free2core();
174 get3core();
175 layout();
176 /* may be disposed of: verify, advance, stoff, nexts, nchar,
177 gotof, atable, ccpackflg, sfall */
178 # ifdef DEBUG
179 free3core();
180 # endif
181 fother = Bopen(cname,OREAD);
182 if(fother == 0)
183 error("Lex driver missing, file %s",cname);
184 while ( (i=Bgetc(fother)) != Beof)
185 Bputc(&fout, i);
187 Bterm(fother);
188 Bterm(&fout);
189 if(
190 # ifdef DEBUG
191 debug ||
192 # endif
193 report == 1)statistics();
194 if(fin)
195 Bterm(fin);
196 exits(0); /* success return code */
199 static void
200 get1core(void)
202 ccptr = ccl = myalloc(CCLSIZE,sizeof(*ccl));
203 pcptr = pchar = myalloc(pchlen, sizeof(*pchar));
204 def = myalloc(DEFSIZE,sizeof(*def));
205 subs = myalloc(DEFSIZE,sizeof(*subs));
206 dp = dchar = myalloc(DEFCHAR,sizeof(*dchar));
207 sname = myalloc(STARTSIZE,sizeof(*sname));
208 sp = stchar = myalloc(STARTCHAR,sizeof(*stchar));
209 if(ccl == 0 || def == 0 || subs == 0 || dchar == 0 || sname == 0 || stchar == 0)
210 error("Too little core to begin");
213 static void
214 free1core(void)
216 free(def);
217 free(subs);
218 free(dchar);
221 static void
222 get2core(void)
224 int i;
226 gotof = myalloc(nstates,sizeof(*gotof));
227 nexts = myalloc(ntrans,sizeof(*nexts));
228 nchar = myalloc(ntrans,sizeof(*nchar));
229 state = myalloc(nstates,sizeof(*state));
230 atable = myalloc(nstates,sizeof(*atable));
231 sfall = myalloc(nstates,sizeof(*sfall));
232 cpackflg = myalloc(nstates,sizeof(*cpackflg));
233 tmpstat = myalloc(tptr+1,sizeof(*tmpstat));
234 foll = myalloc(tptr+1,sizeof(*foll));
235 nxtpos = positions = myalloc(maxpos,sizeof(*positions));
236 if(tmpstat == 0 || foll == 0 || positions == 0 ||
237 gotof == 0 || nexts == 0 || nchar == 0 || state == 0 || atable == 0 || sfall == 0 || cpackflg == 0 )
238 error("Too little core for state generation");
239 for(i=0;i<=tptr;i++)foll[i] = 0;
242 static void
243 free2core(void)
245 free(positions);
246 free(tmpstat);
247 free(foll);
248 free(name);
249 free(left);
250 free(right);
251 free(parent);
252 free(nullstr);
253 free(state);
254 free(sname);
255 free(stchar);
256 free(ccl);
259 static void
260 get3core(void)
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");
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);
281 # endif
282 void *
283 myalloc(int a, int b)
285 void *i;
286 i = calloc(a, b);
287 if(i==0)
288 warning("OOPS - calloc returns a 0");
289 return(i);
292 void
293 yyerror(char *s)
295 fprint(2, "line %d: %s\n", yyline, s);