reader.c 33 KB

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