OUTPUT.C 26 KB

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