sub2.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. # include "ldefs.h"
  2. void
  3. cfoll(int v)
  4. {
  5. int i,j,k;
  6. uchar *p;
  7. i = name[v];
  8. if(i < NCH) i = 1; /* character */
  9. switch(i){
  10. case 1: case RSTR: case RCCL: case RNCCL: case RNULLS:
  11. for(j=0;j<tptr;j++)
  12. tmpstat[j] = FALSE;
  13. count = 0;
  14. follow(v);
  15. # ifdef PP
  16. padd(foll,v); /* packing version */
  17. # endif
  18. # ifndef PP
  19. add(foll,v); /* no packing version */
  20. # endif
  21. if(i == RSTR) cfoll(left[v]);
  22. else if(i == RCCL || i == RNCCL){ /* compress ccl list */
  23. for(j=1; j<NCH;j++)
  24. symbol[j] = (i==RNCCL);
  25. p = ptr[v];
  26. while(*p)
  27. symbol[*p++] = (i == RCCL);
  28. p = pcptr;
  29. for(j=1;j<NCH;j++)
  30. if(symbol[j]){
  31. for(k=0;p+k < pcptr; k++)
  32. if(cindex[j] == *(p+k))
  33. break;
  34. if(p+k >= pcptr)*pcptr++ = cindex[j];
  35. }
  36. *pcptr++ = 0;
  37. if(pcptr > pchar + pchlen)
  38. error("Too many packed character classes");
  39. ptr[v] = p;
  40. name[v] = RCCL; /* RNCCL eliminated */
  41. # ifdef DEBUG
  42. if(debug && *p){
  43. print("ccl %d: %d",v,*p++);
  44. while(*p)
  45. print(", %d",*p++);
  46. print("\n");
  47. }
  48. # endif
  49. }
  50. break;
  51. case CARAT:
  52. cfoll(left[v]);
  53. break;
  54. case STAR: case PLUS: case QUEST: case RSCON:
  55. cfoll(left[v]);
  56. break;
  57. case BAR: case RCAT: case DIV: case RNEWE:
  58. cfoll(left[v]);
  59. cfoll(right[v]);
  60. break;
  61. # ifdef DEBUG
  62. case FINAL:
  63. case S1FINAL:
  64. case S2FINAL:
  65. break;
  66. default:
  67. warning("bad switch cfoll %d",v);
  68. # endif
  69. }
  70. }
  71. # ifdef DEBUG
  72. void
  73. pfoll(void)
  74. {
  75. int i,k,*p;
  76. int j;
  77. /* print sets of chars which may follow positions */
  78. print("pos\tchars\n");
  79. for(i=0;i<tptr;i++)
  80. if(p=foll[i]){
  81. j = *p++;
  82. if(j >= 1){
  83. print("%d:\t%d",i,*p++);
  84. for(k=2;k<=j;k++)
  85. print(", %d",*p++);
  86. print("\n");
  87. }
  88. }
  89. }
  90. # endif
  91. void
  92. add(int **array, int n)
  93. {
  94. int i, *temp;
  95. uchar *ctemp;
  96. temp = nxtpos;
  97. ctemp = tmpstat;
  98. array[n] = nxtpos; /* note no packing is done in positions */
  99. *temp++ = count;
  100. for(i=0;i<tptr;i++)
  101. if(ctemp[i] == TRUE)
  102. *temp++ = i;
  103. nxtpos = temp;
  104. if(nxtpos >= positions+maxpos)
  105. error("Too many positions %s",(maxpos== MAXPOS?"\nTry using %p num":""));
  106. }
  107. void
  108. follow(int v)
  109. {
  110. int p;
  111. if(v >= tptr-1)return;
  112. p = parent[v];
  113. if(p == 0) return;
  114. switch(name[p]){
  115. /* will not be CHAR RNULLS FINAL S1FINAL S2FINAL RCCL RNCCL */
  116. case RSTR:
  117. if(tmpstat[p] == FALSE){
  118. count++;
  119. tmpstat[p] = TRUE;
  120. }
  121. break;
  122. case STAR: case PLUS:
  123. first(v);
  124. follow(p);
  125. break;
  126. case BAR: case QUEST: case RNEWE:
  127. follow(p);
  128. break;
  129. case RCAT: case DIV:
  130. if(v == left[p]){
  131. if(nullstr[right[p]])
  132. follow(p);
  133. first(right[p]);
  134. }
  135. else follow(p);
  136. break;
  137. case RSCON: case CARAT:
  138. follow(p);
  139. break;
  140. # ifdef DEBUG
  141. default:
  142. warning("bad switch follow %d",p);
  143. # endif
  144. }
  145. }
  146. void
  147. first(int v) /* calculate set of positions with v as root which can be active initially */
  148. {
  149. int i;
  150. uchar *p;
  151. i = name[v];
  152. if(i < NCH)i = 1;
  153. switch(i){
  154. case 1: case RCCL: case RNCCL: case RNULLS: case FINAL: case S1FINAL: case S2FINAL:
  155. if(tmpstat[v] == FALSE){
  156. count++;
  157. tmpstat[v] = TRUE;
  158. }
  159. break;
  160. case BAR: case RNEWE:
  161. first(left[v]);
  162. first(right[v]);
  163. break;
  164. case CARAT:
  165. if(stnum % 2 == 1)
  166. first(left[v]);
  167. break;
  168. case RSCON:
  169. i = stnum/2 +1;
  170. p = (uchar *)right[v];
  171. while(*p)
  172. if(*p++ == i){
  173. first(left[v]);
  174. break;
  175. }
  176. break;
  177. case STAR: case QUEST: case PLUS: case RSTR:
  178. first(left[v]);
  179. break;
  180. case RCAT: case DIV:
  181. first(left[v]);
  182. if(nullstr[left[v]])
  183. first(right[v]);
  184. break;
  185. # ifdef DEBUG
  186. default:
  187. warning("bad switch first %d",v);
  188. # endif
  189. }
  190. }
  191. void
  192. cgoto(void)
  193. {
  194. int i, j, s;
  195. int npos, curpos, n;
  196. int tryit;
  197. uchar tch[NCH];
  198. int tst[NCH];
  199. uchar *q;
  200. /* generate initial state, for each start condition */
  201. Bprint(&fout,"int yyvstop[] = {\n0,\n");
  202. while(stnum < 2 || stnum/2 < sptr){
  203. for(i = 0; i<tptr; i++) tmpstat[i] = 0;
  204. count = 0;
  205. if(tptr > 0)first(tptr-1);
  206. add(state,stnum);
  207. # ifdef DEBUG
  208. if(debug){
  209. if(stnum > 1)
  210. print("%s:\n",sname[stnum/2]);
  211. pstate(stnum);
  212. }
  213. # endif
  214. stnum++;
  215. }
  216. stnum--;
  217. /* even stnum = might not be at line begin */
  218. /* odd stnum = must be at line begin */
  219. /* even states can occur anywhere, odd states only at line begin */
  220. for(s = 0; s <= stnum; s++){
  221. tryit = FALSE;
  222. cpackflg[s] = FALSE;
  223. sfall[s] = -1;
  224. acompute(s);
  225. for(i=0;i<NCH;i++) symbol[i] = 0;
  226. npos = *state[s];
  227. for(i = 1; i<=npos; i++){
  228. curpos = *(state[s]+i);
  229. if(name[curpos] < NCH) symbol[name[curpos]] = TRUE;
  230. else switch(name[curpos]){
  231. case RCCL:
  232. tryit = TRUE;
  233. q = ptr[curpos];
  234. while(*q){
  235. for(j=1;j<NCH;j++)
  236. if(cindex[j] == *q)
  237. symbol[j] = TRUE;
  238. q++;
  239. }
  240. break;
  241. case RSTR:
  242. symbol[right[curpos]] = TRUE;
  243. break;
  244. # ifdef DEBUG
  245. case RNULLS:
  246. case FINAL:
  247. case S1FINAL:
  248. case S2FINAL:
  249. break;
  250. default:
  251. warning("bad switch cgoto %d state %d",curpos,s);
  252. break;
  253. # endif
  254. }
  255. }
  256. # ifdef DEBUG
  257. if(debug){
  258. print("State %d transitions on:\n\t",s);
  259. charc = 0;
  260. for(i = 1; i<NCH; i++){
  261. if(symbol[i]) allprint(i);
  262. if(charc > LINESIZE){
  263. charc = 0;
  264. print("\n\t");
  265. }
  266. }
  267. print("\n");
  268. }
  269. # endif
  270. /* for each char, calculate next state */
  271. n = 0;
  272. for(i = 1; i<NCH; i++){
  273. if(symbol[i]){
  274. nextstate(s,i); /* executed for each state, transition pair */
  275. xstate = notin(stnum);
  276. if(xstate == -2) warning("bad state %d %o",s,i);
  277. else if(xstate == -1){
  278. if(stnum >= nstates)
  279. error("Too many states %s",(nstates == NSTATES ? "\nTry using %n num":""));
  280. add(state,++stnum);
  281. # ifdef DEBUG
  282. if(debug)pstate(stnum);
  283. # endif
  284. tch[n] = i;
  285. tst[n++] = stnum;
  286. } else { /* xstate >= 0 ==> state exists */
  287. tch[n] = i;
  288. tst[n++] = xstate;
  289. }
  290. }
  291. }
  292. tch[n] = 0;
  293. tst[n] = -1;
  294. /* pack transitions into permanent array */
  295. if(n > 0) packtrans(s,tch,tst,n,tryit);
  296. else gotof[s] = -1;
  297. }
  298. Bprint(&fout,"0};\n");
  299. }
  300. /* Beware -- 70% of total CPU time is spent in this subroutine -
  301. if you don't believe me - try it yourself ! */
  302. void
  303. nextstate(int s, int c)
  304. {
  305. int j, *newpos;
  306. uchar *temp, *tz;
  307. int *pos, i, *f, num, curpos, number;
  308. /* state to goto from state s on char c */
  309. num = *state[s];
  310. temp = tmpstat;
  311. pos = state[s] + 1;
  312. for(i = 0; i<num; i++){
  313. curpos = *pos++;
  314. j = name[curpos];
  315. if(j < NCH && j == c
  316. || j == RSTR && c == right[curpos]
  317. || j == RCCL && member(c, ptr[curpos])){
  318. f = foll[curpos];
  319. number = *f;
  320. newpos = f+1;
  321. for(j=0;j<number;j++)
  322. temp[*newpos++] = 2;
  323. }
  324. }
  325. j = 0;
  326. tz = temp + tptr;
  327. while(temp < tz){
  328. if(*temp == 2){
  329. j++;
  330. *temp++ = 1;
  331. }
  332. else *temp++ = 0;
  333. }
  334. count = j;
  335. }
  336. int
  337. notin(int n)
  338. { /* see if tmpstat occurs previously */
  339. int *j,k;
  340. uchar *temp;
  341. int i;
  342. if(count == 0)
  343. return(-2);
  344. temp = tmpstat;
  345. for(i=n;i>=0;i--){ /* for each state */
  346. j = state[i];
  347. if(count == *j++){
  348. for(k=0;k<count;k++)
  349. if(!temp[*j++])break;
  350. if(k >= count)
  351. return(i);
  352. }
  353. }
  354. return(-1);
  355. }
  356. void
  357. packtrans(int st, uchar *tch, int *tst, int cnt, int tryit)
  358. {
  359. /* pack transitions into nchar, nexts */
  360. /* nchar is terminated by '\0', nexts uses cnt, followed by elements */
  361. /* gotof[st] = index into nchr, nexts for state st */
  362. /* sfall[st] = t implies t is fall back state for st */
  363. /* == -1 implies no fall back */
  364. int i,j,k;
  365. int cmin, cval, tcnt, diff, p, *ast;
  366. uchar *ach;
  367. int go[NCH], temp[NCH], c;
  368. int swork[NCH];
  369. uchar cwork[NCH];
  370. int upper;
  371. rcount += cnt;
  372. cmin = -1;
  373. cval = NCH;
  374. ast = tst;
  375. ach = tch;
  376. /* try to pack transitions using ccl's */
  377. if(tryit){ /* ccl's used */
  378. for(i=1;i<NCH;i++){
  379. go[i] = temp[i] = -1;
  380. symbol[i] = 1;
  381. }
  382. for(i=0;i<cnt;i++){
  383. go[tch[i]] = tst[i];
  384. symbol[tch[i]] = 0;
  385. }
  386. for(i=0; i<cnt;i++){
  387. c = match[tch[i]];
  388. if(go[c] != tst[i] || c == tch[i])
  389. temp[tch[i]] = tst[i];
  390. }
  391. /* fill in error entries */
  392. for(i=1;i<NCH;i++)
  393. if(symbol[i]) temp[i] = -2; /* error trans */
  394. /* count them */
  395. k = 0;
  396. for(i=1;i<NCH;i++)
  397. if(temp[i] != -1)k++;
  398. if(k <cnt){ /* compress by char */
  399. # ifdef DEBUG
  400. if(debug) print("use compression %d, %d vs %d\n",st,k,cnt);
  401. # endif
  402. k = 0;
  403. for(i=1;i<NCH;i++)
  404. if(temp[i] != -1){
  405. cwork[k] = i;
  406. swork[k++] = (temp[i] == -2 ? -1 : temp[i]);
  407. }
  408. cwork[k] = 0;
  409. # ifdef PC
  410. ach = cwork;
  411. ast = swork;
  412. cnt = k;
  413. cpackflg[st] = TRUE;
  414. # endif
  415. }
  416. }
  417. for(i=0; i<st; i++){ /* get most similar state */
  418. /* reject state with more transitions, state already represented by a third state,
  419. and state which is compressed by char if ours is not to be */
  420. if(sfall[i] != -1) continue;
  421. if(cpackflg[st] == 1) if(!(cpackflg[i] == 1)) continue;
  422. p = gotof[i];
  423. if(p == -1) /* no transitions */ continue;
  424. tcnt = nexts[p];
  425. if(tcnt > cnt) continue;
  426. diff = 0;
  427. j = 0;
  428. upper = p + tcnt;
  429. while(ach[j] && p < upper){
  430. while(ach[j] < nchar[p] && ach[j]){diff++; j++; }
  431. if(ach[j] == 0)break;
  432. if(ach[j] > nchar[p]){diff=NCH;break;}
  433. /* ach[j] == nchar[p] */
  434. if(ast[j] != nexts[++p] || ast[j] == -1 || (cpackflg[st] && ach[j] != match[ach[j]]))diff++;
  435. j++;
  436. }
  437. while(ach[j]){
  438. diff++;
  439. j++;
  440. }
  441. if(p < upper)diff = NCH;
  442. if(diff < cval && diff < tcnt){
  443. cval = diff;
  444. cmin = i;
  445. if(cval == 0)break;
  446. }
  447. }
  448. /* cmin = state "most like" state st */
  449. # ifdef DEBUG
  450. if(debug)print("select st %d for st %d diff %d\n",cmin,st,cval);
  451. # endif
  452. # ifdef PS
  453. if(cmin != -1){ /* if we can use st cmin */
  454. gotof[st] = nptr;
  455. k = 0;
  456. sfall[st] = cmin;
  457. p = gotof[cmin]+1;
  458. j = 0;
  459. while(ach[j]){
  460. /* if cmin has a transition on c, then so will st */
  461. /* st may be "larger" than cmin, however */
  462. while(ach[j] < nchar[p-1] && ach[j]){
  463. k++;
  464. nchar[nptr] = ach[j];
  465. nexts[++nptr] = ast[j];
  466. j++;
  467. }
  468. if(nchar[p-1] == 0)break;
  469. if(ach[j] > nchar[p-1]){
  470. warning("bad transition %d %d",st,cmin);
  471. goto nopack;
  472. }
  473. /* ach[j] == nchar[p-1] */
  474. if(ast[j] != nexts[p] || ast[j] == -1 || (cpackflg[st] && ach[j] != match[ach[j]])){
  475. k++;
  476. nchar[nptr] = ach[j];
  477. nexts[++nptr] = ast[j];
  478. }
  479. p++;
  480. j++;
  481. }
  482. while(ach[j]){
  483. nchar[nptr] = ach[j];
  484. nexts[++nptr] = ast[j++];
  485. k++;
  486. }
  487. nexts[gotof[st]] = cnt = k;
  488. nchar[nptr++] = 0;
  489. } else {
  490. # endif
  491. nopack:
  492. /* stick it in */
  493. gotof[st] = nptr;
  494. nexts[nptr] = cnt;
  495. for(i=0;i<cnt;i++){
  496. nchar[nptr] = ach[i];
  497. nexts[++nptr] = ast[i];
  498. }
  499. nchar[nptr++] = 0;
  500. # ifdef PS
  501. }
  502. # endif
  503. if(cnt < 1){
  504. gotof[st] = -1;
  505. nptr--;
  506. } else
  507. if(nptr > ntrans)
  508. error("Too many transitions %s",(ntrans==NTRANS?"\nTry using %a num":""));
  509. }
  510. # ifdef DEBUG
  511. void
  512. pstate(int s)
  513. {
  514. int *p,i,j;
  515. print("State %d:\n",s);
  516. p = state[s];
  517. i = *p++;
  518. if(i == 0) return;
  519. print("%4d",*p++);
  520. for(j = 1; j<i; j++){
  521. print(", %4d",*p++);
  522. if(j%30 == 0)print("\n");
  523. }
  524. print("\n");
  525. }
  526. # endif
  527. int
  528. member(int d, uchar *t)
  529. {
  530. int c;
  531. uchar *s;
  532. c = d;
  533. s = t;
  534. c = cindex[c];
  535. while(*s)
  536. if(*s++ == c) return(1);
  537. return(0);
  538. }
  539. # ifdef DEBUG
  540. void
  541. stprt(int i)
  542. {
  543. int p, t;
  544. print("State %d:",i);
  545. /* print actions, if any */
  546. t = atable[i];
  547. if(t != -1)print(" final");
  548. print("\n");
  549. if(cpackflg[i] == TRUE)print("backup char in use\n");
  550. if(sfall[i] != -1)print("fall back state %d\n",sfall[i]);
  551. p = gotof[i];
  552. if(p == -1) return;
  553. print("(%d transitions)\n",nexts[p]);
  554. while(nchar[p]){
  555. charc = 0;
  556. if(nexts[p+1] >= 0)
  557. print("%d\t",nexts[p+1]);
  558. else print("err\t");
  559. allprint(nchar[p++]);
  560. while(nexts[p] == nexts[p+1] && nchar[p]){
  561. if(charc > LINESIZE){
  562. charc = 0;
  563. print("\n\t");
  564. }
  565. allprint(nchar[p++]);
  566. }
  567. print("\n");
  568. }
  569. print("\n");
  570. }
  571. # endif
  572. void
  573. acompute(int s) /* compute action list = set of poss. actions */
  574. {
  575. int *p, i, j;
  576. int cnt, m;
  577. int temp[300], k, neg[300], n;
  578. k = 0;
  579. n = 0;
  580. p = state[s];
  581. cnt = *p++;
  582. if(cnt > 300)
  583. error("Too many positions for one state - acompute");
  584. for(i=0;i<cnt;i++){
  585. if(name[*p] == FINAL)temp[k++] = left[*p];
  586. else if(name[*p] == S1FINAL){temp[k++] = left[*p];
  587. if (left[*p] >= NACTIONS) error("Too many right contexts");
  588. extra[left[*p]] = 1;
  589. } else if(name[*p] == S2FINAL)neg[n++] = left[*p];
  590. p++;
  591. }
  592. atable[s] = -1;
  593. if(k < 1 && n < 1) return;
  594. # ifdef DEBUG
  595. if(debug) print("final %d actions:",s);
  596. # endif
  597. /* sort action list */
  598. for(i=0; i<k; i++)
  599. for(j=i+1;j<k;j++)
  600. if(temp[j] < temp[i]){
  601. m = temp[j];
  602. temp[j] = temp[i];
  603. temp[i] = m;
  604. }
  605. /* remove dups */
  606. for(i=0;i<k-1;i++)
  607. if(temp[i] == temp[i+1]) temp[i] = 0;
  608. /* copy to permanent quarters */
  609. atable[s] = aptr;
  610. # ifdef DEBUG
  611. Bprint(&fout,"/* actions for state %d */",s);
  612. # endif
  613. Bputc(&fout, '\n');
  614. for(i=0;i<k;i++)
  615. if(temp[i] != 0){
  616. Bprint(&fout,"%d,\n",temp[i]);
  617. # ifdef DEBUG
  618. if(debug)
  619. print("%d ",temp[i]);
  620. # endif
  621. aptr++;
  622. }
  623. for(i=0;i<n;i++){ /* copy fall back actions - all neg */
  624. Bprint(&fout,"%d,\n",neg[i]);
  625. aptr++;
  626. # ifdef DEBUG
  627. if(debug)print("%d ",neg[i]);
  628. # endif
  629. }
  630. # ifdef DEBUG
  631. if(debug)print("\n");
  632. # endif
  633. Bprint(&fout,"0,\n");
  634. aptr++;
  635. }
  636. # ifdef DEBUG
  637. void
  638. pccl(void) {
  639. /* print character class sets */
  640. int i, j;
  641. print("char class intersection\n");
  642. for(i=0; i< ccount; i++){
  643. charc = 0;
  644. print("class %d:\n\t",i);
  645. for(j=1;j<NCH;j++)
  646. if(cindex[j] == i){
  647. allprint(j);
  648. if(charc > LINESIZE){
  649. print("\n\t");
  650. charc = 0;
  651. }
  652. }
  653. print("\n");
  654. }
  655. charc = 0;
  656. print("match:\n");
  657. for(i=0;i<NCH;i++){
  658. allprint(match[i]);
  659. if(charc > LINESIZE){
  660. print("\n");
  661. charc = 0;
  662. }
  663. }
  664. print("\n");
  665. }
  666. # endif
  667. void
  668. mkmatch(void)
  669. {
  670. int i;
  671. uchar tab[NCH];
  672. for(i=0; i<ccount; i++)
  673. tab[i] = 0;
  674. for(i=1;i<NCH;i++)
  675. if(tab[cindex[i]] == 0)
  676. tab[cindex[i]] = i;
  677. /* tab[i] = principal char for new ccl i */
  678. for(i = 1; i<NCH; i++)
  679. match[i] = tab[cindex[i]];
  680. }
  681. void
  682. layout(void)
  683. {
  684. /* format and output final program's tables */
  685. int i, j, k;
  686. int top, bot, startup, omin;
  687. for(i=0; i<outsize;i++)
  688. verify[i] = advance[i] = 0;
  689. omin = 0;
  690. yytop = 0;
  691. for(i=0; i<= stnum; i++){ /* for each state */
  692. j = gotof[i];
  693. if(j == -1){
  694. stoff[i] = 0;
  695. continue;
  696. }
  697. bot = j;
  698. while(nchar[j])j++;
  699. top = j - 1;
  700. # ifdef DEBUG
  701. if (debug) {
  702. print("State %d: (layout)\n", i);
  703. for(j=bot; j<=top;j++) {
  704. print(" %o", nchar[j]);
  705. if (j%10==0) print("\n");
  706. }
  707. print("\n");
  708. }
  709. # endif
  710. while(verify[omin+NCH]) omin++;
  711. startup = omin;
  712. # ifdef DEBUG
  713. if (debug) print("bot,top %d, %d startup begins %d\n",bot,top,startup);
  714. # endif
  715. do {
  716. startup += 1;
  717. if(startup > outsize - NCH)
  718. error("output table overflow");
  719. for(j = bot; j<= top; j++){
  720. k = startup + nchar[j];
  721. if(verify[k])break;
  722. }
  723. } while (j <= top);
  724. /* have found place */
  725. # ifdef DEBUG
  726. if (debug) print(" startup going to be %d\n", startup);
  727. # endif
  728. for(j = bot; j<= top; j++){
  729. k = startup + nchar[j];
  730. verify[k] = i+1; /* state number + 1*/
  731. advance[k] = nexts[j+1]+1; /* state number + 1*/
  732. if(yytop < k) yytop = k;
  733. }
  734. stoff[i] = startup;
  735. }
  736. /* stoff[i] = offset into verify, advance for trans for state i */
  737. /* put out yywork */
  738. Bprint(&fout,"# define YYTYPE %s\n",stnum+1 >= NCH ? "int" : "Uchar");
  739. Bprint(&fout,"struct yywork { YYTYPE verify, advance; } yycrank[] = {\n");
  740. for(i=0;i<=yytop;i+=4){
  741. for(j=0;j<4;j++){
  742. k = i+j;
  743. if(verify[k])
  744. Bprint(&fout,"%d,%d,\t",verify[k],advance[k]);
  745. else
  746. Bprint(&fout,"0,0,\t");
  747. }
  748. Bputc(&fout, '\n');
  749. }
  750. Bprint(&fout,"0,0};\n");
  751. /* put out yysvec */
  752. Bprint(&fout,"struct yysvf yysvec[] = {\n");
  753. Bprint(&fout,"0,\t0,\t0,\n");
  754. for(i=0;i<=stnum;i++){ /* for each state */
  755. if(cpackflg[i])stoff[i] = -stoff[i];
  756. Bprint(&fout,"yycrank+%d,\t",stoff[i]);
  757. if(sfall[i] != -1)
  758. Bprint(&fout,"yysvec+%d,\t", sfall[i]+1); /* state + 1 */
  759. else Bprint(&fout,"0,\t\t");
  760. if(atable[i] != -1)
  761. Bprint(&fout,"yyvstop+%d,",atable[i]);
  762. else Bprint(&fout,"0,\t");
  763. # ifdef DEBUG
  764. Bprint(&fout,"\t\t/* state %d */",i);
  765. # endif
  766. Bputc(&fout, '\n');
  767. }
  768. Bprint(&fout,"0,\t0,\t0};\n");
  769. /* put out yymatch */
  770. Bprint(&fout,"struct yywork *yytop = yycrank+%d;\n",yytop);
  771. Bprint(&fout,"struct yysvf *yybgin = yysvec+1;\n");
  772. Bprint(&fout,"Uchar yymatch[] = {\n");
  773. for(i=0; i<NCH; i+=8){
  774. for(j=0; j<8; j++){
  775. int fbch;
  776. fbch = match[i+j];
  777. if(isprint(fbch) && fbch != '\'' && fbch != '\\')
  778. Bprint(&fout,"'%c' ,",fbch);
  779. else Bprint(&fout,"0%-3o,",fbch);
  780. }
  781. Bputc(&fout, '\n');
  782. }
  783. Bprint(&fout,"0};\n");
  784. /* put out yyextra */
  785. Bprint(&fout,"Uchar yyextra[] = {\n");
  786. for(i=0;i<casecount;i+=8){
  787. for(j=0;j<8;j++)
  788. Bprint(&fout, "%d,", i+j<NACTIONS ?
  789. extra[i+j] : 0);
  790. Bputc(&fout, '\n');
  791. }
  792. Bprint(&fout,"0};\n");
  793. }
  794. # ifdef PP
  795. void
  796. padd(int **array, int n)
  797. {
  798. int i, *j, k;
  799. array[n] = nxtpos;
  800. if(count == 0){
  801. *nxtpos++ = 0;
  802. return;
  803. }
  804. for(i=tptr-1;i>=0;i--){
  805. j = array[i];
  806. if(j && *j++ == count){
  807. for(k=0;k<count;k++)
  808. if(!tmpstat[*j++])break;
  809. if(k >= count){
  810. array[n] = array[i];
  811. return;
  812. }
  813. }
  814. }
  815. add(array,n);
  816. }
  817. # endif