output.c 23 KB

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