lmain.c 6.5 KB

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