output.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  1. /* Output the generated parsing program for bison,
  2. Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3. This file is part of Bison, the GNU Compiler Compiler.
  4. Bison is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 1, or (at your option)
  7. any later version.
  8. Bison is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Bison; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. /* functions to output parsing data to various files. Entries are:
  16. output_headers ()
  17. Output constant strings to the beginning of certain files.
  18. output_trailers()
  19. Output constant strings to the ends of certain files.
  20. output ()
  21. Output the parsing tables and the parser code to ftable.
  22. The parser tables consist of: (starred ones needed only for the semantic parser)
  23. yytranslate = vector mapping yylex's token numbers into bison's token numbers.
  24. yytname = vector of string-names indexed by bison token number
  25. yyrline = vector of line-numbers of all rules. For yydebug printouts.
  26. * yyrhs = vector of items of all rules.
  27. This is exactly what ritems contains.
  28. * yyprhs[r] = index in yyrhs of first item for rule r.
  29. yyr1[r] = symbol number of symbol that rule r derives.
  30. yyr2[r] = number of symbols composing right hand side of rule r.
  31. * yystos[s] = the symbol number of the symbol that leads to state s.
  32. yydefact[s] = default rule to reduce with in state s,
  33. when yytable doesn't specify something else to do.
  34. Zero means the default is an error.
  35. yydefgoto[i] = default state to go to after a reduction of a rule that
  36. generates variable ntokens + i, except when yytable
  37. specifies something else to do.
  38. yypact[s] = index in yytable of the portion describing state s.
  39. The lookahed token's type is used to index that portion
  40. to find out what to do.
  41. If the value in yytable is positive,
  42. we shift the token and go to that state.
  43. If the value is negative, it is minus a rule number to reduce by.
  44. If the value is zero, the default action from yydefact[s] is used.
  45. yypgoto[i] = the index in yytable of the portion describing
  46. what to do after reducing a rule that derives variable i + ntokens.
  47. This portion is indexed by the parser state number
  48. as of before the text for this nonterminal was read.
  49. The value from yytable is the state to go to.
  50. yytable = a vector filled with portions for different uses,
  51. found via yypact and yypgoto.
  52. yycheck = a vector indexed in parallel with yytable.
  53. It indicates, in a roundabout way, the bounds of the
  54. portion you are trying to examine.
  55. Suppose that the portion of yytable starts at index p
  56. and the index to be examined within the portion is i.
  57. Then if yycheck[p+i] != i, i is outside the bounds
  58. of what is actually allocated, and the default
  59. (from yydefact or yydefgoto) should be used.
  60. Otherwise, yytable[p+i] should be used.
  61. YYFINAL = the state number of the termination state.
  62. YYFLAG = most negative short int. Used to flag ??
  63. YYNTBASE = ntokens.
  64. */
  65. #include <stdio.h>
  66. #include "machine.h"
  67. #include "new.h"
  68. #include "files.h"
  69. #include "gram.h"
  70. #include "state.h"
  71. #define MAXTABLE 32767
  72. extern int debugflag;
  73. extern int nolinesflag;
  74. extern char **tags;
  75. extern int tokensetsize;
  76. extern int final_state;
  77. extern core **state_table;
  78. extern shifts **shift_table;
  79. extern errs **err_table;
  80. extern reductions **reduction_table;
  81. extern short *accessing_symbol;
  82. extern unsigned *LA;
  83. extern short *LAruleno;
  84. extern short *lookaheads;
  85. extern char *consistent;
  86. extern short *goto_map;
  87. extern short *from_state;
  88. extern short *to_state;
  89. extern char *parser_name;
  90. void output_token_translations();
  91. void output_gram();
  92. void output_stos();
  93. void output_rule_data();
  94. void output_defines();
  95. void output_actions();
  96. void token_actions();
  97. void save_row();
  98. void goto_actions();
  99. void save_column();
  100. void sort_actions();
  101. void pack_table();
  102. void output_base();
  103. void output_table();
  104. void output_check();
  105. void output_parser();
  106. void output_program();
  107. void free_itemset();
  108. void free_shifts();
  109. void free_reductions();
  110. void free_itemsets();
  111. int action_row();
  112. int default_goto();
  113. int matching_state();
  114. int pack_vector();
  115. extern void berror();
  116. extern void fatals();
  117. static int nvectors;
  118. static int nentries;
  119. static short **froms;
  120. static short **tos;
  121. static short *tally;
  122. static short *width;
  123. static short *actrow;
  124. static short *state_count;
  125. static short *order;
  126. static short *base;
  127. static short *pos;
  128. static short *table;
  129. static short *check;
  130. static int lowzero;
  131. static int high;
  132. #define GUARDSTR "\n#include \"%s\"\nextern int yyerror;\n\
  133. extern int yycost;\nextern char * yymsg;\nextern YYSTYPE yyval;\n\n\
  134. yyguard(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
  135. register YYLTYPE *yylsp;\n\
  136. {\n yyerror = 0;\nyycost = 0;\n yymsg = 0;\nswitch (n)\n {"
  137. #define ACTSTR "\n#include \"%s\"\nextern YYSTYPE yyval;\
  138. \nextern int yychar;\
  139. yyaction(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
  140. register YYLTYPE *yylsp;\n{\n switch (n)\n{"
  141. #define ACTSTR_SIMPLE "\n switch (yyn) {\n"
  142. void
  143. output_headers()
  144. {
  145. int c;
  146. if (semantic_parser)
  147. fprintf(fguard, GUARDSTR, attrsfile);
  148. fprintf(faction, (semantic_parser ? ACTSTR : ACTSTR_SIMPLE), attrsfile);
  149. if (semantic_parser)
  150. fprintf(ftable, "#include \"%s\"\n", attrsfile);
  151. fprintf(ftable, "#include <stdio.h>\n#include <stdlib.h>\n");
  152. if (!semantic_parser) /* JF Put out other stuff */
  153. {
  154. rewind(fattrs);
  155. while ((c=getc(fattrs))!=EOF)
  156. putc(c,ftable);
  157. }
  158. fprintf(ftable, "#include \"%s\"\n\n",defsfile);
  159. }
  160. void
  161. output_trailers()
  162. {
  163. if (semantic_parser)
  164. {
  165. fprintf(fguard, "\n }\n}\n");
  166. fprintf(faction, "\n }\n}\n");
  167. }
  168. else
  169. fprintf(faction, "\n}\n");
  170. }
  171. void
  172. output()
  173. {
  174. int c;
  175. /* output_token_defines(ftable); / * JF put out token defines FIRST */
  176. if (debugflag)
  177. fprintf(ftable, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n\n",
  178. !!debugflag);
  179. free_itemsets();
  180. output_defines();
  181. output_token_translations();
  182. if (semantic_parser)
  183. output_gram();
  184. FREE(ritem);
  185. if (semantic_parser)
  186. output_stos();
  187. output_rule_data();
  188. output_actions();
  189. output_parser();
  190. output_program();
  191. }
  192. void
  193. output_token_translations()
  194. {
  195. register int i, j;
  196. /* register short *sp; JF unused */
  197. if (translations)
  198. {
  199. fprintf(ftable,
  200. "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
  201. max_user_token_number, nsyms);
  202. if (ntokens < 127) /* play it very safe; check maximum element value. */
  203. fprintf(ftable, "\nstatic const char yytranslate[] = { 0");
  204. else
  205. fprintf(ftable, "\nstatic const short yytranslate[] = { 0");
  206. j = 10;
  207. for (i = 1; i <= max_user_token_number; i++)
  208. {
  209. putc(',', ftable);
  210. if (j >= 10)
  211. {
  212. putc('\n', ftable);
  213. j = 1;
  214. }
  215. else
  216. {
  217. j++;
  218. }
  219. fprintf(ftable, "%6d", token_translations[i]);
  220. }
  221. fprintf(ftable, "\n};\n");
  222. }
  223. else
  224. {
  225. fprintf(ftable, "\n#define YYTRANSLATE(x) (x)\n");
  226. }
  227. }
  228. void
  229. output_gram()
  230. {
  231. register int i;
  232. register int j;
  233. register short *sp;
  234. fprintf(ftable, "\nstatic const short yyprhs[] = { 0");
  235. j = 10;
  236. for (i = 1; i <= nrules; i++)
  237. {
  238. putc(',', ftable);
  239. if (j >= 10)
  240. {
  241. putc('\n', ftable);
  242. j = 1;
  243. }
  244. else
  245. {
  246. j++;
  247. }
  248. fprintf(ftable, "%6d", rrhs[i]);
  249. }
  250. fprintf(ftable, "\n};\n\nstatic const short yyrhs[] = {%6d", ritem[0]);
  251. j = 10;
  252. for (sp = ritem + 1; *sp; sp++)
  253. {
  254. putc(',', ftable);
  255. if (j >= 10)
  256. {
  257. putc('\n', ftable);
  258. j = 1;
  259. }
  260. else
  261. {
  262. j++;
  263. }
  264. if (*sp > 0)
  265. fprintf(ftable, "%6d", *sp);
  266. else
  267. fprintf(ftable, " 0");
  268. }
  269. fprintf(ftable, "\n};\n");
  270. }
  271. void
  272. output_stos()
  273. {
  274. register int i;
  275. register int j;
  276. fprintf(ftable, "\nstatic const short yystos[] = { 0");
  277. j = 10;
  278. for (i = 1; i < nstates; i++)
  279. {
  280. putc(',', ftable);
  281. if (j >= 10)
  282. {
  283. putc('\n', ftable);
  284. j = 1;
  285. }
  286. else
  287. {
  288. j++;
  289. }
  290. fprintf(ftable, "%6d", accessing_symbol[i]);
  291. }
  292. fprintf(ftable, "\n};\n");
  293. }
  294. void
  295. output_rule_data()
  296. {
  297. register int i;
  298. register int j;
  299. fprintf(ftable, "\nstatic const short yyrline[] = { 0");
  300. j = 10;
  301. for (i = 1; i <= nrules; i++)
  302. {
  303. putc(',', ftable);
  304. if (j >= 10)
  305. {
  306. putc('\n', ftable);
  307. j = 1;
  308. }
  309. else
  310. {
  311. j++;
  312. }
  313. fprintf(ftable, "%6d", rline[i]);
  314. }
  315. /* Output the table of token names. */
  316. fprintf(ftable, "\n};\n\nstatic const char * const yytname[] = { 0");
  317. j = 10;
  318. for (i = 1; i <= ntokens; i++)
  319. {
  320. register char *p;
  321. putc(',', ftable);
  322. if (j >= 10)
  323. {
  324. putc('\n', ftable);
  325. j = 1;
  326. }
  327. else
  328. {
  329. j++;
  330. }
  331. putc ('\"', ftable);
  332. for (p = tags[i]; *p; p++)
  333. if (*p == '"' || *p == '\\')
  334. fprintf(ftable, "\\%c", *p);
  335. else if (*p == '\n')
  336. fprintf(ftable, "\\n");
  337. else if (*p == '\t')
  338. fprintf(ftable, "\\t");
  339. else if (*p == '\b')
  340. fprintf(ftable, "\\b");
  341. else if (*p < 040 || *p >= 0177)
  342. fprintf(ftable, "\\%03o", *p);
  343. else
  344. putc(*p, ftable);
  345. putc ('\"', ftable);
  346. }
  347. fprintf(ftable, "\n};\n\nstatic const short yyr1[] = { 0");
  348. j = 10;
  349. for (i = 1; i <= nrules; i++)
  350. {
  351. putc(',', ftable);
  352. if (j >= 10)
  353. {
  354. putc('\n', ftable);
  355. j = 1;
  356. }
  357. else
  358. {
  359. j++;
  360. }
  361. fprintf(ftable, "%6d", rlhs[i]);
  362. }
  363. FREE(rlhs + 1);
  364. fprintf(ftable, "\n};\n\nstatic const short yyr2[] = { 0");
  365. j = 10;
  366. for (i = 1; i < nrules; i++)
  367. {
  368. putc(',', ftable);
  369. if (j >= 10)
  370. {
  371. putc('\n', ftable);
  372. j = 1;
  373. }
  374. else
  375. {
  376. j++;
  377. }
  378. fprintf(ftable, "%6d", rrhs[i + 1] - rrhs[i] - 1);
  379. }
  380. putc(',', ftable);
  381. if (j >= 10)
  382. putc('\n', ftable);
  383. fprintf(ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1);
  384. FREE(rrhs + 1);
  385. }
  386. void
  387. output_defines()
  388. {
  389. fprintf(ftable, "\n\n#define\tYYFINAL\t\t%d\n", final_state);
  390. fprintf(ftable, "#define\tYYFLAG\t\t%d\n", MINSHORT);
  391. fprintf(ftable, "#define\tYYNTBASE\t%d\n", ntokens);
  392. }
  393. /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck. */
  394. void
  395. output_actions()
  396. {
  397. nvectors = nstates + nvars;
  398. froms = NEW2(nvectors, short *);
  399. tos = NEW2(nvectors, short *);
  400. tally = NEW2(nvectors, short);
  401. width = NEW2(nvectors, short);
  402. token_actions();
  403. free_shifts();
  404. free_reductions();
  405. FREE(lookaheads);
  406. FREE(LA);
  407. FREE(LAruleno);
  408. FREE(accessing_symbol);
  409. goto_actions();
  410. FREE(goto_map + ntokens);
  411. FREE(from_state);
  412. FREE(to_state);
  413. sort_actions();
  414. pack_table();
  415. output_base();
  416. output_table();
  417. output_check();
  418. }
  419. /* figure out the actions for the specified state, indexed by lookahead token type.
  420. The yydefact table is output now. The detailed info
  421. is saved for putting into yytable later. */
  422. void
  423. token_actions()
  424. {
  425. register int i;
  426. register int j;
  427. register int k;
  428. actrow = NEW2(ntokens, short);
  429. k = action_row(0);
  430. fprintf(ftable, "\nstatic const short yydefact[] = {%6d", k);
  431. save_row(0);
  432. j = 10;
  433. for (i = 1; i < nstates; i++)
  434. {
  435. putc(',', ftable);
  436. if (j >= 10)
  437. {
  438. putc('\n', ftable);
  439. j = 1;
  440. }
  441. else
  442. {
  443. j++;
  444. }
  445. k = action_row(i);
  446. fprintf(ftable, "%6d", k);
  447. save_row(i);
  448. }
  449. fprintf(ftable, "\n};\n");
  450. FREE(actrow);
  451. }
  452. /* Decide what to do for each type of token if seen as the lookahead token in specified state.
  453. The value returned is used as the default action (yydefact) for the state.
  454. In addition, actrow is filled with what to do for each kind of token,
  455. index by symbol number, with zero meaning do the default action.
  456. The value MINSHORT, a very negative number, means this situation
  457. is an error. The parser recognizes this value specially.
  458. This is where conflicts are resolved. The loop over lookahead rules
  459. considered lower-numbered rules last, and the last rule considered that likes
  460. a token gets to handle it. */
  461. int
  462. action_row(state)
  463. int state;
  464. {
  465. register int i;
  466. register int j;
  467. register int k;
  468. register int m;
  469. register int n;
  470. register int count;
  471. register int default_rule;
  472. register int nreds;
  473. register int max;
  474. register int rule;
  475. register int shift_state;
  476. register int symbol;
  477. register unsigned mask;
  478. register unsigned *wordp;
  479. register reductions *redp;
  480. register shifts *shiftp;
  481. register errs *errp;
  482. int nodefault = 0; /* set nonzero to inhibit having any default reduction */
  483. for (i = 0; i < ntokens; i++)
  484. actrow[i] = 0;
  485. default_rule = 0;
  486. nreds = 0;
  487. redp = reduction_table[state];
  488. if (redp)
  489. {
  490. nreds = redp->nreds;
  491. if (nreds >= 1)
  492. {
  493. /* loop over all the rules available here which require lookahead */
  494. m = lookaheads[state];
  495. n = lookaheads[state + 1];
  496. for (i = n - 1; i >= m; i--)
  497. {
  498. rule = - LAruleno[i];
  499. wordp = LA + i * tokensetsize;
  500. mask = 1;
  501. /* and find each token which the rule finds acceptable to come next */
  502. for (j = 0; j < ntokens; j++)
  503. {
  504. /* and record this rule as the rule to use if that token follows. */
  505. if (mask & *wordp)
  506. actrow[j] = rule;
  507. mask <<= 1;
  508. if (mask == 0)
  509. {
  510. mask = 1;
  511. wordp++;
  512. }
  513. }
  514. }
  515. }
  516. }
  517. shiftp = shift_table[state];
  518. /* now see which tokens are allowed for shifts in this state.
  519. For them, record the shift as the thing to do. So shift is preferred to reduce. */
  520. if (shiftp)
  521. {
  522. k = shiftp->nshifts;
  523. for (i = 0; i < k; i++)
  524. {
  525. shift_state = shiftp->shifts[i];
  526. if (! shift_state) continue;
  527. symbol = accessing_symbol[shift_state];
  528. if (ISVAR(symbol))
  529. break;
  530. actrow[symbol] = shift_state;
  531. /* do not use any default reduction if there is a shift for error */
  532. if (symbol == error_token_number) nodefault = 1;
  533. }
  534. }
  535. errp = err_table[state];
  536. /* See which tokens are an explicit error in this state
  537. (due to %nonassoc). For them, record MINSHORT as the action. */
  538. if (errp)
  539. {
  540. k = errp->nerrs;
  541. for (i = 0; i < k; i++)
  542. {
  543. symbol = errp->errs[i];
  544. actrow[symbol] = MINSHORT;
  545. }
  546. }
  547. /* now find the most common reduction and make it the default action for this state. */
  548. if (nreds >= 1 && ! nodefault)
  549. {
  550. if (consistent[state])
  551. default_rule = redp->rules[0];
  552. else
  553. {
  554. max = 0;
  555. for (i = m; i < n; i++)
  556. {
  557. count = 0;
  558. rule = - LAruleno[i];
  559. for (j = 0; j < ntokens; j++)
  560. {
  561. if (actrow[j] == rule)
  562. count++;
  563. }
  564. if (count > max)
  565. {
  566. max = count;
  567. default_rule = rule;
  568. }
  569. }
  570. /* actions which match the default are replaced with zero,
  571. which means "use the default" */
  572. if (max > 0)
  573. {
  574. for (j = 0; j < ntokens; j++)
  575. {
  576. if (actrow[j] == default_rule)
  577. actrow[j] = 0;
  578. }
  579. default_rule = - default_rule;
  580. }
  581. }
  582. }
  583. /* If have no default rule, the default is an error.
  584. So replace any action which says "error" with "use default". */
  585. if (default_rule == 0)
  586. for (j = 0; j < ntokens; j++)
  587. {
  588. if (actrow[j] == MINSHORT)
  589. actrow[j] = 0;
  590. }
  591. return (default_rule);
  592. }
  593. void
  594. save_row(state)
  595. int state;
  596. {
  597. register int i;
  598. register int count;
  599. register short *sp;
  600. register short *sp1;
  601. register short *sp2;
  602. count = 0;
  603. for (i = 0; i < ntokens; i++)
  604. {
  605. if (actrow[i] != 0)
  606. count++;
  607. }
  608. if (count == 0)
  609. return;
  610. froms[state] = sp1 = sp = NEW2(count, short);
  611. tos[state] = sp2 = NEW2(count, short);
  612. for (i = 0; i < ntokens; i++)
  613. {
  614. if (actrow[i] != 0)
  615. {
  616. *sp1++ = i;
  617. *sp2++ = actrow[i];
  618. }
  619. }
  620. tally[state] = count;
  621. width[state] = sp1[-1] - sp[0] + 1;
  622. }
  623. /* figure out what to do after reducing with each rule,
  624. depending on the saved state from before the beginning
  625. of parsing the data that matched this rule.
  626. The yydefgoto table is output now. The detailed info
  627. is saved for putting into yytable later. */
  628. void
  629. goto_actions()
  630. {
  631. register int i;
  632. register int j;
  633. register int k;
  634. state_count = NEW2(nstates, short);
  635. k = default_goto(ntokens);
  636. fprintf(ftable, "\nstatic const short yydefgoto[] = {%6d", k);
  637. save_column(ntokens, k);
  638. j = 10;
  639. for (i = ntokens + 1; i < nsyms; i++)
  640. {
  641. putc(',', ftable);
  642. if (j >= 10)
  643. {
  644. putc('\n', ftable);
  645. j = 1;
  646. }
  647. else
  648. {
  649. j++;
  650. }
  651. k = default_goto(i);
  652. fprintf(ftable, "%6d", k);
  653. save_column(i, k);
  654. }
  655. fprintf(ftable, "\n};\n");
  656. FREE(state_count);
  657. }
  658. int
  659. default_goto(symbol)
  660. int symbol;
  661. {
  662. register int i;
  663. register int m;
  664. register int n;
  665. register int default_state;
  666. register int max;
  667. m = goto_map[symbol];
  668. n = goto_map[symbol + 1];
  669. if (m == n)
  670. return (-1);
  671. for (i = 0; i < nstates; i++)
  672. state_count[i] = 0;
  673. for (i = m; i < n; i++)
  674. state_count[to_state[i]]++;
  675. max = 0;
  676. default_state = -1;
  677. for (i = 0; i < nstates; i++)
  678. {
  679. if (state_count[i] > max)
  680. {
  681. max = state_count[i];
  682. default_state = i;
  683. }
  684. }
  685. return (default_state);
  686. }
  687. void
  688. save_column(symbol, default_state)
  689. int symbol;
  690. int default_state;
  691. {
  692. register int i;
  693. register int m;
  694. register int n;
  695. register short *sp;
  696. register short *sp1;
  697. register short *sp2;
  698. register int count;
  699. register int symno;
  700. m = goto_map[symbol];
  701. n = goto_map[symbol + 1];
  702. count = 0;
  703. for (i = m; i < n; i++)
  704. {
  705. if (to_state[i] != default_state)
  706. count++;
  707. }
  708. if (count == 0)
  709. return;
  710. symno = symbol - ntokens + nstates;
  711. froms[symno] = sp1 = sp = NEW2(count, short);
  712. tos[symno] = sp2 = NEW2(count, short);
  713. for (i = m; i < n; i++)
  714. {
  715. if (to_state[i] != default_state)
  716. {
  717. *sp1++ = from_state[i];
  718. *sp2++ = to_state[i];
  719. }
  720. }
  721. tally[symno] = count;
  722. width[symno] = sp1[-1] - sp[0] + 1;
  723. }
  724. /* the next few functions decide how to pack
  725. the actions and gotos information into yytable. */
  726. void
  727. sort_actions()
  728. {
  729. register int i;
  730. register int j;
  731. register int k;
  732. register int t;
  733. register int w;
  734. order = NEW2(nvectors, short);
  735. nentries = 0;
  736. for (i = 0; i < nvectors; i++)
  737. {
  738. if (tally[i] > 0)
  739. {
  740. t = tally[i];
  741. w = width[i];
  742. j = nentries - 1;
  743. while (j >= 0 && (width[order[j]] < w))
  744. j--;
  745. while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
  746. j--;
  747. for (k = nentries - 1; k > j; k--)
  748. order[k + 1] = order[k];
  749. order[j + 1] = i;
  750. nentries++;
  751. }
  752. }
  753. }
  754. void
  755. pack_table()
  756. {
  757. register int i;
  758. register int place;
  759. register int state;
  760. base = NEW2(nvectors, short);
  761. pos = NEW2(nentries, short);
  762. table = NEW2(MAXTABLE, short);
  763. check = NEW2(MAXTABLE, short);
  764. lowzero = 0;
  765. high = 0;
  766. for (i = 0; i < nvectors; i++)
  767. base[i] = MINSHORT;
  768. for (i = 0; i < MAXTABLE; i++)
  769. check[i] = -1;
  770. for (i = 0; i < nentries; i++)
  771. {
  772. state = matching_state(i);
  773. if (state < 0)
  774. place = pack_vector(i);
  775. else
  776. place = base[state];
  777. pos[i] = place;
  778. base[order[i]] = place;
  779. }
  780. for (i = 0; i < nvectors; i++)
  781. {
  782. if (froms[i])
  783. FREE(froms[i]);
  784. if (tos[i])
  785. FREE(tos[i]);
  786. }
  787. FREE(froms);
  788. FREE(tos);
  789. FREE(pos);
  790. }
  791. int
  792. matching_state(vector)
  793. int vector;
  794. {
  795. register int i;
  796. register int j;
  797. register int k;
  798. register int t;
  799. register int w;
  800. register int match;
  801. register int prev;
  802. i = order[vector];
  803. if (i >= nstates)
  804. return (-1);
  805. t = tally[i];
  806. w = width[i];
  807. for (prev = vector - 1; prev >= 0; prev--)
  808. {
  809. j = order[prev];
  810. if (width[j] != w || tally[j] != t)
  811. return (-1);
  812. match = 1;
  813. for (k = 0; match && k < t; k++)
  814. {
  815. if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
  816. match = 0;
  817. }
  818. if (match)
  819. return (j);
  820. }
  821. return (-1);
  822. }
  823. int
  824. pack_vector(vector)
  825. int vector;
  826. {
  827. register int i;
  828. register int j;
  829. register int k;
  830. register int t;
  831. register int loc;
  832. register int ok;
  833. register short *from;
  834. register short *to;
  835. i = order[vector];
  836. t = tally[i];
  837. if (t == 0)
  838. berror("pack_vector");
  839. from = froms[i];
  840. to = tos[i];
  841. for (j = lowzero - from[0]; j < MAXTABLE; j++)
  842. {
  843. ok = 1;
  844. for (k = 0; ok && k < t; k++)
  845. {
  846. loc = j + from[k];
  847. if (loc > MAXTABLE)
  848. fatals("maximum table size (%d) exceeded",MAXTABLE);
  849. if (table[loc] != 0)
  850. ok = 0;
  851. }
  852. for (k = 0; ok && k < vector; k++)
  853. {
  854. if (pos[k] == j)
  855. ok = 0;
  856. }
  857. if (ok)
  858. {
  859. for (k = 0; k < t; k++)
  860. {
  861. loc = j + from[k];
  862. table[loc] = to[k];
  863. check[loc] = from[k];
  864. }
  865. while (table[lowzero] != 0)
  866. lowzero++;
  867. if (loc > high)
  868. high = loc;
  869. return (j);
  870. }
  871. }
  872. berror("pack_vector");
  873. return 0; /* JF keep lint happy */
  874. }
  875. /* the following functions output yytable, yycheck
  876. and the vectors whose elements index the portion starts */
  877. void
  878. output_base()
  879. {
  880. register int i;
  881. register int j;
  882. fprintf(ftable, "\nstatic const short yypact[] = {%6d", base[0]);
  883. j = 10;
  884. for (i = 1; i < nstates; i++)
  885. {
  886. putc(',', ftable);
  887. if (j >= 10)
  888. {
  889. putc('\n', ftable);
  890. j = 1;
  891. }
  892. else
  893. {
  894. j++;
  895. }
  896. fprintf(ftable, "%6d", base[i]);
  897. }
  898. fprintf(ftable, "\n};\n\nstatic const short yypgoto[] = {%6d", base[nstates]);
  899. j = 10;
  900. for (i = nstates + 1; i < nvectors; i++)
  901. {
  902. putc(',', ftable);
  903. if (j >= 10)
  904. {
  905. putc('\n', ftable);
  906. j = 1;
  907. }
  908. else
  909. {
  910. j++;
  911. }
  912. fprintf(ftable, "%6d", base[i]);
  913. }
  914. fprintf(ftable, "\n};\n");
  915. FREE(base);
  916. }
  917. void
  918. output_table()
  919. {
  920. register int i;
  921. register int j;
  922. fprintf(ftable, "\n\n#define\tYYLAST\t\t%d\n\n", high);
  923. fprintf(ftable, "\nstatic const short yytable[] = {%6d", table[0]);
  924. j = 10;
  925. for (i = 1; i <= high; i++)
  926. {
  927. putc(',', ftable);
  928. if (j >= 10)
  929. {
  930. putc('\n', ftable);
  931. j = 1;
  932. }
  933. else
  934. {
  935. j++;
  936. }
  937. fprintf(ftable, "%6d", table[i]);
  938. }
  939. fprintf(ftable, "\n};\n");
  940. FREE(table);
  941. }
  942. void
  943. output_check()
  944. {
  945. register int i;
  946. register int j;
  947. fprintf(ftable, "\nstatic const short yycheck[] = {%6d", check[0]);
  948. j = 10;
  949. for (i = 1; i <= high; i++)
  950. {
  951. putc(',', ftable);
  952. if (j >= 10)
  953. {
  954. putc('\n', ftable);
  955. j = 1;
  956. }
  957. else
  958. {
  959. j++;
  960. }
  961. fprintf(ftable, "%6d", check[i]);
  962. }
  963. fprintf(ftable, "\n};\n");
  964. FREE(check);
  965. }
  966. /* copy the parser code into the ftable file at the end. */
  967. void
  968. output_parser()
  969. {
  970. register int c;
  971. #ifdef DONTDEF
  972. FILE *fpars;
  973. #else
  974. #define fpars fparser
  975. #endif
  976. if (pure_parser)
  977. fprintf(ftable, "#define YYIMPURE 1\n\n");
  978. else
  979. fprintf(ftable, "#define YYPURE 1\n\n");
  980. #ifdef DONTDEF /* JF no longer needed 'cuz open_extra_files changes the
  981. currently open parser from bison.simple to bison.hairy */
  982. if (semantic_parser)
  983. fpars = fparser;
  984. else fpars = fparser1;
  985. #endif
  986. /* Loop over lines in the standard parser file. */
  987. while (1) {
  988. int write_line = 1;
  989. char *s;
  990. c = getc(fpars);
  991. /*
  992. * if nolinesflag is set, ignore lines beginning with #line
  993. */
  994. if (nolinesflag) {
  995. if (c == '#') {
  996. c = getc(fpars);
  997. if (c == 'l') {
  998. c = getc(fpars);
  999. if (c == 'i') {
  1000. c = getc(fpars);
  1001. if (c == 'n') {
  1002. c = getc(fpars);
  1003. if (c == 'e') {
  1004. write_line = 0;
  1005. } else {
  1006. fprintf(ftable, "#lin");
  1007. }
  1008. } else {
  1009. fprintf(ftable, "#li");
  1010. }
  1011. } else {
  1012. fprintf(ftable, "#l");
  1013. }
  1014. } else {
  1015. fprintf(ftable, "#");
  1016. }
  1017. }
  1018. }
  1019. /* now write out the line... */
  1020. for ( ; c != '\n' && c != EOF; c = getc(fpars)) {
  1021. if (write_line) {
  1022. if (c == '$') {
  1023. /* $ in the parser file indicates where to put the actions.
  1024. Copy them in at this point. */
  1025. rewind(faction);
  1026. for(c=getc(faction);c!=EOF;c=getc(faction)) {
  1027. putc(c,ftable);
  1028. }
  1029. } else if (c == '@') {
  1030. /* `@' in the parser file is replaced by the parser name */
  1031. for(s = parser_name; *s != '\0'; s++) {
  1032. putc(*s,ftable);
  1033. }
  1034. } else {
  1035. putc(c, ftable);
  1036. }
  1037. }
  1038. }
  1039. if (c == EOF) {
  1040. break;
  1041. }
  1042. putc(c, ftable);
  1043. }
  1044. }
  1045. void
  1046. output_program()
  1047. {
  1048. register int c;
  1049. extern int lineno;
  1050. if (!nolinesflag)
  1051. fprintf(ftable, "#line %d \"%s\"\n", lineno, infile);
  1052. c = getc(finput);
  1053. while (c != EOF)
  1054. {
  1055. putc(c, ftable);
  1056. c = getc(finput);
  1057. }
  1058. }
  1059. void
  1060. free_itemsets()
  1061. {
  1062. register core *cp,*cptmp;
  1063. FREE(state_table);
  1064. for (cp = first_state; cp; cp = cptmp) {
  1065. cptmp=cp->next;
  1066. FREE(cp);
  1067. }
  1068. }
  1069. void
  1070. free_shifts()
  1071. {
  1072. register shifts *sp,*sptmp;/* JF derefrenced freed ptr */
  1073. FREE(shift_table);
  1074. for (sp = first_shift; sp; sp = sptmp) {
  1075. sptmp=sp->next;
  1076. FREE(sp);
  1077. }
  1078. }
  1079. void
  1080. free_reductions()
  1081. {
  1082. register reductions *rp,*rptmp;/* JF fixed freed ptr */
  1083. FREE(reduction_table);
  1084. for (rp = first_reduction; rp; rp = rptmp) {
  1085. rptmp=rp->next;
  1086. FREE(rp);
  1087. }
  1088. }