/contrib/byacc/lr0.c

https://bitbucket.org/iorivur/freebsd-bhyve-with-suspend-resume · C · 599 lines · 505 code · 89 blank · 5 comment · 62 complexity · e2ce5f380aa8d311404a519bac9db251 MD5 · raw file

  1. /* $Id: lr0.c,v 1.13 2012/05/26 00:40:47 tom Exp $ */
  2. #include "defs.h"
  3. static core *new_state(int symbol);
  4. static Value_t get_state(int symbol);
  5. static void allocate_itemsets(void);
  6. static void allocate_storage(void);
  7. static void append_states(void);
  8. static void free_storage(void);
  9. static void generate_states(void);
  10. static void initialize_states(void);
  11. static void new_itemsets(void);
  12. static void save_reductions(void);
  13. static void save_shifts(void);
  14. static void set_derives(void);
  15. static void set_nullable(void);
  16. int nstates;
  17. core *first_state;
  18. shifts *first_shift;
  19. reductions *first_reduction;
  20. static core **state_set;
  21. static core *this_state;
  22. static core *last_state;
  23. static shifts *last_shift;
  24. static reductions *last_reduction;
  25. static int nshifts;
  26. static short *shift_symbol;
  27. static Value_t *redset;
  28. static Value_t *shiftset;
  29. static Value_t **kernel_base;
  30. static Value_t **kernel_end;
  31. static Value_t *kernel_items;
  32. static void
  33. allocate_itemsets(void)
  34. {
  35. short *itemp;
  36. short *item_end;
  37. int symbol;
  38. int i;
  39. int count;
  40. int max;
  41. short *symbol_count;
  42. count = 0;
  43. symbol_count = NEW2(nsyms, short);
  44. item_end = ritem + nitems;
  45. for (itemp = ritem; itemp < item_end; itemp++)
  46. {
  47. symbol = *itemp;
  48. if (symbol >= 0)
  49. {
  50. count++;
  51. symbol_count[symbol]++;
  52. }
  53. }
  54. kernel_base = NEW2(nsyms, short *);
  55. kernel_items = NEW2(count, short);
  56. count = 0;
  57. max = 0;
  58. for (i = 0; i < nsyms; i++)
  59. {
  60. kernel_base[i] = kernel_items + count;
  61. count += symbol_count[i];
  62. if (max < symbol_count[i])
  63. max = symbol_count[i];
  64. }
  65. shift_symbol = symbol_count;
  66. kernel_end = NEW2(nsyms, short *);
  67. }
  68. static void
  69. allocate_storage(void)
  70. {
  71. allocate_itemsets();
  72. shiftset = NEW2(nsyms, short);
  73. redset = NEW2(nrules + 1, short);
  74. state_set = NEW2(nitems, core *);
  75. }
  76. static void
  77. append_states(void)
  78. {
  79. int i;
  80. int j;
  81. Value_t symbol;
  82. #ifdef TRACE
  83. fprintf(stderr, "Entering append_states()\n");
  84. #endif
  85. for (i = 1; i < nshifts; i++)
  86. {
  87. symbol = shift_symbol[i];
  88. j = i;
  89. while (j > 0 && shift_symbol[j - 1] > symbol)
  90. {
  91. shift_symbol[j] = shift_symbol[j - 1];
  92. j--;
  93. }
  94. shift_symbol[j] = symbol;
  95. }
  96. for (i = 0; i < nshifts; i++)
  97. {
  98. symbol = shift_symbol[i];
  99. shiftset[i] = get_state(symbol);
  100. }
  101. }
  102. static void
  103. free_storage(void)
  104. {
  105. FREE(shift_symbol);
  106. FREE(redset);
  107. FREE(shiftset);
  108. FREE(kernel_base);
  109. FREE(kernel_end);
  110. FREE(kernel_items);
  111. FREE(state_set);
  112. }
  113. static void
  114. generate_states(void)
  115. {
  116. allocate_storage();
  117. itemset = NEW2(nitems, short);
  118. ruleset = NEW2(WORDSIZE(nrules), unsigned);
  119. set_first_derives();
  120. initialize_states();
  121. while (this_state)
  122. {
  123. closure(this_state->items, this_state->nitems);
  124. save_reductions();
  125. new_itemsets();
  126. append_states();
  127. if (nshifts > 0)
  128. save_shifts();
  129. this_state = this_state->next;
  130. }
  131. free_storage();
  132. }
  133. static Value_t
  134. get_state(int symbol)
  135. {
  136. int key;
  137. short *isp1;
  138. short *isp2;
  139. short *iend;
  140. core *sp;
  141. int found;
  142. int n;
  143. #ifdef TRACE
  144. fprintf(stderr, "Entering get_state(%d)\n", symbol);
  145. #endif
  146. isp1 = kernel_base[symbol];
  147. iend = kernel_end[symbol];
  148. n = (int)(iend - isp1);
  149. key = *isp1;
  150. assert(0 <= key && key < nitems);
  151. sp = state_set[key];
  152. if (sp)
  153. {
  154. found = 0;
  155. while (!found)
  156. {
  157. if (sp->nitems == n)
  158. {
  159. found = 1;
  160. isp1 = kernel_base[symbol];
  161. isp2 = sp->items;
  162. while (found && isp1 < iend)
  163. {
  164. if (*isp1++ != *isp2++)
  165. found = 0;
  166. }
  167. }
  168. if (!found)
  169. {
  170. if (sp->link)
  171. {
  172. sp = sp->link;
  173. }
  174. else
  175. {
  176. sp = sp->link = new_state(symbol);
  177. found = 1;
  178. }
  179. }
  180. }
  181. }
  182. else
  183. {
  184. state_set[key] = sp = new_state(symbol);
  185. }
  186. return (sp->number);
  187. }
  188. static void
  189. initialize_states(void)
  190. {
  191. unsigned i;
  192. short *start_derives;
  193. core *p;
  194. start_derives = derives[start_symbol];
  195. for (i = 0; start_derives[i] >= 0; ++i)
  196. continue;
  197. p = (core *)MALLOC(sizeof(core) + i * sizeof(short));
  198. NO_SPACE(p);
  199. p->next = 0;
  200. p->link = 0;
  201. p->number = 0;
  202. p->accessing_symbol = 0;
  203. p->nitems = (Value_t) i;
  204. for (i = 0; start_derives[i] >= 0; ++i)
  205. p->items[i] = rrhs[start_derives[i]];
  206. first_state = last_state = this_state = p;
  207. nstates = 1;
  208. }
  209. static void
  210. new_itemsets(void)
  211. {
  212. Value_t i;
  213. int shiftcount;
  214. short *isp;
  215. short *ksp;
  216. Value_t symbol;
  217. for (i = 0; i < nsyms; i++)
  218. kernel_end[i] = 0;
  219. shiftcount = 0;
  220. isp = itemset;
  221. while (isp < itemsetend)
  222. {
  223. i = *isp++;
  224. symbol = ritem[i];
  225. if (symbol > 0)
  226. {
  227. ksp = kernel_end[symbol];
  228. if (!ksp)
  229. {
  230. shift_symbol[shiftcount++] = symbol;
  231. ksp = kernel_base[symbol];
  232. }
  233. *ksp++ = (Value_t) (i + 1);
  234. kernel_end[symbol] = ksp;
  235. }
  236. }
  237. nshifts = shiftcount;
  238. }
  239. static core *
  240. new_state(int symbol)
  241. {
  242. unsigned n;
  243. core *p;
  244. short *isp1;
  245. short *isp2;
  246. short *iend;
  247. #ifdef TRACE
  248. fprintf(stderr, "Entering new_state(%d)\n", symbol);
  249. #endif
  250. if (nstates >= MAXSHORT)
  251. fatal("too many states");
  252. isp1 = kernel_base[symbol];
  253. iend = kernel_end[symbol];
  254. n = (unsigned)(iend - isp1);
  255. p = (core *)allocate((sizeof(core) + (n - 1) * sizeof(short)));
  256. p->accessing_symbol = (Value_t) symbol;
  257. p->number = (Value_t) nstates;
  258. p->nitems = (Value_t) n;
  259. isp2 = p->items;
  260. while (isp1 < iend)
  261. *isp2++ = *isp1++;
  262. last_state->next = p;
  263. last_state = p;
  264. nstates++;
  265. return (p);
  266. }
  267. /* show_cores is used for debugging */
  268. void
  269. show_cores(void)
  270. {
  271. core *p;
  272. int i, j, k, n;
  273. int itemno;
  274. k = 0;
  275. for (p = first_state; p; ++k, p = p->next)
  276. {
  277. if (k)
  278. printf("\n");
  279. printf("state %d, number = %d, accessing symbol = %s\n",
  280. k, p->number, symbol_name[p->accessing_symbol]);
  281. n = p->nitems;
  282. for (i = 0; i < n; ++i)
  283. {
  284. itemno = p->items[i];
  285. printf("%4d ", itemno);
  286. j = itemno;
  287. while (ritem[j] >= 0)
  288. ++j;
  289. printf("%s :", symbol_name[rlhs[-ritem[j]]]);
  290. j = rrhs[-ritem[j]];
  291. while (j < itemno)
  292. printf(" %s", symbol_name[ritem[j++]]);
  293. printf(" .");
  294. while (ritem[j] >= 0)
  295. printf(" %s", symbol_name[ritem[j++]]);
  296. printf("\n");
  297. fflush(stdout);
  298. }
  299. }
  300. }
  301. /* show_ritems is used for debugging */
  302. void
  303. show_ritems(void)
  304. {
  305. int i;
  306. for (i = 0; i < nitems; ++i)
  307. printf("ritem[%d] = %d\n", i, ritem[i]);
  308. }
  309. /* show_rrhs is used for debugging */
  310. void
  311. show_rrhs(void)
  312. {
  313. int i;
  314. for (i = 0; i < nrules; ++i)
  315. printf("rrhs[%d] = %d\n", i, rrhs[i]);
  316. }
  317. /* show_shifts is used for debugging */
  318. void
  319. show_shifts(void)
  320. {
  321. shifts *p;
  322. int i, j, k;
  323. k = 0;
  324. for (p = first_shift; p; ++k, p = p->next)
  325. {
  326. if (k)
  327. printf("\n");
  328. printf("shift %d, number = %d, nshifts = %d\n", k, p->number,
  329. p->nshifts);
  330. j = p->nshifts;
  331. for (i = 0; i < j; ++i)
  332. printf("\t%d\n", p->shift[i]);
  333. }
  334. }
  335. static void
  336. save_shifts(void)
  337. {
  338. shifts *p;
  339. short *sp1;
  340. short *sp2;
  341. short *send;
  342. p = (shifts *)allocate((sizeof(shifts) +
  343. (unsigned)(nshifts - 1) * sizeof(short)));
  344. p->number = this_state->number;
  345. p->nshifts = (Value_t) nshifts;
  346. sp1 = shiftset;
  347. sp2 = p->shift;
  348. send = shiftset + nshifts;
  349. while (sp1 < send)
  350. *sp2++ = *sp1++;
  351. if (last_shift)
  352. {
  353. last_shift->next = p;
  354. last_shift = p;
  355. }
  356. else
  357. {
  358. first_shift = p;
  359. last_shift = p;
  360. }
  361. }
  362. static void
  363. save_reductions(void)
  364. {
  365. short *isp;
  366. short *rp1;
  367. short *rp2;
  368. int item;
  369. Value_t count;
  370. reductions *p;
  371. short *rend;
  372. count = 0;
  373. for (isp = itemset; isp < itemsetend; isp++)
  374. {
  375. item = ritem[*isp];
  376. if (item < 0)
  377. {
  378. redset[count++] = (Value_t) - item;
  379. }
  380. }
  381. if (count)
  382. {
  383. p = (reductions *)allocate((sizeof(reductions) +
  384. (unsigned)(count - 1) *
  385. sizeof(short)));
  386. p->number = this_state->number;
  387. p->nreds = count;
  388. rp1 = redset;
  389. rp2 = p->rules;
  390. rend = rp1 + count;
  391. while (rp1 < rend)
  392. *rp2++ = *rp1++;
  393. if (last_reduction)
  394. {
  395. last_reduction->next = p;
  396. last_reduction = p;
  397. }
  398. else
  399. {
  400. first_reduction = p;
  401. last_reduction = p;
  402. }
  403. }
  404. }
  405. static void
  406. set_derives(void)
  407. {
  408. Value_t i, k;
  409. int lhs;
  410. short *rules;
  411. derives = NEW2(nsyms, short *);
  412. rules = NEW2(nvars + nrules, short);
  413. k = 0;
  414. for (lhs = start_symbol; lhs < nsyms; lhs++)
  415. {
  416. derives[lhs] = rules + k;
  417. for (i = 0; i < nrules; i++)
  418. {
  419. if (rlhs[i] == lhs)
  420. {
  421. rules[k] = i;
  422. k++;
  423. }
  424. }
  425. rules[k] = -1;
  426. k++;
  427. }
  428. #ifdef DEBUG
  429. print_derives();
  430. #endif
  431. }
  432. #ifdef DEBUG
  433. void
  434. print_derives(void)
  435. {
  436. int i;
  437. short *sp;
  438. printf("\nDERIVES\n\n");
  439. for (i = start_symbol; i < nsyms; i++)
  440. {
  441. printf("%s derives ", symbol_name[i]);
  442. for (sp = derives[i]; *sp >= 0; sp++)
  443. {
  444. printf(" %d", *sp);
  445. }
  446. putchar('\n');
  447. }
  448. putchar('\n');
  449. }
  450. #endif
  451. static void
  452. set_nullable(void)
  453. {
  454. int i, j;
  455. int empty;
  456. int done_flag;
  457. nullable = TMALLOC(char, nsyms);
  458. NO_SPACE(nullable);
  459. for (i = 0; i < nsyms; ++i)
  460. nullable[i] = 0;
  461. done_flag = 0;
  462. while (!done_flag)
  463. {
  464. done_flag = 1;
  465. for (i = 1; i < nitems; i++)
  466. {
  467. empty = 1;
  468. while ((j = ritem[i]) >= 0)
  469. {
  470. if (!nullable[j])
  471. empty = 0;
  472. ++i;
  473. }
  474. if (empty)
  475. {
  476. j = rlhs[-j];
  477. if (!nullable[j])
  478. {
  479. nullable[j] = 1;
  480. done_flag = 0;
  481. }
  482. }
  483. }
  484. }
  485. #ifdef DEBUG
  486. for (i = 0; i < nsyms; i++)
  487. {
  488. if (nullable[i])
  489. printf("%s is nullable\n", symbol_name[i]);
  490. else
  491. printf("%s is not nullable\n", symbol_name[i]);
  492. }
  493. #endif
  494. }
  495. void
  496. lr0(void)
  497. {
  498. set_derives();
  499. set_nullable();
  500. generate_states();
  501. }
  502. #ifdef NO_LEAKS
  503. void
  504. lr0_leaks(void)
  505. {
  506. DO_FREE(derives[start_symbol]);
  507. DO_FREE(derives);
  508. DO_FREE(nullable);
  509. }
  510. #endif