lalr.c 14 KB

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