reader.c 42 KB

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