LR0.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /* Generate the nondeterministic finite state machine 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. /* See comments in state.h for the data structures that represent it.
  16. The entry point is generate_states. */
  17. #include <stdio.h>
  18. #include "system.h"
  19. #include "machine.h"
  20. #include "new.h"
  21. #include "gram.h"
  22. #include "state.h"
  23. extern char *nullable;
  24. extern short *itemset;
  25. extern short *itemsetend;
  26. int nstates;
  27. int final_state;
  28. core *first_state;
  29. shifts *first_shift;
  30. reductions *first_reduction;
  31. int get_state();
  32. core *new_state();
  33. void new_itemsets();
  34. void append_states();
  35. void initialize_states();
  36. void save_shifts();
  37. void save_reductions();
  38. void augment_automaton();
  39. void insert_start_shift();
  40. extern void initialize_closure();
  41. extern void closure();
  42. extern void finalize_closure();
  43. extern void toomany();
  44. static core *this_state;
  45. static core *last_state;
  46. static shifts *last_shift;
  47. static reductions *last_reduction;
  48. static int nshifts;
  49. static short *shift_symbol;
  50. static short *redset;
  51. static short *shiftset;
  52. static short **kernel_base;
  53. static short **kernel_end;
  54. static short *kernel_items;
  55. /* hash table for states, to recognize equivalent ones. */
  56. #define STATE_TABLE_SIZE 1009
  57. static core **state_table;
  58. void
  59. allocate_itemsets()
  60. {
  61. register short *itemp;
  62. register int symbol;
  63. register int i;
  64. register int count;
  65. register short *symbol_count;
  66. count = 0;
  67. symbol_count = NEW2(nsyms, short);
  68. itemp = ritem;
  69. symbol = *itemp++;
  70. while (symbol)
  71. {
  72. if (symbol > 0)
  73. {
  74. count++;
  75. symbol_count[symbol]++;
  76. }
  77. symbol = *itemp++;
  78. }
  79. /* see comments before new_itemsets. All the vectors of items
  80. live inside kernel_items. The number of active items after
  81. some symbol cannot be more than the number of times that symbol
  82. appears as an item, which is symbol_count[symbol].
  83. We allocate that much space for each symbol. */
  84. kernel_base = NEW2(nsyms, short *);
  85. kernel_items = NEW2(count, short);
  86. count = 0;
  87. for (i = 0; i < nsyms; i++)
  88. {
  89. kernel_base[i] = kernel_items + count;
  90. count += symbol_count[i];
  91. }
  92. shift_symbol = symbol_count;
  93. kernel_end = NEW2(nsyms, short *);
  94. }
  95. void
  96. allocate_storage()
  97. {
  98. allocate_itemsets();
  99. shiftset = NEW2(nsyms, short);
  100. redset = NEW2(nrules + 1, short);
  101. state_table = NEW2(STATE_TABLE_SIZE, core *);
  102. }
  103. void
  104. free_storage()
  105. {
  106. FREE(shift_symbol);
  107. FREE(redset);
  108. FREE(shiftset);
  109. FREE(kernel_base);
  110. FREE(kernel_end);
  111. FREE(kernel_items);
  112. FREE(state_table);
  113. }
  114. /* compute the nondeterministic finite state machine (see state.h for details)
  115. from the grammar. */
  116. void
  117. generate_states()
  118. {
  119. allocate_storage();
  120. initialize_closure(nitems);
  121. initialize_states();
  122. while (this_state)
  123. {
  124. /* Set up ruleset and itemset for the transitions out of this state.
  125. ruleset gets a 1 bit for each rule that could reduce now.
  126. itemset gets a vector of all the items that could be accepted next. */
  127. closure(this_state->items, this_state->nitems);
  128. /* record the reductions allowed out of this state */
  129. save_reductions();
  130. /* find the itemsets of the states that shifts can reach */
  131. new_itemsets();
  132. /* find or create the core structures for those states */
  133. append_states();
  134. /* create the shifts structures for the shifts to those states,
  135. now that the state numbers transitioning to are known */
  136. if (nshifts > 0)
  137. save_shifts();
  138. /* states are queued when they are created; process them all */
  139. this_state = this_state->next;
  140. }
  141. /* discard various storage */
  142. finalize_closure();
  143. free_storage();
  144. /* set up initial and final states as parser wants them */
  145. augment_automaton();
  146. }
  147. /* Find which symbols can be shifted in the current state,
  148. and for each one record which items would be active after that shift.
  149. Uses the contents of itemset.
  150. shift_symbol is set to a vector of the symbols that can be shifted.
  151. For each symbol in the grammar, kernel_base[symbol] points to
  152. a vector of item numbers activated if that symbol is shifted,
  153. and kernel_end[symbol] points after the end of that vector. */
  154. void
  155. new_itemsets()
  156. {
  157. register int i;
  158. register int shiftcount;
  159. register short *isp;
  160. register short *ksp;
  161. register int symbol;
  162. #ifdef TRACE
  163. fprintf(stderr, "Entering new_itemsets\n");
  164. #endif
  165. for (i = 0; i < nsyms; i++)
  166. kernel_end[i] = NULL;
  167. shiftcount = 0;
  168. isp = itemset;
  169. while (isp < itemsetend)
  170. {
  171. i = *isp++;
  172. symbol = ritem[i];
  173. if (symbol > 0)
  174. {
  175. ksp = kernel_end[symbol];
  176. if (!ksp)
  177. {
  178. shift_symbol[shiftcount++] = symbol;
  179. ksp = kernel_base[symbol];
  180. }
  181. *ksp++ = i + 1;
  182. kernel_end[symbol] = ksp;
  183. }
  184. }
  185. nshifts = shiftcount;
  186. }
  187. /* Use the information computed by new_itemsets to find the state numbers
  188. reached by each shift transition from the current state.
  189. shiftset is set up as a vector of state numbers of those states. */
  190. void
  191. append_states()
  192. {
  193. register int i;
  194. register int j;
  195. register int symbol;
  196. #ifdef TRACE
  197. fprintf(stderr, "Entering append_states\n");
  198. #endif
  199. /* first sort shift_symbol into increasing order */
  200. for (i = 1; i < nshifts; i++)
  201. {
  202. symbol = shift_symbol[i];
  203. j = i;
  204. while (j > 0 && shift_symbol[j - 1] > symbol)
  205. {
  206. shift_symbol[j] = shift_symbol[j - 1];
  207. j--;
  208. }
  209. shift_symbol[j] = symbol;
  210. }
  211. for (i = 0; i < nshifts; i++)
  212. {
  213. symbol = shift_symbol[i];
  214. shiftset[i] = get_state(symbol);
  215. }
  216. }
  217. /* find the state number for the state we would get to
  218. (from the current state) by shifting symbol.
  219. Create a new state if no equivalent one exists already.
  220. Used by append_states */
  221. int
  222. get_state(symbol)
  223. int symbol;
  224. {
  225. register int key;
  226. register short *isp1;
  227. register short *isp2;
  228. register short *iend;
  229. register core *sp;
  230. register int found;
  231. int n;
  232. #ifdef TRACE
  233. fprintf(stderr, "Entering get_state, symbol = %d\n", symbol);
  234. #endif
  235. isp1 = kernel_base[symbol];
  236. iend = kernel_end[symbol];
  237. n = iend - isp1;
  238. /* add up the target state's active item numbers to get a hash key */
  239. key = 0;
  240. while (isp1 < iend)
  241. key += *isp1++;
  242. key = key % STATE_TABLE_SIZE;
  243. sp = state_table[key];
  244. if (sp)
  245. {
  246. found = 0;
  247. while (!found)
  248. {
  249. if (sp->nitems == n)
  250. {
  251. found = 1;
  252. isp1 = kernel_base[symbol];
  253. isp2 = sp->items;
  254. while (found && isp1 < iend)
  255. {
  256. if (*isp1++ != *isp2++)
  257. found = 0;
  258. }
  259. }
  260. if (!found)
  261. {
  262. if (sp->link)
  263. {
  264. sp = sp->link;
  265. }
  266. else /* bucket exhausted and no match */
  267. {
  268. sp = sp->link = new_state(symbol);
  269. found = 1;
  270. }
  271. }
  272. }
  273. }
  274. else /* bucket is empty */
  275. {
  276. state_table[key] = sp = new_state(symbol);
  277. }
  278. return (sp->number);
  279. }
  280. /* subroutine of get_state. create a new state for those items, if necessary. */
  281. core *
  282. new_state(symbol)
  283. int symbol;
  284. {
  285. register int n;
  286. register core *p;
  287. register short *isp1;
  288. register short *isp2;
  289. register short *iend;
  290. #ifdef TRACE
  291. fprintf(stderr, "Entering new_state, symbol = %d\n", symbol);
  292. #endif
  293. if (nstates >= MAXSHORT)
  294. toomany("states");
  295. isp1 = kernel_base[symbol];
  296. iend = kernel_end[symbol];
  297. n = iend - isp1;
  298. p = (core *) xmalloc((unsigned) (sizeof(core) + (n - 1) * sizeof(short)));
  299. p->accessing_symbol = symbol;
  300. p->number = nstates;
  301. p->nitems = n;
  302. isp2 = p->items;
  303. while (isp1 < iend)
  304. *isp2++ = *isp1++;
  305. last_state->next = p;
  306. last_state = p;
  307. nstates++;
  308. return (p);
  309. }
  310. void
  311. initialize_states()
  312. {
  313. register core *p;
  314. /* register unsigned *rp1; JF unused */
  315. /* register unsigned *rp2; JF unused */
  316. /* register unsigned *rend; JF unused */
  317. p = (core *) xmalloc((unsigned) (sizeof(core) - sizeof(short)));
  318. first_state = last_state = this_state = p;
  319. nstates = 1;
  320. }
  321. void
  322. save_shifts()
  323. {
  324. register shifts *p;
  325. register short *sp1;
  326. register short *sp2;
  327. register short *send;
  328. p = (shifts *) xmalloc((unsigned) (sizeof(shifts) +
  329. (nshifts - 1) * sizeof(short)));
  330. p->number = this_state->number;
  331. p->nshifts = nshifts;
  332. sp1 = shiftset;
  333. sp2 = p->shifts;
  334. send = shiftset + nshifts;
  335. while (sp1 < send)
  336. *sp2++ = *sp1++;
  337. if (last_shift)
  338. {
  339. last_shift->next = p;
  340. last_shift = p;
  341. }
  342. else
  343. {
  344. first_shift = p;
  345. last_shift = p;
  346. }
  347. }
  348. /* find which rules can be used for reduction transitions from the current state
  349. and make a reductions structure for the state to record their rule numbers. */
  350. void
  351. save_reductions()
  352. {
  353. register short *isp;
  354. register short *rp1;
  355. register short *rp2;
  356. register int item;
  357. register int count;
  358. register reductions *p;
  359. short *rend;
  360. /* find and count the active items that represent ends of rules */
  361. count = 0;
  362. for (isp = itemset; isp < itemsetend; isp++)
  363. {
  364. item = ritem[*isp];
  365. if (item < 0)
  366. {
  367. redset[count++] = -item;
  368. }
  369. }
  370. /* make a reductions structure and copy the data into it. */
  371. if (count)
  372. {
  373. p = (reductions *) xmalloc((unsigned) (sizeof(reductions) +
  374. (count - 1) * sizeof(short)));
  375. p->number = this_state->number;
  376. p->nreds = count;
  377. rp1 = redset;
  378. rp2 = p->rules;
  379. rend = rp1 + count;
  380. while (rp1 < rend)
  381. *rp2++ = *rp1++;
  382. if (last_reduction)
  383. {
  384. last_reduction->next = p;
  385. last_reduction = p;
  386. }
  387. else
  388. {
  389. first_reduction = p;
  390. last_reduction = p;
  391. }
  392. }
  393. }
  394. /* Make sure that the initial state has a shift that accepts the
  395. grammar's start symbol and goes to the next-to-final state,
  396. which has a shift going to the final state, which has a shift
  397. to the termination state.
  398. Create such states and shifts if they don't happen to exist already. */
  399. void
  400. augment_automaton()
  401. {
  402. register int i;
  403. register int k;
  404. /* register int found; JF unused */
  405. register core *statep;
  406. register shifts *sp;
  407. register shifts *sp2;
  408. register shifts *sp1;
  409. sp = first_shift;
  410. if (sp)
  411. {
  412. if (sp->number == 0)
  413. {
  414. k = sp->nshifts;
  415. statep = first_state->next;
  416. /* The states reached by shifts from first_state are numbered 1...K.
  417. Look for one reached by start_symbol. */
  418. while (statep->accessing_symbol < start_symbol
  419. && statep->number < k)
  420. statep = statep->next;
  421. if (statep->accessing_symbol == start_symbol)
  422. {
  423. /* We already have a next-to-final state.
  424. Make sure it has a shift to what will be the final state. */
  425. k = statep->number;
  426. while (sp && sp->number < k)
  427. {
  428. sp1 = sp;
  429. sp = sp->next;
  430. }
  431. if (sp && sp->number == k)
  432. {
  433. sp2 = (shifts *) xmalloc((unsigned) (sizeof(shifts)
  434. + sp->nshifts * sizeof(short)));
  435. sp2->number = k;
  436. sp2->nshifts = sp->nshifts + 1;
  437. sp2->shifts[0] = nstates;
  438. for (i = sp->nshifts; i > 0; i--)
  439. sp2->shifts[i] = sp->shifts[i - 1];
  440. /* Patch sp2 into the chain of shifts in place of sp,
  441. following sp1. */
  442. sp2->next = sp->next;
  443. sp1->next = sp2;
  444. if (sp == last_shift)
  445. last_shift = sp2;
  446. FREE(sp);
  447. }
  448. else
  449. {
  450. sp2 = NEW(shifts);
  451. sp2->number = k;
  452. sp2->nshifts = 1;
  453. sp2->shifts[0] = nstates;
  454. /* Patch sp2 into the chain of shifts between sp1 and sp. */
  455. sp2->next = sp;
  456. sp1->next = sp2;
  457. if (sp == 0)
  458. last_shift = sp2;
  459. }
  460. }
  461. else
  462. {
  463. /* There is no next-to-final state as yet. */
  464. /* Add one more shift in first_shift,
  465. going to the next-to-final state (yet to be made). */
  466. sp = first_shift;
  467. sp2 = (shifts *) xmalloc(sizeof(shifts)
  468. + sp->nshifts * sizeof(short));
  469. sp2->nshifts = sp->nshifts + 1;
  470. /* Stick this shift into the vector at the proper place. */
  471. statep = first_state->next;
  472. for (k = 0, i = 0; i < sp->nshifts; k++, i++)
  473. {
  474. if (statep->accessing_symbol > start_symbol && i == k)
  475. sp2->shifts[k++] = nstates;
  476. sp2->shifts[k] = sp->shifts[i];
  477. statep = statep->next;
  478. }
  479. if (i == k)
  480. sp2->shifts[k++] = nstates;
  481. /* Patch sp2 into the chain of shifts
  482. in place of sp, at the beginning. */
  483. sp2->next = sp->next;
  484. first_shift = sp2;
  485. if (last_shift == sp)
  486. last_shift = sp2;
  487. FREE(sp);
  488. /* Create the next-to-final state, with shift to
  489. what will be the final state. */
  490. insert_start_shift();
  491. }
  492. }
  493. else
  494. {
  495. /* The initial state didn't even have any shifts.
  496. Give it one shift, to the next-to-final state. */
  497. sp = NEW(shifts);
  498. sp->nshifts = 1;
  499. sp->shifts[0] = nstates;
  500. /* Patch sp into the chain of shifts at the beginning. */
  501. sp->next = first_shift;
  502. first_shift = sp;
  503. /* Create the next-to-final state, with shift to
  504. what will be the final state. */
  505. insert_start_shift();
  506. }
  507. }
  508. else
  509. {
  510. /* There are no shifts for any state.
  511. Make one shift, from the initial state to the next-to-final state. */
  512. sp = NEW(shifts);
  513. sp->nshifts = 1;
  514. sp->shifts[0] = nstates;
  515. /* Initialize the chain of shifts with sp. */
  516. first_shift = sp;
  517. last_shift = sp;
  518. /* Create the next-to-final state, with shift to
  519. what will be the final state. */
  520. insert_start_shift();
  521. }
  522. /* Make the final state--the one that follows a shift from the
  523. next-to-final state.
  524. The symbol for that shift is 0 (end-of-file). */
  525. statep = (core *) xmalloc((unsigned) (sizeof(core) - sizeof(short)));
  526. statep->number = nstates;
  527. last_state->next = statep;
  528. last_state = statep;
  529. /* Make the shift from the final state to the termination state. */
  530. sp = NEW(shifts);
  531. sp->number = nstates++;
  532. sp->nshifts = 1;
  533. sp->shifts[0] = nstates;
  534. last_shift->next = sp;
  535. last_shift = sp;
  536. /* Note that the variable `final_state' refers to what we sometimes call
  537. the termination state. */
  538. final_state = nstates;
  539. /* Make the termination state. */
  540. statep = (core *) xmalloc((unsigned) (sizeof(core) - sizeof(short)));
  541. statep->number = nstates++;
  542. last_state->next = statep;
  543. last_state = statep;
  544. }
  545. /* subroutine of augment_automaton.
  546. Create the next-to-final state, to which a shift has already been made in
  547. the initial state. */
  548. void
  549. insert_start_shift()
  550. {
  551. register core *statep;
  552. register shifts *sp;
  553. statep = (core *) xmalloc((unsigned) (sizeof(core) - sizeof(short)));
  554. statep->number = nstates;
  555. statep->accessing_symbol = start_symbol;
  556. last_state->next = statep;
  557. last_state = statep;
  558. /* Make a shift from this state to (what will be) the final state. */
  559. sp = NEW(shifts);
  560. sp->number = nstates++;
  561. sp->nshifts = 1;
  562. sp->shifts[0] = nstates;
  563. last_shift->next = sp;
  564. last_shift = sp;
  565. }