reader.c 30 KB

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