output.c 26 KB

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