reader.c 29 KB

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