reader.c 34 KB

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