sub2.c 17 KB

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