1 /* lex [-[dynvt]] [file] ... [file] */
3 /* Copyright 1976, Bell Telephone Laboratories, Inc.,
4 written by Eric Schmidt, August 27, 1976 */
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;
17 char *cname = "/sys/lib/lex/ncform";
23 int nstates = NSTATES, maxpos = MAXPOS;
24 int treesize = TREESIZE, ntrans = NTRANS;
26 int outsize = NOUTPUT;
29 int debug; /* 1 = on */
34 int yyline; /* line number of file */
35 char *yyfile; /* filename for error messages */
41 int chset; /* 1 = char set modified */
42 Biobuf *fin = 0, *fother;
51 uchar pushc[TOKENSIZE];
52 uchar slist[STARTSIZE];
53 uchar **def, **subs, *dchar;
54 uchar **sname, *stchar;
59 uchar *bptr; /* store input position */
69 int *sfall; /* fallback state num */
70 uchar *cpackflg; /* true if state has been character packed */
78 uchar extra[NACTIONS];
80 int pchlen = TOKENSIZE;
82 int *verify, *advance, *stoff;
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);
94 main(int argc, char **argv)
100 case 'd': debug++; break;
101 case 'y': yydebug = TRUE; break;
104 Binit(&fout, 1, OWRITE);
118 warning("Unknown option %c", ARGC());
123 yyfile = argv[fptr++];
124 fin = Bopen(yyfile, OREAD);
126 error ("%s - can't open file: %r", yyfile);
132 fin = myalloc(sizeof(Biobuf), 1);
135 Binit(fin, 0, OREAD);
137 if(Bgetc(fin) == Beof) /* no input */
141 /* may be gotten: def, subs, sname, stchar, ccl, dchar */
143 /* may be gotten: name, left, right, nullstr, parent, ptr */
144 strcpy((char*)sp, "INITIAL");
146 sp += strlen("INITIAL") + 1;
148 if(yyparse()) exits("error"); /* error return code */
149 /* may be disposed of: def, subs, dchar */
151 /* may be gotten: tmpstat, foll, positions, gotof, nexts, nchar, state, atable, sfall, cpackflg */
159 if(tptr>0)cfoll(tptr-1);
166 print("Print %d states:\n",stnum+1);
167 for(i=0;i<=stnum;i++)stprt(i);
170 /* may be disposed of: positions, tmpstat, foll, state, name, left, right, parent, ccl, stchar, sname */
171 /* may be gotten: verify, advance, stoff */
175 /* may be disposed of: verify, advance, stoff, nexts, nchar,
176 gotof, atable, ccpackflg, sfall */
180 fother = Bopen(cname,OREAD);
182 error("Lex driver missing, file %s: %r",cname);
183 while ( (i=Bgetc(fother)) != Beof)
192 report == 1)statistics();
195 exits(0); /* success return code */
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");
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;
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");
283 myalloc(int a, int b)
288 warning("OOPS - calloc returns a 0");
295 fprint(2, "%s:%d %s\n", yyfile, yyline, s);