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