2
0

reader.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  1. /*
  2. * Copyright (c) 1989 The Regents of the University of California.
  3. * All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Robert Paul Corbett.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. All advertising materials mentioning features or use of this software
  17. * must display the following acknowledgement:
  18. * This product includes software developed by the University of
  19. * California, Berkeley and its contributors.
  20. * 4. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. */
  36. #ifndef lint
  37. static char sccsid[] = "@(#)reader.c 5.7 (Berkeley) 1/20/91";
  38. #endif /* not lint */
  39. #include "defs.h"
  40. /* The line size must be a positive integer. One hundred was chosen */
  41. /* because few lines in Yacc input grammars exceed 100 characters. */
  42. /* Note that if a line exceeds LINESIZE characters, the line buffer */
  43. /* will be expanded to accomodate it. */
  44. #define LINESIZE 100
  45. char *cache;
  46. int cinc, cache_size;
  47. int ntags, tagmax;
  48. char **tag_table;
  49. char saw_eof;
  50. char *cptr, *line;
  51. int linesize;
  52. bucket *goal;
  53. int prec;
  54. int gensym;
  55. char last_was_action;
  56. int maxitems;
  57. bucket **pitem;
  58. int maxrules;
  59. bucket **plhs;
  60. int name_pool_size;
  61. char *name_pool;
  62. char *line_format = "\t\t\t\t\t// line %d \"%s\"\n";
  63. cachec(c)
  64. int c;
  65. {
  66. assert(cinc >= 0);
  67. if (cinc >= cache_size)
  68. {
  69. cache_size += 256;
  70. cache = REALLOC(cache, cache_size);
  71. if (cache == 0) no_space();
  72. }
  73. cache[cinc] = c;
  74. ++cinc;
  75. }
  76. get_line()
  77. {
  78. register FILE *f = input_file;
  79. register int c;
  80. register int i;
  81. if (saw_eof || (c = getc(f)) == EOF)
  82. {
  83. if (line) { FREE(line); line = 0; }
  84. cptr = 0;
  85. saw_eof = 1;
  86. return;
  87. }
  88. if (line == 0 || linesize != (LINESIZE + 1))
  89. {
  90. if (line) FREE(line);
  91. linesize = LINESIZE + 1;
  92. line = MALLOC(linesize);
  93. if (line == 0) no_space();
  94. }
  95. i = 0;
  96. ++lineno;
  97. for (;;)
  98. {
  99. line[i] = c;
  100. if (c == '\n') { cptr = line; return; }
  101. if (++i >= linesize)
  102. {
  103. linesize += LINESIZE;
  104. line = REALLOC(line, linesize);
  105. if (line == 0) no_space();
  106. }
  107. c = getc(f);
  108. if (c == EOF)
  109. {
  110. line[i] = '\n';
  111. saw_eof = 1;
  112. cptr = line;
  113. return;
  114. }
  115. }
  116. }
  117. char *
  118. dup_line()
  119. {
  120. register char *p, *s, *t;
  121. if (line == 0) return (0);
  122. s = line;
  123. while (*s != '\n') ++s;
  124. p = MALLOC(s - line + 1);
  125. if (p == 0) no_space();
  126. s = line;
  127. t = p;
  128. while ((*t++ = *s++) != '\n') continue;
  129. return (p);
  130. }
  131. skip_comment()
  132. {
  133. register char *s;
  134. int st_lineno = lineno;
  135. char *st_line = dup_line();
  136. char *st_cptr = st_line + (cptr - line);
  137. s = cptr + 2;
  138. for (;;)
  139. {
  140. if (*s == '*' && s[1] == '/')
  141. {
  142. cptr = s + 2;
  143. FREE(st_line);
  144. return;
  145. }
  146. if (*s == '\n')
  147. {
  148. get_line();
  149. if (line == 0)
  150. unterminated_comment(st_lineno, st_line, st_cptr);
  151. s = cptr;
  152. }
  153. else
  154. ++s;
  155. }
  156. }
  157. int
  158. nextc()
  159. {
  160. register char *s;
  161. if (line == 0)
  162. {
  163. get_line();
  164. if (line == 0)
  165. return (EOF);
  166. }
  167. s = cptr;
  168. for (;;)
  169. {
  170. switch (*s)
  171. {
  172. case '\n':
  173. get_line();
  174. if (line == 0) return (EOF);
  175. s = cptr;
  176. break;
  177. case ' ':
  178. case '\t':
  179. case '\f':
  180. case '\r':
  181. case '\v':
  182. case ',':
  183. case ';':
  184. ++s;
  185. break;
  186. case '\\':
  187. cptr = s;
  188. return ('%');
  189. case '/':
  190. if (s[1] == '*')
  191. {
  192. cptr = s;
  193. skip_comment();
  194. s = cptr;
  195. break;
  196. }
  197. else if (s[1] == '/')
  198. {
  199. get_line();
  200. if (line == 0) return (EOF);
  201. s = cptr;
  202. break;
  203. }
  204. /* fall through */
  205. default:
  206. cptr = s;
  207. return (*s);
  208. }
  209. }
  210. }
  211. int
  212. keyword()
  213. {
  214. register int c;
  215. char *t_cptr = cptr;
  216. c = *++cptr;
  217. if (isalpha(c))
  218. {
  219. cinc = 0;
  220. for (;;)
  221. {
  222. if (isalpha(c))
  223. {
  224. if (isupper(c)) c = tolower(c);
  225. cachec(c);
  226. }
  227. else if (isdigit(c) || c == '_' || c == '.' || c == '$')
  228. cachec(c);
  229. else
  230. break;
  231. c = *++cptr;
  232. }
  233. cachec(NUL);
  234. if (strcmp(cache, "token") == 0 || strcmp(cache, "term") == 0)
  235. return (TOKEN);
  236. if (strcmp(cache, "type") == 0)
  237. return (TYPE);
  238. if (strcmp(cache, "left") == 0)
  239. return (LEFT);
  240. if (strcmp(cache, "right") == 0)
  241. return (RIGHT);
  242. if (strcmp(cache, "nonassoc") == 0 || strcmp(cache, "binary") == 0)
  243. return (NONASSOC);
  244. if (strcmp(cache, "start") == 0)
  245. return (START);
  246. }
  247. else
  248. {
  249. ++cptr;
  250. if (c == '{')
  251. return (TEXT);
  252. if (c == '%' || c == '\\')
  253. return (MARK);
  254. if (c == '<')
  255. return (LEFT);
  256. if (c == '>')
  257. return (RIGHT);
  258. if (c == '0')
  259. return (TOKEN);
  260. if (c == '2')
  261. return (NONASSOC);
  262. }
  263. syntax_error(lineno, line, t_cptr);
  264. /*NOTREACHED*/
  265. }
  266. copy_text(f)
  267. FILE *f;
  268. {
  269. register int c;
  270. int quote;
  271. int need_newline = 0;
  272. int t_lineno = lineno;
  273. char *t_line = dup_line();
  274. char *t_cptr = t_line + (cptr - line - 2);
  275. if (*cptr == '\n')
  276. {
  277. get_line();
  278. if (line == 0)
  279. unterminated_text(t_lineno, t_line, t_cptr);
  280. }
  281. fprintf(f, line_format, lineno, input_file_name);
  282. loop:
  283. c = *cptr++;
  284. switch (c)
  285. {
  286. case '\n':
  287. next_line:
  288. putc('\n', f);
  289. need_newline = 0;
  290. get_line();
  291. if (line) goto loop;
  292. unterminated_text(t_lineno, t_line, t_cptr);
  293. case '\'':
  294. case '"':
  295. {
  296. int s_lineno = lineno;
  297. char *s_line = dup_line();
  298. char *s_cptr = s_line + (cptr - line - 1);
  299. quote = c;
  300. putc(c, f);
  301. for (;;)
  302. {
  303. c = *cptr++;
  304. putc(c, f);
  305. if (c == quote)
  306. {
  307. need_newline = 1;
  308. FREE(s_line);
  309. goto loop;
  310. }
  311. if (c == '\n')
  312. unterminated_string(s_lineno, s_line, s_cptr);
  313. if (c == '\\')
  314. {
  315. c = *cptr++;
  316. putc(c, f);
  317. if (c == '\n')
  318. {
  319. get_line();
  320. if (line == 0)
  321. unterminated_string(s_lineno, s_line, s_cptr);
  322. }
  323. }
  324. }
  325. }
  326. case '/':
  327. putc(c, f);
  328. need_newline = 1;
  329. c = *cptr;
  330. if (c == '/')
  331. {
  332. do putc(c, f); while ((c = *++cptr) != '\n');
  333. goto next_line;
  334. }
  335. if (c == '*')
  336. {
  337. int c_lineno = lineno;
  338. char *c_line = dup_line();
  339. char *c_cptr = c_line + (cptr - line - 1);
  340. putc('*', f);
  341. ++cptr;
  342. for (;;)
  343. {
  344. c = *cptr++;
  345. putc(c, f);
  346. if (c == '*' && *cptr == '/')
  347. {
  348. putc('/', f);
  349. ++cptr;
  350. FREE(c_line);
  351. goto loop;
  352. }
  353. if (c == '\n')
  354. {
  355. get_line();
  356. if (line == 0)
  357. unterminated_comment(c_lineno, c_line, c_cptr);
  358. }
  359. }
  360. }
  361. need_newline = 1;
  362. goto loop;
  363. case '%':
  364. case '\\':
  365. if (*cptr == '}')
  366. {
  367. if (need_newline) putc('\n', f);
  368. ++cptr;
  369. FREE(t_line);
  370. return;
  371. }
  372. /* fall through */
  373. default:
  374. putc(c, f);
  375. need_newline = 1;
  376. goto loop;
  377. }
  378. }
  379. int
  380. hexval(c)
  381. int c;
  382. {
  383. if (c >= '0' && c <= '9')
  384. return (c - '0');
  385. if (c >= 'A' && c <= 'F')
  386. return (c - 'A' + 10);
  387. if (c >= 'a' && c <= 'f')
  388. return (c - 'a' + 10);
  389. return (-1);
  390. }
  391. bucket *
  392. get_literal()
  393. {
  394. register int c, quote;
  395. register int i;
  396. register int n;
  397. register char *s;
  398. register bucket *bp;
  399. int s_lineno = lineno;
  400. char *s_line = dup_line();
  401. char *s_cptr = s_line + (cptr - line);
  402. quote = *cptr++;
  403. cinc = 0;
  404. for (;;)
  405. {
  406. c = *cptr++;
  407. if (c == quote) break;
  408. if (c == '\n') unterminated_string(s_lineno, s_line, s_cptr);
  409. if (c == '\\')
  410. {
  411. char *c_cptr = cptr - 1;
  412. c = *cptr++;
  413. switch (c)
  414. {
  415. case '\n':
  416. get_line();
  417. if (line == 0) unterminated_string(s_lineno, s_line, s_cptr);
  418. continue;
  419. case '0': case '1': case '2': case '3':
  420. case '4': case '5': case '6': case '7':
  421. n = c - '0';
  422. c = *cptr;
  423. if (IS_OCTAL(c))
  424. {
  425. n = (n << 3) + (c - '0');
  426. c = *++cptr;
  427. if (IS_OCTAL(c))
  428. {
  429. n = (n << 3) + (c - '0');
  430. ++cptr;
  431. }
  432. }
  433. if (n > MAXCHAR) illegal_character(c_cptr);
  434. c = n;
  435. break;
  436. case 'x':
  437. c = *cptr++;
  438. n = hexval(c);
  439. if (n < 0 || n >= 16)
  440. illegal_character(c_cptr);
  441. for (;;)
  442. {
  443. c = *cptr;
  444. i = hexval(c);
  445. if (i < 0 || i >= 16) break;
  446. ++cptr;
  447. n = (n << 4) + i;
  448. if (n > MAXCHAR) illegal_character(c_cptr);
  449. }
  450. c = n;
  451. break;
  452. case 'a': c = 7; break;
  453. case 'b': c = '\b'; break;
  454. case 'f': c = '\f'; break;
  455. case 'n': c = '\n'; break;
  456. case 'r': c = '\r'; break;
  457. case 't': c = '\t'; break;
  458. case 'v': c = '\v'; break;
  459. }
  460. }
  461. cachec(c);
  462. }
  463. FREE(s_line);
  464. n = cinc;
  465. s = MALLOC(n);
  466. if (s == 0) no_space();
  467. for (i = 0; i < n; ++i)
  468. s[i] = cache[i];
  469. cinc = 0;
  470. if (n == 1)
  471. cachec('\'');
  472. else
  473. cachec('"');
  474. for (i = 0; i < n; ++i)
  475. {
  476. c = ((unsigned char *)s)[i];
  477. if (c == '\\' || c == cache[0])
  478. {
  479. cachec('\\');
  480. cachec(c);
  481. }
  482. else if (isprint(c))
  483. cachec(c);
  484. else
  485. {
  486. cachec('\\');
  487. switch (c)
  488. {
  489. case 7: cachec('a'); break;
  490. case '\b': cachec('b'); break;
  491. case '\f': cachec('f'); break;
  492. case '\n': cachec('n'); break;
  493. case '\r': cachec('r'); break;
  494. case '\t': cachec('t'); break;
  495. case '\v': cachec('v'); break;
  496. default:
  497. cachec(((c >> 6) & 7) + '0');
  498. cachec(((c >> 3) & 7) + '0');
  499. cachec((c & 7) + '0');
  500. break;
  501. }
  502. }
  503. }
  504. if (n == 1)
  505. cachec('\'');
  506. else
  507. cachec('"');
  508. cachec(NUL);
  509. bp = lookup(cache);
  510. bp->class = TERM;
  511. if (n == 1 && bp->value == UNDEFINED)
  512. bp->value = *(unsigned char *)s;
  513. FREE(s);
  514. return (bp);
  515. }
  516. int
  517. is_reserved(name)
  518. char *name;
  519. {
  520. char *s;
  521. if (strcmp(name, ".") == 0 ||
  522. strcmp(name, "$accept") == 0 ||
  523. strcmp(name, "$end") == 0)
  524. return (1);
  525. if (name[0] == '$' && name[1] == '$' && isdigit(name[2]))
  526. {
  527. s = name + 3;
  528. while (isdigit(*s)) ++s;
  529. if (*s == NUL) return (1);
  530. }
  531. return (0);
  532. }
  533. bucket *
  534. get_name()
  535. {
  536. register int c;
  537. cinc = 0;
  538. for (c = *cptr; IS_IDENT(c); c = *++cptr)
  539. cachec(c);
  540. cachec(NUL);
  541. if (is_reserved(cache)) used_reserved(cache);
  542. return (lookup(cache));
  543. }
  544. int
  545. get_number()
  546. {
  547. register int c;
  548. register int n;
  549. n = 0;
  550. for (c = *cptr; isdigit(c); c = *++cptr)
  551. n = 10*n + (c - '0');
  552. return (n);
  553. }
  554. char *
  555. get_tag(int emptyOk)
  556. {
  557. register int c;
  558. register int i;
  559. register char *s;
  560. int t_lineno = lineno;
  561. char *t_line = dup_line();
  562. char *t_cptr = t_line + (cptr - line);
  563. ++cptr;
  564. c = nextc();
  565. if (c == EOF) unexpected_EOF();
  566. if (emptyOk && c == '>') {
  567. ++cptr; return 0; // 0 indicates empty tag if emptyOk
  568. }
  569. if (!isalpha(c) && c != '_' && c != '$')
  570. illegal_tag(t_lineno, t_line, t_cptr);
  571. cinc = 0;
  572. do { cachec(c); c = *++cptr; } while (IS_IDENT(c));
  573. cachec(NUL);
  574. c = nextc();
  575. if (c == EOF) unexpected_EOF();
  576. if (c != '>')
  577. illegal_tag(t_lineno, t_line, t_cptr);
  578. ++cptr;
  579. for (i = 0; i < ntags; ++i)
  580. {
  581. if (strcmp(cache, tag_table[i]) == 0)
  582. return (tag_table[i]);
  583. }
  584. if (ntags >= tagmax)
  585. {
  586. tagmax += 16;
  587. tag_table = (char **)
  588. (tag_table ? REALLOC(tag_table, tagmax*sizeof(char *))
  589. : MALLOC(tagmax*sizeof(char *)));
  590. if (tag_table == 0) no_space();
  591. }
  592. s = MALLOC(cinc);
  593. if (s == 0) no_space();
  594. strcpy(s, cache);
  595. tag_table[ntags] = s;
  596. ++ntags;
  597. FREE(t_line);
  598. return (s);
  599. }
  600. declare_tokens(assoc)
  601. int assoc;
  602. {
  603. register int c;
  604. register bucket *bp;
  605. int value;
  606. char *tag = 0;
  607. if (assoc != TOKEN) ++prec;
  608. c = nextc();
  609. if (c == EOF) unexpected_EOF();
  610. if (c == '<')
  611. {
  612. tag = get_tag(0);
  613. c = nextc();
  614. if (c == EOF) unexpected_EOF();
  615. }
  616. for (;;)
  617. {
  618. if (isalpha(c) || c == '_' || c == '.' || c == '$')
  619. bp = get_name();
  620. else if (c == '\'' || c == '"')
  621. bp = get_literal();
  622. else
  623. return;
  624. if (bp == goal) tokenized_start(bp->name);
  625. bp->class = TERM;
  626. if (tag)
  627. {
  628. if (bp->tag && tag != bp->tag)
  629. retyped_warning(bp->name);
  630. bp->tag = tag;
  631. }
  632. if (assoc != TOKEN)
  633. {
  634. if (bp->prec && prec != bp->prec)
  635. reprec_warning(bp->name);
  636. bp->assoc = assoc;
  637. bp->prec = prec;
  638. }
  639. c = nextc();
  640. if (c == EOF) unexpected_EOF();
  641. value = UNDEFINED;
  642. if (isdigit(c))
  643. {
  644. value = get_number();
  645. if (bp->value != UNDEFINED && value != bp->value)
  646. revalued_warning(bp->name);
  647. bp->value = value;
  648. c = nextc();
  649. if (c == EOF) unexpected_EOF();
  650. }
  651. }
  652. }
  653. declare_types()
  654. {
  655. register int c;
  656. register bucket *bp;
  657. char *tag;
  658. c = nextc();
  659. if (c == EOF) unexpected_EOF();
  660. if (c != '<') syntax_error(lineno, line, cptr);
  661. tag = get_tag(0);
  662. for (;;)
  663. {
  664. c = nextc();
  665. if (isalpha(c) || c == '_' || c == '.' || c == '$')
  666. bp = get_name();
  667. else if (c == '\'' || c == '"')
  668. bp = get_literal();
  669. else
  670. return;
  671. if (bp->tag && tag != bp->tag)
  672. retyped_warning(bp->name);
  673. bp->tag = tag;
  674. }
  675. }
  676. declare_start()
  677. {
  678. register int c;
  679. register bucket *bp;
  680. c = nextc();
  681. if (c == EOF) unexpected_EOF();
  682. if (!isalpha(c) && c != '_' && c != '.' && c != '$')
  683. syntax_error(lineno, line, cptr);
  684. bp = get_name();
  685. if (bp->class == TERM)
  686. terminal_start(bp->name);
  687. if (goal && goal != bp)
  688. restarted_warning();
  689. goal = bp;
  690. }
  691. read_declarations()
  692. {
  693. register int c, k;
  694. cache_size = 256;
  695. cache = MALLOC(cache_size);
  696. if (cache == 0) no_space();
  697. for (;;)
  698. {
  699. c = nextc();
  700. if (c == EOF) unexpected_EOF();
  701. if (c != '%') syntax_error(lineno, line, cptr);
  702. switch (k = keyword())
  703. {
  704. case MARK:
  705. return;
  706. case TEXT:
  707. copy_text(prolog_file);
  708. break;
  709. case TOKEN:
  710. case LEFT:
  711. case RIGHT:
  712. case NONASSOC:
  713. declare_tokens(k);
  714. break;
  715. case TYPE:
  716. declare_types();
  717. break;
  718. case START:
  719. declare_start();
  720. break;
  721. }
  722. }
  723. }
  724. initialize_grammar()
  725. {
  726. nitems = 4;
  727. maxitems = 300;
  728. pitem = (bucket **) MALLOC(maxitems*sizeof(bucket *));
  729. if (pitem == 0) no_space();
  730. pitem[0] = 0;
  731. pitem[1] = 0;
  732. pitem[2] = 0;
  733. pitem[3] = 0;
  734. nrules = 3;
  735. maxrules = 100;
  736. plhs = (bucket **) MALLOC(maxrules*sizeof(bucket *));
  737. if (plhs == 0) no_space();
  738. plhs[0] = 0;
  739. plhs[1] = 0;
  740. plhs[2] = 0;
  741. rprec = (short *) MALLOC(maxrules*sizeof(short));
  742. if (rprec == 0) no_space();
  743. rprec[0] = 0;
  744. rprec[1] = 0;
  745. rprec[2] = 0;
  746. rassoc = (char *) MALLOC(maxrules*sizeof(char));
  747. if (rassoc == 0) no_space();
  748. rassoc[0] = TOKEN;
  749. rassoc[1] = TOKEN;
  750. rassoc[2] = TOKEN;
  751. }
  752. expand_items()
  753. {
  754. maxitems += 300;
  755. pitem = (bucket **) REALLOC(pitem, maxitems*sizeof(bucket *));
  756. if (pitem == 0) no_space();
  757. }
  758. expand_rules()
  759. {
  760. maxrules += 100;
  761. plhs = (bucket **) REALLOC(plhs, maxrules*sizeof(bucket *));
  762. if (plhs == 0) no_space();
  763. rprec = (short *) REALLOC(rprec, maxrules*sizeof(short));
  764. if (rprec == 0) no_space();
  765. rassoc = (char *) REALLOC(rassoc, maxrules*sizeof(char));
  766. if (rassoc == 0) no_space();
  767. }
  768. advance_to_start()
  769. {
  770. register int c;
  771. register bucket *bp;
  772. char *s_cptr;
  773. int s_lineno;
  774. for (;;)
  775. {
  776. c = nextc();
  777. if (c != '%') break;
  778. s_cptr = cptr;
  779. switch (keyword())
  780. {
  781. case MARK:
  782. no_grammar();
  783. case TEXT:
  784. copy_text(local_file);
  785. break;
  786. case START:
  787. declare_start();
  788. break;
  789. default:
  790. syntax_error(lineno, line, s_cptr);
  791. }
  792. }
  793. c = nextc();
  794. if (!isalpha(c) && c != '_' && c != '.' && c != '_')
  795. syntax_error(lineno, line, cptr);
  796. bp = get_name();
  797. if (goal == 0)
  798. {
  799. if (bp->class == TERM)
  800. terminal_start(bp->name);
  801. goal = bp;
  802. }
  803. s_lineno = lineno;
  804. c = nextc();
  805. if (c == EOF) unexpected_EOF();
  806. if (c != ':') syntax_error(lineno, line, cptr);
  807. start_rule(bp, s_lineno);
  808. ++cptr;
  809. }
  810. start_rule(bp, s_lineno)
  811. register bucket *bp;
  812. int s_lineno;
  813. {
  814. if (bp->class == TERM)
  815. terminal_lhs(s_lineno);
  816. bp->class = NONTERM;
  817. if (nrules >= maxrules)
  818. expand_rules();
  819. plhs[nrules] = bp;
  820. rprec[nrules] = UNDEFINED;
  821. rassoc[nrules] = TOKEN;
  822. }
  823. end_rule()
  824. {
  825. register int i;
  826. if (!last_was_action && plhs[nrules]->tag)
  827. {
  828. for (i = nitems - 1; pitem[i]; --i) continue;
  829. if (pitem[i+1] == 0 || pitem[i+1]->tag != plhs[nrules]->tag)
  830. default_action_warning(); /** if classes don't match exactly **/
  831. } /** bug: could be superclass... **/
  832. last_was_action = 0;
  833. if (nitems >= maxitems) expand_items();
  834. pitem[nitems] = 0;
  835. ++nitems;
  836. ++nrules;
  837. }
  838. insert_empty_rule()
  839. {
  840. register bucket *bp, **bpp;
  841. assert(cache);
  842. sprintf(cache, "$$%d", ++gensym);
  843. bp = make_bucket(cache);
  844. last_symbol->next = bp;
  845. last_symbol = bp;
  846. bp->tag = plhs[nrules]->tag;
  847. bp->class = NONTERM;
  848. if ((nitems += 2) > maxitems)
  849. expand_items();
  850. bpp = pitem + nitems - 1;
  851. *bpp-- = bp;
  852. while (bpp[0] = bpp[-1]) --bpp;
  853. if (++nrules >= maxrules)
  854. expand_rules();
  855. plhs[nrules] = plhs[nrules-1];
  856. plhs[nrules-1] = bp;
  857. rprec[nrules] = rprec[nrules-1];
  858. rprec[nrules-1] = 0;
  859. rassoc[nrules] = rassoc[nrules-1];
  860. rassoc[nrules-1] = TOKEN;
  861. }
  862. add_symbol()
  863. {
  864. register int c;
  865. register bucket *bp;
  866. int s_lineno = lineno;
  867. c = *cptr;
  868. if (c == '\'' || c == '"')
  869. bp = get_literal();
  870. else
  871. bp = get_name();
  872. c = nextc();
  873. if (c == ':')
  874. {
  875. end_rule();
  876. start_rule(bp, s_lineno);
  877. ++cptr;
  878. return;
  879. }
  880. if (last_was_action)
  881. insert_empty_rule();
  882. last_was_action = 0;
  883. if (++nitems > maxitems)
  884. expand_items();
  885. pitem[nitems-1] = bp;
  886. }
  887. copy_action()
  888. {
  889. register int c;
  890. register int i, n;
  891. int depth;
  892. int quote;
  893. char *tag;
  894. register FILE *f = action_file;
  895. int a_lineno = lineno;
  896. char *a_line = dup_line();
  897. char *a_cptr = a_line + (cptr - line);
  898. if (last_was_action)
  899. insert_empty_rule();
  900. last_was_action = 1;
  901. fprintf(f, "case %d:\n", nrules - 2);
  902. fprintf(f, line_format, lineno, input_file_name);
  903. putc(' ', f); putc(' ', f);
  904. if (*cptr == '=') ++cptr;
  905. n = 0;
  906. for (i = nitems - 1; pitem[i]; --i) ++n;
  907. depth = 0;
  908. loop:
  909. c = *cptr;
  910. if (c == '$')
  911. {
  912. if (cptr[1] == '<')
  913. {
  914. int d_lineno = lineno;
  915. char *d_line = dup_line();
  916. char *d_cptr = d_line + (cptr - line);
  917. ++cptr;
  918. tag = get_tag(1);
  919. c = *cptr;
  920. if (c == '$')
  921. { if (tag && strcmp(tag, "Object"))
  922. fprintf(f, "((%s)yyVal)", tag);
  923. else fprintf(f, "yyVal");
  924. ++cptr;
  925. FREE(d_line);
  926. goto loop;
  927. }
  928. else if (isdigit(c))
  929. {
  930. i = get_number();
  931. if (i > n) dollar_warning(d_lineno, i);
  932. if (tag && strcmp(tag, "Object"))
  933. fprintf(f, "((%s)yyVals[%d+yyTop])", tag, i - n);
  934. else fprintf(f, "yyVals[%d+yyTop]", i - n);
  935. FREE(d_line);
  936. goto loop;
  937. }
  938. else if (c == '-' && isdigit(cptr[1]))
  939. {
  940. ++cptr;
  941. i = -get_number() - n;
  942. if (tag && strcmp(tag, "Object"))
  943. fprintf(f, "((%s)yyVals[%d+yyTop])", tag, i);
  944. else fprintf(f, "yyVals[%d+yyTop]", tag, i);
  945. FREE(d_line);
  946. goto loop;
  947. }
  948. else
  949. dollar_error(d_lineno, d_line, d_cptr);
  950. }
  951. else if (cptr[1] == '$')
  952. {
  953. if (ntags && plhs[nrules]->tag == 0)
  954. untyped_lhs();
  955. fprintf(f, "yyVal");
  956. cptr += 2;
  957. goto loop;
  958. }
  959. else if (isdigit(cptr[1]))
  960. {
  961. ++cptr;
  962. i = get_number();
  963. if (ntags)
  964. {
  965. if (i <= 0 || i > n)
  966. unknown_rhs(i);
  967. tag = pitem[nitems + i - n - 1]->tag;
  968. if (tag == 0)
  969. untyped_rhs(i, pitem[nitems + i - n - 1]->name),
  970. fprintf(f, "yyVals[%d+yyTop]", i - n);
  971. else if (strcmp(tag, "Object"))
  972. fprintf(f, "((%s)yyVals[%d+yyTop])", tag, i - n);
  973. else
  974. fprintf(f, "yyVals[%d+yyTop]", i - n);
  975. }
  976. else
  977. {
  978. if (i > n)
  979. dollar_warning(lineno, i);
  980. fprintf(f, "yyVals[%d+yyTop]", i - n);
  981. }
  982. goto loop;
  983. }
  984. else if (cptr[1] == '-')
  985. {
  986. cptr += 2;
  987. i = get_number();
  988. if (ntags)
  989. unknown_rhs(-i);
  990. fprintf(f, "yyVals[%d+yyTop]", -i - n);
  991. goto loop;
  992. }
  993. }
  994. if (isalpha(c) || c == '_' || c == '$')
  995. {
  996. do
  997. {
  998. putc(c, f);
  999. c = *++cptr;
  1000. } while (isalnum(c) || c == '_' || c == '$');
  1001. goto loop;
  1002. }
  1003. putc(c, f);
  1004. ++cptr;
  1005. switch (c)
  1006. {
  1007. case '\n':
  1008. next_line:
  1009. get_line();
  1010. if (line) goto loop;
  1011. unterminated_action(a_lineno, a_line, a_cptr);
  1012. case ';':
  1013. if (depth > 0) goto loop;
  1014. fprintf(f, "\nbreak;\n");
  1015. return;
  1016. case '{':
  1017. ++depth;
  1018. goto loop;
  1019. case '}':
  1020. if (--depth > 0) goto loop;
  1021. fprintf(f, "\n break;\n");
  1022. return;
  1023. case '\'':
  1024. case '"':
  1025. {
  1026. int s_lineno = lineno;
  1027. char *s_line = dup_line();
  1028. char *s_cptr = s_line + (cptr - line - 1);
  1029. quote = c;
  1030. for (;;)
  1031. {
  1032. c = *cptr++;
  1033. putc(c, f);
  1034. if (c == quote)
  1035. {
  1036. FREE(s_line);
  1037. goto loop;
  1038. }
  1039. if (c == '\n')
  1040. unterminated_string(s_lineno, s_line, s_cptr);
  1041. if (c == '\\')
  1042. {
  1043. c = *cptr++;
  1044. putc(c, f);
  1045. if (c == '\n')
  1046. {
  1047. get_line();
  1048. if (line == 0)
  1049. unterminated_string(s_lineno, s_line, s_cptr);
  1050. }
  1051. }
  1052. }
  1053. }
  1054. case '/':
  1055. c = *cptr;
  1056. if (c == '/')
  1057. {
  1058. putc('*', f);
  1059. while ((c = *++cptr) != '\n')
  1060. {
  1061. if (c == '*' && cptr[1] == '/')
  1062. fprintf(f, "* ");
  1063. else
  1064. putc(c, f);
  1065. }
  1066. fprintf(f, "*/\n");
  1067. goto next_line;
  1068. }
  1069. if (c == '*')
  1070. {
  1071. int c_lineno = lineno;
  1072. char *c_line = dup_line();
  1073. char *c_cptr = c_line + (cptr - line - 1);
  1074. putc('*', f);
  1075. ++cptr;
  1076. for (;;)
  1077. {
  1078. c = *cptr++;
  1079. putc(c, f);
  1080. if (c == '*' && *cptr == '/')
  1081. {
  1082. putc('/', f);
  1083. ++cptr;
  1084. FREE(c_line);
  1085. goto loop;
  1086. }
  1087. if (c == '\n')
  1088. {
  1089. get_line();
  1090. if (line == 0)
  1091. unterminated_comment(c_lineno, c_line, c_cptr);
  1092. }
  1093. }
  1094. }
  1095. goto loop;
  1096. default:
  1097. goto loop;
  1098. }
  1099. }
  1100. int
  1101. mark_symbol()
  1102. {
  1103. register int c;
  1104. register bucket *bp;
  1105. c = cptr[1];
  1106. if (c == '%' || c == '\\')
  1107. {
  1108. cptr += 2;
  1109. return (1);
  1110. }
  1111. if (c == '=')
  1112. cptr += 2;
  1113. else if ((c == 'p' || c == 'P') &&
  1114. ((c = cptr[2]) == 'r' || c == 'R') &&
  1115. ((c = cptr[3]) == 'e' || c == 'E') &&
  1116. ((c = cptr[4]) == 'c' || c == 'C') &&
  1117. ((c = cptr[5], !IS_IDENT(c))))
  1118. cptr += 5;
  1119. else
  1120. syntax_error(lineno, line, cptr);
  1121. c = nextc();
  1122. if (isalpha(c) || c == '_' || c == '.' || c == '$')
  1123. bp = get_name();
  1124. else if (c == '\'' || c == '"')
  1125. bp = get_literal();
  1126. else
  1127. {
  1128. syntax_error(lineno, line, cptr);
  1129. /*NOTREACHED*/
  1130. }
  1131. if (rprec[nrules] != UNDEFINED && bp->prec != rprec[nrules])
  1132. prec_redeclared();
  1133. rprec[nrules] = bp->prec;
  1134. rassoc[nrules] = bp->assoc;
  1135. return (0);
  1136. }
  1137. read_grammar()
  1138. {
  1139. register int c;
  1140. initialize_grammar();
  1141. advance_to_start();
  1142. for (;;)
  1143. {
  1144. c = nextc();
  1145. if (c == EOF) break;
  1146. if (isalpha(c) || c == '_' || c == '.' || c == '$' || c == '\'' ||
  1147. c == '"')
  1148. add_symbol();
  1149. else if (c == '{' || c == '=')
  1150. copy_action();
  1151. else if (c == '|')
  1152. {
  1153. end_rule();
  1154. start_rule(plhs[nrules-1], 0);
  1155. ++cptr;
  1156. }
  1157. else if (c == '%')
  1158. {
  1159. if (mark_symbol()) break;
  1160. }
  1161. else
  1162. syntax_error(lineno, line, cptr);
  1163. }
  1164. end_rule();
  1165. }
  1166. free_tags()
  1167. {
  1168. register int i;
  1169. if (tag_table == 0) return;
  1170. for (i = 0; i < ntags; ++i)
  1171. {
  1172. assert(tag_table[i]);
  1173. FREE(tag_table[i]);
  1174. }
  1175. FREE(tag_table);
  1176. }
  1177. pack_names()
  1178. {
  1179. register bucket *bp;
  1180. register char *p, *s, *t;
  1181. name_pool_size = 13; /* 13 == sizeof("$end") + sizeof("$accept") */
  1182. for (bp = first_symbol; bp; bp = bp->next)
  1183. name_pool_size += strlen(bp->name) + 1;
  1184. name_pool = MALLOC(name_pool_size);
  1185. if (name_pool == 0) no_space();
  1186. strcpy(name_pool, "$accept");
  1187. strcpy(name_pool+8, "$end");
  1188. t = name_pool + 13;
  1189. for (bp = first_symbol; bp; bp = bp->next)
  1190. {
  1191. p = t;
  1192. s = bp->name;
  1193. while (*t++ = *s++) continue;
  1194. FREE(bp->name);
  1195. bp->name = p;
  1196. }
  1197. }
  1198. check_symbols()
  1199. {
  1200. register bucket *bp;
  1201. if (goal->class == UNKNOWN)
  1202. undefined_goal(goal->name);
  1203. for (bp = first_symbol; bp; bp = bp->next)
  1204. {
  1205. if (bp->class == UNKNOWN)
  1206. {
  1207. undefined_symbol_warning(bp->name);
  1208. bp->class = TERM;
  1209. }
  1210. }
  1211. }
  1212. pack_symbols()
  1213. {
  1214. register bucket *bp;
  1215. register bucket **v;
  1216. register int i, j, k, n;
  1217. nsyms = 2;
  1218. ntokens = 1;
  1219. for (bp = first_symbol; bp; bp = bp->next)
  1220. {
  1221. ++nsyms;
  1222. if (bp->class == TERM) ++ntokens;
  1223. }
  1224. start_symbol = ntokens;
  1225. nvars = nsyms - ntokens;
  1226. symbol_name = (char **) MALLOC(nsyms*sizeof(char *));
  1227. if (symbol_name == 0) no_space();
  1228. symbol_value = (short *) MALLOC(nsyms*sizeof(short));
  1229. if (symbol_value == 0) no_space();
  1230. symbol_prec = (short *) MALLOC(nsyms*sizeof(short));
  1231. if (symbol_prec == 0) no_space();
  1232. symbol_assoc = MALLOC(nsyms);
  1233. if (symbol_assoc == 0) no_space();
  1234. v = (bucket **) MALLOC(nsyms*sizeof(bucket *));
  1235. if (v == 0) no_space();
  1236. v[0] = 0;
  1237. v[start_symbol] = 0;
  1238. i = 1;
  1239. j = start_symbol + 1;
  1240. for (bp = first_symbol; bp; bp = bp->next)
  1241. {
  1242. if (bp->class == TERM)
  1243. v[i++] = bp;
  1244. else
  1245. v[j++] = bp;
  1246. }
  1247. assert(i == ntokens && j == nsyms);
  1248. for (i = 1; i < ntokens; ++i)
  1249. v[i]->index = i;
  1250. goal->index = start_symbol + 1;
  1251. k = start_symbol + 2;
  1252. while (++i < nsyms)
  1253. if (v[i] != goal)
  1254. {
  1255. v[i]->index = k;
  1256. ++k;
  1257. }
  1258. goal->value = 0;
  1259. k = 1;
  1260. for (i = start_symbol + 1; i < nsyms; ++i)
  1261. {
  1262. if (v[i] != goal)
  1263. {
  1264. v[i]->value = k;
  1265. ++k;
  1266. }
  1267. }
  1268. k = 0;
  1269. for (i = 1; i < ntokens; ++i)
  1270. {
  1271. n = v[i]->value;
  1272. if (n > 256)
  1273. {
  1274. for (j = k++; j > 0 && symbol_value[j-1] > n; --j)
  1275. symbol_value[j] = symbol_value[j-1];
  1276. symbol_value[j] = n;
  1277. }
  1278. }
  1279. if (v[1]->value == UNDEFINED)
  1280. v[1]->value = 256;
  1281. j = 0;
  1282. n = 257;
  1283. for (i = 2; i < ntokens; ++i)
  1284. {
  1285. if (v[i]->value == UNDEFINED)
  1286. {
  1287. while (j < k && n == symbol_value[j])
  1288. {
  1289. while (++j < k && n == symbol_value[j]) continue;
  1290. ++n;
  1291. }
  1292. v[i]->value = n;
  1293. ++n;
  1294. }
  1295. }
  1296. symbol_name[0] = name_pool + 8;
  1297. symbol_value[0] = 0;
  1298. symbol_prec[0] = 0;
  1299. symbol_assoc[0] = TOKEN;
  1300. for (i = 1; i < ntokens; ++i)
  1301. {
  1302. symbol_name[i] = v[i]->name;
  1303. symbol_value[i] = v[i]->value;
  1304. symbol_prec[i] = v[i]->prec;
  1305. symbol_assoc[i] = v[i]->assoc;
  1306. }
  1307. symbol_name[start_symbol] = name_pool;
  1308. symbol_value[start_symbol] = -1;
  1309. symbol_prec[start_symbol] = 0;
  1310. symbol_assoc[start_symbol] = TOKEN;
  1311. for (++i; i < nsyms; ++i)
  1312. {
  1313. k = v[i]->index;
  1314. symbol_name[k] = v[i]->name;
  1315. symbol_value[k] = v[i]->value;
  1316. symbol_prec[k] = v[i]->prec;
  1317. symbol_assoc[k] = v[i]->assoc;
  1318. }
  1319. FREE(v);
  1320. }
  1321. pack_grammar()
  1322. {
  1323. register int i, j;
  1324. int assoc, prec;
  1325. ritem = (short *) MALLOC(nitems*sizeof(short));
  1326. if (ritem == 0) no_space();
  1327. rlhs = (short *) MALLOC(nrules*sizeof(short));
  1328. if (rlhs == 0) no_space();
  1329. rrhs = (short *) MALLOC((nrules+1)*sizeof(short));
  1330. if (rrhs == 0) no_space();
  1331. rprec = (short *) REALLOC(rprec, nrules*sizeof(short));
  1332. if (rprec == 0) no_space();
  1333. rassoc = REALLOC(rassoc, nrules);
  1334. if (rassoc == 0) no_space();
  1335. ritem[0] = -1;
  1336. ritem[1] = goal->index;
  1337. ritem[2] = 0;
  1338. ritem[3] = -2;
  1339. rlhs[0] = 0;
  1340. rlhs[1] = 0;
  1341. rlhs[2] = start_symbol;
  1342. rrhs[0] = 0;
  1343. rrhs[1] = 0;
  1344. rrhs[2] = 1;
  1345. j = 4;
  1346. for (i = 3; i < nrules; ++i)
  1347. {
  1348. rlhs[i] = plhs[i]->index;
  1349. rrhs[i] = j;
  1350. assoc = TOKEN;
  1351. prec = 0;
  1352. while (pitem[j])
  1353. {
  1354. ritem[j] = pitem[j]->index;
  1355. if (pitem[j]->class == TERM)
  1356. {
  1357. prec = pitem[j]->prec;
  1358. assoc = pitem[j]->assoc;
  1359. }
  1360. ++j;
  1361. }
  1362. ritem[j] = -i;
  1363. ++j;
  1364. if (rprec[i] == UNDEFINED)
  1365. {
  1366. rprec[i] = prec;
  1367. rassoc[i] = assoc;
  1368. }
  1369. }
  1370. rrhs[i] = j;
  1371. FREE(plhs);
  1372. FREE(pitem);
  1373. }
  1374. print_grammar()
  1375. {
  1376. register int i, j, k;
  1377. int spacing;
  1378. register FILE *f = verbose_file;
  1379. if (!vflag) return;
  1380. k = 1;
  1381. for (i = 2; i < nrules; ++i)
  1382. {
  1383. if (rlhs[i] != rlhs[i-1])
  1384. {
  1385. if (i != 2) fprintf(f, "\n");
  1386. fprintf(f, "%4d %s :", i - 2, symbol_name[rlhs[i]]);
  1387. spacing = strlen(symbol_name[rlhs[i]]) + 1;
  1388. }
  1389. else
  1390. {
  1391. fprintf(f, "%4d ", i - 2);
  1392. j = spacing;
  1393. while (--j >= 0) putc(' ', f);
  1394. putc('|', f);
  1395. }
  1396. while (ritem[k] >= 0)
  1397. {
  1398. fprintf(f, " %s", symbol_name[ritem[k]]);
  1399. ++k;
  1400. }
  1401. ++k;
  1402. putc('\n', f);
  1403. }
  1404. }
  1405. reader()
  1406. {
  1407. create_symbol_table();
  1408. read_declarations();
  1409. read_grammar();
  1410. free_symbol_table();
  1411. free_tags();
  1412. pack_names();
  1413. check_symbols();
  1414. pack_symbols();
  1415. pack_grammar();
  1416. free_symbols();
  1417. print_grammar();
  1418. }