lmain.c 6.0 KB

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