lmain.c 5.9 KB

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