output.c 28 KB

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