reader.c 37 KB

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