Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1438 lines
33 KiB

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