lalr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /* Compute look-ahead criteria 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. /* Compute how to make the finite state machine deterministic;
  18. find which rules need lookahead in each state, and which lookahead tokens they accept.
  19. lalr(), the entry point, builds these data structures:
  20. goto_map, from_state and to_state
  21. record each shift transition which accepts a variable (a nonterminal).
  22. ngotos is the number of such transitions.
  23. from_state[t] is the state number which a transition leads from
  24. and to_state[t] is the state number it leads to.
  25. All the transitions that accept a particular variable are grouped together and
  26. goto_map[i - ntokens] is the index in from_state and to_state of the first of them.
  27. consistent[s] is nonzero if no lookahead is needed to decide what to do in state s.
  28. LAruleno is a vector which records the rules that need lookahead in various states.
  29. The elements of LAruleno that apply to state s are those from
  30. l2ookaheads[s] through l2ookaheads[s+1]-1.
  31. Each element of LAruleno is a rule number.
  32. If lr is the length of LAruleno, then a number from 0 to lr-1
  33. can specify both a rule and a state where the rule might be applied.
  34. LA is a lr by ntokens matrix of bits.
  35. LA[l, i] is 1 if the rule LAruleno[l] is applicable in the appropriate state
  36. when the next token is symbol i.
  37. If LA[l, i] and LA[l, j] are both 1 for i != j, it is a conflict.
  38. */
  39. #include <stdio.h>
  40. #include "machine.h"
  41. #include "types.h"
  42. #include "state.h"
  43. #include "new.h"
  44. #include "gram.h"
  45. extern short **derives;
  46. extern char *nullable;
  47. int tokensetsize;
  48. short *l2ookaheads;
  49. short *LAruleno;
  50. unsigned *LA;
  51. short *accessing_symbol;
  52. char *consistent;
  53. core **state_table;
  54. shifts **shift_table;
  55. r2eductions **reduction_table;
  56. short *goto_map;
  57. short *from_state;
  58. short *to_state;
  59. short **transpose();
  60. static int infinity;
  61. static int maxrhs;
  62. static int ngotos;
  63. static unsigned *F;
  64. static short **includes;
  65. static shorts **lookback;
  66. static short **R;
  67. static short *INDEX;
  68. static short *VERTICES;
  69. static int top;
  70. lalr()
  71. {
  72. tokensetsize = WORDSIZE(ntokens);
  73. set_state_table();
  74. set_accessing_symbol();
  75. set_shift_table();
  76. set_reduction_table();
  77. set_maxrhs();
  78. i2nitialize_LA();
  79. set_goto_map();
  80. i8nitialize_F();
  81. build_relations();
  82. c2ompute_FOLLOWS();
  83. compute_lookaheads();
  84. }
  85. set_state_table()
  86. {
  87. register core *sp;
  88. state_table = NEW2(nstates, core *);
  89. for (sp = first_state; sp; sp = sp->next)
  90. state_table[sp->number] = sp;
  91. }
  92. set_accessing_symbol()
  93. {
  94. register core *sp;
  95. accessing_symbol = NEW2(nstates, short);
  96. for (sp = first_state; sp; sp = sp->next)
  97. accessing_symbol[sp->number] = sp->accessing_symbol;
  98. }
  99. set_shift_table()
  100. {
  101. register shifts *sp;
  102. shift_table = NEW2(nstates, shifts *);
  103. for (sp = first_shift; sp; sp = sp->next)
  104. shift_table[sp->number] = sp;
  105. }
  106. set_reduction_table()
  107. {
  108. register r2eductions *rp;
  109. reduction_table = NEW2(nstates, r2eductions *);
  110. for (rp = first_reduction; rp; rp = rp->next)
  111. reduction_table[rp->number] = rp;
  112. }
  113. set_maxrhs()
  114. {
  115. register short *itemp;
  116. register int length;
  117. register int max;
  118. length = 0;
  119. max = 0;
  120. for (itemp = ritem; *itemp; itemp++)
  121. {
  122. if (*itemp > 0)
  123. {
  124. length++;
  125. }
  126. else
  127. {
  128. if (length > max) max = length;
  129. length = 0;
  130. }
  131. }
  132. maxrhs = max;
  133. }
  134. i2nitialize_LA()
  135. {
  136. register int i;
  137. register int j;
  138. register int count;
  139. register r2eductions *rp;
  140. register shifts *sp;
  141. register short *np;
  142. consistent = NEW2(nstates, char);
  143. l2ookaheads = NEW2(nstates + 1, short);
  144. count = 0;
  145. for (i = 0; i < nstates; i++)
  146. {
  147. register int j;
  148. l2ookaheads[i] = count;
  149. rp = reduction_table[i];
  150. sp = shift_table[i];
  151. if (rp && (rp->nreds > 1
  152. || (sp && ! ISVAR(accessing_symbol[sp->shifts[0]]))))
  153. count += rp->nreds;
  154. else
  155. consistent[i] = 1;
  156. if (sp)
  157. for (j = 0; j < sp->nshifts; j++)
  158. {
  159. if (accessing_symbol[sp->shifts[j]] == error_token_number)
  160. {
  161. consistent[i] = 0;
  162. break;
  163. }
  164. }
  165. }
  166. l2ookaheads[nstates] = count;
  167. LA = NEW2(count * tokensetsize, unsigned);
  168. LAruleno = NEW2(count, short);
  169. lookback = NEW2(count, shorts *);
  170. np = LAruleno;
  171. for (i = 0; i < nstates; i++)
  172. {
  173. if (!consistent[i])
  174. {
  175. if (rp = reduction_table[i])
  176. for (j = 0; j < rp->nreds; j++)
  177. *np++ = rp->rules[j];
  178. }
  179. }
  180. }
  181. set_goto_map()
  182. {
  183. register shifts *sp;
  184. register int i;
  185. register int symbol;
  186. register int k;
  187. register short *temp_map;
  188. register int state2;
  189. register int state1;
  190. goto_map = NEW2(nvars + 1, short) - ntokens;
  191. temp_map = NEW2(nvars + 1, short) - ntokens;
  192. ngotos = 0;
  193. for (sp = first_shift; sp; sp = sp->next)
  194. {
  195. for (i = sp->nshifts - 1; i >= 0; i--)
  196. {
  197. symbol = accessing_symbol[sp->shifts[i]];
  198. if (ISTOKEN(symbol)) break;
  199. if (ngotos == MAXSHORT)
  200. toomany("gotos");
  201. ngotos++;
  202. goto_map[symbol]++;
  203. }
  204. }
  205. k = 0;
  206. for (i = ntokens; i < nsyms; i++)
  207. {
  208. temp_map[i] = k;
  209. k += goto_map[i];
  210. }
  211. for (i = ntokens; i < nsyms; i++)
  212. goto_map[i] = temp_map[i];
  213. goto_map[nsyms] = ngotos;
  214. temp_map[nsyms] = ngotos;
  215. from_state = NEW2(ngotos, short);
  216. to_state = NEW2(ngotos, short);
  217. for (sp = first_shift; sp; sp = sp->next)
  218. {
  219. state1 = sp->number;
  220. for (i = sp->nshifts - 1; i >= 0; i--)
  221. {
  222. state2 = sp->shifts[i];
  223. symbol = accessing_symbol[state2];
  224. if (ISTOKEN(symbol)) break;
  225. k = temp_map[symbol]++;
  226. from_state[k] = state1;
  227. to_state[k] = state2;
  228. }
  229. }
  230. FREE(temp_map + ntokens);
  231. }
  232. /* Map_goto maps a state/symbol pair into its numeric representation. */
  233. int
  234. map_goto(state, symbol)
  235. int state;
  236. int symbol;
  237. {
  238. register int high;
  239. register int low;
  240. register int middle;
  241. register int s;
  242. low = goto_map[symbol];
  243. high = goto_map[symbol + 1];
  244. while (low <= high)
  245. {
  246. middle = (low + high) / 2;
  247. s = from_state[middle];
  248. if (s == state)
  249. return (middle);
  250. else if (s < state)
  251. low = middle + 1;
  252. else
  253. high = middle - 1;
  254. }
  255. berror("map_goto");
  256. /* NOTREACHED */
  257. }
  258. i8nitialize_F()
  259. {
  260. register int i;
  261. register int j;
  262. register int k;
  263. register shifts *sp;
  264. register short *edge;
  265. register unsigned *rowp;
  266. register short *rp;
  267. register short **reads;
  268. register int nedges;
  269. register int stateno;
  270. register int symbol;
  271. register int nwords;
  272. nwords = ngotos * tokensetsize;
  273. F = NEW2(nwords, unsigned);
  274. reads = NEW2(ngotos, short *);
  275. edge = NEW2(ngotos + 1, short);
  276. nedges = 0;
  277. rowp = F;
  278. for (i = 0; i < ngotos; i++)
  279. {
  280. stateno = to_state[i];
  281. sp = shift_table[stateno];
  282. if (sp)
  283. {
  284. k = sp->nshifts;
  285. for (j = 0; j < k; j++)
  286. {
  287. symbol = accessing_symbol[sp->shifts[j]];
  288. if (ISVAR(symbol))
  289. break;
  290. SETBIT(rowp, symbol);
  291. }
  292. for (; j < k; j++)
  293. {
  294. symbol = accessing_symbol[sp->shifts[j]];
  295. if (nullable[symbol])
  296. edge[nedges++] = map_goto(stateno, symbol);
  297. }
  298. if (nedges)
  299. {
  300. reads[i] = rp = NEW2(nedges + 1, short);
  301. for (j = 0; j < nedges; j++)
  302. rp[j] = edge[j];
  303. rp[nedges] = -1;
  304. nedges = 0;
  305. }
  306. }
  307. rowp += tokensetsize;
  308. }
  309. digraph(reads);
  310. for (i = 0; i < ngotos; i++)
  311. {
  312. if (reads[i])
  313. FREE(reads[i]);
  314. }
  315. FREE(reads);
  316. FREE(edge);
  317. }
  318. build_relations()
  319. {
  320. register int i;
  321. register int j;
  322. register int k;
  323. register short *rulep;
  324. register short *rp;
  325. register shifts *sp;
  326. register int length;
  327. register int nedges;
  328. register int done;
  329. register int state1;
  330. register int stateno;
  331. register int symbol1;
  332. register int symbol2;
  333. register short *shortp;
  334. register short *edge;
  335. register short *states;
  336. register short **new_includes;
  337. includes = NEW2(ngotos, short *);
  338. edge = NEW2(ngotos + 1, short);
  339. states = NEW2(maxrhs + 1, short);
  340. for (i = 0; i < ngotos; i++)
  341. {
  342. nedges = 0;
  343. state1 = from_state[i];
  344. symbol1 = accessing_symbol[to_state[i]];
  345. for (rulep = derives[symbol1]; *rulep > 0; rulep++)
  346. {
  347. length = 1;
  348. states[0] = state1;
  349. stateno = state1;
  350. for (rp = ritem + rrhs[*rulep]; *rp > 0; rp++)
  351. {
  352. symbol2 = *rp;
  353. sp = shift_table[stateno];
  354. k = sp->nshifts;
  355. for (j = 0; j < k; j++)
  356. {
  357. stateno = sp->shifts[j];
  358. if (accessing_symbol[stateno] == symbol2) break;
  359. }
  360. states[length++] = stateno;
  361. }
  362. if (!consistent[stateno])
  363. add_lookback_edge(stateno, *rulep, i);
  364. length--;
  365. done = 0;
  366. while (!done)
  367. {
  368. done = 1;
  369. rp--;
  370. /* JF added rp>=ritem && I hope to god its right! */
  371. if (rp>=ritem && ISVAR(*rp))
  372. {
  373. stateno = states[--length];
  374. edge[nedges++] = map_goto(stateno, *rp);
  375. if (nullable[*rp]) done = 0;
  376. }
  377. }
  378. }
  379. if (nedges)
  380. {
  381. includes[i] = shortp = NEW2(nedges + 1, short);
  382. for (j = 0; j < nedges; j++)
  383. shortp[j] = edge[j];
  384. shortp[nedges] = -1;
  385. }
  386. }
  387. new_includes = transpose(includes, ngotos);
  388. for (i = 0; i < ngotos; i++)
  389. if (includes[i])
  390. FREE(includes[i]);
  391. FREE(includes);
  392. includes = new_includes;
  393. FREE(edge);
  394. FREE(states);
  395. }
  396. add_lookback_edge(stateno, ruleno, gotono)
  397. int stateno;
  398. int ruleno;
  399. int gotono;
  400. {
  401. register int i;
  402. register int k;
  403. register int found;
  404. register shorts *sp;
  405. i = l2ookaheads[stateno];
  406. k = l2ookaheads[stateno + 1];
  407. found = 0;
  408. while (!found && i < k)
  409. {
  410. if (LAruleno[i] == ruleno)
  411. found = 1;
  412. else
  413. i++;
  414. }
  415. if (found == 0)
  416. berror("add_lookback_edge");
  417. sp = NEW(shorts);
  418. sp->next = lookback[i];
  419. sp->value = gotono;
  420. lookback[i] = sp;
  421. }
  422. short **
  423. transpose(R, n)
  424. short **R;
  425. int n;
  426. {
  427. register short **new_R;
  428. register short **temp_R;
  429. register short *nedges;
  430. register short *sp;
  431. register int i;
  432. register int k;
  433. nedges = NEW2(n, short);
  434. for (i = 0; i < n; i++)
  435. {
  436. sp = R[i];
  437. if (sp)
  438. {
  439. while (*sp >= 0)
  440. nedges[*sp++]++;
  441. }
  442. }
  443. new_R = NEW2(n, short *);
  444. temp_R = NEW2(n, short *);
  445. for (i = 0; i < n; i++)
  446. {
  447. k = nedges[i];
  448. if (k > 0)
  449. {
  450. sp = NEW2(k + 1, short);
  451. new_R[i] = sp;
  452. temp_R[i] = sp;
  453. sp[k] = -1;
  454. }
  455. }
  456. FREE(nedges);
  457. for (i = 0; i < n; i++)
  458. {
  459. sp = R[i];
  460. if (sp)
  461. {
  462. while (*sp >= 0)
  463. *temp_R[*sp++]++ = i;
  464. }
  465. }
  466. FREE(temp_R);
  467. return (new_R);
  468. }
  469. c2ompute_FOLLOWS()
  470. {
  471. register int i;
  472. digraph(includes);
  473. for (i = 0; i < ngotos; i++)
  474. {
  475. if (includes[i]) FREE(includes[i]);
  476. }
  477. FREE(includes);
  478. }
  479. compute_lookaheads()
  480. {
  481. register int i;
  482. register int n;
  483. register unsigned *fp1;
  484. register unsigned *fp2;
  485. register unsigned *fp3;
  486. register shorts *sp;
  487. register unsigned *rowp;
  488. /* register short *rulep; JF unused */
  489. /* register int count; JF unused */
  490. register shorts *sptmp;/* JF */
  491. rowp = LA;
  492. n = l2ookaheads[nstates];
  493. for (i = 0; i < n; i++)
  494. {
  495. fp3 = rowp + tokensetsize;
  496. for (sp = lookback[i]; sp; sp = sp->next)
  497. {
  498. fp1 = rowp;
  499. fp2 = F + tokensetsize * sp->value;
  500. while (fp1 < fp3)
  501. *fp1++ |= *fp2++;
  502. }
  503. rowp = fp3;
  504. }
  505. for (i = 0; i < n; i++)
  506. {/* JF removed ref to freed storage */
  507. for (sp = lookback[i]; sp; sp = sptmp) {
  508. sptmp=sp->next;
  509. FREE(sp);
  510. }
  511. }
  512. FREE(lookback);
  513. FREE(F);
  514. }
  515. digraph(relation)
  516. short **relation;
  517. {
  518. register int i;
  519. infinity = ngotos + 2;
  520. INDEX = NEW2(ngotos + 1, short);
  521. VERTICES = NEW2(ngotos + 1, short);
  522. top = 0;
  523. R = relation;
  524. for (i = 0; i < ngotos; i++)
  525. INDEX[i] = 0;
  526. for (i = 0; i < ngotos; i++)
  527. {
  528. if (INDEX[i] == 0 && R[i])
  529. traverse(i);
  530. }
  531. FREE(INDEX);
  532. FREE(VERTICES);
  533. }
  534. traverse(i)
  535. register int i;
  536. {
  537. register unsigned *fp1;
  538. register unsigned *fp2;
  539. register unsigned *fp3;
  540. register int j;
  541. register short *rp;
  542. int height;
  543. unsigned *base;
  544. VERTICES[++top] = i;
  545. INDEX[i] = height = top;
  546. base = F + i * tokensetsize;
  547. fp3 = base + tokensetsize;
  548. rp = R[i];
  549. if (rp)
  550. {
  551. while ((j = *rp++) >= 0)
  552. {
  553. if (INDEX[j] == 0)
  554. traverse(j);
  555. if (INDEX[i] > INDEX[j])
  556. INDEX[i] = INDEX[j];
  557. fp1 = base;
  558. fp2 = F + j * tokensetsize;
  559. while (fp1 < fp3)
  560. *fp1++ |= *fp2++;
  561. }
  562. }
  563. if (INDEX[i] == height)
  564. {
  565. for (;;)
  566. {
  567. j = VERTICES[top--];
  568. INDEX[j] = infinity;
  569. if (i == j)
  570. break;
  571. fp1 = base;
  572. fp2 = F + j * tokensetsize;
  573. while (fp1 < fp3)
  574. *fp2++ = *fp1++;
  575. }
  576. }
  577. }