reader.c 38 KB

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