reader.c 37 KB

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