lalr.c 14 KB

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