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 ccount = 1; 20 int casecount = 1; 21 int aptr = 1; 22 int nstates = NSTATES, maxpos = MAXPOS; 23 int treesize = TREESIZE, ntrans = NTRANS; 24 int yytop; 25 int outsize = NOUTPUT; 26 int sptr = 1; 27 int report = 2; 28 int debug; /* 1 = on */ 29 int charc; 30 int sargc; 31 char **sargv; 32 uchar buf[520]; 33 int yyline; /* line number of file */ 34 int eof; 35 int lgatflg; 36 int divflg; 37 int funcflag; 38 int pflag; 39 int chset; /* 1 = char set modified */ 40 Biobuf *fin, *fother; 41 int fptr; 42 int *name; 43 int *left; 44 int *right; 45 int *parent; 46 uchar *nullstr; 47 int tptr; 48 uchar pushc[TOKENSIZE]; 49 uchar slist[STARTSIZE]; 50 uchar **def, **subs, *dchar; 51 uchar **sname, *stchar; 52 uchar *ccl; 53 uchar *ccptr; 54 uchar *dp, *sp; 55 int dptr; 56 uchar *bptr; /* store input position */ 57 uchar *tmpstat; 58 int count; 59 int **foll; 60 int *nxtpos; 61 int *positions; 62 int *gotof; 63 int *nexts; 64 uchar *nchar; 65 int **state; 66 int *sfall; /* fallback state num */ 67 uchar *cpackflg; /* true if state has been character packed */ 68 int *atable; 69 int nptr; 70 uchar symbol[NCH]; 71 uchar cindex[NCH]; 72 int xstate; 73 int stnum; 74 uchar match[NCH]; 75 uchar extra[NACTIONS]; 76 uchar *pchar, *pcptr; 77 int pchlen = TOKENSIZE; 78 long rcount; 79 int *verify, *advance, *stoff; 80 int scon; 81 uchar *psave; 82 83 static void free1core(void); 84 static void free2core(void); 85 static void free3core(void); 86 static void get1core(void); 87 static void get2core(void); 88 static void get3core(void); 89 90 void 91 main(int argc, char **argv) 92 { 93 int i; 94 95 ARGBEGIN { 96 # ifdef DEBUG 97 case 'd': debug++; break; 98 case 'y': yydebug = TRUE; break; 99 # endif 100 case 't': case 'T': 101 Binit(&fout, 1, OWRITE); 102 errorf= 2; 103 foutopen = 1; 104 break; 105 case 'v': case 'V': 106 report = 1; 107 break; 108 case 'n': case 'N': 109 report = 0; 110 break; 111 default: 112 warning("Unknown option %c", ARGC()); 113 } ARGEND 114 sargc = argc; 115 sargv = argv; 116 if (argc > 0){ 117 fin = Bopen(argv[fptr++], OREAD); 118 if(fin == 0) 119 error ("Can't read input file %s",argv[0]); 120 sargc--; 121 sargv++; 122 } 123 else { 124 fin = myalloc(sizeof(Biobuf), 1); 125 if(fin == 0) 126 exits("core"); 127 Binit(fin, 0, OREAD); 128 } 129 if(Bgetc(fin) == Beof) /* no input */ 130 exits(0); 131 Bseek(fin, 0, 0); 132 gch(); 133 /* may be gotten: def, subs, sname, stchar, ccl, dchar */ 134 get1core(); 135 /* may be gotten: name, left, right, nullstr, parent */ 136 strcpy((char*)sp, "INITIAL"); 137 sname[0] = sp; 138 sp += strlen("INITIAL") + 1; 139 sname[1] = 0; 140 if(yyparse()) exits("error"); /* error return code */ 141 /* may be disposed of: def, subs, dchar */ 142 free1core(); 143 /* may be gotten: tmpstat, foll, positions, gotof, nexts, nchar, state, atable, sfall, cpackflg */ 144 get2core(); 145 ptail(); 146 mkmatch(); 147 # ifdef DEBUG 148 if(debug) pccl(); 149 # endif 150 sect = ENDSECTION; 151 if(tptr>0)cfoll(tptr-1); 152 # ifdef DEBUG 153 if(debug)pfoll(); 154 # endif 155 cgoto(); 156 # ifdef DEBUG 157 if(debug){ 158 print("Print %d states:\n",stnum+1); 159 for(i=0;i<=stnum;i++)stprt(i); 160 } 161 # endif 162 /* may be disposed of: positions, tmpstat, foll, state, name, left, right, parent, ccl, stchar, sname */ 163 /* may be gotten: verify, advance, stoff */ 164 free2core(); 165 get3core(); 166 layout(); 167 /* may be disposed of: verify, advance, stoff, nexts, nchar, 168 gotof, atable, ccpackflg, sfall */ 169 # ifdef DEBUG 170 free3core(); 171 # endif 172 fother = Bopen(cname,OREAD); 173 if(fother == 0) 174 error("Lex driver missing, file %s",cname); 175 while ( (i=Bgetc(fother)) != Beof) 176 Bputc(&fout, i); 177 178 Bterm(fother); 179 Bterm(&fout); 180 if( 181 # ifdef DEBUG 182 debug || 183 # endif 184 report == 1)statistics(); 185 Bterm(fin); 186 exits(0); /* success return code */ 187 } 188 189 static void 190 get1core(void) 191 { 192 ccptr = ccl = myalloc(CCLSIZE,sizeof(*ccl)); 193 pcptr = pchar = myalloc(pchlen, sizeof(*pchar)); 194 def = myalloc(DEFSIZE,sizeof(*def)); 195 subs = myalloc(DEFSIZE,sizeof(*subs)); 196 dp = dchar = myalloc(DEFCHAR,sizeof(*dchar)); 197 sname = myalloc(STARTSIZE,sizeof(*sname)); 198 sp = stchar = myalloc(STARTCHAR,sizeof(*stchar)); 199 if(ccl == 0 || def == 0 || subs == 0 || dchar == 0 || sname == 0 || stchar == 0) 200 error("Too little core to begin"); 201 } 202 203 static void 204 free1core(void) 205 { 206 free(def); 207 free(subs); 208 free(dchar); 209 } 210 211 static void 212 get2core(void) 213 { 214 int i; 215 216 gotof = myalloc(nstates,sizeof(*gotof)); 217 nexts = myalloc(ntrans,sizeof(*nexts)); 218 nchar = myalloc(ntrans,sizeof(*nchar)); 219 state = myalloc(nstates,sizeof(*state)); 220 atable = myalloc(nstates,sizeof(*atable)); 221 sfall = myalloc(nstates,sizeof(*sfall)); 222 cpackflg = myalloc(nstates,sizeof(*cpackflg)); 223 tmpstat = myalloc(tptr+1,sizeof(*tmpstat)); 224 foll = myalloc(tptr+1,sizeof(*foll)); 225 nxtpos = positions = myalloc(maxpos,sizeof(*positions)); 226 if(tmpstat == 0 || foll == 0 || positions == 0 || 227 gotof == 0 || nexts == 0 || nchar == 0 || state == 0 || atable == 0 || sfall == 0 || cpackflg == 0 ) 228 error("Too little core for state generation"); 229 for(i=0;i<=tptr;i++)foll[i] = 0; 230 } 231 232 static void 233 free2core(void) 234 { 235 free(positions); 236 free(tmpstat); 237 free(foll); 238 free(name); 239 free(left); 240 free(right); 241 free(parent); 242 free(nullstr); 243 free(state); 244 free(sname); 245 free(stchar); 246 free(ccl); 247 } 248 249 static void 250 get3core(void) 251 { 252 verify = myalloc(outsize,sizeof(*verify)); 253 advance = myalloc(outsize,sizeof(*advance)); 254 stoff = myalloc(stnum+2,sizeof(*stoff)); 255 if(verify == 0 || advance == 0 || stoff == 0) 256 error("Too little core for final packing"); 257 } 258 # ifdef DEBUG 259 static void 260 free3core(void){ 261 free(advance); 262 free(verify); 263 free(stoff); 264 free(gotof); 265 free(nexts); 266 free(nchar); 267 free(atable); 268 free(sfall); 269 free(cpackflg); 270 } 271 # endif 272 void * 273 myalloc(int a, int b) 274 { 275 void *i; 276 i = calloc(a, b); 277 if(i==0) 278 warning("OOPS - calloc returns a 0"); 279 return(i); 280 } 281 282 void 283 yyerror(char *s) 284 { 285 fprint(2, "line %d: %s\n", yyline, s); 286 } 287