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 int eof; 36 int lgatflg; 37 int divflg; 38 int funcflag; 39 int pflag; 40 int chset; /* 1 = char set modified */ 41 Biobuf *fin = 0, *fother; 42 int fptr; 43 int *name; 44 int *left; 45 int *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; 84 85 static void free1core(void); 86 static void free2core(void); 87 static void free3core(void); 88 static void get1core(void); 89 static void get2core(void); 90 static void get3core(void); 91 92 void 93 main(int argc, char **argv) 94 { 95 int i; 96 97 ARGBEGIN { 98 # ifdef DEBUG 99 case 'd': debug++; break; 100 case 'y': yydebug = TRUE; break; 101 # endif 102 case 't': case 'T': 103 Binit(&fout, 1, OWRITE); 104 errorf= 2; 105 foutopen = 1; 106 break; 107 case 'v': case 'V': 108 report = 1; 109 break; 110 case 'n': case 'N': 111 report = 0; 112 break; 113 case '9': 114 nine = 1; 115 break; 116 default: 117 warning("Unknown option %c", ARGC()); 118 } ARGEND 119 sargc = argc; 120 sargv = argv; 121 if (argc > 0){ 122 fin = Bopen(argv[fptr++], OREAD); 123 if(fin == 0) 124 error ("Can't read input file %s",argv[0]); 125 sargc--; 126 sargv++; 127 } 128 else { 129 fin = myalloc(sizeof(Biobuf), 1); 130 if(fin == 0) 131 exits("core"); 132 Binit(fin, 0, OREAD); 133 } 134 if(Bgetc(fin) == Beof) /* no input */ 135 exits(0); 136 Bseek(fin, 0, 0); 137 gch(); 138 /* may be gotten: def, subs, sname, stchar, ccl, dchar */ 139 get1core(); 140 /* may be gotten: name, left, right, nullstr, parent, ptr */ 141 strcpy((char*)sp, "INITIAL"); 142 sname[0] = sp; 143 sp += strlen("INITIAL") + 1; 144 sname[1] = 0; 145 if(yyparse()) exits("error"); /* error return code */ 146 /* may be disposed of: def, subs, dchar */ 147 free1core(); 148 /* may be gotten: tmpstat, foll, positions, gotof, nexts, nchar, state, atable, sfall, cpackflg */ 149 get2core(); 150 ptail(); 151 mkmatch(); 152 # ifdef DEBUG 153 if(debug) pccl(); 154 # endif 155 sect = ENDSECTION; 156 if(tptr>0)cfoll(tptr-1); 157 # ifdef DEBUG 158 if(debug)pfoll(); 159 # endif 160 cgoto(); 161 # ifdef DEBUG 162 if(debug){ 163 print("Print %d states:\n",stnum+1); 164 for(i=0;i<=stnum;i++)stprt(i); 165 } 166 # endif 167 /* may be disposed of: positions, tmpstat, foll, state, name, left, right, parent, ccl, stchar, sname */ 168 /* may be gotten: verify, advance, stoff */ 169 free2core(); 170 get3core(); 171 layout(); 172 /* may be disposed of: verify, advance, stoff, nexts, nchar, 173 gotof, atable, ccpackflg, sfall */ 174 # ifdef DEBUG 175 free3core(); 176 # endif 177 fother = Bopen(cname,OREAD); 178 if(fother == 0) 179 error("Lex driver missing, file %s",cname); 180 while ( (i=Bgetc(fother)) != Beof) 181 Bputc(&fout, i); 182 183 Bterm(fother); 184 Bterm(&fout); 185 if( 186 # ifdef DEBUG 187 debug || 188 # endif 189 report == 1)statistics(); 190 if (fin) 191 Bterm(fin); 192 exits(0); /* success return code */ 193 } 194 195 static void 196 get1core(void) 197 { 198 ccptr = ccl = myalloc(CCLSIZE,sizeof(*ccl)); 199 pcptr = pchar = myalloc(pchlen, sizeof(*pchar)); 200 def = myalloc(DEFSIZE,sizeof(*def)); 201 subs = myalloc(DEFSIZE,sizeof(*subs)); 202 dp = dchar = myalloc(DEFCHAR,sizeof(*dchar)); 203 sname = myalloc(STARTSIZE,sizeof(*sname)); 204 sp = stchar = myalloc(STARTCHAR,sizeof(*stchar)); 205 if(ccl == 0 || def == 0 || subs == 0 || dchar == 0 || sname == 0 || stchar == 0) 206 error("Too little core to begin"); 207 } 208 209 static void 210 free1core(void) 211 { 212 free(def); 213 free(subs); 214 free(dchar); 215 } 216 217 static void 218 get2core(void) 219 { 220 int i; 221 222 gotof = myalloc(nstates,sizeof(*gotof)); 223 nexts = myalloc(ntrans,sizeof(*nexts)); 224 nchar = myalloc(ntrans,sizeof(*nchar)); 225 state = myalloc(nstates,sizeof(*state)); 226 atable = myalloc(nstates,sizeof(*atable)); 227 sfall = myalloc(nstates,sizeof(*sfall)); 228 cpackflg = myalloc(nstates,sizeof(*cpackflg)); 229 tmpstat = myalloc(tptr+1,sizeof(*tmpstat)); 230 foll = myalloc(tptr+1,sizeof(*foll)); 231 nxtpos = positions = myalloc(maxpos,sizeof(*positions)); 232 if(tmpstat == 0 || foll == 0 || positions == 0 || 233 gotof == 0 || nexts == 0 || nchar == 0 || state == 0 || atable == 0 || sfall == 0 || cpackflg == 0 ) 234 error("Too little core for state generation"); 235 for(i=0;i<=tptr;i++)foll[i] = 0; 236 } 237 238 static void 239 free2core(void) 240 { 241 free(positions); 242 free(tmpstat); 243 free(foll); 244 free(name); 245 free(left); 246 free(right); 247 free(parent); 248 free(nullstr); 249 free(ptr); 250 free(state); 251 free(sname); 252 free(stchar); 253 free(ccl); 254 } 255 256 static void 257 get3core(void) 258 { 259 verify = myalloc(outsize,sizeof(*verify)); 260 advance = myalloc(outsize,sizeof(*advance)); 261 stoff = myalloc(stnum+2,sizeof(*stoff)); 262 if(verify == 0 || advance == 0 || stoff == 0) 263 error("Too little core for final packing"); 264 } 265 # ifdef DEBUG 266 static void 267 free3core(void){ 268 free(advance); 269 free(verify); 270 free(stoff); 271 free(gotof); 272 free(nexts); 273 free(nchar); 274 free(atable); 275 free(sfall); 276 free(cpackflg); 277 } 278 # endif 279 void * 280 myalloc(int a, int b) 281 { 282 void *i; 283 i = calloc(a, b); 284 if(i==0) 285 warning("OOPS - calloc returns a 0"); 286 return(i); 287 } 288 289 void 290 yyerror(char *s) 291 { 292 fprint(2, "line %d: %s\n", yyline, s); 293 } 294