output.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. #include "defs.h"
  2. static int nvectors;
  3. static int nentries;
  4. static Yshort **froms;
  5. static Yshort **tos;
  6. static Yshort *conflicts, nconflicts;
  7. static Yshort *tally;
  8. static Yshort *width;
  9. static Yshort *state_count;
  10. static Yshort *order;
  11. static Yshort *base;
  12. static Yshort *pos;
  13. static int maxtable;
  14. static Yshort *table;
  15. static Yshort *check;
  16. static int lowzero;
  17. static int high;
  18. void output()
  19. {
  20. free_itemsets();
  21. free_shifts();
  22. free_reductions();
  23. output_stored_text();
  24. output_defines();
  25. output_rule_data();
  26. output_yydefred();
  27. output_actions();
  28. free_parser();
  29. output_debug();
  30. output_stype();
  31. if (rflag) write_section("tables");
  32. write_section("header");
  33. output_trailing_text();
  34. write_section("body");
  35. output_semantic_actions();
  36. write_section("trailer");
  37. }
  38. void output_rule_data()
  39. {
  40. register int i;
  41. register int j;
  42. if (!rflag)
  43. fprintf(output_file, "static ");
  44. fprintf(output_file, "int yylhs[] = {%42d,",
  45. symbol_value[start_symbol]);
  46. j = 10;
  47. for (i = 3; i < nrules; i++)
  48. {
  49. if (j >= 10)
  50. {
  51. if (!rflag) ++outline;
  52. putc('\n', output_file);
  53. j = 1;
  54. }
  55. else
  56. ++j;
  57. fprintf(output_file, "%5d,", symbol_value[rlhs[i]]);
  58. }
  59. if (!rflag) outline += 2;
  60. fprintf(output_file, "\n};\n");
  61. if (!rflag)
  62. fprintf(output_file, "static ");
  63. fprintf(output_file, "int yylen[] = {%42d,", 2);
  64. j = 10;
  65. for (i = 3; i < nrules; i++)
  66. {
  67. if (j >= 10)
  68. {
  69. if (!rflag) ++outline;
  70. putc('\n', output_file);
  71. j = 1;
  72. }
  73. else
  74. j++;
  75. fprintf(output_file, "%5d,", rrhs[i + 1] - rrhs[i] - 1);
  76. }
  77. if (!rflag) outline += 2;
  78. fprintf(output_file, "\n};\n");
  79. }
  80. void output_yydefred()
  81. {
  82. register int i, j;
  83. if (!rflag)
  84. fprintf(output_file, "static ");
  85. fprintf(output_file, "int yydefred[] = {%39d,",
  86. (defred[0] ? defred[0] - 2 : 0));
  87. j = 10;
  88. for (i = 1; i < nstates; i++)
  89. {
  90. if (j < 10)
  91. ++j;
  92. else
  93. {
  94. if (!rflag) ++outline;
  95. putc('\n', output_file);
  96. j = 1;
  97. }
  98. fprintf(output_file, "%5d,", (defred[i] ? defred[i] - 2 : 0));
  99. }
  100. if (!rflag) outline += 2;
  101. fprintf(output_file, "\n};\n");
  102. }
  103. void output_actions()
  104. {
  105. nvectors = 3*nstates + nvars;
  106. froms = NEW2(nvectors, Yshort *);
  107. tos = NEW2(nvectors, Yshort *);
  108. tally = NEW2(nvectors, Yshort);
  109. width = NEW2(nvectors, Yshort);
  110. if (SRtotal+RRtotal)
  111. conflicts = NEW2(4*(SRtotal+RRtotal), Yshort);
  112. else
  113. conflicts = 0;
  114. nconflicts = 0;
  115. token_actions();
  116. FREE(lookaheads);
  117. FREE(LA);
  118. FREE(LAruleno);
  119. FREE(accessing_symbol);
  120. goto_actions();
  121. FREE(goto_map + ntokens);
  122. FREE(from_state);
  123. FREE(to_state);
  124. sort_actions();
  125. pack_table();
  126. output_base();
  127. output_table();
  128. output_check();
  129. output_ctable();
  130. }
  131. int find_conflict_base(int cbase)
  132. {
  133. int i,j;
  134. for (i=0; i<cbase; i++) {
  135. for (j=0; j+cbase < nconflicts; j++) {
  136. if (conflicts[i+j] != conflicts[cbase+j])
  137. break; }
  138. if (j+cbase >= nconflicts)
  139. return i; }
  140. return cbase;
  141. }
  142. void token_actions()
  143. {
  144. register int i, j;
  145. register int shiftcount, reducecount, conflictcount, csym, cbase;
  146. register int max, min;
  147. register Yshort *actionrow, *r, *s;
  148. register action *p;
  149. actionrow = NEW2(3*ntokens, Yshort);
  150. for (i = 0; i < nstates; ++i) {
  151. if (parser[i]) {
  152. for (j = 0; j < 3*ntokens; ++j)
  153. actionrow[j] = 0;
  154. shiftcount = 0;
  155. reducecount = 0;
  156. conflictcount = 0;
  157. csym = -1;
  158. cbase = nconflicts;
  159. for (p = parser[i]; p; p = p->next) {
  160. if (csym != -1 && csym != p->symbol) {
  161. conflictcount++;
  162. conflicts[nconflicts++] = -1;
  163. j = find_conflict_base(cbase);
  164. actionrow[csym + 2*ntokens] = j + 1;
  165. if (j == cbase) {
  166. cbase = nconflicts; }
  167. else {
  168. if (conflicts[cbase] == -1) cbase++;
  169. nconflicts = cbase; }
  170. csym = -1; }
  171. if (p->suppressed == 0) {
  172. if (p->action_code == SHIFT) {
  173. ++shiftcount;
  174. actionrow[p->symbol] = p->number; }
  175. else if (p->action_code == REDUCE &&
  176. p->number != defred[i]) {
  177. ++reducecount;
  178. actionrow[p->symbol + ntokens] = p->number; } }
  179. else if (p->suppressed == 1) {
  180. csym = p->symbol;
  181. if (p->action_code == SHIFT) {
  182. conflicts[nconflicts++] = p->number; }
  183. else if (p->action_code == REDUCE &&
  184. p->number != defred[i]) {
  185. if (cbase == nconflicts) {
  186. if (cbase) cbase--;
  187. else conflicts[nconflicts++] = -1; }
  188. conflicts[nconflicts++] = p->number - 2; } } }
  189. if (csym != -1) {
  190. conflictcount++;
  191. conflicts[nconflicts++] = -1;
  192. j = find_conflict_base(cbase);
  193. actionrow[csym + 2*ntokens] = j + 1;
  194. if (j == cbase) {
  195. cbase = nconflicts; }
  196. else {
  197. if (conflicts[cbase] == -1) cbase++;
  198. nconflicts = cbase; } }
  199. tally[i] = shiftcount;
  200. tally[nstates+i] = reducecount;
  201. tally[2*nstates+i] = conflictcount;
  202. width[i] = 0;
  203. width[nstates+i] = 0;
  204. width[2*nstates+i] = 0;
  205. if (shiftcount > 0) {
  206. froms[i] = r = NEW2(shiftcount, Yshort);
  207. tos[i] = s = NEW2(shiftcount, Yshort);
  208. min = MAXSHORT;
  209. max = 0;
  210. for (j = 0; j < ntokens; ++j) {
  211. if (actionrow[j]) {
  212. if (min > symbol_value[j])
  213. min = symbol_value[j];
  214. if (max < symbol_value[j])
  215. max = symbol_value[j];
  216. *r++ = symbol_value[j];
  217. *s++ = actionrow[j]; } }
  218. width[i] = max - min + 1; }
  219. if (reducecount > 0) {
  220. froms[nstates+i] = r = NEW2(reducecount, Yshort);
  221. tos[nstates+i] = s = NEW2(reducecount, Yshort);
  222. min = MAXSHORT;
  223. max = 0;
  224. for (j = 0; j < ntokens; ++j) {
  225. if (actionrow[ntokens+j]) {
  226. if (min > symbol_value[j])
  227. min = symbol_value[j];
  228. if (max < symbol_value[j])
  229. max = symbol_value[j];
  230. *r++ = symbol_value[j];
  231. *s++ = actionrow[ntokens+j] - 2; } }
  232. width[nstates+i] = max - min + 1; }
  233. if (conflictcount > 0) {
  234. froms[2*nstates+i] = r = NEW2(conflictcount, Yshort);
  235. tos[2*nstates+i] = s = NEW2(conflictcount, Yshort);
  236. min = MAXSHORT;
  237. max = 0;
  238. for (j = 0; j < ntokens; ++j) {
  239. if (actionrow[2*ntokens+j]) {
  240. if (min > symbol_value[j])
  241. min = symbol_value[j];
  242. if (max < symbol_value[j])
  243. max = symbol_value[j];
  244. *r++ = symbol_value[j];
  245. *s++ = actionrow[2*ntokens+j] - 1; } }
  246. width[2*nstates+i] = max - min + 1; } } }
  247. FREE(actionrow);
  248. }
  249. void goto_actions()
  250. {
  251. register int i, j, k;
  252. state_count = NEW2(nstates, Yshort);
  253. k = default_goto(start_symbol + 1);
  254. if (!rflag)
  255. fprintf(output_file, "static ");
  256. fprintf(output_file, "int yydgoto[] = {%40d,", k);
  257. save_column(start_symbol + 1, k);
  258. j = 10;
  259. for (i = start_symbol + 2; i < nsyms; i++)
  260. {
  261. if (j >= 10)
  262. {
  263. if (!rflag) ++outline;
  264. putc('\n', output_file);
  265. j = 1;
  266. }
  267. else
  268. ++j;
  269. k = default_goto(i);
  270. fprintf(output_file, "%5d,", k);
  271. save_column(i, k);
  272. }
  273. if (!rflag) outline += 2;
  274. fprintf(output_file, "\n};\n");
  275. FREE(state_count);
  276. }
  277. int default_goto(int symbol)
  278. {
  279. register int i;
  280. register int m;
  281. register int n;
  282. register int default_state;
  283. register int max;
  284. m = goto_map[symbol];
  285. n = goto_map[symbol + 1];
  286. if (m == n) return (0);
  287. for (i = 0; i < nstates; i++)
  288. state_count[i] = 0;
  289. for (i = m; i < n; i++)
  290. state_count[to_state[i]]++;
  291. max = 0;
  292. default_state = 0;
  293. for (i = 0; i < nstates; i++)
  294. {
  295. if (state_count[i] > max)
  296. {
  297. max = state_count[i];
  298. default_state = i;
  299. }
  300. }
  301. return (default_state);
  302. }
  303. void save_column(int symbol, int default_state)
  304. {
  305. register int i;
  306. register int m;
  307. register int n;
  308. register Yshort *sp;
  309. register Yshort *sp1;
  310. register Yshort *sp2;
  311. register int count;
  312. register int symno;
  313. m = goto_map[symbol];
  314. n = goto_map[symbol + 1];
  315. count = 0;
  316. for (i = m; i < n; i++)
  317. {
  318. if (to_state[i] != default_state)
  319. ++count;
  320. }
  321. if (count == 0) return;
  322. symno = symbol_value[symbol] + 3*nstates;
  323. froms[symno] = sp1 = sp = NEW2(count, Yshort);
  324. tos[symno] = sp2 = NEW2(count, Yshort);
  325. for (i = m; i < n; i++)
  326. {
  327. if (to_state[i] != default_state)
  328. {
  329. *sp1++ = from_state[i];
  330. *sp2++ = to_state[i];
  331. }
  332. }
  333. tally[symno] = count;
  334. width[symno] = sp1[-1] - sp[0] + 1;
  335. }
  336. void sort_actions()
  337. {
  338. register int i;
  339. register int j;
  340. register int k;
  341. register int t;
  342. register int w;
  343. order = NEW2(nvectors, Yshort);
  344. nentries = 0;
  345. for (i = 0; i < nvectors; i++)
  346. {
  347. if (tally[i] > 0)
  348. {
  349. t = tally[i];
  350. w = width[i];
  351. j = nentries - 1;
  352. while (j >= 0 && (width[order[j]] < w))
  353. j--;
  354. while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
  355. j--;
  356. for (k = nentries - 1; k > j; k--)
  357. order[k + 1] = order[k];
  358. order[j + 1] = i;
  359. nentries++;
  360. }
  361. }
  362. }
  363. void pack_table()
  364. {
  365. register int i;
  366. register int place;
  367. register int state;
  368. base = NEW2(nvectors, Yshort);
  369. pos = NEW2(nentries, Yshort);
  370. maxtable = 1000;
  371. table = NEW2(maxtable, Yshort);
  372. check = NEW2(maxtable, Yshort);
  373. lowzero = 0;
  374. high = 0;
  375. for (i = 0; i < maxtable; i++)
  376. check[i] = -1;
  377. for (i = 0; i < nentries; i++)
  378. {
  379. state = matching_vector(i);
  380. if (state < 0)
  381. place = pack_vector(i);
  382. else
  383. place = base[state];
  384. pos[i] = place;
  385. base[order[i]] = place;
  386. }
  387. for (i = 0; i < nvectors; i++)
  388. {
  389. if (froms[i])
  390. FREE(froms[i]);
  391. if (tos[i])
  392. FREE(tos[i]);
  393. }
  394. FREE(froms);
  395. FREE(tos);
  396. FREE(tally);
  397. FREE(width);
  398. FREE(pos);
  399. }
  400. /* The function matching_vector determines if the vector specified by */
  401. /* the input parameter matches a previously considered vector. The */
  402. /* test at the start of the function checks if the vector represents */
  403. /* a row of shifts over terminal symbols or a row of reductions, or a */
  404. /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
  405. /* check if a column of shifts over a nonterminal symbols matches a */
  406. /* previously considered vector. Because of the nature of LR parsing */
  407. /* tables, no two columns can match. Therefore, the only possible */
  408. /* match would be between a row and a column. Such matches are */
  409. /* unlikely. Therefore, to save time, no attempt is made to see if a */
  410. /* column matches a previously considered vector. */
  411. /* */
  412. /* Matching_vector is poorly designed. The test could easily be made */
  413. /* faster. Also, it depends on the vectors being in a specific */
  414. /* order. */
  415. /* Not really any point in checking for matching conflicts -- it is */
  416. /* extremely unlikely to occur, and conflicts are (hopefully) rare. */
  417. int matching_vector(int vector)
  418. {
  419. register int i;
  420. register int j;
  421. register int k;
  422. register int t;
  423. register int w;
  424. register int match;
  425. register int prev;
  426. i = order[vector];
  427. if (i >= 2*nstates)
  428. return (-1);
  429. t = tally[i];
  430. w = width[i];
  431. for (prev = vector - 1; prev >= 0; prev--)
  432. {
  433. j = order[prev];
  434. if (width[j] != w || tally[j] != t)
  435. return (-1);
  436. match = 1;
  437. for (k = 0; match && k < t; k++)
  438. {
  439. if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
  440. match = 0;
  441. }
  442. if (match)
  443. return (j);
  444. }
  445. return (-1);
  446. }
  447. int pack_vector(int vector)
  448. {
  449. register int i, j, k, l;
  450. register int t;
  451. register int loc;
  452. register int ok;
  453. register Yshort *from;
  454. register Yshort *to;
  455. int newmax;
  456. i = order[vector];
  457. t = tally[i];
  458. assert(t);
  459. from = froms[i];
  460. to = tos[i];
  461. j = lowzero - from[0];
  462. for (k = 1; k < t; ++k)
  463. if (lowzero - from[k] > j)
  464. j = lowzero - from[k];
  465. for (;; ++j)
  466. {
  467. if (j == 0)
  468. continue;
  469. ok = 1;
  470. for (k = 0; ok && k < t; k++)
  471. {
  472. loc = j + from[k];
  473. if (loc >= maxtable)
  474. {
  475. if (loc >= MAXTABLE)
  476. fatal("maximum table size exceeded");
  477. newmax = maxtable;
  478. do { newmax += 200; } while (newmax <= loc);
  479. table = (Yshort *) REALLOC(table, newmax*sizeof(Yshort));
  480. if (table == 0) no_space();
  481. check = (Yshort *) REALLOC(check, newmax*sizeof(Yshort));
  482. if (check == 0) no_space();
  483. for (l = maxtable; l < newmax; ++l)
  484. {
  485. table[l] = 0;
  486. check[l] = -1;
  487. }
  488. maxtable = newmax;
  489. }
  490. if (check[loc] != -1)
  491. ok = 0;
  492. }
  493. for (k = 0; ok && k < vector; k++)
  494. {
  495. if (pos[k] == j)
  496. ok = 0;
  497. }
  498. if (ok)
  499. {
  500. for (k = 0; k < t; k++)
  501. {
  502. loc = j + from[k];
  503. table[loc] = to[k];
  504. check[loc] = from[k];
  505. if (loc > high) high = loc;
  506. }
  507. while (check[lowzero] != -1)
  508. ++lowzero;
  509. return (j);
  510. }
  511. }
  512. }
  513. void output_base()
  514. {
  515. register int i, j;
  516. if (!rflag)
  517. fprintf(output_file, "static ");
  518. fprintf(output_file, "int yysindex[] = {%39d,", base[0]);
  519. j = 10;
  520. for (i = 1; i < nstates; i++) {
  521. if (j >= 10) {
  522. if (!rflag) ++outline;
  523. putc('\n', output_file);
  524. j = 1; }
  525. else
  526. ++j;
  527. fprintf(output_file, "%5d,", base[i]); }
  528. if (!rflag) outline += 2;
  529. fprintf(output_file, "\n};\n");
  530. if (!rflag)
  531. fprintf(output_file, "static ");
  532. fprintf(output_file, "int yyrindex[] = {%39d,", base[nstates]);
  533. j = 10;
  534. for (i = nstates + 1; i < 2*nstates; i++) {
  535. if (j >= 10) {
  536. if (!rflag) ++outline;
  537. putc('\n', output_file);
  538. j = 1; }
  539. else
  540. ++j;
  541. fprintf(output_file, "%5d,", base[i]); }
  542. if (!rflag) outline += 2;
  543. fprintf(output_file, "\n};\n");
  544. if (!rflag)
  545. fprintf(output_file, "static ");
  546. fprintf(output_file, "int yycindex[] = {%39d,", base[2*nstates]);
  547. j = 10;
  548. for (i = 2*nstates + 1; i < 3*nstates; i++) {
  549. if (j >= 10) {
  550. if (!rflag) ++outline;
  551. putc('\n', output_file);
  552. j = 1; }
  553. else
  554. ++j;
  555. fprintf(output_file, "%5d,", base[i]); }
  556. if (!rflag) outline += 2;
  557. fprintf(output_file, "\n};\n");
  558. if (!rflag)
  559. fprintf(output_file, "static ");
  560. fprintf(output_file, "int yygindex[] = {%39d,",
  561. base[3*nstates]);
  562. j = 10;
  563. for (i = 3*nstates + 1; i < nvectors - 1; i++) {
  564. if (j >= 10) {
  565. if (!rflag) ++outline;
  566. putc('\n', output_file);
  567. j = 1; }
  568. else
  569. ++j;
  570. fprintf(output_file, "%5d,", base[i]); }
  571. if (!rflag) outline += 2;
  572. fprintf(output_file, "\n};\n");
  573. FREE(base);
  574. }
  575. void output_table()
  576. {
  577. register int i;
  578. register int j;
  579. ++outline;
  580. fprintf(stderr, "YYTABLESIZE: %d\n", high);
  581. if(high >= MAXSHORT) {
  582. fprintf(stderr, "Table is longer than %d elements. It's not gonna fly.\n", MAXSHORT);
  583. exit(1);
  584. }
  585. fprintf(code_file, "#define YYTABLESIZE %d\n", high);
  586. if (!rflag)
  587. fprintf(output_file, "static ");
  588. fprintf(output_file, "int yytable[] = {%40d,", table[0]);
  589. j = 10;
  590. for (i = 1; i <= high; i++)
  591. {
  592. if (j >= 10)
  593. {
  594. if (!rflag) ++outline;
  595. putc('\n', output_file);
  596. j = 1;
  597. }
  598. else
  599. ++j;
  600. fprintf(output_file, "%5d,", table[i]);
  601. }
  602. if (!rflag) outline += 2;
  603. fprintf(output_file, "\n};\n");
  604. FREE(table);
  605. }
  606. void output_check()
  607. {
  608. register int i;
  609. register int j;
  610. if (!rflag)
  611. fprintf(output_file, "static ");
  612. fprintf(output_file, "int yycheck[] = {%40d,", check[0]);
  613. j = 10;
  614. for (i = 1; i <= high; i++)
  615. {
  616. if (j >= 10)
  617. {
  618. if (!rflag) ++outline;
  619. putc('\n', output_file);
  620. j = 1;
  621. }
  622. else
  623. ++j;
  624. fprintf(output_file, "%5d,", check[i]);
  625. }
  626. if (!rflag) outline += 2;
  627. fprintf(output_file, "\n};\n");
  628. FREE(check);
  629. }
  630. void output_ctable()
  631. {
  632. register int i;
  633. register int j;
  634. if (!rflag)
  635. fprintf(output_file, "static ");
  636. fprintf(output_file, "int yyctable[] = {%39d,", conflicts ?
  637. conflicts[0] : 0);
  638. j = 10;
  639. for (i = 1; i < nconflicts; i++)
  640. {
  641. if (j >= 10)
  642. {
  643. if (!rflag) ++outline;
  644. putc('\n', output_file);
  645. j = 1;
  646. }
  647. else
  648. ++j;
  649. fprintf(output_file, "%5d,", conflicts[i]);
  650. }
  651. if (!rflag) outline += 2;
  652. fprintf(output_file, "\n};\n");
  653. if (conflicts)
  654. FREE(conflicts);
  655. }
  656. int is_C_identifier(char *name)
  657. {
  658. register char *s;
  659. register int c;
  660. s = name;
  661. c = *s;
  662. if (c == '"')
  663. {
  664. c = *++s;
  665. if (!isalpha(c) && c != '_' && c != '$')
  666. return (0);
  667. while ((c = *++s) != '"')
  668. {
  669. if (!isalnum(c) && c != '_' && c != '$')
  670. return (0);
  671. }
  672. return (1);
  673. }
  674. if (!isalpha(c) && c != '_' && c != '$')
  675. return (0);
  676. while ((c = *++s))
  677. {
  678. if (!isalnum(c) && c != '_' && c != '$')
  679. return (0);
  680. }
  681. return (1);
  682. }
  683. void output_defines()
  684. {
  685. register int c, i;
  686. register char *s;
  687. FILE *dc_file;
  688. /* ASF: changed to separate (_yacc_defines_keywords and _yacc_defines_yystype instead of
  689. _yacc_defines_h_) keyword definitions of YYSTYPE because keywords may conflict with other
  690. things */
  691. if(dflag) {
  692. /***
  693. fprintf(defines_file, "#ifndef _yacc_defines_h_\n");
  694. fprintf(defines_file, "#define _yacc_defines_h_\n\n");
  695. ***/
  696. fprintf(defines_file, "#ifndef _yacc_defines_keywords\n");
  697. fprintf(defines_file, "#define _yacc_defines_keywords\n\n");
  698. }
  699. /* VM: Print to either code file or defines file but not to both */
  700. dc_file = dflag ? defines_file : code_file;
  701. for (i = 2; i < ntokens; ++i)
  702. {
  703. s = symbol_name[i];
  704. if (is_C_identifier(s))
  705. {
  706. fprintf(dc_file, "#define ");
  707. c = *s;
  708. if (c == '"')
  709. {
  710. while ((c = *++s) != '"')
  711. {
  712. putc(c, dc_file);
  713. }
  714. }
  715. else
  716. {
  717. do
  718. {
  719. putc(c, dc_file);
  720. }
  721. while ((c = *++s));
  722. }
  723. ++outline;
  724. fprintf(dc_file, " %d\n", symbol_value[i]);
  725. }
  726. }
  727. ++outline;
  728. fprintf(dc_file, "#define YYERRCODE %d\n", symbol_value[1]);
  729. if(dflag) {
  730. fprintf(defines_file, "\n#endif /* _yacc_defines_keywords */\n\n");
  731. }
  732. if (dflag && unionized)
  733. {
  734. fprintf(defines_file, "#ifndef _yacc_defines_yystype\n");
  735. fprintf(defines_file, "#define _yacc_defines_yystype\n");
  736. fclose(union_file);
  737. union_file = fopen(union_file_name, "r");
  738. if (union_file == NULL) open_error(union_file_name);
  739. while ((c = getc(union_file)) != EOF) {
  740. putc(c, defines_file);
  741. }
  742. /* ASF: we define it on the Parser class
  743. fprintf(defines_file, "extern YYSTYPE yylval;\n"); */
  744. fprintf(defines_file, "\n#endif /* _yacc_defines_yystype */\n\n");
  745. }
  746. /***
  747. if(dflag) {
  748. fprintf(defines_file, "\n#endif\n");
  749. }
  750. ***/
  751. }
  752. void output_stored_text()
  753. {
  754. register int c;
  755. register FILE *in, *out;
  756. register int state; /* 0=middle of line, 1=start of line, 2=seen '#' */
  757. state = 1;
  758. fclose(text_file);
  759. text_file = fopen(text_file_name, "r");
  760. if (text_file == NULL)
  761. open_error(text_file_name);
  762. in = text_file;
  763. if ((c = getc(in)) == EOF)
  764. return;
  765. out = code_file;
  766. do {
  767. if (c == '\n') {
  768. ++outline;
  769. if (state == 2) {
  770. fprintf(out, line_format+1, outline + 1, code_file_name);
  771. state = 1;
  772. continue; }
  773. state = 1; }
  774. else if (state == 1 && c == '#')
  775. state = 2;
  776. else
  777. state = 0;
  778. putc(c, out);
  779. } while ((c = getc(in)) != EOF);
  780. if (!lflag)
  781. fprintf(out, line_format, ++outline + 1, code_file_name);
  782. }
  783. void output_debug()
  784. {
  785. register int i, j, k, max;
  786. char **symnam, *s;
  787. ++outline;
  788. fprintf(code_file, "#define YYFINAL %d\n", final_state);
  789. outline += 3;
  790. fprintf(code_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
  791. tflag);
  792. if (rflag)
  793. fprintf(output_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
  794. tflag);
  795. max = 0;
  796. for (i = 2; i < ntokens; ++i)
  797. if (symbol_value[i] > max)
  798. max = symbol_value[i];
  799. ++outline;
  800. fprintf(code_file, "#define YYMAXTOKEN %d\n", max);
  801. symnam = (char **) MALLOC((max+1)*sizeof(char *));
  802. if (symnam == 0) no_space();
  803. /* Note that it is not necessary to initialize the element */
  804. /* symnam[max]. */
  805. for (i = 0; i < max; ++i)
  806. symnam[i] = 0;
  807. for (i = ntokens - 1; i >= 2; --i)
  808. symnam[symbol_value[i]] = symbol_name[i];
  809. symnam[0] = "end-of-file";
  810. if (!rflag) ++outline;
  811. fprintf(output_file, "#if YYDEBUG\n");
  812. if (!rflag)
  813. fprintf(output_file, "static ");
  814. fprintf(output_file, "const char *yyname[] = {");
  815. j = 80;
  816. for (i = 0; i <= max; ++i)
  817. {
  818. if ((s = symnam[i]))
  819. {
  820. if (s[0] == '"')
  821. {
  822. k = 7;
  823. while (*++s != '"')
  824. {
  825. ++k;
  826. if (*s == '\\')
  827. {
  828. k += 2;
  829. if (*++s == '\\')
  830. ++k;
  831. }
  832. }
  833. j += k;
  834. if (j > 80)
  835. {
  836. if (!rflag) ++outline;
  837. putc('\n', output_file);
  838. j = k;
  839. }
  840. fprintf(output_file, "\"\\\"");
  841. s = symnam[i];
  842. while (*++s != '"')
  843. {
  844. if (*s == '\\')
  845. {
  846. fprintf(output_file, "\\\\");
  847. if (*++s == '\\')
  848. fprintf(output_file, "\\\\");
  849. else
  850. putc(*s, output_file);
  851. }
  852. else
  853. putc(*s, output_file);
  854. }
  855. fprintf(output_file, "\\\"\",");
  856. }
  857. else if (s[0] == '\'')
  858. {
  859. if (s[1] == '"')
  860. {
  861. j += 7;
  862. if (j > 80)
  863. {
  864. if (!rflag) ++outline;
  865. putc('\n', output_file);
  866. j = 7;
  867. }
  868. fprintf(output_file, "\"'\\\"'\",");
  869. }
  870. else
  871. {
  872. k = 5;
  873. while (*++s != '\'')
  874. {
  875. ++k;
  876. if (*s == '\\')
  877. {
  878. k += 2;
  879. if (*++s == '\\')
  880. ++k;
  881. }
  882. }
  883. j += k;
  884. if (j > 80)
  885. {
  886. if (!rflag) ++outline;
  887. putc('\n', output_file);
  888. j = k;
  889. }
  890. fprintf(output_file, "\"'");
  891. s = symnam[i];
  892. while (*++s != '\'')
  893. {
  894. if (*s == '\\')
  895. {
  896. fprintf(output_file, "\\\\");
  897. if (*++s == '\\')
  898. fprintf(output_file, "\\\\");
  899. else
  900. putc(*s, output_file);
  901. }
  902. else
  903. putc(*s, output_file);
  904. }
  905. fprintf(output_file, "'\",");
  906. }
  907. }
  908. else
  909. {
  910. k = strlen(s) + 3;
  911. j += k;
  912. if (j > 80)
  913. {
  914. if (!rflag) ++outline;
  915. putc('\n', output_file);
  916. j = k;
  917. }
  918. putc('"', output_file);
  919. do { putc(*s, output_file); } while (*++s);
  920. fprintf(output_file, "\",");
  921. }
  922. }
  923. else
  924. {
  925. j += 2;
  926. if (j > 80)
  927. {
  928. if (!rflag) ++outline;
  929. putc('\n', output_file);
  930. j = 2;
  931. }
  932. fprintf(output_file, "0,");
  933. }
  934. }
  935. if (!rflag) outline += 2;
  936. fprintf(output_file, "\n};\n");
  937. FREE(symnam);
  938. if (!rflag) ++outline;
  939. if (!rflag)
  940. fprintf(output_file, "static ");
  941. fprintf(output_file, "const char *yyrule[] = {\n");
  942. for (i = 2; i < nrules; ++i)
  943. {
  944. fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
  945. for (j = rrhs[i]; ritem[j] > 0; ++j)
  946. {
  947. s = symbol_name[ritem[j]];
  948. if (s[0] == '"')
  949. {
  950. fprintf(output_file, " \\\"");
  951. while (*++s != '"')
  952. {
  953. if (*s == '\\')
  954. {
  955. if (s[1] == '\\')
  956. fprintf(output_file, "\\\\\\\\");
  957. else
  958. fprintf(output_file, "\\\\%c", s[1]);
  959. ++s;
  960. }
  961. else
  962. putc(*s, output_file);
  963. }
  964. fprintf(output_file, "\\\"");
  965. }
  966. else if (s[0] == '\'')
  967. {
  968. if (s[1] == '"')
  969. fprintf(output_file, " '\\\"'");
  970. else if (s[1] == '\\')
  971. {
  972. if (s[2] == '\\')
  973. fprintf(output_file, " '\\\\\\\\");
  974. else
  975. fprintf(output_file, " '\\\\%c", s[2]);
  976. s += 2;
  977. while (*++s != '\'')
  978. putc(*s, output_file);
  979. putc('\'', output_file);
  980. }
  981. else
  982. fprintf(output_file, " '%c'", s[1]);
  983. }
  984. else
  985. fprintf(output_file, " %s", s);
  986. }
  987. if (!rflag) ++outline;
  988. fprintf(output_file, "\",\n");
  989. }
  990. if (!rflag) outline += 2;
  991. fprintf(output_file, "};\n#endif\n");
  992. }
  993. void output_stype()
  994. {
  995. if (!unionized && ntags == 0)
  996. {
  997. outline += 3;
  998. fprintf(code_file, "#ifndef YYSTYPE\ntypedef int YYSTYPE;\n#endif\n");
  999. }
  1000. }
  1001. void output_trailing_text()
  1002. {
  1003. register int c, last;
  1004. register FILE *in, *out;
  1005. if (line == 0)
  1006. return;
  1007. in = input_file;
  1008. out = code_file;
  1009. c = *cptr;
  1010. if (c == '\n')
  1011. {
  1012. ++lineno;
  1013. if ((c = getc(in)) == EOF)
  1014. return;
  1015. if (!lflag)
  1016. {
  1017. ++outline;
  1018. fprintf(out, line_format, lineno, (inc_file?inc_file_name:input_file_name));
  1019. }
  1020. if (c == '\n')
  1021. ++outline;
  1022. putc(c, out);
  1023. last = c;
  1024. }
  1025. else
  1026. {
  1027. if (!lflag)
  1028. {
  1029. ++outline;
  1030. fprintf(out, line_format, lineno, (inc_file?inc_file_name:input_file_name));
  1031. }
  1032. do { putc(c, out); } while ((c = *++cptr) != '\n');
  1033. ++outline;
  1034. putc('\n', out);
  1035. last = '\n';
  1036. }
  1037. while ((c = getc(in)) != EOF)
  1038. {
  1039. if (c == '\n')
  1040. ++outline;
  1041. putc(c, out);
  1042. last = c;
  1043. }
  1044. if (last != '\n')
  1045. {
  1046. ++outline;
  1047. putc('\n', out);
  1048. }
  1049. if (!lflag)
  1050. fprintf(out, line_format, ++outline + 1, code_file_name);
  1051. }
  1052. void output_semantic_actions()
  1053. {
  1054. register int c, last;
  1055. register FILE *out;
  1056. register int state; /* 0=middle of line, 1=start of line, 2=seen '#' */
  1057. state = 1;
  1058. fclose(action_file);
  1059. action_file = fopen(action_file_name, "r");
  1060. if (action_file == NULL)
  1061. open_error(action_file_name);
  1062. if ((c = getc(action_file)) == EOF)
  1063. return;
  1064. out = code_file;
  1065. do {
  1066. last = c;
  1067. if (c == '\n') {
  1068. ++outline;
  1069. if (state == 2) {
  1070. fprintf(out, line_format+1, outline + 1, code_file_name);
  1071. state = 1;
  1072. continue; }
  1073. state = 1; }
  1074. else if (state == 1 && c == '#')
  1075. state = 2;
  1076. else
  1077. state = 0;
  1078. putc(c, out);
  1079. } while ((c = getc(action_file)) != EOF);
  1080. if (last != '\n')
  1081. {
  1082. ++outline;
  1083. putc('\n', out);
  1084. }
  1085. if (!lflag)
  1086. fprintf(out, line_format, ++outline + 1, code_file_name);
  1087. }
  1088. void free_itemsets()
  1089. {
  1090. register core *cp, *next;
  1091. FREE(state_table);
  1092. for (cp = first_state; cp; cp = next)
  1093. {
  1094. next = cp->next;
  1095. FREE(cp);
  1096. }
  1097. }
  1098. void free_shifts()
  1099. {
  1100. register shifts *sp, *next;
  1101. FREE(shift_table);
  1102. for (sp = first_shift; sp; sp = next)
  1103. {
  1104. next = sp->next;
  1105. FREE(sp);
  1106. }
  1107. }
  1108. void free_reductions()
  1109. {
  1110. register reductions *rp, *next;
  1111. FREE(reduction_table);
  1112. for (rp = first_reduction; rp; rp = next)
  1113. {
  1114. next = rp->next;
  1115. FREE(rp);
  1116. }
  1117. }
  1118. void write_section(char *section_name)
  1119. {
  1120. char **section;
  1121. FILE *fp;
  1122. int i;
  1123. struct section *sl;
  1124. for(sl=&section_list[0]; sl->name; sl++) {
  1125. if(strcmp(sl->name,section_name)==0) {
  1126. break;
  1127. }
  1128. }
  1129. if(sl->name==0) {
  1130. fprintf(stderr, "Cannot find section '%s' in your skeleton file\n", section_name);
  1131. exit(1);
  1132. }
  1133. section = sl->ptr;
  1134. fp = code_file;
  1135. for (i = lflag ? 1 : 0; section[i]; ++i)
  1136. {
  1137. ++outline;
  1138. fprintf(fp, "%s\n", section[i]);
  1139. }
  1140. }