READER.C 37 KB

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