output.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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 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. 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. /* output_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. if (semantic_parser)
  144. fprintf(ftable, "#include \"%s\"\n", attrsfile);
  145. fprintf(ftable, "#include <stdio.h>\n\n");
  146. /* Make "const" do nothing if not in ANSI C. */
  147. fprintf (ftable, "#ifndef __STDC__\n#define const\n#endif\n\n");
  148. free_itemsets();
  149. output_defines();
  150. output_token_translations();
  151. if (semantic_parser)
  152. output_gram();
  153. FREE(ritem);
  154. if (semantic_parser)
  155. output_stos();
  156. output_rule_data();
  157. output_actions();
  158. output_parser();
  159. output_program();
  160. }
  161. output_token_translations()
  162. {
  163. register int i, j;
  164. /* register short *sp; JF unused */
  165. if (translations)
  166. {
  167. fprintf(ftable,
  168. "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
  169. max_user_token_number, nsyms);
  170. if (ntokens < 127) /* play it very safe; check maximum element value. */
  171. fprintf(ftable, "\nstatic const char yytranslate[] = { 0");
  172. else
  173. fprintf(ftable, "\nstatic const short yytranslate[] = { 0");
  174. j = 10;
  175. for (i = 1; i <= max_user_token_number; i++)
  176. {
  177. putc(',', ftable);
  178. if (j >= 10)
  179. {
  180. putc('\n', ftable);
  181. j = 1;
  182. }
  183. else
  184. {
  185. j++;
  186. }
  187. fprintf(ftable, "%6d", token_translations[i]);
  188. }
  189. fprintf(ftable, "\n};\n");
  190. }
  191. else
  192. {
  193. fprintf(ftable, "\n#define YYTRANSLATE(x) (x)\n");
  194. }
  195. }
  196. output_gram()
  197. {
  198. register int i;
  199. register int j;
  200. register short *sp;
  201. fprintf(ftable, "\nstatic const short yyprhs[] = { 0");
  202. j = 10;
  203. for (i = 1; i <= nrules; i++)
  204. {
  205. putc(',', ftable);
  206. if (j >= 10)
  207. {
  208. putc('\n', ftable);
  209. j = 1;
  210. }
  211. else
  212. {
  213. j++;
  214. }
  215. fprintf(ftable, "%6d", rrhs[i]);
  216. }
  217. fprintf(ftable, "\n};\n\nstatic const short yyrhs[] = {%6d", ritem[0]);
  218. j = 10;
  219. for (sp = ritem + 1; *sp; sp++)
  220. {
  221. putc(',', ftable);
  222. if (j >= 10)
  223. {
  224. putc('\n', ftable);
  225. j = 1;
  226. }
  227. else
  228. {
  229. j++;
  230. }
  231. if (*sp > 0)
  232. fprintf(ftable, "%6d", *sp);
  233. else
  234. fprintf(ftable, " 0");
  235. }
  236. fprintf(ftable, "\n};\n");
  237. }
  238. output_stos()
  239. {
  240. register int i;
  241. register int j;
  242. fprintf(ftable, "\nstatic const short yystos[] = { 0");
  243. j = 10;
  244. for (i = 1; i < nstates; i++)
  245. {
  246. putc(',', ftable);
  247. if (j >= 10)
  248. {
  249. putc('\n', ftable);
  250. j = 1;
  251. }
  252. else
  253. {
  254. j++;
  255. }
  256. fprintf(ftable, "%6d", accessing_symbol[i]);
  257. }
  258. fprintf(ftable, "\n};\n");
  259. }
  260. output_rule_data()
  261. {
  262. register int i;
  263. register int j;
  264. fprintf(ftable, "\nstatic const short yyrline[] = { 0");
  265. j = 10;
  266. for (i = 1; i <= nrules; i++)
  267. {
  268. putc(',', ftable);
  269. if (j >= 10)
  270. {
  271. putc('\n', ftable);
  272. j = 1;
  273. }
  274. else
  275. {
  276. j++;
  277. }
  278. fprintf(ftable, "%6d", rline[i]);
  279. }
  280. /* Output the table of token names. */
  281. fprintf(ftable, "\n};\n\nstatic const char * const yytname[] = { 0");
  282. j = 10;
  283. for (i = 1; i <= ntokens; i++)
  284. {
  285. register char *p;
  286. putc(',', ftable);
  287. if (j >= 10)
  288. {
  289. putc('\n', ftable);
  290. j = 1;
  291. }
  292. else
  293. {
  294. j++;
  295. }
  296. putc ('\"', ftable);
  297. for (p = tags[i]; *p; p++)
  298. if (*p == '"' || *p == '\\')
  299. fprintf(ftable, "\\%c", *p);
  300. else if (*p == '\n')
  301. fprintf(ftable, "\\n");
  302. else if (*p == '\t')
  303. fprintf(ftable, "\\t");
  304. else if (*p == '\b')
  305. fprintf(ftable, "\\b");
  306. else if (*p < 040 || *p >= 0177)
  307. fprintf(ftable, "\\%03o", *p);
  308. else
  309. putc(*p, ftable);
  310. putc ('\"', ftable);
  311. }
  312. fprintf(ftable, "\n};\n\nstatic const short yyr1[] = { 0");
  313. j = 10;
  314. for (i = 1; i <= nrules; i++)
  315. {
  316. putc(',', ftable);
  317. if (j >= 10)
  318. {
  319. putc('\n', ftable);
  320. j = 1;
  321. }
  322. else
  323. {
  324. j++;
  325. }
  326. fprintf(ftable, "%6d", rlhs[i]);
  327. }
  328. FREE(rlhs + 1);
  329. fprintf(ftable, "\n};\n\nstatic const short yyr2[] = { 0");
  330. j = 10;
  331. for (i = 1; i < nrules; i++)
  332. {
  333. putc(',', ftable);
  334. if (j >= 10)
  335. {
  336. putc('\n', ftable);
  337. j = 1;
  338. }
  339. else
  340. {
  341. j++;
  342. }
  343. fprintf(ftable, "%6d", rrhs[i + 1] - rrhs[i] - 1);
  344. }
  345. putc(',', ftable);
  346. if (j >= 10)
  347. putc('\n', ftable);
  348. fprintf(ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1);
  349. FREE(rrhs + 1);
  350. }
  351. output_defines()
  352. {
  353. fprintf(ftable, "\n\n#define\tYYFINAL\t\t%d\n", final_state);
  354. fprintf(ftable, "#define\tYYFLAG\t\t%d\n", MINSHORT);
  355. fprintf(ftable, "#define\tYYNTBASE\t%d\n", ntokens);
  356. }
  357. /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck. */
  358. output_actions()
  359. {
  360. nvectors = nstates + nvars;
  361. froms = NEW2(nvectors, short *);
  362. tos = NEW2(nvectors, short *);
  363. tally = NEW2(nvectors, short);
  364. width = NEW2(nvectors, short);
  365. token_actions();
  366. free_shifts();
  367. free_reductions();
  368. FREE(lookaheads);
  369. FREE(LA);
  370. FREE(LAruleno);
  371. FREE(accessing_symbol);
  372. goto_actions();
  373. FREE(goto_map + ntokens);
  374. FREE(from_state);
  375. FREE(to_state);
  376. sort_actions();
  377. pack_table();
  378. output_base();
  379. output_table();
  380. output_check();
  381. }
  382. /* figure out the actions for the specified state, indexed by lookahead token type.
  383. The yydefact table is output now. The detailed info
  384. is saved for putting into yytable later. */
  385. token_actions()
  386. {
  387. register int i;
  388. register int j;
  389. register int k;
  390. actrow = NEW2(ntokens, short);
  391. k = action_row(0);
  392. fprintf(ftable, "\nstatic const short yydefact[] = {%6d", k);
  393. save_row(0);
  394. j = 10;
  395. for (i = 1; i < nstates; i++)
  396. {
  397. putc(',', ftable);
  398. if (j >= 10)
  399. {
  400. putc('\n', ftable);
  401. j = 1;
  402. }
  403. else
  404. {
  405. j++;
  406. }
  407. k = action_row(i);
  408. fprintf(ftable, "%6d", k);
  409. save_row(i);
  410. }
  411. fprintf(ftable, "\n};\n");
  412. FREE(actrow);
  413. }
  414. /* Decide what to do for each type of token if seen as the lookahead token in specified state.
  415. The value returned is used as the default action (yydefact) for the state.
  416. In addition, actrow is filled with what to do for each kind of token,
  417. index by symbol number, with zero meaning do the default action.
  418. The value MINSHORT, a very negative number, means this situation
  419. is an error. The parser recognizes this value specially.
  420. This is where conflicts are resolved. The loop over lookahead rules
  421. considered lower-numbered rules last, and the last rule considered that likes
  422. a token gets to handle it. */
  423. int
  424. action_row(state)
  425. int state;
  426. {
  427. register int i;
  428. register int j;
  429. register int k;
  430. register int m;
  431. register int n;
  432. register int count;
  433. register int default_rule;
  434. register int nreds;
  435. register int max;
  436. register int rule;
  437. register int shift_state;
  438. register int symbol;
  439. register unsigned mask;
  440. register unsigned *wordp;
  441. register reductions *redp;
  442. register shifts *shiftp;
  443. register errs *errp;
  444. int nodefault = 0; /* set nonzero to inhibit having any default reduction */
  445. for (i = 0; i < ntokens; i++)
  446. actrow[i] = 0;
  447. default_rule = 0;
  448. nreds = 0;
  449. redp = reduction_table[state];
  450. if (redp)
  451. {
  452. nreds = redp->nreds;
  453. if (nreds >= 1)
  454. {
  455. /* loop over all the rules available here which require lookahead */
  456. m = lookaheads[state];
  457. n = lookaheads[state + 1];
  458. for (i = n - 1; i >= m; i--)
  459. {
  460. rule = - LAruleno[i];
  461. wordp = LA + i * tokensetsize;
  462. mask = 1;
  463. /* and find each token which the rule finds acceptable to come next */
  464. for (j = 0; j < ntokens; j++)
  465. {
  466. /* and record this rule as the rule to use if that token follows. */
  467. if (mask & *wordp)
  468. actrow[j] = rule;
  469. mask <<= 1;
  470. if (mask == 0)
  471. {
  472. mask = 1;
  473. wordp++;
  474. }
  475. }
  476. }
  477. }
  478. }
  479. shiftp = shift_table[state];
  480. /* now see which tokens are allowed for shifts in this state.
  481. For them, record the shift as the thing to do. So shift is preferred to reduce. */
  482. if (shiftp)
  483. {
  484. k = shiftp->nshifts;
  485. for (i = 0; i < k; i++)
  486. {
  487. shift_state = shiftp->shifts[i];
  488. if (! shift_state) continue;
  489. symbol = accessing_symbol[shift_state];
  490. if (ISVAR(symbol))
  491. break;
  492. actrow[symbol] = shift_state;
  493. /* do not use any default reduction if there is a shift for error */
  494. if (symbol == error_token_number) nodefault = 1;
  495. }
  496. }
  497. errp = err_table[state];
  498. /* See which tokens are an explicit error in this state
  499. (due to %nonassoc). For them, record MINSHORT as the action. */
  500. if (errp)
  501. {
  502. k = errp->nerrs;
  503. for (i = 0; i < k; i++)
  504. {
  505. symbol = errp->errs[i];
  506. actrow[symbol] = MINSHORT;
  507. }
  508. }
  509. /* now find the most common reduction and make it the default action for this state. */
  510. if (nreds >= 1 && ! nodefault)
  511. {
  512. if (consistent[state])
  513. default_rule = redp->rules[0];
  514. else
  515. {
  516. max = 0;
  517. for (i = m; i < n; i++)
  518. {
  519. count = 0;
  520. rule = - LAruleno[i];
  521. for (j = 0; j < ntokens; j++)
  522. {
  523. if (actrow[j] == rule)
  524. count++;
  525. }
  526. if (count > max)
  527. {
  528. max = count;
  529. default_rule = rule;
  530. }
  531. }
  532. /* actions which match the default are replaced with zero,
  533. which means "use the default" */
  534. if (max > 0)
  535. {
  536. for (j = 0; j < ntokens; j++)
  537. {
  538. if (actrow[j] == default_rule)
  539. actrow[j] = 0;
  540. }
  541. default_rule = - default_rule;
  542. }
  543. }
  544. }
  545. /* If have no default rule, the default is an error.
  546. So replace any action which says "error" with "use default". */
  547. if (default_rule == 0)
  548. for (j = 0; j < ntokens; j++)
  549. {
  550. if (actrow[j] == MINSHORT)
  551. actrow[j] = 0;
  552. }
  553. return (default_rule);
  554. }
  555. save_row(state)
  556. int state;
  557. {
  558. register int i;
  559. register int count;
  560. register short *sp;
  561. register short *sp1;
  562. register short *sp2;
  563. count = 0;
  564. for (i = 0; i < ntokens; i++)
  565. {
  566. if (actrow[i] != 0)
  567. count++;
  568. }
  569. if (count == 0)
  570. return;
  571. froms[state] = sp1 = sp = NEW2(count, short);
  572. tos[state] = sp2 = NEW2(count, short);
  573. for (i = 0; i < ntokens; i++)
  574. {
  575. if (actrow[i] != 0)
  576. {
  577. *sp1++ = i;
  578. *sp2++ = actrow[i];
  579. }
  580. }
  581. tally[state] = count;
  582. width[state] = sp1[-1] - sp[0] + 1;
  583. }
  584. /* figure out what to do after reducing with each rule,
  585. depending on the saved state from before the beginning
  586. of parsing the data that matched this rule.
  587. The yydefgoto table is output now. The detailed info
  588. is saved for putting into yytable later. */
  589. goto_actions()
  590. {
  591. register int i;
  592. register int j;
  593. register int k;
  594. state_count = NEW2(nstates, short);
  595. k = default_goto(ntokens);
  596. fprintf(ftable, "\nstatic const short yydefgoto[] = {%6d", k);
  597. save_column(ntokens, k);
  598. j = 10;
  599. for (i = ntokens + 1; i < nsyms; i++)
  600. {
  601. putc(',', ftable);
  602. if (j >= 10)
  603. {
  604. putc('\n', ftable);
  605. j = 1;
  606. }
  607. else
  608. {
  609. j++;
  610. }
  611. k = default_goto(i);
  612. fprintf(ftable, "%6d", k);
  613. save_column(i, k);
  614. }
  615. fprintf(ftable, "\n};\n");
  616. FREE(state_count);
  617. }
  618. int
  619. default_goto(symbol)
  620. int symbol;
  621. {
  622. register int i;
  623. register int m;
  624. register int n;
  625. register int default_state;
  626. register int max;
  627. m = goto_map[symbol];
  628. n = goto_map[symbol + 1];
  629. if (m == n)
  630. return (-1);
  631. for (i = 0; i < nstates; i++)
  632. state_count[i] = 0;
  633. for (i = m; i < n; i++)
  634. state_count[to_state[i]]++;
  635. max = 0;
  636. default_state = -1;
  637. for (i = 0; i < nstates; i++)
  638. {
  639. if (state_count[i] > max)
  640. {
  641. max = state_count[i];
  642. default_state = i;
  643. }
  644. }
  645. return (default_state);
  646. }
  647. save_column(symbol, default_state)
  648. int symbol;
  649. int default_state;
  650. {
  651. register int i;
  652. register int m;
  653. register int n;
  654. register short *sp;
  655. register short *sp1;
  656. register short *sp2;
  657. register int count;
  658. register int symno;
  659. m = goto_map[symbol];
  660. n = goto_map[symbol + 1];
  661. count = 0;
  662. for (i = m; i < n; i++)
  663. {
  664. if (to_state[i] != default_state)
  665. count++;
  666. }
  667. if (count == 0)
  668. return;
  669. symno = symbol - ntokens + nstates;
  670. froms[symno] = sp1 = sp = NEW2(count, short);
  671. tos[symno] = sp2 = NEW2(count, short);
  672. for (i = m; i < n; i++)
  673. {
  674. if (to_state[i] != default_state)
  675. {
  676. *sp1++ = from_state[i];
  677. *sp2++ = to_state[i];
  678. }
  679. }
  680. tally[symno] = count;
  681. width[symno] = sp1[-1] - sp[0] + 1;
  682. }
  683. /* the next few functions decide how to pack
  684. the actions and gotos information into yytable. */
  685. sort_actions()
  686. {
  687. register int i;
  688. register int j;
  689. register int k;
  690. register int t;
  691. register int w;
  692. order = NEW2(nvectors, short);
  693. nentries = 0;
  694. for (i = 0; i < nvectors; i++)
  695. {
  696. if (tally[i] > 0)
  697. {
  698. t = tally[i];
  699. w = width[i];
  700. j = nentries - 1;
  701. while (j >= 0 && (width[order[j]] < w))
  702. j--;
  703. while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
  704. j--;
  705. for (k = nentries - 1; k > j; k--)
  706. order[k + 1] = order[k];
  707. order[j + 1] = i;
  708. nentries++;
  709. }
  710. }
  711. }
  712. pack_table()
  713. {
  714. register int i;
  715. register int place;
  716. register int state;
  717. base = NEW2(nvectors, short);
  718. pos = NEW2(nentries, short);
  719. table = NEW2(MAXTABLE, short);
  720. check = NEW2(MAXTABLE, short);
  721. lowzero = 0;
  722. high = 0;
  723. for (i = 0; i < nvectors; i++)
  724. base[i] = MINSHORT;
  725. for (i = 0; i < MAXTABLE; i++)
  726. check[i] = -1;
  727. for (i = 0; i < nentries; i++)
  728. {
  729. state = matching_state(i);
  730. if (state < 0)
  731. place = pack_vector(i);
  732. else
  733. place = base[state];
  734. pos[i] = place;
  735. base[order[i]] = place;
  736. }
  737. for (i = 0; i < nvectors; i++)
  738. {
  739. FREE(froms[i]);
  740. FREE(tos[i]);
  741. }
  742. FREE(froms);
  743. FREE(tos);
  744. FREE(pos);
  745. }
  746. int
  747. matching_state(vector)
  748. int vector;
  749. {
  750. register int i;
  751. register int j;
  752. register int k;
  753. register int t;
  754. register int w;
  755. register int match;
  756. register int prev;
  757. i = order[vector];
  758. if (i >= nstates)
  759. return (-1);
  760. t = tally[i];
  761. w = width[i];
  762. for (prev = vector - 1; prev >= 0; prev--)
  763. {
  764. j = order[prev];
  765. if (width[j] != w || tally[j] != t)
  766. return (-1);
  767. match = 1;
  768. for (k = 0; match && k < t; k++)
  769. {
  770. if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
  771. match = 0;
  772. }
  773. if (match)
  774. return (j);
  775. }
  776. return (-1);
  777. }
  778. int
  779. pack_vector(vector)
  780. int vector;
  781. {
  782. register int i;
  783. register int j;
  784. register int k;
  785. register int t;
  786. register int loc;
  787. register int ok;
  788. register short *from;
  789. register short *to;
  790. i = order[vector];
  791. t = tally[i];
  792. if (t == 0)
  793. berror("pack_vector");
  794. from = froms[i];
  795. to = tos[i];
  796. for (j = lowzero - from[0]; j < MAXTABLE; j++)
  797. {
  798. ok = 1;
  799. for (k = 0; ok && k < t; k++)
  800. {
  801. loc = j + from[k];
  802. if (loc > MAXTABLE)
  803. fatals("maximum table size (%d) exceeded",MAXTABLE);
  804. if (table[loc] != 0)
  805. ok = 0;
  806. }
  807. for (k = 0; ok && k < vector; k++)
  808. {
  809. if (pos[k] == j)
  810. ok = 0;
  811. }
  812. if (ok)
  813. {
  814. for (k = 0; k < t; k++)
  815. {
  816. loc = j + from[k];
  817. table[loc] = to[k];
  818. check[loc] = from[k];
  819. }
  820. while (table[lowzero] != 0)
  821. lowzero++;
  822. if (loc > high)
  823. high = loc;
  824. return (j);
  825. }
  826. }
  827. berror("pack_vector");
  828. return 0; /* JF keep lint happy */
  829. }
  830. /* the following functions output yytable, yycheck
  831. and the vectors whose elements index the portion starts */
  832. output_base()
  833. {
  834. register int i;
  835. register int j;
  836. fprintf(ftable, "\nstatic const short yypact[] = {%6d", base[0]);
  837. j = 10;
  838. for (i = 1; i < nstates; i++)
  839. {
  840. putc(',', ftable);
  841. if (j >= 10)
  842. {
  843. putc('\n', ftable);
  844. j = 1;
  845. }
  846. else
  847. {
  848. j++;
  849. }
  850. fprintf(ftable, "%6d", base[i]);
  851. }
  852. fprintf(ftable, "\n};\n\nstatic const short yypgoto[] = {%6d", base[nstates]);
  853. j = 10;
  854. for (i = nstates + 1; i < nvectors; i++)
  855. {
  856. putc(',', ftable);
  857. if (j >= 10)
  858. {
  859. putc('\n', ftable);
  860. j = 1;
  861. }
  862. else
  863. {
  864. j++;
  865. }
  866. fprintf(ftable, "%6d", base[i]);
  867. }
  868. fprintf(ftable, "\n};\n");
  869. FREE(base);
  870. }
  871. output_table()
  872. {
  873. register int i;
  874. register int j;
  875. fprintf(ftable, "\n\n#define\tYYLAST\t\t%d\n\n", high);
  876. fprintf(ftable, "\nstatic const short yytable[] = {%6d", table[0]);
  877. j = 10;
  878. for (i = 1; i <= high; i++)
  879. {
  880. putc(',', ftable);
  881. if (j >= 10)
  882. {
  883. putc('\n', ftable);
  884. j = 1;
  885. }
  886. else
  887. {
  888. j++;
  889. }
  890. fprintf(ftable, "%6d", table[i]);
  891. }
  892. fprintf(ftable, "\n};\n");
  893. FREE(table);
  894. }
  895. output_check()
  896. {
  897. register int i;
  898. register int j;
  899. fprintf(ftable, "\nstatic const short yycheck[] = {%6d", check[0]);
  900. j = 10;
  901. for (i = 1; i <= high; i++)
  902. {
  903. putc(',', ftable);
  904. if (j >= 10)
  905. {
  906. putc('\n', ftable);
  907. j = 1;
  908. }
  909. else
  910. {
  911. j++;
  912. }
  913. fprintf(ftable, "%6d", check[i]);
  914. }
  915. fprintf(ftable, "\n};\n");
  916. FREE(check);
  917. }
  918. /* copy the parser code into the ftable file at the end. */
  919. output_parser()
  920. {
  921. register int c;
  922. #ifdef DONTDEF
  923. FILE *fpars;
  924. #else
  925. #define fpars fparser
  926. #endif
  927. if (pure_parser)
  928. fprintf(ftable, "#define YYIMPURE 1\n\n");
  929. else
  930. fprintf(ftable, "#define YYPURE 1\n\n");
  931. #ifdef DONTDEF /* JF no longer needed 'cuz open_extra_files changes the
  932. currently open parser from bison.simple to bison.hairy */
  933. if (semantic_parser)
  934. fpars = fparser;
  935. else fpars = fparser1;
  936. #endif
  937. c = getc(fpars);
  938. while (c != EOF)
  939. {
  940. if (c == '$') {
  941. #ifdef DONTDEF
  942. fprintf(ftable, "#include \"%s\"\n", actfile);
  943. #else
  944. /* JF don't #include the action file. Stuff it right in. */
  945. rewind(faction);
  946. for(c=getc(faction);c!=EOF;c=getc(faction))
  947. putc(c,ftable);
  948. #endif
  949. } else
  950. putc(c, ftable);
  951. c = getc(fpars);
  952. }
  953. }
  954. output_program()
  955. {
  956. register int c;
  957. extern int lineno;
  958. fprintf(ftable, "#line %d \"%s\"\n", lineno, infile);
  959. c = getc(finput);
  960. while (c != EOF)
  961. {
  962. putc(c, ftable);
  963. c = getc(finput);
  964. }
  965. }
  966. free_itemsets()
  967. {
  968. register core *cp,*cptmp;
  969. FREE(state_table);
  970. for (cp = first_state; cp; cp = cptmp) {
  971. cptmp=cp->next;
  972. FREE(cp);
  973. }
  974. }
  975. free_shifts()
  976. {
  977. register shifts *sp,*sptmp;/* JF derefrenced freed ptr */
  978. FREE(shift_table);
  979. for (sp = first_shift; sp; sp = sptmp) {
  980. sptmp=sp->next;
  981. FREE(sp);
  982. }
  983. }
  984. free_reductions()
  985. {
  986. register reductions *rp,*rptmp;/* JF fixed freed ptr */
  987. FREE(reduction_table);
  988. for (rp = first_reduction; rp; rp = rptmp) {
  989. rptmp=rp->next;
  990. FREE(rp);
  991. }
  992. }