reader.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  1. /* Input parser for bison
  2. Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  3. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the BISON General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute BISON,
  9. but only under the conditions described in the BISON General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with BISON so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, you are welcome to use, share and improve this program.
  15. You are forbidden to forbid anyone else to use, share and improve
  16. what you give them. Help stamp out software-hoarding! */
  17. /* read in the grammar specification and record it in the format described in gram.h.
  18. All guards are copied into the fguard file and all actions into faction,
  19. in each case forming the body of a C function (yyguard or yyaction)
  20. which contains a switch statement to decide which guard or action to execute.
  21. The entry point is reader(). */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <ctype.h>
  25. #include <string.h>
  26. #include "files.h"
  27. #include "new.h"
  28. #include "symtab.h"
  29. #include "lex.h"
  30. #include "gram.h"
  31. #define LTYPESTR "\n#ifndef YYLTYPE\ntypedef\n struct yyltype\n\
  32. {\n int timestamp;\n int first_line;\n int first_column;\n\
  33. int last_line;\n int last_column;\n char *text;\n }\n\
  34. yyltype;\n\n#define YYLTYPE yyltype\n#endif\n\n"
  35. /* Number of slots allocated (but not necessarily used yet) in `rline' */
  36. int rline_allocated;
  37. extern int definesflag;
  38. extern bucket *symval;
  39. extern int numval;
  40. extern int failure;
  41. extern int expected_conflicts;
  42. extern void init_lex (void);
  43. extern void tabinit (void);
  44. extern bucket *getsym (char *key);
  45. extern void output_headers (void);
  46. extern void output_trailers (void);
  47. extern void free_symtab (void);
  48. extern int skip_white_space (void);
  49. extern int parse_percent_token (void);
  50. extern void open_extra_files (void);
  51. extern void fatal (char *s);
  52. extern int lex (void);
  53. extern void unlex (int token);
  54. extern void done (int k);
  55. typedef
  56. struct symbol_list
  57. {
  58. struct symbol_list *next;
  59. bucket *sym;
  60. bucket *ruleprec;
  61. }
  62. symbol_list;
  63. /* forward declarations */
  64. static void read_declarations (void);
  65. static void copy_definition (void);
  66. static void parse_token_decl (int what_is, int what_is_not);
  67. static void parse_start_decl (void);
  68. static void parse_type_decl (void);
  69. static void parse_assoc_decl (int assoc);
  70. static void parse_union_decl (void);
  71. static void parse_expect_decl (void);
  72. static void output_ltype (void);
  73. static void copy_action (symbol_list *rule, int stack_offset);
  74. static void readgram (void);
  75. static void record_rule_line (void);
  76. static void packsymbols (void);
  77. static void output_token_defines (FILE *file);
  78. static void packgram (void);
  79. static int read_signed_integer (FILE *stream);
  80. static int get_type (void);
  81. int lineno;
  82. symbol_list *grammar;
  83. int start_flag;
  84. bucket *startval;
  85. char **tags;
  86. static int typed; /* nonzero if %union has been seen. */
  87. static int lastprec; /* incremented for each %left, %right or %nonassoc seen */
  88. static int gensym_count; /* incremented for each generated symbol */
  89. static bucket *errtoken;
  90. /* Nonzero if any action or guard uses the @n construct. */
  91. static int yylsp_needed;
  92. void reader (void)
  93. {
  94. start_flag = 0;
  95. startval = NULL; /* start symbol not specified yet. */
  96. translations = 0; /* initially assume token number translation not needed. */
  97. nsyms = 1;
  98. nvars = 0;
  99. nrules = 0;
  100. nitems = 0;
  101. rline_allocated = 10;
  102. rline = NEW2(rline_allocated, short);
  103. typed = 0;
  104. lastprec = 0;
  105. gensym_count = 0;
  106. semantic_parser = 0;
  107. pure_parser = 0;
  108. yylsp_needed = 0;
  109. grammar = NULL;
  110. init_lex();
  111. lineno = 1;
  112. /* initialize the symbol table. */
  113. tabinit();
  114. /* construct the error token */
  115. errtoken = getsym("error");
  116. errtoken->class = STOKEN;
  117. /* construct a token that represents all undefined literal tokens. */
  118. /* it is always token number 2. */
  119. getsym("$illegal.")->class = STOKEN;
  120. /* Read the declaration section. Copy %{ ... %} groups to ftable and fdefines file.
  121. Also notice any %token, %left, etc. found there. */
  122. fprintf(ftable, "\n/* A Bison-1.00 parser, made from %s */\n\n", infile);
  123. read_declarations();
  124. /* output the definition of YYLTYPE into the fattrs and fdefines files. */
  125. output_ltype();
  126. /* start writing the guard and action files, if they are needed. */
  127. output_headers();
  128. /* read in the grammar, build grammar in list form. write out guards and actions. */
  129. readgram();
  130. /* write closing delimiters for actions and guards. */
  131. output_trailers();
  132. if (yylsp_needed)
  133. fprintf(ftable, "#define YYLSP_NEEDED\n\n");
  134. /* assign the symbols their symbol numbers.
  135. Write #defines for the token symbols into fdefines if requested. */
  136. packsymbols();
  137. /* convert the grammar into the format described in gram.h. */
  138. packgram();
  139. /* free the symbol table data structure
  140. since symbols are now all referred to by symbol number. */
  141. free_symtab();
  142. }
  143. /* read from finput until %% is seen. Discard the %%.
  144. Handle any % declarations,
  145. and copy the contents of any %{ ... %} groups to fattrs. */
  146. static void read_declarations (void)
  147. {
  148. register int c;
  149. register int tok;
  150. for (;;)
  151. {
  152. c = skip_white_space();
  153. if (c == '%')
  154. {
  155. tok = parse_percent_token();
  156. switch (tok)
  157. {
  158. case TWO_PERCENTS:
  159. return;
  160. case PERCENT_LEFT_CURLY:
  161. copy_definition();
  162. break;
  163. case TOKEN:
  164. parse_token_decl (STOKEN, SNTERM);
  165. break;
  166. case NTERM:
  167. parse_token_decl (SNTERM, STOKEN);
  168. break;
  169. case TYPE:
  170. parse_type_decl();
  171. break;
  172. case START:
  173. parse_start_decl();
  174. break;
  175. case UNION:
  176. parse_union_decl();
  177. break;
  178. case EXPECT:
  179. parse_expect_decl();
  180. break;
  181. case LEFT:
  182. parse_assoc_decl(LEFT_ASSOC);
  183. break;
  184. case RIGHT:
  185. parse_assoc_decl(RIGHT_ASSOC);
  186. break;
  187. case NONASSOC:
  188. parse_assoc_decl(NON_ASSOC);
  189. break;
  190. case SEMANTIC_PARSER:
  191. semantic_parser = 1;
  192. open_extra_files();
  193. break;
  194. case PURE_PARSER:
  195. pure_parser = 1;
  196. break;
  197. default:
  198. fatal("junk after % in definition section");
  199. }
  200. }
  201. else if (c == EOF)
  202. fatal("no input grammar");
  203. else { /* JF changed msg */
  204. /* fatals("Unrecognized char '%c' in declaration section",c); */
  205. fprintf(stderr,"Unrecognized char '%c' in declaration section",c);
  206. exit (1);
  207. }
  208. }
  209. }
  210. /* copy the contents of a %{ ... %} into the definitions file.
  211. The %{ has already been read. Return after reading the %}. */
  212. static void copy_definition (void)
  213. {
  214. register int c;
  215. register int match;
  216. register int ended;
  217. register int after_percent; /* -1 while reading a character if prev char was % */
  218. fprintf(fattrs, "#line %d \"%s\"\n", lineno, infile);
  219. after_percent = 0;
  220. c = getc(finput);
  221. for (;;)
  222. {
  223. switch (c)
  224. {
  225. case '\n':
  226. putc(c, fattrs);
  227. lineno++;
  228. break;
  229. case '%':
  230. after_percent = -1;
  231. break;
  232. case '\'':
  233. case '"':
  234. match = c;
  235. putc(c, fattrs);
  236. c = getc(finput);
  237. while (c != match)
  238. {
  239. if (c == EOF || c == '\n')
  240. fatal("unterminated string");
  241. putc(c, fattrs);
  242. if (c == '\\')
  243. {
  244. c = getc(finput);
  245. if (c == EOF || c == '\n')
  246. fatal("unterminated string");
  247. putc(c, fattrs);
  248. if (c == '\n')
  249. lineno++;
  250. }
  251. c = getc(finput);
  252. }
  253. putc(c, fattrs);
  254. break;
  255. case '/':
  256. putc(c, fattrs);
  257. c = getc(finput);
  258. if (c != '*')
  259. continue;
  260. putc(c, fattrs);
  261. c = getc(finput);
  262. ended = 0;
  263. while (!ended)
  264. {
  265. if (c == '*')
  266. {
  267. while (c == '*')
  268. {
  269. putc(c, fattrs);
  270. c = getc(finput);
  271. }
  272. if (c == '/')
  273. {
  274. putc(c, fattrs);
  275. ended = 1;
  276. }
  277. }
  278. else if (c == '\n')
  279. {
  280. lineno++;
  281. putc(c, fattrs);
  282. c = getc(finput);
  283. }
  284. else if (c == EOF)
  285. fatal("unterminated comment in %{ definition");
  286. else
  287. {
  288. putc(c, fattrs);
  289. c = getc(finput);
  290. }
  291. }
  292. break;
  293. case EOF:
  294. fatal("unterminated %{ definition");
  295. default:
  296. putc(c, fattrs);
  297. }
  298. c = getc(finput);
  299. if (after_percent)
  300. {
  301. if (c == '}')
  302. return;
  303. putc('%', fattrs);
  304. }
  305. after_percent = 0;
  306. }
  307. }
  308. /* parse what comes after %token or %nterm.
  309. For %token, what_is is STOKEN and what_is_not is SNTERM.
  310. For %nterm, the arguments are reversed. */
  311. static void parse_token_decl (int what_is, int what_is_not)
  312. {
  313. /* register int start_lineno; JF */
  314. register int token = 0;
  315. register int prev;
  316. register char *typename = 0;
  317. int k;
  318. extern char token_buffer[];
  319. /* start_lineno = lineno; JF */
  320. for (;;)
  321. {
  322. if(ungetc(skip_white_space(), finput) == '%')
  323. return;
  324. /* if (lineno != start_lineno)
  325. return; JF */
  326. /* we have not passed a newline, so the token now starting is in this declaration */
  327. prev = token;
  328. if ((token = lex()) == TYPENAME)
  329. {
  330. k = strlen(token_buffer);
  331. if (typename) free (typename);
  332. typename = NEW2(k + 1, char);
  333. strcpy(typename, token_buffer);
  334. }
  335. else if (token == IDENTIFIER)
  336. {
  337. if (symval->class == what_is_not) {
  338. /* fatals("symbol %s redefined", symval->tag); */
  339. fprintf(stderr,"symbol %s redefined", symval->tag);
  340. exit (1);
  341. }
  342. symval->class = what_is;
  343. if (what_is == SNTERM)
  344. symval->value = nvars++;
  345. if (typename)
  346. {
  347. if (symval->type_name == NULL)
  348. symval->type_name = typename;
  349. else {
  350. /* fatals("type redeclaration for %s", symval->tag); */
  351. fprintf(stderr,"type redeclaration for %s", symval->tag);
  352. exit (1);
  353. }
  354. }
  355. }
  356. else if (prev == IDENTIFIER && token == NUMBER)
  357. {
  358. symval->user_token_number = numval;
  359. translations = 1;
  360. }
  361. else {
  362. fatal("invalid text in %token or %nterm declaration");
  363. }
  364. }
  365. }
  366. /* parse what comes after %start */
  367. static void parse_start_decl (void)
  368. {
  369. if (start_flag)
  370. fatal("multiple %start declarations");
  371. start_flag = 1;
  372. if (lex() != IDENTIFIER)
  373. fatal("invalid %start declaration");
  374. startval = symval;
  375. }
  376. /* read in a %type declaration and record its information for get_type_name to access */
  377. static void parse_type_decl (void)
  378. {
  379. register int k;
  380. register char *name;
  381. /* register int start_lineno; JF */
  382. extern char token_buffer[];
  383. if (lex() != TYPENAME)
  384. fatal("ill-formed %type declaration");
  385. k = strlen(token_buffer);
  386. name = NEW2(k + 1, char);
  387. strcpy(name, token_buffer);
  388. /* start_lineno = lineno; */
  389. for (;;)
  390. {
  391. register int t;
  392. if(ungetc(skip_white_space(), finput) == '%')
  393. return;
  394. /* if (lineno != start_lineno)
  395. return; JF */
  396. /* we have not passed a newline, so the token now starting is in this declaration */
  397. t = lex();
  398. switch (t)
  399. {
  400. case COMMA:
  401. break;
  402. case IDENTIFIER:
  403. if (symval->type_name == NULL)
  404. symval->type_name = name;
  405. else {
  406. /* fatals("type redeclaration for %s", symval->tag); */
  407. fprintf(stderr,"type redeclaration for %s", symval->tag);
  408. exit (1);
  409. }
  410. break;
  411. default:
  412. fatal("invalid %type declaration");
  413. }
  414. }
  415. }
  416. /* read in a %left, %right or %nonassoc declaration and record its information. */
  417. /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
  418. static void parse_assoc_decl (int assoc)
  419. {
  420. register int k;
  421. register char *name = NULL;
  422. /* register int start_lineno; JF */
  423. register int prev = 0; /* JF added = 0 to keep lint happy */
  424. extern char token_buffer[];
  425. lastprec++; /* assign a new precedence level. */
  426. /* start_lineno = lineno; */
  427. for (;;)
  428. {
  429. register int t;
  430. if(ungetc(skip_white_space(), finput) == '%')
  431. return;
  432. /* if (lineno != start_lineno)
  433. return; JF */
  434. /* we have not passed a newline, so the token now starting is in this declaration */
  435. t = lex();
  436. switch (t)
  437. {
  438. case TYPENAME:
  439. k = strlen(token_buffer);
  440. name = NEW2(k + 1, char);
  441. strcpy(name, token_buffer);
  442. break;
  443. case COMMA:
  444. break;
  445. case IDENTIFIER:
  446. symval->prec = lastprec;
  447. symval->assoc = assoc;
  448. if (symval->class == SNTERM) {
  449. /* fatals("symbol %s redefined", symval->tag); */
  450. fprintf(stderr,"symbol %s redefined", symval->tag);
  451. exit (1);
  452. }
  453. symval->class = STOKEN;
  454. if (name)
  455. { /* record the type, if one is specified */
  456. if (symval->type_name == NULL)
  457. symval->type_name = name;
  458. else {
  459. /* fatals("type redeclaration for %s", symval->tag); */
  460. fprintf(stderr,"type redeclaration for %s", symval->tag);
  461. exit (1);
  462. }
  463. }
  464. break;
  465. case NUMBER:
  466. if (prev == IDENTIFIER)
  467. {
  468. symval->user_token_number = numval;
  469. translations = 1;
  470. }
  471. else
  472. fatal("invalid text in association declaration");
  473. break;
  474. case SEMICOLON:
  475. return;
  476. default:
  477. fatal("malformatted association declaration");
  478. }
  479. prev = t;
  480. }
  481. }
  482. /* copy the union declaration into fattrs (and fdefines),
  483. where it is made into the
  484. definition of YYSTYPE, the type of elements of the parser value stack. */
  485. static void parse_union_decl (void)
  486. {
  487. register int c;
  488. register int count;
  489. register int in_comment;
  490. if (typed)
  491. fatal("multiple %union declarations");
  492. typed = 1;
  493. fprintf(fattrs, "\n#line %d \"%s\"\n", lineno, infile);
  494. fprintf(fattrs, "typedef union");
  495. if (fdefines)
  496. fprintf(fdefines, "typedef union");
  497. count = 0;
  498. in_comment = 0;
  499. c = getc(finput);
  500. while (c != EOF)
  501. {
  502. putc(c, fattrs);
  503. if (fdefines)
  504. putc(c, fdefines);
  505. switch (c)
  506. {
  507. case '\n':
  508. lineno++;
  509. break;
  510. case '/':
  511. c = getc(finput);
  512. if (c != '*')
  513. ungetc(c, finput);
  514. else
  515. {
  516. putc('*', fattrs);
  517. if (fdefines)
  518. putc('*', fdefines);
  519. c = getc(finput);
  520. in_comment = 1;
  521. while (in_comment)
  522. {
  523. if (c == EOF)
  524. fatal("unterminated comment");
  525. putc(c, fattrs);
  526. if (fdefines)
  527. putc(c, fdefines);
  528. if (c == '*')
  529. {
  530. c = getc(finput);
  531. if (c == '/')
  532. {
  533. putc('/', fattrs);
  534. if (fdefines)
  535. putc('/', fdefines);
  536. in_comment = 0;
  537. }
  538. }
  539. else
  540. c = getc(finput);
  541. }
  542. }
  543. break;
  544. case '{':
  545. count++;
  546. break;
  547. case '}':
  548. count--;
  549. if (count == 0)
  550. {
  551. fprintf(fattrs, " YYSTYPE;\n");
  552. if (fdefines)
  553. fprintf(fdefines, " YYSTYPE;\n");
  554. /* JF don't choke on trailing semi */
  555. c=skip_white_space();
  556. if(c!=';') ungetc(c,finput);
  557. return;
  558. }
  559. }
  560. c = getc(finput);
  561. }
  562. }
  563. /* parse the declaration %expect N which says to expect N
  564. shift-reduce conflicts. */
  565. static void parse_expect_decl (void)
  566. {
  567. register int c;
  568. register int count;
  569. char buffer[20];
  570. c = getc(finput);
  571. while (c == ' ' || c == '\t')
  572. c = getc(finput);
  573. count = 0;
  574. while (c >= '0' && c <= '9')
  575. {
  576. if (count < 20)
  577. buffer[count++] = c;
  578. c = getc(finput);
  579. }
  580. ungetc (c, finput);
  581. expected_conflicts = atoi (buffer);
  582. }
  583. /* that's all of parsing the declaration section */
  584. static void output_ltype (void)
  585. {
  586. fprintf(fattrs, LTYPESTR);/* JF added YYABORT() */
  587. if (fdefines)
  588. fprintf(fdefines, LTYPESTR);/* JF added YYABORT() */
  589. fprintf(fattrs, "#define\tYYACCEPT\treturn(0)\n");
  590. fprintf(fattrs, "#define\tYYABORT\treturn(1)\n");
  591. fprintf(fattrs, "#define\tYYERROR\treturn(1)\n");
  592. if (fdefines)
  593. {
  594. fprintf(fdefines, "#define\tYYACCEPT\treturn(0)\n");
  595. fprintf(fdefines, "#define\tYYABORT\treturn(1)\n");
  596. fprintf(fdefines, "#define\tYYERROR\treturn(1)\n");
  597. }
  598. }
  599. /* Get the data type (alternative in the union) of the value for symbol n in rule rule. */
  600. char *get_type_name (int n, symbol_list *rule)
  601. {
  602. static char *msg = "invalid $ value";
  603. register int i;
  604. register symbol_list *rp;
  605. if (n < 0)
  606. fatal(msg);
  607. rp = rule;
  608. i = 0;
  609. while (i < n)
  610. {
  611. rp = rp->next;
  612. if (rp == NULL || rp->sym == NULL)
  613. fatal(msg);
  614. i++;
  615. }
  616. return (rp->sym->type_name);
  617. }
  618. /* after %guard is seen in the input file,
  619. copy the actual guard into the guards file.
  620. If the guard is followed by an action, copy that into the actions file.
  621. stack_offset is the number of values in the current rule so far,
  622. which says where to find $0 with respect to the top of the stack,
  623. for the simple parser in which the stack is not popped until after the guard is run. */
  624. void copy_guard (symbol_list *rule, int stack_offset)
  625. {
  626. register int c;
  627. register int n;
  628. register int count;
  629. register int match;
  630. register int ended;
  631. register char *type_name;
  632. extern char token_buffer[];
  633. /* offset is always 0 if parser has already popped the stack pointer */
  634. if (semantic_parser) stack_offset = 0;
  635. fprintf(fguard, "\ncase %d:\n", nrules);
  636. fprintf(fguard, "#line %d \"%s\"\n", lineno, infile);
  637. putc('{', fguard);
  638. count = 0;
  639. c = getc(finput);
  640. while (count > 0 || c != ';' && c != '{')
  641. {
  642. switch (c)
  643. {
  644. case '\n':
  645. putc(c, fguard);
  646. lineno++;
  647. break;
  648. case '{':
  649. putc(c, fguard);
  650. count++;
  651. break;
  652. case '}':
  653. putc(c, fguard);
  654. if (count > 0)
  655. count--;
  656. else
  657. fatal("unmatched right brace ('}')");
  658. case '\'':
  659. case '"':
  660. match = c;
  661. putc(c, fguard);
  662. c = getc(finput);
  663. while (c != match)
  664. {
  665. if (c == EOF || c == '\n')
  666. fatal("unterminated string");
  667. putc(c, fguard);
  668. if (c == '\\')
  669. {
  670. c = getc(finput);
  671. if (c == EOF || c == '\n')
  672. fatal("unterminated string");
  673. putc(c, fguard);
  674. if (c == '\n')
  675. lineno++;
  676. }
  677. c = getc(finput);
  678. }
  679. putc(c, fguard);
  680. break;
  681. case '/':
  682. putc(c, fguard);
  683. c = getc(finput);
  684. if (c != '*')
  685. continue;
  686. putc(c, fguard);
  687. c = getc(finput);
  688. ended = 0;
  689. while (!ended)
  690. {
  691. if (c == '*')
  692. {
  693. while (c == '*')
  694. {
  695. putc(c, fguard);
  696. c = getc(finput);
  697. }
  698. if (c == '/')
  699. {
  700. putc(c, fguard);
  701. ended = 1;
  702. }
  703. }
  704. else if (c == '\n')
  705. {
  706. lineno++;
  707. putc(c, fguard);
  708. c = getc(finput);
  709. }
  710. else if (c == EOF)
  711. fatal("unterminated comment");
  712. else
  713. {
  714. putc(c, fguard);
  715. c = getc(finput);
  716. }
  717. }
  718. break;
  719. case '$':
  720. c = getc(finput);
  721. type_name = NULL;
  722. if (c == '<')
  723. {
  724. register char *cp = token_buffer;
  725. while ((c = getc(finput)) != '>' && c > 0)
  726. *cp++ = c;
  727. *cp = 0;
  728. type_name = token_buffer;
  729. c = getc(finput);
  730. }
  731. if (c == '$')
  732. {
  733. fprintf(fguard, "yyval");
  734. if (!type_name) type_name = rule->sym->type_name;
  735. if (type_name)
  736. fprintf(fguard, ".%s", type_name);
  737. if(!type_name && typed) /* JF */
  738. fprintf(stderr,"%s:%d: warning: $$ of '%s' has no declared type.\n",infile,lineno,rule->sym->tag);
  739. }
  740. else if (isdigit(c) || c == '-')
  741. {
  742. ungetc (c, finput);
  743. n = read_signed_integer(finput);
  744. c = getc(finput);
  745. if (!type_name && n > 0)
  746. type_name = get_type_name(n, rule);
  747. fprintf(fguard, "yyvsp[%d]", n - stack_offset);
  748. if (type_name)
  749. fprintf(fguard, ".%s", type_name);
  750. if(!type_name && typed) /* JF */
  751. fprintf(stderr,"%s:%d: warning: $%d of '%s' has no declared type.\n",infile,lineno,n,rule->sym->tag);
  752. continue;
  753. }
  754. else {
  755. /* fatals("$%c is invalid",c); */ /* JF changed style */
  756. fprintf(stderr,"$%c is invalid",c);
  757. exit (1);
  758. }
  759. break;
  760. case '@':
  761. c = getc(finput);
  762. if (isdigit(c) || c == '-')
  763. {
  764. ungetc (c, finput);
  765. n = read_signed_integer(finput);
  766. c = getc(finput);
  767. }
  768. else {
  769. /* fatals("@%c is invalid",c); */ /* JF changed style */
  770. fprintf(stderr,"@%c is invalid",c);
  771. exit (1);
  772. }
  773. fprintf(fguard, "yylsp[%d]", n - stack_offset);
  774. yylsp_needed = 1;
  775. continue;
  776. case EOF:
  777. fatal("unterminated %guard clause");
  778. default:
  779. putc(c, fguard);
  780. }
  781. c = getc(finput);
  782. }
  783. fprintf(fguard, ";\n break;}");
  784. if (c == '{')
  785. copy_action(rule, stack_offset);
  786. else if (c == '=')
  787. {
  788. c = getc(finput);
  789. if (c == '{')
  790. copy_action(rule, stack_offset);
  791. }
  792. }
  793. /* Assuming that a { has just been seen, copy everything up to the matching }
  794. into the actions file.
  795. stack_offset is the number of values in the current rule so far,
  796. which says where to find $0 with respect to the top of the stack. */
  797. static void copy_action (symbol_list *rule, int stack_offset)
  798. {
  799. register int c;
  800. register int n;
  801. register int count;
  802. register int match;
  803. register int ended;
  804. register char *type_name;
  805. extern char token_buffer[];
  806. /* offset is always 0 if parser has already popped the stack pointer */
  807. if (semantic_parser) stack_offset = 0;
  808. fprintf(faction, "\ncase %d:\n", nrules);
  809. fprintf(faction, "#line %d \"%s\"\n", lineno, infile);
  810. putc('{', faction);
  811. count = 1;
  812. c = getc(finput);
  813. while (count > 0)
  814. {
  815. while (c != '}')
  816. {
  817. switch (c)
  818. {
  819. case '\n':
  820. putc(c, faction);
  821. lineno++;
  822. break;
  823. case '{':
  824. putc(c, faction);
  825. count++;
  826. break;
  827. case '\'':
  828. case '"':
  829. match = c;
  830. putc(c, faction);
  831. c = getc(finput);
  832. while (c != match)
  833. {
  834. if (c == EOF || c == '\n')
  835. fatal("unterminated string");
  836. putc(c, faction);
  837. if (c == '\\')
  838. {
  839. c = getc(finput);
  840. if (c == EOF)
  841. fatal("unterminated string");
  842. putc(c, faction);
  843. if (c == '\n')
  844. lineno++;
  845. }
  846. c = getc(finput);
  847. }
  848. putc(c, faction);
  849. break;
  850. case '/':
  851. putc(c, faction);
  852. c = getc(finput);
  853. if (c != '*')
  854. continue;
  855. putc(c, faction);
  856. c = getc(finput);
  857. ended = 0;
  858. while (!ended)
  859. {
  860. if (c == '*')
  861. {
  862. while (c == '*')
  863. {
  864. putc(c, faction);
  865. c = getc(finput);
  866. }
  867. if (c == '/')
  868. {
  869. putc(c, faction);
  870. ended = 1;
  871. }
  872. }
  873. else if (c == '\n')
  874. {
  875. lineno++;
  876. putc(c, faction);
  877. c = getc(finput);
  878. }
  879. else if (c == EOF)
  880. fatal("unterminated comment");
  881. else
  882. {
  883. putc(c, faction);
  884. c = getc(finput);
  885. }
  886. }
  887. break;
  888. case '$':
  889. c = getc(finput);
  890. type_name = NULL;
  891. if (c == '<')
  892. {
  893. register char *cp = token_buffer;
  894. while ((c = getc(finput)) != '>' && c > 0)
  895. *cp++ = c;
  896. *cp = 0;
  897. type_name = token_buffer;
  898. c = getc(finput);
  899. }
  900. if (c == '$')
  901. {
  902. fprintf(faction, "yyval");
  903. if (!type_name) type_name = get_type_name(0, rule);
  904. if (type_name)
  905. fprintf(faction, ".%s", type_name);
  906. if(!type_name && typed) /* JF */
  907. fprintf(stderr,"%s:%d: warning: $$ of '%s' has no declared type.\n",infile,lineno,rule->sym->tag);
  908. }
  909. else if (isdigit(c) || c == '-')
  910. {
  911. ungetc (c, finput);
  912. n = read_signed_integer(finput);
  913. c = getc(finput);
  914. if (!type_name && n > 0)
  915. type_name = get_type_name(n, rule);
  916. fprintf(faction, "yyvsp[%d]", n - stack_offset);
  917. if (type_name)
  918. fprintf(faction, ".%s", type_name);
  919. if(!type_name && typed) /* JF */
  920. fprintf(stderr,"%s:%d: warning: $%d of '%s' has no declared type.\n",infile,lineno,n,rule->sym->tag);
  921. continue;
  922. }
  923. else {
  924. /* fatals("$%c is invalid",c); */ /* JF changed format */
  925. fprintf(stderr,"$%c is invalid",c);
  926. exit (1);
  927. }
  928. break;
  929. case '@':
  930. c = getc(finput);
  931. if (isdigit(c) || c == '-')
  932. {
  933. ungetc (c, finput);
  934. n = read_signed_integer(finput);
  935. c = getc(finput);
  936. }
  937. else
  938. fatal("invalid @-construct");
  939. fprintf(faction, "yylsp[%d]", n - stack_offset);
  940. yylsp_needed = 1;
  941. continue;
  942. case EOF:
  943. fatal("unmatched '{'");
  944. default:
  945. putc(c, faction);
  946. }
  947. c = getc(finput);
  948. }
  949. /* above loop exits when c is '}' */
  950. if (--count)
  951. {
  952. putc(c, faction);
  953. c = getc(finput);
  954. }
  955. }
  956. fprintf(faction, ";\n break;}");
  957. }
  958. /* generate a dummy symbol, a nonterminal,
  959. whose name cannot conflict with the user's names. */
  960. bucket *gensym (void)
  961. {
  962. register bucket *sym;
  963. extern char token_buffer[];
  964. sprintf (token_buffer, "@%d", ++gensym_count);
  965. sym = getsym(token_buffer);
  966. sym->class = SNTERM;
  967. sym->value = nvars++;
  968. return (sym);
  969. }
  970. /* Parse the input grammar into a one symbol_list structure.
  971. Each rule is represented by a sequence of symbols: the left hand side
  972. followed by the contents of the right hand side, followed by a null pointer
  973. instead of a symbol to terminate the rule.
  974. The next symbol is the lhs of the following rule.
  975. All guards and actions are copied out to the appropriate files,
  976. labelled by the rule number they apply to. */
  977. static void readgram (void)
  978. {
  979. register int t;
  980. register bucket *lhs;
  981. register symbol_list *p;
  982. register symbol_list *p1;
  983. register bucket *bp;
  984. symbol_list *crule; /* points to first symbol_list of current rule. */
  985. /* its symbol is the lhs of the rule. */
  986. symbol_list *crule1; /* points to the symbol_list preceding crule. */
  987. p1 = NULL;
  988. t = lex();
  989. while (t != TWO_PERCENTS && t != ENDFILE)
  990. {
  991. if (t == IDENTIFIER || t == BAR)
  992. {
  993. register int actionflag = 0;
  994. int rulelength = 0; /* number of symbols in rhs of this rule so far */
  995. int xactions = 0; /* JF for error checking */
  996. bucket *first_rhs = 0;
  997. if (t == IDENTIFIER)
  998. {
  999. lhs = symval;
  1000. t = lex();
  1001. if (t != COLON)
  1002. fatal("ill-formed rule");
  1003. }
  1004. if (nrules == 0)
  1005. {
  1006. if (t == BAR)
  1007. fatal("grammar starts with vertical bar");
  1008. if (!start_flag)
  1009. startval = lhs;
  1010. }
  1011. /* start a new rule and record its lhs. */
  1012. nrules++;
  1013. nitems++;
  1014. record_rule_line ();
  1015. p = NEW(symbol_list);
  1016. p->sym = lhs;
  1017. crule1 = p1;
  1018. if (p1)
  1019. p1->next = p;
  1020. else
  1021. grammar = p;
  1022. p1 = p;
  1023. crule = p;
  1024. /* mark the rule's lhs as a nonterminal if not already so. */
  1025. if (lhs->class == SUNKNOWN)
  1026. {
  1027. lhs->class = SNTERM;
  1028. lhs->value = nvars;
  1029. nvars++;
  1030. }
  1031. else if (lhs->class == STOKEN) {
  1032. /* fatals("rule given for %s, which is a token", lhs->tag); */
  1033. fprintf(stderr,"rule given for %s, which is a token", lhs->tag);
  1034. exit (1);
  1035. }
  1036. /* read the rhs of the rule. */
  1037. for (;;)
  1038. {
  1039. t = lex();
  1040. if (! (t == IDENTIFIER || t == LEFT_CURLY)) break;
  1041. /* if next token is an identifier, see if a colon follows it.
  1042. If one does, exit this rule now. */
  1043. if (t == IDENTIFIER)
  1044. {
  1045. register bucket *ssave;
  1046. register int t1;
  1047. ssave = symval;
  1048. t1 = lex();
  1049. unlex(t1);
  1050. symval = ssave;
  1051. if (t1 == COLON) break;
  1052. if(!first_rhs) /* JF */
  1053. first_rhs = symval;
  1054. /* not followed by colon => process as part of this rule's rhs. */
  1055. if (actionflag)
  1056. {
  1057. register bucket *sdummy;
  1058. /* if this symbol was preceded by an action, */
  1059. /* make a dummy nonterminal to replace that action in this rule */
  1060. /* and make another rule to associate the action to the dummy. */
  1061. /* Since the action was written out with this rule's number, */
  1062. /* we must write give the new rule this number */
  1063. /* by inserting the new rule before it. */
  1064. /* make a dummy nonterminal, a gensym. */
  1065. sdummy = gensym();
  1066. /* make a new rule, whose body is empty, before the current one. */
  1067. /* so that the action just read can belong to it. */
  1068. nrules++;
  1069. nitems++;
  1070. record_rule_line ();
  1071. p = NEW(symbol_list);
  1072. if (crule1)
  1073. crule1->next = p;
  1074. else grammar = p;
  1075. p->sym = sdummy;
  1076. crule1 = NEW(symbol_list);
  1077. p->next = crule1;
  1078. crule1->next = crule;
  1079. /* insert the dummy generated by that rule into this rule. */
  1080. nitems++;
  1081. p = NEW(symbol_list);
  1082. p->sym = sdummy;
  1083. p1->next = p;
  1084. p1 = p;
  1085. actionflag = 0;
  1086. }
  1087. nitems++;
  1088. p = NEW(symbol_list);
  1089. p->sym = symval;
  1090. p1->next = p;
  1091. p1 = p;
  1092. }
  1093. else /* handle an action. */
  1094. {
  1095. copy_action(crule, rulelength);
  1096. actionflag = 1;
  1097. xactions++; /* JF */
  1098. }
  1099. rulelength++;
  1100. }
  1101. /* Put an empty link in the list to mark the end of this rule */
  1102. p = NEW(symbol_list);
  1103. p1->next = p;
  1104. p1 = p;
  1105. if (t == PREC)
  1106. {
  1107. t = lex();
  1108. crule->ruleprec = symval;
  1109. t = lex();
  1110. }
  1111. if (t == GUARD)
  1112. {
  1113. if (! semantic_parser)
  1114. fatal("%guard present but %semantic_parser not specified");
  1115. copy_guard(crule, rulelength);
  1116. t = lex();
  1117. }
  1118. else if (t == LEFT_CURLY)
  1119. {
  1120. if (actionflag) fatal("two actions at end of one rule");
  1121. copy_action(crule, rulelength);
  1122. t = lex();
  1123. }
  1124. /* JF if we'd end up using default, get a warning */
  1125. else if(!xactions && first_rhs && lhs->type_name!=first_rhs->type_name) {
  1126. if(lhs->type_name == 0 || first_rhs->type_name == 0 ||
  1127. strcmp(lhs->type_name,first_rhs->type_name))
  1128. fprintf(stderr,"%s:%d: warning: type clash ('%s' '%s') on default action\n",
  1129. infile,
  1130. lineno,
  1131. lhs->type_name ? lhs->type_name : "",
  1132. first_rhs->type_name ? first_rhs->type_name : "");
  1133. }
  1134. if (t == SEMICOLON)
  1135. t = lex();
  1136. }
  1137. /* these things can appear as alternatives to rules. */
  1138. else if (t == TOKEN)
  1139. {
  1140. parse_token_decl(STOKEN, SNTERM);
  1141. t = lex();
  1142. }
  1143. else if (t == NTERM)
  1144. {
  1145. parse_token_decl(SNTERM, STOKEN);
  1146. t = lex();
  1147. }
  1148. else if (t == TYPE)
  1149. {
  1150. t = get_type();
  1151. }
  1152. else if (t == UNION)
  1153. {
  1154. parse_union_decl();
  1155. t = lex();
  1156. }
  1157. else if (t == EXPECT)
  1158. {
  1159. parse_expect_decl();
  1160. t = lex();
  1161. }
  1162. else if (t == START)
  1163. {
  1164. parse_start_decl();
  1165. t = lex();
  1166. }
  1167. else
  1168. fatal("invalid input");
  1169. }
  1170. if (nrules == 0)
  1171. fatal("no input grammar");
  1172. if (typed == 0)/* JF put out same default YYSTYPE as YACC does */
  1173. {
  1174. fprintf(fattrs, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
  1175. if (fdefines)
  1176. fprintf(fdefines, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
  1177. }
  1178. /* Report any undefined symbols and consider them nonterminals. */
  1179. for (bp = firstsymbol; bp; bp = bp->next)
  1180. if (bp->class == SUNKNOWN)
  1181. {
  1182. fprintf(stderr, "symbol %s used, not defined as token, and no rules for it\n",
  1183. bp->tag);
  1184. failure = 1;
  1185. bp->class = SNTERM;
  1186. bp->value = nvars++;
  1187. }
  1188. ntokens = nsyms - nvars;
  1189. }
  1190. static void record_rule_line (void)
  1191. {
  1192. /* Record each rule's source line number in rline table. */
  1193. if (nrules >= rline_allocated)
  1194. {
  1195. rline_allocated = nrules * 2;
  1196. rline = (short *) realloc (rline,
  1197. rline_allocated * sizeof (short));
  1198. if (rline == 0)
  1199. {
  1200. fprintf (stderr, "bison: memory exhausted\n");
  1201. done (1);
  1202. }
  1203. }
  1204. rline[nrules] = lineno;
  1205. }
  1206. /* read in a %type declaration and record its information for get_type_name to access */
  1207. static int get_type (void)
  1208. {
  1209. register int k;
  1210. register int t;
  1211. register char *name;
  1212. extern char token_buffer[];
  1213. t = lex();
  1214. if (t != TYPENAME)
  1215. fatal("ill-formed %type declaration");
  1216. k = strlen(token_buffer);
  1217. name = NEW2(k + 1, char);
  1218. strcpy(name, token_buffer);
  1219. for (;;)
  1220. {
  1221. t = lex();
  1222. switch (t)
  1223. {
  1224. case SEMICOLON:
  1225. return (lex());
  1226. case COMMA:
  1227. break;
  1228. case IDENTIFIER:
  1229. if (symval->type_name == NULL)
  1230. symval->type_name = name;
  1231. else {
  1232. /* fatals("type redeclaration for %s", symval->tag); */
  1233. fprintf(stderr,"type redeclaration for %s", symval->tag);
  1234. exit (1);
  1235. }
  1236. break;
  1237. default:
  1238. return (t);
  1239. }
  1240. }
  1241. }
  1242. /* assign symbol numbers, and write definition of token names into fdefines.
  1243. Set up vectors tags and sprec of names and precedences of symbols. */
  1244. static void packsymbols (void)
  1245. {
  1246. register bucket *bp;
  1247. register int tokno = 1;
  1248. register int i;
  1249. register int last_user_token_number;
  1250. /* int lossage = 0; JF set but not used */
  1251. tags = NEW2(nsyms + 1, char *);
  1252. tags[0] = "$";
  1253. sprec = NEW2(nsyms, short);
  1254. sassoc = NEW2(nsyms, short);
  1255. max_user_token_number = 255;
  1256. last_user_token_number = 255;
  1257. for (bp = firstsymbol; bp; bp = bp->next)
  1258. {
  1259. if (bp->class == SNTERM)
  1260. {
  1261. bp->value += ntokens;
  1262. }
  1263. else
  1264. {
  1265. if (translations && !(bp->user_token_number))
  1266. bp->user_token_number = ++last_user_token_number;
  1267. if (bp->user_token_number > max_user_token_number)
  1268. max_user_token_number = bp->user_token_number;
  1269. bp->value = tokno++;
  1270. }
  1271. tags[bp->value] = bp->tag;
  1272. sprec[bp->value] = bp->prec;
  1273. sassoc[bp->value] = bp->assoc;
  1274. }
  1275. if (translations)
  1276. {
  1277. register int i;
  1278. token_translations = NEW2(max_user_token_number+1, short);
  1279. /* initialize all entries for literal tokens to 2,
  1280. the internal token number for $illegal., which represents all invalid inputs. */
  1281. for (i = 0; i <= max_user_token_number; i++)
  1282. token_translations[i] = 2;
  1283. }
  1284. for (bp = firstsymbol; bp; bp = bp->next)
  1285. {
  1286. if (bp->value >= ntokens) continue;
  1287. if (translations)
  1288. {
  1289. if (token_translations[bp->user_token_number] != 2)
  1290. {
  1291. /* JF made this a call to fatals() */
  1292. /*
  1293. fatals( "tokens %s and %s both assigned number %d",
  1294. tags[token_translations[bp->user_token_number]],
  1295. bp->tag,
  1296. bp->user_token_number);
  1297. */
  1298. fprintf(stderr,"tokens %s and %s both assigned number %d",
  1299. tags[token_translations[bp->user_token_number]],
  1300. bp->tag,
  1301. bp->user_token_number);
  1302. exit (1);
  1303. }
  1304. token_translations[bp->user_token_number] = bp->value;
  1305. }
  1306. }
  1307. error_token_number = errtoken->value;
  1308. output_token_defines(ftable);
  1309. if (startval->class == SUNKNOWN) {
  1310. /* fatals("the start symbol %s is undefined", startval->tag); */
  1311. fprintf(stderr,"the start symbol %s is undefined", startval->tag);
  1312. exit (1);
  1313. }
  1314. else if (startval->class == STOKEN) {
  1315. /* fatals("the start symbol %s is a token", startval->tag); */
  1316. fprintf(stderr,"the start symbol %s is a token", startval->tag);
  1317. exit (1);
  1318. }
  1319. start_symbol = startval->value;
  1320. if (definesflag)
  1321. {
  1322. output_token_defines(fdefines);
  1323. if (semantic_parser)
  1324. for (i = ntokens; i < nsyms; i++)
  1325. {
  1326. /* don't make these for dummy nonterminals made by gensym. */
  1327. if (*tags[i] != '@')
  1328. fprintf(fdefines, "#define\tNT%s\t%d\n", tags[i], i);
  1329. }
  1330. fclose(fdefines);
  1331. fdefines = NULL;
  1332. }
  1333. }
  1334. static void output_token_defines (FILE *file)
  1335. {
  1336. bucket *bp;
  1337. for (bp = firstsymbol; bp; bp = bp->next)
  1338. {
  1339. if (bp->value >= ntokens) continue;
  1340. /* For named tokens, but not literal ones, define the name. */
  1341. /* The value is the user token number. */
  1342. if ('\'' != *tags[bp->value] && bp != errtoken)
  1343. {
  1344. register char *cp = tags[bp->value];
  1345. register char c;
  1346. /* Don't #define nonliteral tokens whose names contain periods. */
  1347. while ((c = *cp++) && c != '.');
  1348. if (!c)
  1349. {
  1350. fprintf(file, "#define\t%s\t%d\n", tags[bp->value],
  1351. (translations ? bp->user_token_number : bp->value));
  1352. if (semantic_parser)
  1353. fprintf(file, "#define\tT%s\t%d\n", tags[bp->value],
  1354. bp->value);
  1355. }
  1356. }
  1357. }
  1358. putc('\n', file);
  1359. }
  1360. /* convert the rules into the representation using rrhs, rlhs and ritems. */
  1361. static void packgram (void)
  1362. {
  1363. register int itemno;
  1364. register int ruleno;
  1365. register symbol_list *p;
  1366. /* register bucket *bp; JF unused */
  1367. bucket *ruleprec;
  1368. ritem = NEW2(nitems + 1, short);
  1369. rlhs = NEW2(nrules, short) - 1;
  1370. rrhs = NEW2(nrules, short) - 1;
  1371. rprec = NEW2(nrules, short) - 1;
  1372. rassoc = NEW2(nrules, short) - 1;
  1373. itemno = 0;
  1374. ruleno = 1;
  1375. p = grammar;
  1376. while (p)
  1377. {
  1378. rlhs[ruleno] = p->sym->value;
  1379. rrhs[ruleno] = itemno;
  1380. ruleprec = p->ruleprec;
  1381. p = p->next;
  1382. while (p && p->sym)
  1383. {
  1384. ritem[itemno++] = p->sym->value;
  1385. /* a rule gets the precedence and associativity of the last token in it. */
  1386. if (p->sym->class == STOKEN)
  1387. {
  1388. rprec[ruleno] = p->sym->prec;
  1389. rassoc[ruleno] = p->sym->assoc;
  1390. }
  1391. if (p) p = p->next;
  1392. }
  1393. /* if this rule has a %prec, specified symbol's precedence replaces the default */
  1394. if (ruleprec)
  1395. {
  1396. rprec[ruleno] = ruleprec->prec;
  1397. rassoc[ruleno] = ruleprec->assoc;
  1398. }
  1399. ritem[itemno++] = -ruleno;
  1400. ruleno++;
  1401. if (p) p = p->next;
  1402. }
  1403. ritem[itemno] = 0;
  1404. }
  1405. /* Read a signed integer from STREAM and return its value. */
  1406. static int read_signed_integer (FILE *stream)
  1407. {
  1408. register int c = getc(stream);
  1409. register int sign = 1;
  1410. register int n;
  1411. if (c == '-')
  1412. {
  1413. c = getc(stream);
  1414. sign = -1;
  1415. }
  1416. n = 0;
  1417. while (isdigit(c))
  1418. {
  1419. n = 10*n + (c - '0');
  1420. c = getc(stream);
  1421. }
  1422. ungetc(c, stream);
  1423. return n * sign;
  1424. }