output.c 26 KB

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