output.c 20 KB

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