LR0.c 14 KB

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