lmain.c 5.9 KB

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