Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1902 lines
41 KiB

  1. #include "defs.h"
  2. /* The line size must be a positive integer. One hundred was chosen */
  3. /* because few lines in Yacc input grammars exceed 100 characters. */
  4. /* Note that if a line exceeds LINESIZE characters, the line buffer */
  5. /* will be expanded to accomodate it. */
  6. #define LINESIZE 100
  7. char *cache;
  8. int cinc, cache_size;
  9. int ntags, tagmax;
  10. char **tag_table;
  11. char saw_eof, unionized;
  12. char *cptr, *line;
  13. int linesize;
  14. bucket *goal;
  15. int prec;
  16. int gensym;
  17. char last_was_action;
  18. int maxitems;
  19. bucket **pitem;
  20. int maxrules;
  21. bucket **plhs;
  22. int name_pool_size;
  23. char *name_pool;
  24. char line_format[] = "#line %d \"%s\"\n";
  25. #if defined(TRIPLISH)
  26. extern int ParserChoice;
  27. #endif
  28. #if defined(KYLEP_CHANGE)
  29. /* BYACC prototypes, with type safety */
  30. void start_rule( register bucket * bp, int s_lineno );
  31. #endif // KYLEP_CHANGE
  32. cachec(c)
  33. int c;
  34. {
  35. assert(cinc >= 0);
  36. if (cinc >= cache_size)
  37. {
  38. cache_size += 256;
  39. cache = REALLOC(cache, cache_size);
  40. if (cache == 0) no_space();
  41. }
  42. #if defined(KYLEP_CHANGE)
  43. cache[cinc] = (char) c;
  44. #else
  45. cache[cinc] = c;
  46. #endif
  47. ++cinc;
  48. }
  49. #if defined(KYLEP_CHANGE)
  50. void
  51. #endif
  52. get_line()
  53. {
  54. register FILE *f = input_file;
  55. register int c;
  56. register int i;
  57. if (saw_eof || (c = getc(f)) == EOF)
  58. {
  59. if (line) { FREE(line); line = 0; }
  60. cptr = 0;
  61. saw_eof = 1;
  62. return;
  63. }
  64. if (line == 0 || linesize != (LINESIZE + 1))
  65. {
  66. if (line) FREE(line);
  67. linesize = LINESIZE + 1;
  68. line = MALLOC(linesize);
  69. if (line == 0) no_space();
  70. }
  71. i = 0;
  72. ++lineno;
  73. for (;;)
  74. {
  75. #if defined(KYLEP_CHANGE)
  76. line[i] = (char) c;
  77. #else
  78. line[i] = c;
  79. #endif
  80. if (c == '\n') { cptr = line; return; }
  81. if (++i >= linesize)
  82. {
  83. linesize += LINESIZE;
  84. line = REALLOC(line, linesize);
  85. if (line == 0) no_space();
  86. }
  87. c = getc(f);
  88. if (c == EOF)
  89. {
  90. line[i] = '\n';
  91. saw_eof = 1;
  92. cptr = line;
  93. return;
  94. }
  95. }
  96. }
  97. char *
  98. dup_line()
  99. {
  100. register char *p, *s, *t;
  101. if (line == 0) return (0);
  102. s = line;
  103. while (*s != '\n') ++s;
  104. p = MALLOC(s - line + 1);
  105. if (p == 0) no_space();
  106. s = line;
  107. t = p;
  108. while ((*t++ = *s++) != '\n') continue;
  109. return (p);
  110. }
  111. #if defined(KYLEP_CHANGE)
  112. void
  113. #endif
  114. skip_comment()
  115. {
  116. register char *s;
  117. int st_lineno = lineno;
  118. char *st_line = dup_line();
  119. char *st_cptr = st_line + (cptr - line);
  120. s = cptr + 2;
  121. for (;;)
  122. {
  123. if (*s == '*' && s[1] == '/')
  124. {
  125. cptr = s + 2;
  126. FREE(st_line);
  127. return;
  128. }
  129. if (*s == '\n')
  130. {
  131. get_line();
  132. if (line == 0)
  133. unterminated_comment(st_lineno, st_line, st_cptr);
  134. s = cptr;
  135. }
  136. else
  137. ++s;
  138. }
  139. }
  140. int
  141. nextc()
  142. {
  143. register char *s;
  144. if (line == 0)
  145. {
  146. get_line();
  147. if (line == 0)
  148. return (EOF);
  149. }
  150. s = cptr;
  151. for (;;)
  152. {
  153. switch (*s)
  154. {
  155. case '\n':
  156. get_line();
  157. if (line == 0) return (EOF);
  158. s = cptr;
  159. break;
  160. case ' ':
  161. case '\t':
  162. case '\f':
  163. case '\r':
  164. case '\v':
  165. case ',':
  166. case ';':
  167. ++s;
  168. break;
  169. case '\\':
  170. cptr = s;
  171. return ('%');
  172. case '/':
  173. if (s[1] == '*')
  174. {
  175. cptr = s;
  176. skip_comment();
  177. s = cptr;
  178. break;
  179. }
  180. else if (s[1] == '/')
  181. {
  182. get_line();
  183. if (line == 0) return (EOF);
  184. s = cptr;
  185. break;
  186. }
  187. /* fall through */
  188. default:
  189. cptr = s;
  190. return (*s);
  191. }
  192. }
  193. }
  194. int
  195. keyword()
  196. {
  197. register int c;
  198. char *t_cptr = cptr;
  199. c = *++cptr;
  200. if (isalpha(c))
  201. {
  202. cinc = 0;
  203. for (;;)
  204. {
  205. if (isalpha(c))
  206. {
  207. if (isupper(c)) c = tolower(c);
  208. cachec(c);
  209. }
  210. else if (isdigit(c) || c == '_' || c == '.' || c == '$')
  211. cachec(c);
  212. else
  213. break;
  214. c = *++cptr;
  215. }
  216. cachec(NUL);
  217. if (strcmp(cache, "token") == 0 || strcmp(cache, "term") == 0)
  218. return (TOKEN);
  219. if (strcmp(cache, "type") == 0)
  220. return (TYPE);
  221. if (strcmp(cache, "left") == 0)
  222. return (LEFT);
  223. if (strcmp(cache, "right") == 0)
  224. return (RIGHT);
  225. if (strcmp(cache, "nonassoc") == 0 || strcmp(cache, "binary") == 0)
  226. return (NONASSOC);
  227. if (strcmp(cache, "start") == 0)
  228. return (START);
  229. if (strcmp(cache, "union") == 0)
  230. return (UNION);
  231. if (strcmp(cache, "ident") == 0)
  232. return (IDENT);
  233. }
  234. else
  235. {
  236. ++cptr;
  237. if (c == '{')
  238. return (TEXT);
  239. if (c == '%' || c == '\\')
  240. return (MARK);
  241. if (c == '<')
  242. return (LEFT);
  243. if (c == '>')
  244. return (RIGHT);
  245. if (c == '0')
  246. return (TOKEN);
  247. if (c == '2')
  248. return (NONASSOC);
  249. }
  250. syntax_error(lineno, line, t_cptr);
  251. /*NOTREACHED*/
  252. #if defined(KYLEP_CHANGE)
  253. return (NONASSOC);
  254. #endif
  255. }
  256. #if defined(KYLEP_CHANGE)
  257. void
  258. #endif
  259. copy_ident()
  260. {
  261. register int c;
  262. register FILE *f = output_file;
  263. c = nextc();
  264. if (c == EOF) unexpected_EOF();
  265. if (c != '"') syntax_error(lineno, line, cptr);
  266. ++outline;
  267. fprintf(f, "#ident \"");
  268. for (;;)
  269. {
  270. c = *++cptr;
  271. if (c == '\n')
  272. {
  273. fprintf(f, "\"\n");
  274. return;
  275. }
  276. putc(c, f);
  277. if (c == '"')
  278. {
  279. putc('\n', f);
  280. ++cptr;
  281. return;
  282. }
  283. }
  284. }
  285. #if defined(KYLEP_CHANGE)
  286. void
  287. #endif
  288. copy_text()
  289. {
  290. register int c;
  291. int quote;
  292. register FILE *f = text_file;
  293. int need_newline = 0;
  294. int t_lineno = lineno;
  295. char *t_line = dup_line();
  296. char *t_cptr = t_line + (cptr - line - 2);
  297. if (*cptr == '\n')
  298. {
  299. get_line();
  300. if (line == 0)
  301. unterminated_text(t_lineno, t_line, t_cptr);
  302. }
  303. if (!lflag) fprintf(f, line_format, lineno, input_file_name);
  304. loop:
  305. c = *cptr++;
  306. switch (c)
  307. {
  308. case '\n':
  309. next_line:
  310. putc('\n', f);
  311. need_newline = 0;
  312. get_line();
  313. if (line) goto loop;
  314. unterminated_text(t_lineno, t_line, t_cptr);
  315. case '\'':
  316. case '"':
  317. {
  318. int s_lineno = lineno;
  319. char *s_line = dup_line();
  320. char *s_cptr = s_line + (cptr - line - 1);
  321. quote = c;
  322. putc(c, f);
  323. for (;;)
  324. {
  325. c = *cptr++;
  326. putc(c, f);
  327. if (c == quote)
  328. {
  329. need_newline = 1;
  330. FREE(s_line);
  331. goto loop;
  332. }
  333. if (c == '\n')
  334. unterminated_string(s_lineno, s_line, s_cptr);
  335. if (c == '\\')
  336. {
  337. c = *cptr++;
  338. putc(c, f);
  339. if (c == '\n')
  340. {
  341. get_line();
  342. if (line == 0)
  343. unterminated_string(s_lineno, s_line, s_cptr);
  344. }
  345. }
  346. }
  347. }
  348. case '/':
  349. putc(c, f);
  350. need_newline = 1;
  351. c = *cptr;
  352. if (c == '/')
  353. {
  354. #if defined(KYLEP_CHANGE)
  355. putc('/', f);
  356. while ((c = *++cptr) != '\n')
  357. {
  358. putc(c, f);
  359. }
  360. #else
  361. putc('*', f);
  362. while ((c = *++cptr) != '\n')
  363. {
  364. if (c == '*' && cptr[1] == '/')
  365. fprintf(f, "* ");
  366. else
  367. putc(c, f);
  368. }
  369. fprintf(f, "*/");
  370. #endif // KYLEP_CHANGE
  371. goto next_line;
  372. }
  373. if (c == '*')
  374. {
  375. int c_lineno = lineno;
  376. char *c_line = dup_line();
  377. char *c_cptr = c_line + (cptr - line - 1);
  378. putc('*', f);
  379. ++cptr;
  380. for (;;)
  381. {
  382. c = *cptr++;
  383. putc(c, f);
  384. if (c == '*' && *cptr == '/')
  385. {
  386. putc('/', f);
  387. ++cptr;
  388. FREE(c_line);
  389. goto loop;
  390. }
  391. if (c == '\n')
  392. {
  393. get_line();
  394. if (line == 0)
  395. unterminated_comment(c_lineno, c_line, c_cptr);
  396. }
  397. }
  398. }
  399. need_newline = 1;
  400. goto loop;
  401. case '%':
  402. case '\\':
  403. if (*cptr == '}')
  404. {
  405. if (need_newline) putc('\n', f);
  406. ++cptr;
  407. FREE(t_line);
  408. return;
  409. }
  410. /* fall through */
  411. default:
  412. putc(c, f);
  413. need_newline = 1;
  414. goto loop;
  415. }
  416. }
  417. #if defined(KYLEP_CHANGE)
  418. void
  419. #endif
  420. copy_union()
  421. {
  422. register int c;
  423. int quote;
  424. int depth;
  425. int u_lineno = lineno;
  426. char *u_line = dup_line();
  427. char *u_cptr = u_line + (cptr - line - 6);
  428. if (unionized) over_unionized(cptr - 6);
  429. unionized = 1;
  430. if (!lflag)
  431. fprintf(text_file, line_format, lineno, input_file_name);
  432. fprintf(text_file, "typedef union");
  433. if (dflag) fprintf(union_file, "typedef union");
  434. depth = 0;
  435. loop:
  436. c = *cptr++;
  437. putc(c, text_file);
  438. if (dflag) putc(c, union_file);
  439. switch (c)
  440. {
  441. case '\n':
  442. next_line:
  443. get_line();
  444. if (line == 0) unterminated_union(u_lineno, u_line, u_cptr);
  445. goto loop;
  446. case '{':
  447. ++depth;
  448. goto loop;
  449. case '}':
  450. if (--depth == 0)
  451. {
  452. fprintf(text_file, " YYSTYPE;\n");
  453. FREE(u_line);
  454. return;
  455. }
  456. goto loop;
  457. case '\'':
  458. case '"':
  459. {
  460. int s_lineno = lineno;
  461. char *s_line = dup_line();
  462. char *s_cptr = s_line + (cptr - line - 1);
  463. quote = c;
  464. for (;;)
  465. {
  466. c = *cptr++;
  467. putc(c, text_file);
  468. if (dflag) putc(c, union_file);
  469. if (c == quote)
  470. {
  471. FREE(s_line);
  472. goto loop;
  473. }
  474. if (c == '\n')
  475. unterminated_string(s_lineno, s_line, s_cptr);
  476. if (c == '\\')
  477. {
  478. c = *cptr++;
  479. putc(c, text_file);
  480. if (dflag) putc(c, union_file);
  481. if (c == '\n')
  482. {
  483. get_line();
  484. if (line == 0)
  485. unterminated_string(s_lineno, s_line, s_cptr);
  486. }
  487. }
  488. }
  489. }
  490. case '/':
  491. c = *cptr;
  492. if (c == '/')
  493. {
  494. #if defined(KYLEP_CHANGE)
  495. putc('/', text_file);
  496. if (dflag) putc('/', union_file);
  497. while ((c = *++cptr) != '\n')
  498. {
  499. putc(c, text_file);
  500. if (dflag) putc(c, union_file);
  501. }
  502. #else
  503. putc('*', text_file);
  504. if (dflag) putc('*', union_file);
  505. while ((c = *++cptr) != '\n')
  506. {
  507. if (c == '*' && cptr[1] == '/')
  508. {
  509. fprintf(text_file, "* ");
  510. if (dflag) fprintf(union_file, "* ");
  511. }
  512. else
  513. {
  514. putc(c, text_file);
  515. if (dflag) putc(c, union_file);
  516. }
  517. }
  518. fprintf(text_file, "*/\n");
  519. if (dflag) fprintf(union_file, "*/\n");
  520. #endif // KYLEP_CHANGE
  521. goto next_line;
  522. }
  523. if (c == '*')
  524. {
  525. int c_lineno = lineno;
  526. char *c_line = dup_line();
  527. char *c_cptr = c_line + (cptr - line - 1);
  528. putc('*', text_file);
  529. if (dflag) putc('*', union_file);
  530. ++cptr;
  531. for (;;)
  532. {
  533. c = *cptr++;
  534. putc(c, text_file);
  535. if (dflag) putc(c, union_file);
  536. if (c == '*' && *cptr == '/')
  537. {
  538. putc('/', text_file);
  539. if (dflag) putc('/', union_file);
  540. ++cptr;
  541. FREE(c_line);
  542. goto loop;
  543. }
  544. if (c == '\n')
  545. {
  546. get_line();
  547. if (line == 0)
  548. unterminated_comment(c_lineno, c_line, c_cptr);
  549. }
  550. }
  551. }
  552. goto loop;
  553. default:
  554. goto loop;
  555. }
  556. }
  557. int
  558. hexval(c)
  559. int c;
  560. {
  561. if (c >= '0' && c <= '9')
  562. return (c - '0');
  563. if (c >= 'A' && c <= 'F')
  564. return (c - 'A' + 10);
  565. if (c >= 'a' && c <= 'f')
  566. return (c - 'a' + 10);
  567. return (-1);
  568. }
  569. bucket *
  570. get_literal()
  571. {
  572. register int c, quote;
  573. register int i;
  574. register int n;
  575. register char *s;
  576. register bucket *bp;
  577. int s_lineno = lineno;
  578. char *s_line = dup_line();
  579. char *s_cptr = s_line + (cptr - line);
  580. quote = *cptr++;
  581. cinc = 0;
  582. for (;;)
  583. {
  584. c = *cptr++;
  585. if (c == quote) break;
  586. if (c == '\n') unterminated_string(s_lineno, s_line, s_cptr);
  587. if (c == '\\')
  588. {
  589. char *c_cptr = cptr - 1;
  590. c = *cptr++;
  591. switch (c)
  592. {
  593. case '\n':
  594. get_line();
  595. if (line == 0) unterminated_string(s_lineno, s_line, s_cptr);
  596. continue;
  597. case '0': case '1': case '2': case '3':
  598. case '4': case '5': case '6': case '7':
  599. n = c - '0';
  600. c = *cptr;
  601. if (IS_OCTAL(c))
  602. {
  603. n = (n << 3) + (c - '0');
  604. c = *++cptr;
  605. if (IS_OCTAL(c))
  606. {
  607. n = (n << 3) + (c - '0');
  608. ++cptr;
  609. }
  610. }
  611. if (n > MAXCHAR) illegal_character(c_cptr);
  612. c = n;
  613. break;
  614. case 'x':
  615. c = *cptr++;
  616. n = hexval(c);
  617. if (n < 0 || n >= 16)
  618. illegal_character(c_cptr);
  619. for (;;)
  620. {
  621. c = *cptr;
  622. i = hexval(c);
  623. if (i < 0 || i >= 16) break;
  624. ++cptr;
  625. n = (n << 4) + i;
  626. if (n > MAXCHAR) illegal_character(c_cptr);
  627. }
  628. c = n;
  629. break;
  630. case 'a': c = 7; break;
  631. case 'b': c = '\b'; break;
  632. case 'f': c = '\f'; break;
  633. case 'n': c = '\n'; break;
  634. case 'r': c = '\r'; break;
  635. case 't': c = '\t'; break;
  636. case 'v': c = '\v'; break;
  637. }
  638. }
  639. cachec(c);
  640. }
  641. FREE(s_line);
  642. n = cinc;
  643. s = MALLOC(n);
  644. if (s == 0) no_space();
  645. for (i = 0; i < n; ++i)
  646. s[i] = cache[i];
  647. cinc = 0;
  648. if (n == 1)
  649. cachec('\'');
  650. else
  651. cachec('"');
  652. for (i = 0; i < n; ++i)
  653. {
  654. c = ((unsigned char *)s)[i];
  655. if (c == '\\' || c == cache[0])
  656. {
  657. cachec('\\');
  658. cachec(c);
  659. }
  660. else if (isprint(c))
  661. cachec(c);
  662. else
  663. {
  664. cachec('\\');
  665. switch (c)
  666. {
  667. case 7: cachec('a'); break;
  668. case '\b': cachec('b'); break;
  669. case '\f': cachec('f'); break;
  670. case '\n': cachec('n'); break;
  671. case '\r': cachec('r'); break;
  672. case '\t': cachec('t'); break;
  673. case '\v': cachec('v'); break;
  674. default:
  675. cachec(((c >> 6) & 7) + '0');
  676. cachec(((c >> 3) & 7) + '0');
  677. cachec((c & 7) + '0');
  678. break;
  679. }
  680. }
  681. }
  682. if (n == 1)
  683. cachec('\'');
  684. else
  685. cachec('"');
  686. cachec(NUL);
  687. bp = lookup(cache);
  688. bp->class = TERM;
  689. if (n == 1 && bp->value == UNDEFINED)
  690. bp->value = *(unsigned char *)s;
  691. FREE(s);
  692. return (bp);
  693. }
  694. int
  695. is_reserved(name)
  696. char *name;
  697. {
  698. char *s;
  699. if (strcmp(name, ".") == 0 ||
  700. strcmp(name, "$accept") == 0 ||
  701. strcmp(name, "$end") == 0)
  702. return (1);
  703. if (name[0] == '$' && name[1] == '$' && isdigit(name[2]))
  704. {
  705. s = name + 3;
  706. while (isdigit(*s)) ++s;
  707. if (*s == NUL) return (1);
  708. }
  709. return (0);
  710. }
  711. bucket *
  712. get_name()
  713. {
  714. register int c;
  715. cinc = 0;
  716. for (c = *cptr; IS_IDENT(c); c = *++cptr)
  717. cachec(c);
  718. cachec(NUL);
  719. if (is_reserved(cache)) used_reserved(cache);
  720. return (lookup(cache));
  721. }
  722. int
  723. get_number()
  724. {
  725. register int c;
  726. register int n;
  727. n = 0;
  728. for (c = *cptr; isdigit(c); c = *++cptr)
  729. n = 10*n + (c - '0');
  730. return (n);
  731. }
  732. char *
  733. get_tag()
  734. {
  735. register int c;
  736. register int i;
  737. register char *s;
  738. int t_lineno = lineno;
  739. char *t_line = dup_line();
  740. char *t_cptr = t_line + (cptr - line);
  741. ++cptr;
  742. c = nextc();
  743. if (c == EOF) unexpected_EOF();
  744. if (!isalpha(c) && c != '_' && c != '$')
  745. illegal_tag(t_lineno, t_line, t_cptr);
  746. cinc = 0;
  747. do { cachec(c); c = *++cptr; } while (IS_IDENT(c));
  748. cachec(NUL);
  749. c = nextc();
  750. if (c == EOF) unexpected_EOF();
  751. if (c != '>')
  752. illegal_tag(t_lineno, t_line, t_cptr);
  753. ++cptr;
  754. for (i = 0; i < ntags; ++i)
  755. {
  756. if (strcmp(cache, tag_table[i]) == 0)
  757. return (tag_table[i]);
  758. }
  759. if (ntags >= tagmax)
  760. {
  761. tagmax += 16;
  762. tag_table = (char **)
  763. (tag_table ? REALLOC(tag_table, tagmax*sizeof(char *))
  764. : MALLOC(tagmax*sizeof(char *)));
  765. if (tag_table == 0) no_space();
  766. }
  767. s = MALLOC(cinc);
  768. if (s == 0) no_space();
  769. strcpy(s, cache);
  770. tag_table[ntags] = s;
  771. ++ntags;
  772. FREE(t_line);
  773. return (s);
  774. }
  775. #if defined(KYLEP_CHANGE)
  776. void
  777. #endif
  778. declare_tokens(assoc)
  779. int assoc;
  780. {
  781. register int c;
  782. register bucket *bp;
  783. int value;
  784. char *tag = 0;
  785. if (assoc != TOKEN) ++prec;
  786. c = nextc();
  787. if (c == EOF) unexpected_EOF();
  788. if (c == '<')
  789. {
  790. tag = get_tag();
  791. c = nextc();
  792. if (c == EOF) unexpected_EOF();
  793. }
  794. for (;;)
  795. {
  796. if (isalpha(c) || c == '_' || c == '.' || c == '$')
  797. bp = get_name();
  798. else if (c == '\'' || c == '"')
  799. bp = get_literal();
  800. else
  801. return;
  802. if (bp == goal) tokenized_start(bp->name);
  803. bp->class = TERM;
  804. if (tag)
  805. {
  806. if (bp->tag && tag != bp->tag)
  807. retyped_warning(bp->name);
  808. bp->tag = tag;
  809. }
  810. if (assoc != TOKEN)
  811. {
  812. if (bp->prec && prec != bp->prec)
  813. reprec_warning(bp->name);
  814. #if defined(KYLEP_CHANGE)
  815. bp->assoc = (char) assoc;
  816. bp->prec = (short) prec;
  817. #else
  818. bp->assoc = assoc;
  819. bp->prec = prec;
  820. #endif // KYLEP_CHANGE
  821. }
  822. c = nextc();
  823. if (c == EOF) unexpected_EOF();
  824. value = UNDEFINED;
  825. if (isdigit(c))
  826. {
  827. value = get_number();
  828. if (bp->value != UNDEFINED && value != bp->value)
  829. revalued_warning(bp->name);
  830. #if defined(KYLEP_CHANGE)
  831. bp->value = (short) value;
  832. #else
  833. bp->value = value;
  834. #endif // KYLEP_CHANGE
  835. c = nextc();
  836. if (c == EOF) unexpected_EOF();
  837. }
  838. }
  839. }
  840. #if defined(KYLEP_CHANGE)
  841. void
  842. #endif
  843. declare_types()
  844. {
  845. register int c;
  846. register bucket *bp;
  847. char *tag;
  848. c = nextc();
  849. if (c == EOF) unexpected_EOF();
  850. if (c != '<') syntax_error(lineno, line, cptr);
  851. tag = get_tag();
  852. for (;;)
  853. {
  854. c = nextc();
  855. if (isalpha(c) || c == '_' || c == '.' || c == '$')
  856. bp = get_name();
  857. else if (c == '\'' || c == '"')
  858. bp = get_literal();
  859. else
  860. return;
  861. if (bp->tag && tag != bp->tag)
  862. retyped_warning(bp->name);
  863. bp->tag = tag;
  864. }
  865. }
  866. declare_start()
  867. {
  868. register int c;
  869. register bucket *bp;
  870. c = nextc();
  871. if (c == EOF) unexpected_EOF();
  872. if (!isalpha(c) && c != '_' && c != '.' && c != '$')
  873. syntax_error(lineno, line, cptr);
  874. bp = get_name();
  875. if (bp->class == TERM)
  876. terminal_start(bp->name);
  877. if (goal && goal != bp)
  878. restarted_warning();
  879. goal = bp;
  880. }
  881. #if defined(KYLEP_CHANGE)
  882. void
  883. #endif
  884. read_declarations()
  885. {
  886. register int c, k;
  887. cache_size = 256;
  888. cache = MALLOC(cache_size);
  889. if (cache == 0) no_space();
  890. for (;;)
  891. {
  892. c = nextc();
  893. if (c == EOF) unexpected_EOF();
  894. if (c != '%') syntax_error(lineno, line, cptr);
  895. switch (k = keyword())
  896. {
  897. case MARK:
  898. return;
  899. case IDENT:
  900. copy_ident();
  901. break;
  902. case TEXT:
  903. copy_text();
  904. break;
  905. case UNION:
  906. copy_union();
  907. break;
  908. case TOKEN:
  909. case LEFT:
  910. case RIGHT:
  911. case NONASSOC:
  912. declare_tokens(k);
  913. break;
  914. case TYPE:
  915. declare_types();
  916. break;
  917. case START:
  918. declare_start();
  919. break;
  920. }
  921. }
  922. }
  923. initialize_grammar()
  924. {
  925. nitems = 4;
  926. maxitems = 300;
  927. pitem = (bucket **) MALLOC(maxitems*sizeof(bucket *));
  928. if (pitem == 0) no_space();
  929. pitem[0] = 0;
  930. pitem[1] = 0;
  931. pitem[2] = 0;
  932. pitem[3] = 0;
  933. nrules = 3;
  934. maxrules = 100;
  935. plhs = (bucket **) MALLOC(maxrules*sizeof(bucket *));
  936. if (plhs == 0) no_space();
  937. plhs[0] = 0;
  938. plhs[1] = 0;
  939. plhs[2] = 0;
  940. rprec = (short *) MALLOC(maxrules*sizeof(short));
  941. if (rprec == 0) no_space();
  942. rprec[0] = 0;
  943. rprec[1] = 0;
  944. rprec[2] = 0;
  945. rassoc = (char *) MALLOC(maxrules*sizeof(char));
  946. if (rassoc == 0) no_space();
  947. rassoc[0] = TOKEN;
  948. rassoc[1] = TOKEN;
  949. rassoc[2] = TOKEN;
  950. }
  951. expand_items()
  952. {
  953. maxitems += 300;
  954. pitem = (bucket **) REALLOC(pitem, maxitems*sizeof(bucket *));
  955. if (pitem == 0) no_space();
  956. }
  957. expand_rules()
  958. {
  959. maxrules += 100;
  960. plhs = (bucket **) REALLOC(plhs, maxrules*sizeof(bucket *));
  961. if (plhs == 0) no_space();
  962. rprec = (short *) REALLOC(rprec, maxrules*sizeof(short));
  963. if (rprec == 0) no_space();
  964. rassoc = (char *) REALLOC(rassoc, maxrules*sizeof(char));
  965. if (rassoc == 0) no_space();
  966. }
  967. advance_to_start()
  968. {
  969. register int c;
  970. register bucket *bp;
  971. char *s_cptr;
  972. int s_lineno;
  973. for (;;)
  974. {
  975. c = nextc();
  976. if (c != '%') break;
  977. s_cptr = cptr;
  978. switch (keyword())
  979. {
  980. case MARK:
  981. no_grammar();
  982. case TEXT:
  983. copy_text();
  984. break;
  985. case START:
  986. declare_start();
  987. break;
  988. default:
  989. syntax_error(lineno, line, s_cptr);
  990. }
  991. }
  992. c = nextc();
  993. if (!isalpha(c) && c != '_' && c != '.' && c != '_')
  994. syntax_error(lineno, line, cptr);
  995. bp = get_name();
  996. if (goal == 0)
  997. {
  998. if (bp->class == TERM)
  999. terminal_start(bp->name);
  1000. goal = bp;
  1001. }
  1002. s_lineno = lineno;
  1003. c = nextc();
  1004. if (c == EOF) unexpected_EOF();
  1005. if (c != ':') syntax_error(lineno, line, cptr);
  1006. start_rule(bp, s_lineno);
  1007. ++cptr;
  1008. }
  1009. #if defined(KYLEP_CHANGE)
  1010. void
  1011. #endif
  1012. start_rule(bp, s_lineno)
  1013. register bucket *bp;
  1014. int s_lineno;
  1015. {
  1016. if (bp->class == TERM)
  1017. terminal_lhs(s_lineno);
  1018. bp->class = NONTERM;
  1019. if (nrules >= maxrules)
  1020. expand_rules();
  1021. plhs[nrules] = bp;
  1022. rprec[nrules] = UNDEFINED;
  1023. rassoc[nrules] = TOKEN;
  1024. }
  1025. end_rule()
  1026. {
  1027. register int i;
  1028. if (!last_was_action && plhs[nrules]->tag)
  1029. {
  1030. for (i = nitems - 1; pitem[i]; --i) continue;
  1031. if (pitem[i+1] == 0 || pitem[i+1]->tag != plhs[nrules]->tag)
  1032. default_action_warning();
  1033. }
  1034. last_was_action = 0;
  1035. if (nitems >= maxitems) expand_items();
  1036. pitem[nitems] = 0;
  1037. ++nitems;
  1038. ++nrules;
  1039. }
  1040. insert_empty_rule()
  1041. {
  1042. register bucket *bp, **bpp;
  1043. assert(cache);
  1044. sprintf(cache, "$$%d", ++gensym);
  1045. bp = make_bucket(cache);
  1046. last_symbol->next = bp;
  1047. last_symbol = bp;
  1048. bp->tag = plhs[nrules]->tag;
  1049. bp->class = NONTERM;
  1050. if ((nitems += 2) > maxitems)
  1051. expand_items();
  1052. bpp = pitem + nitems - 1;
  1053. *bpp-- = bp;
  1054. while (bpp[0] = bpp[-1]) --bpp;
  1055. if (++nrules >= maxrules)
  1056. expand_rules();
  1057. plhs[nrules] = plhs[nrules-1];
  1058. plhs[nrules-1] = bp;
  1059. rprec[nrules] = rprec[nrules-1];
  1060. rprec[nrules-1] = 0;
  1061. rassoc[nrules] = rassoc[nrules-1];
  1062. rassoc[nrules-1] = TOKEN;
  1063. }
  1064. #if defined(KYLEP_CHANGE)
  1065. void
  1066. #endif
  1067. add_symbol()
  1068. {
  1069. register int c;
  1070. register bucket *bp;
  1071. int s_lineno = lineno;
  1072. c = *cptr;
  1073. if (c == '\'' || c == '"')
  1074. bp = get_literal();
  1075. else
  1076. bp = get_name();
  1077. c = nextc();
  1078. if (c == ':')
  1079. {
  1080. end_rule();
  1081. start_rule(bp, s_lineno);
  1082. ++cptr;
  1083. return;
  1084. }
  1085. if (last_was_action)
  1086. insert_empty_rule();
  1087. last_was_action = 0;
  1088. if (++nitems > maxitems)
  1089. expand_items();
  1090. pitem[nitems-1] = bp;
  1091. }
  1092. #if defined(KYLEP_CHANGE)
  1093. void
  1094. #endif
  1095. copy_action()
  1096. {
  1097. register int c;
  1098. register int i, n;
  1099. int depth;
  1100. int quote;
  1101. char *tag;
  1102. register FILE *f = action_file;
  1103. int a_lineno = lineno;
  1104. char *a_line = dup_line();
  1105. char *a_cptr = a_line + (cptr - line);
  1106. if (last_was_action)
  1107. insert_empty_rule();
  1108. last_was_action = 1;
  1109. fprintf(f, "case %d:\n", nrules - 2);
  1110. if (!lflag)
  1111. fprintf(f, line_format, lineno, input_file_name);
  1112. if (*cptr == '=') ++cptr;
  1113. n = 0;
  1114. for (i = nitems - 1; pitem[i]; --i) ++n;
  1115. depth = 0;
  1116. loop:
  1117. c = *cptr;
  1118. if (c == '$')
  1119. {
  1120. if (cptr[1] == '<')
  1121. {
  1122. int d_lineno = lineno;
  1123. char *d_line = dup_line();
  1124. char *d_cptr = d_line + (cptr - line);
  1125. ++cptr;
  1126. tag = get_tag();
  1127. c = *cptr;
  1128. if (c == '$')
  1129. {
  1130. fprintf(f, "yyval.%s", tag);
  1131. ++cptr;
  1132. FREE(d_line);
  1133. goto loop;
  1134. }
  1135. else if (isdigit(c))
  1136. {
  1137. i = get_number();
  1138. if (i > n) dollar_warning(d_lineno, i);
  1139. fprintf(f, "yyvsp[%d].%s", i - n, tag);
  1140. FREE(d_line);
  1141. goto loop;
  1142. }
  1143. else if (c == '-' && isdigit(cptr[1]))
  1144. {
  1145. ++cptr;
  1146. i = -get_number() - n;
  1147. fprintf(f, "yyvsp[%d].%s", i, tag);
  1148. FREE(d_line);
  1149. goto loop;
  1150. }
  1151. else
  1152. dollar_error(d_lineno, d_line, d_cptr);
  1153. }
  1154. else if (cptr[1] == '$')
  1155. {
  1156. if (ntags)
  1157. {
  1158. tag = plhs[nrules]->tag;
  1159. if (tag == 0) untyped_lhs();
  1160. fprintf(f, "yyval.%s", tag);
  1161. }
  1162. else
  1163. fprintf(f, "yyval");
  1164. cptr += 2;
  1165. goto loop;
  1166. }
  1167. else if (isdigit(cptr[1]))
  1168. {
  1169. ++cptr;
  1170. i = get_number();
  1171. if (ntags)
  1172. {
  1173. if (i <= 0 || i > n)
  1174. unknown_rhs(i);
  1175. tag = pitem[nitems + i - n - 1]->tag;
  1176. if (tag == 0) untyped_rhs(i, pitem[nitems + i - n - 1]->name);
  1177. fprintf(f, "yyvsp[%d].%s", i - n, tag);
  1178. }
  1179. else
  1180. {
  1181. if (i > n)
  1182. dollar_warning(lineno, i);
  1183. fprintf(f, "yyvsp[%d]", i - n);
  1184. }
  1185. goto loop;
  1186. }
  1187. else if (cptr[1] == '-')
  1188. {
  1189. cptr += 2;
  1190. i = get_number();
  1191. if (ntags)
  1192. unknown_rhs(-i);
  1193. fprintf(f, "yyvsp[%d]", -i - n);
  1194. goto loop;
  1195. }
  1196. }
  1197. if (isalpha(c) || c == '_' || c == '$')
  1198. {
  1199. do
  1200. {
  1201. putc(c, f);
  1202. c = *++cptr;
  1203. } while (isalnum(c) || c == '_' || c == '$');
  1204. goto loop;
  1205. }
  1206. putc(c, f);
  1207. ++cptr;
  1208. switch (c)
  1209. {
  1210. case '\n':
  1211. next_line:
  1212. get_line();
  1213. if (line) goto loop;
  1214. unterminated_action(a_lineno, a_line, a_cptr);
  1215. case ';':
  1216. if (depth > 0) goto loop;
  1217. fprintf(f, "\nbreak;\n");
  1218. return;
  1219. case '{':
  1220. ++depth;
  1221. goto loop;
  1222. case '}':
  1223. if (--depth > 0) goto loop;
  1224. fprintf(f, "\nbreak;\n");
  1225. return;
  1226. case '\'':
  1227. case '"':
  1228. {
  1229. int s_lineno = lineno;
  1230. char *s_line = dup_line();
  1231. char *s_cptr = s_line + (cptr - line - 1);
  1232. quote = c;
  1233. for (;;)
  1234. {
  1235. c = *cptr++;
  1236. putc(c, f);
  1237. if (c == quote)
  1238. {
  1239. FREE(s_line);
  1240. goto loop;
  1241. }
  1242. if (c == '\n')
  1243. unterminated_string(s_lineno, s_line, s_cptr);
  1244. if (c == '\\')
  1245. {
  1246. c = *cptr++;
  1247. putc(c, f);
  1248. if (c == '\n')
  1249. {
  1250. get_line();
  1251. if (line == 0)
  1252. unterminated_string(s_lineno, s_line, s_cptr);
  1253. }
  1254. }
  1255. }
  1256. }
  1257. case '/':
  1258. c = *cptr;
  1259. if (c == '/')
  1260. {
  1261. #if defined(KYLEP_CHANGED)
  1262. putc('/', f);
  1263. while ((c = *++cptr) != '\n')
  1264. {
  1265. putc(c, f);
  1266. }
  1267. #else
  1268. putc('*', f);
  1269. while ((c = *++cptr) != '\n')
  1270. {
  1271. if (c == '*' && cptr[1] == '/')
  1272. fprintf(f, "* ");
  1273. else
  1274. putc(c, f);
  1275. }
  1276. fprintf(f, "*/\n");
  1277. #endif // KYLEP_CHANGE
  1278. goto next_line;
  1279. }
  1280. if (c == '*')
  1281. {
  1282. int c_lineno = lineno;
  1283. char *c_line = dup_line();
  1284. char *c_cptr = c_line + (cptr - line - 1);
  1285. putc('*', f);
  1286. ++cptr;
  1287. for (;;)
  1288. {
  1289. c = *cptr++;
  1290. putc(c, f);
  1291. if (c == '*' && *cptr == '/')
  1292. {
  1293. putc('/', f);
  1294. ++cptr;
  1295. FREE(c_line);
  1296. goto loop;
  1297. }
  1298. if (c == '\n')
  1299. {
  1300. get_line();
  1301. if (line == 0)
  1302. unterminated_comment(c_lineno, c_line, c_cptr);
  1303. }
  1304. }
  1305. }
  1306. goto loop;
  1307. default:
  1308. goto loop;
  1309. }
  1310. }
  1311. int
  1312. mark_symbol()
  1313. {
  1314. register int c;
  1315. register bucket *bp;
  1316. c = cptr[1];
  1317. if (c == '%' || c == '\\')
  1318. {
  1319. cptr += 2;
  1320. return (1);
  1321. }
  1322. if (c == '=')
  1323. cptr += 2;
  1324. else if ((c == 'p' || c == 'P') &&
  1325. ((c = cptr[2]) == 'r' || c == 'R') &&
  1326. ((c = cptr[3]) == 'e' || c == 'E') &&
  1327. ((c = cptr[4]) == 'c' || c == 'C') &&
  1328. ((c = cptr[5], !IS_IDENT(c))))
  1329. cptr += 5;
  1330. else
  1331. syntax_error(lineno, line, cptr);
  1332. c = nextc();
  1333. if (isalpha(c) || c == '_' || c == '.' || c == '$')
  1334. bp = get_name();
  1335. else if (c == '\'' || c == '"')
  1336. bp = get_literal();
  1337. else
  1338. {
  1339. syntax_error(lineno, line, cptr);
  1340. /*NOTREACHED*/
  1341. }
  1342. if (rprec[nrules] != UNDEFINED && bp->prec != rprec[nrules])
  1343. prec_redeclared();
  1344. rprec[nrules] = bp->prec;
  1345. rassoc[nrules] = bp->assoc;
  1346. return (0);
  1347. }
  1348. read_grammar()
  1349. {
  1350. register int c;
  1351. initialize_grammar();
  1352. advance_to_start();
  1353. for (;;)
  1354. {
  1355. c = nextc();
  1356. if (c == EOF) break;
  1357. if (isalpha(c) || c == '_' || c == '.' || c == '$' || c == '\'' ||
  1358. c == '"')
  1359. add_symbol();
  1360. else if (c == '{' || c == '=')
  1361. copy_action();
  1362. else if (c == '|')
  1363. {
  1364. end_rule();
  1365. start_rule(plhs[nrules-1], 0);
  1366. ++cptr;
  1367. }
  1368. else if (c == '%')
  1369. {
  1370. if (mark_symbol()) break;
  1371. }
  1372. else
  1373. syntax_error(lineno, line, cptr);
  1374. }
  1375. end_rule();
  1376. }
  1377. #if defined(KYLEP_CHANGE)
  1378. void
  1379. #endif
  1380. free_tags()
  1381. {
  1382. register int i;
  1383. if (tag_table == 0) return;
  1384. for (i = 0; i < ntags; ++i)
  1385. {
  1386. assert(tag_table[i]);
  1387. FREE(tag_table[i]);
  1388. }
  1389. FREE(tag_table);
  1390. }
  1391. pack_names()
  1392. {
  1393. register bucket *bp;
  1394. register char *p, *s, *t;
  1395. name_pool_size = 13; /* 13 == sizeof("$end") + sizeof("$accept") */
  1396. for (bp = first_symbol; bp; bp = bp->next)
  1397. name_pool_size += strlen(bp->name) + 1;
  1398. name_pool = MALLOC(name_pool_size);
  1399. if (name_pool == 0) no_space();
  1400. strcpy(name_pool, "$accept");
  1401. strcpy(name_pool+8, "$end");
  1402. t = name_pool + 13;
  1403. for (bp = first_symbol; bp; bp = bp->next)
  1404. {
  1405. p = t;
  1406. s = bp->name;
  1407. while (*t++ = *s++) continue;
  1408. FREE(bp->name);
  1409. bp->name = p;
  1410. }
  1411. }
  1412. check_symbols()
  1413. {
  1414. register bucket *bp;
  1415. if (goal->class == UNKNOWN)
  1416. undefined_goal(goal->name);
  1417. for (bp = first_symbol; bp; bp = bp->next)
  1418. {
  1419. if (bp->class == UNKNOWN)
  1420. {
  1421. undefined_symbol_warning(bp->name);
  1422. bp->class = TERM;
  1423. }
  1424. }
  1425. }
  1426. pack_symbols()
  1427. {
  1428. register bucket *bp;
  1429. register bucket **v;
  1430. register int i, j, k, n;
  1431. nsyms = 2;
  1432. ntokens = 1;
  1433. for (bp = first_symbol; bp; bp = bp->next)
  1434. {
  1435. ++nsyms;
  1436. if (bp->class == TERM) ++ntokens;
  1437. }
  1438. start_symbol = ntokens;
  1439. nvars = nsyms - ntokens;
  1440. symbol_name = (char **) MALLOC(nsyms*sizeof(char *));
  1441. if (symbol_name == 0) no_space();
  1442. symbol_value = (short *) MALLOC(nsyms*sizeof(short));
  1443. if (symbol_value == 0) no_space();
  1444. symbol_prec = (short *) MALLOC(nsyms*sizeof(short));
  1445. if (symbol_prec == 0) no_space();
  1446. symbol_assoc = MALLOC(nsyms);
  1447. if (symbol_assoc == 0) no_space();
  1448. v = (bucket **) MALLOC(nsyms*sizeof(bucket *));
  1449. if (v == 0) no_space();
  1450. v[0] = 0;
  1451. v[start_symbol] = 0;
  1452. i = 1;
  1453. j = start_symbol + 1;
  1454. for (bp = first_symbol; bp; bp = bp->next)
  1455. {
  1456. if (bp->class == TERM)
  1457. v[i++] = bp;
  1458. else
  1459. v[j++] = bp;
  1460. }
  1461. assert(i == ntokens && j == nsyms);
  1462. for (i = 1; i < ntokens; ++i)
  1463. #if defined(KYLEP_CHANGE)
  1464. v[i]->index = (short) i;
  1465. #else
  1466. v[i]->index = i;
  1467. #endif // KYLEP_CHANGE
  1468. goal->index = start_symbol + 1;
  1469. k = start_symbol + 2;
  1470. while (++i < nsyms)
  1471. if (v[i] != goal)
  1472. {
  1473. #if defined(KYLEP_CHANGE)
  1474. v[i]->index = (short) k;
  1475. #else
  1476. v[i]->index = k;
  1477. #endif // KYLEP_CHANGE
  1478. ++k;
  1479. }
  1480. goal->value = 0;
  1481. k = 1;
  1482. for (i = start_symbol + 1; i < nsyms; ++i)
  1483. {
  1484. if (v[i] != goal)
  1485. {
  1486. #if defined(KYLEP_CHANGE)
  1487. v[i]->value = (short) k;
  1488. #else
  1489. v[i]->value = k;
  1490. #endif // KYLEP_CHANGE
  1491. ++k;
  1492. }
  1493. }
  1494. k = 0;
  1495. for (i = 1; i < ntokens; ++i)
  1496. {
  1497. n = v[i]->value;
  1498. if (n > 256)
  1499. {
  1500. for (j = k++; j > 0 && symbol_value[j-1] > n; --j)
  1501. symbol_value[j] = symbol_value[j-1];
  1502. #if defined(KYLEP_CHANGE)
  1503. symbol_value[j] = (short) n;
  1504. #else
  1505. symbol_value[j] = n;
  1506. #endif // KYLEP_CHANGE
  1507. }
  1508. }
  1509. if (v[1]->value == UNDEFINED)
  1510. v[1]->value = 256;
  1511. j = 0;
  1512. n = 257;
  1513. for (i = 2; i < ntokens; ++i)
  1514. {
  1515. if (v[i]->value == UNDEFINED)
  1516. {
  1517. while (j < k && n == symbol_value[j])
  1518. {
  1519. while (++j < k && n == symbol_value[j]) continue;
  1520. ++n;
  1521. }
  1522. #if defined(KYLEP_CHANGE)
  1523. v[i]->value = (short) n;
  1524. #else
  1525. v[i]->value = n;
  1526. #endif // KYLEP_CHANGE
  1527. ++n;
  1528. }
  1529. }
  1530. symbol_name[0] = name_pool + 8;
  1531. symbol_value[0] = 0;
  1532. symbol_prec[0] = 0;
  1533. symbol_assoc[0] = TOKEN;
  1534. for (i = 1; i < ntokens; ++i)
  1535. {
  1536. symbol_name[i] = v[i]->name;
  1537. symbol_value[i] = v[i]->value;
  1538. symbol_prec[i] = v[i]->prec;
  1539. symbol_assoc[i] = v[i]->assoc;
  1540. }
  1541. symbol_name[start_symbol] = name_pool;
  1542. symbol_value[start_symbol] = -1;
  1543. symbol_prec[start_symbol] = 0;
  1544. symbol_assoc[start_symbol] = TOKEN;
  1545. for (++i; i < nsyms; ++i)
  1546. {
  1547. k = v[i]->index;
  1548. symbol_name[k] = v[i]->name;
  1549. symbol_value[k] = v[i]->value;
  1550. symbol_prec[k] = v[i]->prec;
  1551. symbol_assoc[k] = v[i]->assoc;
  1552. }
  1553. FREE(v);
  1554. }
  1555. pack_grammar()
  1556. {
  1557. register int i, j;
  1558. int assoc, prec;
  1559. ritem = (short *) MALLOC(nitems*sizeof(short));
  1560. if (ritem == 0) no_space();
  1561. rlhs = (short *) MALLOC(nrules*sizeof(short));
  1562. if (rlhs == 0) no_space();
  1563. rrhs = (short *) MALLOC((nrules+1)*sizeof(short));
  1564. if (rrhs == 0) no_space();
  1565. rprec = (short *) REALLOC(rprec, nrules*sizeof(short));
  1566. if (rprec == 0) no_space();
  1567. rassoc = REALLOC(rassoc, nrules);
  1568. if (rassoc == 0) no_space();
  1569. ritem[0] = -1;
  1570. ritem[1] = goal->index;
  1571. ritem[2] = 0;
  1572. ritem[3] = -2;
  1573. rlhs[0] = 0;
  1574. rlhs[1] = 0;
  1575. #if defined(KYLEP_CHANGE)
  1576. rlhs[2] = (short) start_symbol;
  1577. #else
  1578. rlhs[2] = start_symbol;
  1579. #endif // KYLEP_CHANGE
  1580. rrhs[0] = 0;
  1581. rrhs[1] = 0;
  1582. rrhs[2] = 1;
  1583. j = 4;
  1584. for (i = 3; i < nrules; ++i)
  1585. {
  1586. rlhs[i] = plhs[i]->index;
  1587. #if defined(KYLEP_CHANGE)
  1588. rrhs[i] = (short) j;
  1589. #else
  1590. rrhs[i] = j;
  1591. #endif // KYLEP_CHANGE
  1592. assoc = TOKEN;
  1593. prec = 0;
  1594. while (pitem[j])
  1595. {
  1596. ritem[j] = pitem[j]->index;
  1597. if (pitem[j]->class == TERM)
  1598. {
  1599. prec = pitem[j]->prec;
  1600. assoc = pitem[j]->assoc;
  1601. }
  1602. ++j;
  1603. }
  1604. ritem[j] = -i;
  1605. ++j;
  1606. if (rprec[i] == UNDEFINED)
  1607. {
  1608. #if defined(KYLEP_CHANGE)
  1609. rprec[i] = (short) prec;
  1610. rassoc[i] = (char) assoc;
  1611. #else
  1612. rprec[i] = prec;
  1613. rassoc[i] = assoc;
  1614. #endif // KYLEP_CHANGE
  1615. }
  1616. }
  1617. #if defined(KYLEP_CHANGE)
  1618. rrhs[i] = (short) j;
  1619. #else
  1620. rrhs[i] = j;
  1621. #endif // KYLEP_CHANGE
  1622. FREE(plhs);
  1623. FREE(pitem);
  1624. }
  1625. #if defined(KYLEP_CHANGE)
  1626. void
  1627. #endif
  1628. print_grammar()
  1629. {
  1630. register int i, j, k;
  1631. int spacing;
  1632. register FILE *f = verbose_file;
  1633. if (!vflag) return;
  1634. k = 1;
  1635. for (i = 2; i < nrules; ++i)
  1636. {
  1637. if (rlhs[i] != rlhs[i-1])
  1638. {
  1639. if (i != 2) fprintf(f, "\n");
  1640. fprintf(f, "%4d %s :", i - 2, symbol_name[rlhs[i]]);
  1641. spacing = strlen(symbol_name[rlhs[i]]) + 1;
  1642. }
  1643. else
  1644. {
  1645. fprintf(f, "%4d ", i - 2);
  1646. j = spacing;
  1647. while (--j >= 0) putc(' ', f);
  1648. putc('|', f);
  1649. }
  1650. while (ritem[k] >= 0)
  1651. {
  1652. fprintf(f, " %s", symbol_name[ritem[k]]);
  1653. ++k;
  1654. }
  1655. ++k;
  1656. putc('\n', f);
  1657. }
  1658. }
  1659. #if defined(KYLEP_CHANGE)
  1660. void
  1661. #endif
  1662. reader()
  1663. {
  1664. write_section(banner, defines_file);
  1665. write_section(banner, code_file);
  1666. #if defined(TRIPLISH)
  1667. if ( eTriplishParser == ParserChoice )
  1668. write_section(includefiles, code_file);
  1669. #endif
  1670. create_symbol_table();
  1671. read_declarations();
  1672. read_grammar();
  1673. free_symbol_table();
  1674. free_tags();
  1675. pack_names();
  1676. check_symbols();
  1677. pack_symbols();
  1678. pack_grammar();
  1679. free_symbols();
  1680. print_grammar();
  1681. }