reader.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895
  1. #include "defs.h"
  2. #include "mstring.h"
  3. /* The line size must be a positive integer. One hundred was chosen */
  4. /* because few lines in Yacc input grammars exceed 100 characters. */
  5. /* Note that if a line exceeds LINESIZE characters, the line buffer */
  6. /* will be expanded to accomodate it. */
  7. #define LINESIZE 100
  8. /* the maximum nember of arguments (inherited attributes) to a non-terminal */
  9. /* this is a hard limit, but seems more than adequate */
  10. #define MAXARGS 20
  11. char *cache;
  12. int cinc, cache_size;
  13. int ntags, tagmax, havetags=0;
  14. char **tag_table;
  15. char saw_eof, unionized;
  16. char *cptr, *line;
  17. int linesize;
  18. FILE *inc_file = NULL;
  19. char inc_file_name[LINESIZE];
  20. int inc_save_lineno;
  21. int in_ifdef = 0;
  22. int ifdef_skip;
  23. #define MAX_DEFD_VARS 1000
  24. char *defd_vars[MAX_DEFD_VARS] = {NULL};
  25. bucket *goal;
  26. int prec;
  27. int gensym;
  28. char last_was_action;
  29. int maxitems;
  30. bucket **pitem;
  31. int maxrules;
  32. bucket **plhs;
  33. int name_pool_size;
  34. char *name_pool;
  35. char line_format[] = "#line %d \"%s\"\n";
  36. int cachec(int c)
  37. {
  38. assert(cinc >= 0);
  39. if (cinc >= cache_size) {
  40. if (!(cache = REALLOC(cache, cache_size += 256))) no_space(); }
  41. return cache[cinc++] = c;
  42. }
  43. /*
  44. * Get line
  45. * Return: 1 - reg. line finished with '\n'.
  46. * 0 - EOF.
  47. */
  48. char *get_line() {
  49. extern int Eflag;
  50. FILE *f;
  51. int c;
  52. int i;
  53. /* VM: input from main or include file */
  54. NextLine:;
  55. f = inc_file ? inc_file : input_file;
  56. i = 0;
  57. if (saw_eof || (c = getc(f)) == EOF) {
  58. /* VM: end of include file */
  59. if(inc_file) {
  60. fclose(inc_file);
  61. inc_file = NULL;
  62. lineno = inc_save_lineno;
  63. goto NextLine;
  64. }
  65. if (line) FREE(line);
  66. saw_eof = 1;
  67. return line = cptr = 0;
  68. }
  69. if (line == 0 || linesize != (LINESIZE + 1)) {
  70. if (line) FREE(line);
  71. linesize = LINESIZE + 1;
  72. if (!(line = MALLOC(linesize))) no_space();
  73. }
  74. ++lineno;
  75. while ((line[i] = c) != '\n') {
  76. if (++i + 1 >= linesize)
  77. if (!(line = REALLOC(line, linesize += LINESIZE)))
  78. no_space();
  79. if ((c = getc(f)) == EOF) {
  80. c = '\n';
  81. saw_eof = 1;
  82. }
  83. }
  84. line[i+1] = 0;
  85. /* VM: process %ifdef line */
  86. if(strncmp(&line[0], "%ifdef ", 7)==0) {
  87. char var_name[80];
  88. int ii=0;
  89. char **ps;
  90. for(i=7; line[i]!='\n' && line[i]!=' '; i++, ii++) {
  91. var_name[ii] = line[i];
  92. }
  93. var_name[ii] = 0;
  94. if(in_ifdef) {
  95. error(lineno, 0, 0, "Cannot have nested %%ifdef");
  96. }
  97. /* Find the preprocessor variable */
  98. for(ps=&defd_vars[0]; *ps; ps++) {
  99. if(strcmp(*ps,var_name)==0) {
  100. break;
  101. }
  102. }
  103. in_ifdef = 1;
  104. if(*ps) {
  105. ifdef_skip = 0;
  106. } else {
  107. ifdef_skip = 1;
  108. }
  109. goto NextLine;
  110. }
  111. /* VM: process %endif line */
  112. if(strncmp(&line[0], "%endif", 6)==0) {
  113. if(!in_ifdef) {
  114. error(lineno, 0, 0, "There is no corresponding %%ifdef for %%endif");
  115. }
  116. in_ifdef = 0;
  117. goto NextLine;
  118. }
  119. /* VM: skip ordinary lines if ordered by %endif */
  120. if(in_ifdef && ifdef_skip) {
  121. goto NextLine;
  122. }
  123. /* VM: Process %include line */
  124. if(strncmp(&line[0], "%include ", 9)==0) {
  125. int ii=0;
  126. for(i=9; line[i]!='\n' && line[i]!=' '; i++, ii++) {
  127. inc_file_name[ii] = line[i];
  128. }
  129. inc_file_name[ii] = 0;
  130. if(inc_file) {
  131. error(lineno, 0, 0, "Nested include lines are not allowed");
  132. }
  133. inc_file = fopen(inc_file_name, "r");
  134. if(inc_file==NULL) {
  135. error(lineno, 0, 0, "Cannot open include file %s", inc_file_name);
  136. }
  137. inc_save_lineno = lineno;
  138. lineno = 0;
  139. goto NextLine;
  140. }
  141. /* VM: process %define line */
  142. if(strncmp(&line[0], "%define ", 8)==0) {
  143. char var_name[80];
  144. int ii=0;
  145. char **ps;
  146. for(i=8; line[i]!='\n' && line[i]!=' '; i++, ii++) {
  147. var_name[ii] = line[i];
  148. }
  149. var_name[ii] = 0;
  150. /* Find the preprocessor variable */
  151. for(ps=&defd_vars[0]; *ps; ps++) {
  152. if(strcmp(*ps,var_name)==0) {
  153. error(lineno, 0, 0, "Preprocessor variable %s already defined", var_name);
  154. }
  155. }
  156. *ps = MALLOC(strlen(var_name)+1);
  157. strcpy(*ps, var_name);
  158. *++ps = NULL;
  159. goto NextLine;
  160. }
  161. if(Eflag) {
  162. printf("YPP: %s", line);
  163. }
  164. return cptr = line;
  165. }
  166. char *dup_line()
  167. {
  168. register char *p, *s, *t;
  169. if (line == 0) return (0);
  170. s = line;
  171. while (*s != '\n') ++s;
  172. if (!(p = MALLOC(s - line + 1))) no_space();
  173. s = line;
  174. t = p;
  175. while ((*t++ = *s++) != '\n');
  176. return (p);
  177. }
  178. char *skip_comment()
  179. {
  180. register char *s;
  181. int st_lineno = lineno;
  182. char *st_line = dup_line();
  183. char *st_cptr = st_line + (cptr - line);
  184. s = cptr + 2;
  185. while (s[0] != '*' || s[1] != '/') {
  186. if (*s == '\n') {
  187. if ((s = get_line()) == 0)
  188. unterminated_comment(st_lineno, st_line, st_cptr); }
  189. else
  190. ++s; }
  191. FREE(st_line);
  192. return cptr = s + 2;
  193. }
  194. int nextc()
  195. {
  196. register char *s;
  197. if (line == 0 && get_line() == 0)
  198. return (EOF);
  199. s = cptr;
  200. for (;;) {
  201. switch (*s) {
  202. case '\n':
  203. if ((s = get_line()) == 0) return EOF;
  204. break;
  205. case ' ':
  206. case '\t':
  207. case '\f':
  208. case '\r':
  209. case '\v':
  210. case ',':
  211. case ';':
  212. ++s;
  213. break;
  214. case '\\':
  215. cptr = s;
  216. return ('%');
  217. case '/':
  218. if (s[1] == '*') {
  219. cptr = s;
  220. s = skip_comment();
  221. break; }
  222. else if (s[1] == '/') {
  223. if ((s = get_line()) == 0) return EOF;
  224. break; }
  225. /* fall through */
  226. default:
  227. cptr = s;
  228. return (*s); } }
  229. }
  230. static struct keyword { char name[12]; int token; } keywords[] = {
  231. { "binary", NONASSOC },
  232. { "ident", IDENT },
  233. { "left", LEFT },
  234. { "nonassoc", NONASSOC },
  235. { "right", RIGHT },
  236. { "start", START },
  237. { "term", TOKEN },
  238. { "token", TOKEN },
  239. { "type", TYPE },
  240. { "union", UNION },
  241. };
  242. int keyword()
  243. {
  244. register int c;
  245. char *t_cptr = cptr;
  246. struct keyword *key;
  247. c = *++cptr;
  248. if (isalpha(c)) {
  249. cinc = 0;
  250. while (isalnum(c) || c == '_' || c == '.' || c == '$') {
  251. cachec(tolower(c));
  252. c = *++cptr;
  253. }
  254. cachec(NUL);
  255. if ((key = bsearch(cache, keywords, sizeof(keywords)/sizeof(*key),
  256. sizeof(*key), (int(*)(const void*, const void*)) strcmp)))
  257. return key->token;
  258. } else {
  259. ++cptr;
  260. if (c == '{') return (TEXT);
  261. if (c == '%' || c == '\\') return (MARK);
  262. if (c == '<') return (LEFT);
  263. if (c == '>') return (RIGHT);
  264. if (c == '0') return (TOKEN);
  265. if (c == '2') return (NONASSOC);
  266. }
  267. syntax_error(lineno, line, t_cptr);
  268. /*NOTREACHED*/
  269. return 0;
  270. }
  271. void copy_ident()
  272. {
  273. register int c;
  274. register FILE *f = output_file;
  275. if ((c = nextc()) == EOF) unexpected_EOF();
  276. if (c != '"') syntax_error(lineno, line, cptr);
  277. ++outline;
  278. fprintf(f, "#ident \"");
  279. for (;;) {
  280. c = *++cptr;
  281. if (c == '\n') {
  282. fprintf(f, "\"\n");
  283. return; }
  284. putc(c, f);
  285. if (c == '"') {
  286. putc('\n', f);
  287. ++cptr;
  288. return; } }
  289. }
  290. #define OUTC(c) do { /* output a character on f1 and f2, if non-null */ \
  291. int _c = (c); \
  292. putc(_c, f1); \
  293. if (f2) putc(_c, f2); \
  294. } while(0)
  295. void copy_string(int quote, FILE *f1, FILE *f2)
  296. {
  297. register int c;
  298. int s_lineno = lineno;
  299. char *s_line = dup_line();
  300. char *s_cptr = s_line + (cptr - line - 1);
  301. for (;;) {
  302. OUTC(c = *cptr++);
  303. if (c == quote) {
  304. FREE(s_line);
  305. return; }
  306. if (c == '\n')
  307. unterminated_string(s_lineno, s_line, s_cptr);
  308. if (c == '\\') {
  309. OUTC(c = *cptr++);
  310. if (c == '\n') {
  311. if (get_line() == 0)
  312. unterminated_string(s_lineno, s_line, s_cptr); } } }
  313. }
  314. void copy_comment(FILE *f1, FILE *f2)
  315. {
  316. register int c;
  317. if ((c = *cptr) == '/') {
  318. OUTC('*');
  319. while ((c = *++cptr) != '\n') {
  320. OUTC(c);
  321. if (c == '*' && cptr[1] == '/')
  322. OUTC(' '); }
  323. OUTC('*'); OUTC('/'); }
  324. else if (c == '*') {
  325. int c_lineno = lineno;
  326. char *c_line = dup_line();
  327. char *c_cptr = c_line + (cptr - line - 1);
  328. OUTC(c);
  329. while ((c = *++cptr) != '*' || cptr[1] != '/') {
  330. OUTC(c);
  331. if (c == '\n') {
  332. if (get_line() == 0)
  333. unterminated_comment(c_lineno, c_line, c_cptr); } }
  334. OUTC(c);
  335. OUTC('/');
  336. FREE(c_line);
  337. cptr += 2; }
  338. }
  339. #undef OUTC
  340. void copy_text()
  341. {
  342. register int c;
  343. register FILE *f = text_file;
  344. int need_newline = 0;
  345. int t_lineno = lineno;
  346. char *t_line = dup_line();
  347. char *t_cptr = t_line + (cptr - line - 2);
  348. if (*cptr == '\n') {
  349. if (get_line() == 0)
  350. unterminated_text(t_lineno, t_line, t_cptr); }
  351. if (!lflag) fprintf(f, line_format, lineno, (inc_file?inc_file_name:input_file_name));
  352. loop:
  353. switch (c = *cptr++) {
  354. case '\n':
  355. putc('\n', f);
  356. need_newline = 0;
  357. if (get_line()) goto loop;
  358. unterminated_text(t_lineno, t_line, t_cptr);
  359. case '\'':
  360. case '"':
  361. putc(c, f);
  362. copy_string(c, f, 0);
  363. need_newline = 1;
  364. goto loop;
  365. case '/':
  366. putc(c, f);
  367. copy_comment(f, 0);
  368. need_newline = 1;
  369. goto loop;
  370. case '%':
  371. case '\\':
  372. if (*cptr == '}') {
  373. if (need_newline) putc('\n', f);
  374. ++cptr;
  375. FREE(t_line);
  376. return; }
  377. /* fall through */
  378. default:
  379. putc(c, f);
  380. need_newline = 1;
  381. goto loop; }
  382. }
  383. void copy_union()
  384. {
  385. FILE *dc_file;
  386. register int c;
  387. int depth;
  388. int u_lineno = lineno;
  389. char *u_line = dup_line();
  390. char *u_cptr = u_line + (cptr - line - 6);
  391. if (unionized) over_unionized(cptr - 6);
  392. unionized = 1;
  393. if (!lflag)
  394. fprintf(text_file, line_format, lineno, (inc_file?inc_file_name:input_file_name));
  395. /* VM: Print to either code file or defines file but not to both */
  396. dc_file = dflag ? union_file : text_file;
  397. fprintf(dc_file, "\ntypedef union");
  398. depth = 0;
  399. loop:
  400. c = *cptr++;
  401. putc(c, dc_file);
  402. switch (c) {
  403. case '\n':
  404. get_line();
  405. if (line == 0) unterminated_union(u_lineno, u_line, u_cptr);
  406. goto loop;
  407. case '{':
  408. ++depth;
  409. goto loop;
  410. case '}':
  411. if (--depth == 0) {
  412. fprintf(dc_file, " YYSTYPE;\n");
  413. FREE(u_line);
  414. return; }
  415. goto loop;
  416. case '\'':
  417. case '"':
  418. copy_string(c, dc_file, 0);
  419. goto loop;
  420. case '/':
  421. copy_comment(dc_file, 0);
  422. goto loop;
  423. default:
  424. goto loop;
  425. }
  426. }
  427. int hexval(int c)
  428. {
  429. if (c >= '0' && c <= '9')
  430. return (c - '0');
  431. if (c >= 'A' && c <= 'F')
  432. return (c - 'A' + 10);
  433. if (c >= 'a' && c <= 'f')
  434. return (c - 'a' + 10);
  435. return (-1);
  436. }
  437. bucket *get_literal()
  438. {
  439. register int c, quote;
  440. register int i;
  441. register int n;
  442. register char *s;
  443. register bucket *bp;
  444. int s_lineno = lineno;
  445. char *s_line = dup_line();
  446. char *s_cptr = s_line + (cptr - line);
  447. quote = *cptr++;
  448. cinc = 0;
  449. for (;;) {
  450. c = *cptr++;
  451. if (c == quote) break;
  452. if (c == '\n') unterminated_string(s_lineno, s_line, s_cptr);
  453. if (c == '\\') {
  454. char *c_cptr = cptr - 1;
  455. c = *cptr++;
  456. switch (c) {
  457. case '\n':
  458. get_line();
  459. if (line == 0) unterminated_string(s_lineno, s_line, s_cptr);
  460. continue;
  461. case '0': case '1': case '2': case '3':
  462. case '4': case '5': case '6': case '7':
  463. n = c - '0';
  464. c = *cptr;
  465. if (IS_OCTAL(c)) {
  466. n = (n << 3) + (c - '0');
  467. c = *++cptr;
  468. if (IS_OCTAL(c)) {
  469. n = (n << 3) + (c - '0');
  470. ++cptr; } }
  471. if (n > MAXCHAR) illegal_character(c_cptr);
  472. c = n;
  473. break;
  474. case 'x':
  475. c = *cptr++;
  476. n = hexval(c);
  477. if (n < 0 || n >= 16)
  478. illegal_character(c_cptr);
  479. for (;;) {
  480. c = *cptr;
  481. i = hexval(c);
  482. if (i < 0 || i >= 16) break;
  483. ++cptr;
  484. n = (n << 4) + i;
  485. if (n > MAXCHAR) illegal_character(c_cptr); }
  486. c = n;
  487. break;
  488. case 'a': c = 7; break;
  489. case 'b': c = '\b'; break;
  490. case 'f': c = '\f'; break;
  491. case 'n': c = '\n'; break;
  492. case 'r': c = '\r'; break;
  493. case 't': c = '\t'; break;
  494. case 'v': c = '\v'; break; } }
  495. cachec(c); }
  496. FREE(s_line);
  497. n = cinc;
  498. s = MALLOC(n);
  499. if (s == 0) no_space();
  500. for (i = 0; i < n; ++i)
  501. s[i] = cache[i];
  502. cinc = 0;
  503. if (n == 1)
  504. cachec('\'');
  505. else
  506. cachec('"');
  507. for (i = 0; i < n; ++i) {
  508. c = ((unsigned char *)s)[i];
  509. if (c == '\\' || c == cache[0]) {
  510. cachec('\\');
  511. cachec(c); }
  512. else if (isprint(c))
  513. cachec(c);
  514. else {
  515. cachec('\\');
  516. switch (c) {
  517. case 7: cachec('a'); break;
  518. case '\b': cachec('b'); break;
  519. case '\f': cachec('f'); break;
  520. case '\n': cachec('n'); break;
  521. case '\r': cachec('r'); break;
  522. case '\t': cachec('t'); break;
  523. case '\v': cachec('v'); break;
  524. default:
  525. cachec(((c >> 6) & 7) + '0');
  526. cachec(((c >> 3) & 7) + '0');
  527. cachec((c & 7) + '0');
  528. break; } } }
  529. if (n == 1)
  530. cachec('\'');
  531. else
  532. cachec('"');
  533. cachec(NUL);
  534. bp = lookup(cache);
  535. bp->class = TERM;
  536. if (n == 1 && bp->value == UNDEFINED)
  537. bp->value = *(unsigned char *)s;
  538. FREE(s);
  539. return (bp);
  540. }
  541. int is_reserved(char *name)
  542. {
  543. char *s;
  544. if (strcmp(name, ".") == 0 ||
  545. strcmp(name, "$accept") == 0 ||
  546. strcmp(name, "$end") == 0)
  547. return (1);
  548. if (name[0] == '$' && name[1] == '$' && isdigit(name[2])) {
  549. s = name + 3;
  550. while (isdigit(*s)) ++s;
  551. if (*s == NUL) return (1); }
  552. return (0);
  553. }
  554. bucket *get_name()
  555. {
  556. register int c;
  557. cinc = 0;
  558. for (c = *cptr; IS_IDENT(c); c = *++cptr)
  559. cachec(c);
  560. cachec(NUL);
  561. if (is_reserved(cache)) used_reserved(cache);
  562. return (lookup(cache));
  563. }
  564. int get_number()
  565. {
  566. register int c;
  567. register int n;
  568. n = 0;
  569. for (c = *cptr; isdigit(c); c = *++cptr)
  570. n = 10*n + (c - '0');
  571. return (n);
  572. }
  573. //
  574. // Date: Mon, 29 Jun 1998 16:36:47 +0200
  575. // From: Matthias Meixner <[email protected]>
  576. // Organization: TH Darmstadt, Mikroelektronische Systeme
  577. //
  578. // While using your version of BTYacc (V2.1), I have found a bug.
  579. // It does not correctly
  580. // handle typenames, if one typename is a prefix of another one and
  581. // if this type is used after the longer one. In this case BTYacc
  582. // produces invalid code.
  583. //
  584. // e.g. in:
  585. // --------------------------------------------
  586. // %{
  587. //
  588. // #include <stdlib.h>
  589. //
  590. // struct List {
  591. // struct List *next;
  592. // int foo;
  593. // };
  594. //
  595. // %}
  596. // %union {
  597. // struct List *fooList;
  598. // int foo;
  599. // }
  600. //
  601. // %type <fooList> a
  602. // %type <foo> b
  603. //
  604. // %token <foo> A
  605. //
  606. // %%
  607. //
  608. // a: b {$$=malloc(sizeof(*$$));$$->next=NULL;$$->foo=$1;}
  609. // | a b {$$=malloc(sizeof(*$$));$$->next=$1;$$->foo=$2;}
  610. //
  611. // b: A {$$=$1;}
  612. //
  613. static char *cache_tag(char *tag, int len)
  614. {
  615. int i;
  616. char *s;
  617. for (i = 0; i < ntags; ++i) {
  618. if (strncmp(tag, tag_table[i], len) == 0 &&
  619. // VM: this is bug fix proposed by Matthias Meixner
  620. tag_table[i][len]==0)
  621. return (tag_table[i]); }
  622. if (ntags >= tagmax) {
  623. tagmax += 16;
  624. tag_table = tag_table ? RENEW(tag_table, tagmax, char *)
  625. : NEW2(tagmax, char *);
  626. if (tag_table == 0) no_space(); }
  627. s = MALLOC(len + 1);
  628. if (s == 0) no_space();
  629. strncpy(s, tag, len);
  630. s[len] = 0;
  631. tag_table[ntags++] = s;
  632. return s;
  633. }
  634. char *get_tag()
  635. {
  636. register int c;
  637. int t_lineno = lineno;
  638. char *t_line = dup_line();
  639. char *t_cptr = t_line + (cptr - line);
  640. ++cptr;
  641. c = nextc();
  642. if (c == EOF) unexpected_EOF();
  643. if (!isalpha(c) && c != '_' && c != '$')
  644. illegal_tag(t_lineno, t_line, t_cptr);
  645. cinc = 0;
  646. do { cachec(c); c = *++cptr; } while (IS_IDENT(c));
  647. c = nextc();
  648. if (c == EOF) unexpected_EOF();
  649. if (c != '>')
  650. illegal_tag(t_lineno, t_line, t_cptr);
  651. ++cptr;
  652. FREE(t_line);
  653. havetags = 1;
  654. return cache_tag(cache, cinc);
  655. }
  656. static char *scan_id(void)
  657. {
  658. char *b = cptr;
  659. while (isalnum(*cptr) || *cptr == '_' || *cptr == '$') cptr++;
  660. return cache_tag(b, cptr-b);
  661. }
  662. void declare_tokens(int assoc)
  663. {
  664. register int c;
  665. register bucket *bp;
  666. int value;
  667. char *tag = 0;
  668. if (assoc != TOKEN) ++prec;
  669. c = nextc();
  670. if (c == EOF) unexpected_EOF();
  671. if (c == '<') {
  672. tag = get_tag();
  673. c = nextc();
  674. if (c == EOF) unexpected_EOF(); }
  675. for (;;) {
  676. if (isalpha(c) || c == '_' || c == '.' || c == '$')
  677. bp = get_name();
  678. else if (c == '\'' || c == '"')
  679. bp = get_literal();
  680. else
  681. return;
  682. if (bp == goal) tokenized_start(bp->name);
  683. bp->class = TERM;
  684. if (tag) {
  685. if (bp->tag && tag != bp->tag)
  686. retyped_warning(bp->name);
  687. bp->tag = tag; }
  688. if (assoc != TOKEN) {
  689. if (bp->prec && prec != bp->prec)
  690. reprec_warning(bp->name);
  691. bp->assoc = assoc;
  692. bp->prec = prec; }
  693. c = nextc();
  694. if (c == EOF) unexpected_EOF();
  695. value = UNDEFINED;
  696. if (isdigit(c)) {
  697. value = get_number();
  698. if (bp->value != UNDEFINED && value != bp->value)
  699. revalued_warning(bp->name);
  700. bp->value = value;
  701. c = nextc();
  702. if (c == EOF) unexpected_EOF(); } }
  703. }
  704. static void declare_argtypes(bucket *bp)
  705. {
  706. char *tags[MAXARGS];
  707. int args = 0, c;
  708. if (bp->args >= 0) retyped_warning(bp->name);
  709. cptr++; /* skip open paren */
  710. for (;;) {
  711. c = nextc();
  712. if (c == EOF) unexpected_EOF();
  713. if (c != '<') syntax_error(lineno, line, cptr);
  714. tags[args++] = get_tag();
  715. c = nextc();
  716. if (c == ')') break;
  717. if (c == EOF) unexpected_EOF(); }
  718. cptr++; /* skip close paren */
  719. bp->args = args;
  720. if (!(bp->argnames = NEW2(args, char *))) no_space();
  721. if (!(bp->argtags = NEW2(args, char *))) no_space();
  722. while (--args >= 0) {
  723. bp->argtags[args] = tags[args];
  724. bp->argnames[args] = 0; }
  725. }
  726. void declare_types()
  727. {
  728. register int c;
  729. register bucket *bp=0;
  730. char *tag=0;
  731. c = nextc();
  732. if (c == '<') {
  733. tag = get_tag();
  734. c = nextc(); }
  735. if (c == EOF) unexpected_EOF();
  736. for (;;) {
  737. c = nextc();
  738. if (isalpha(c) || c == '_' || c == '.' || c == '$') {
  739. bp = get_name();
  740. if (nextc() == '(')
  741. declare_argtypes(bp);
  742. else
  743. bp->args = 0; }
  744. else if (c == '\'' || c == '"') {
  745. bp = get_literal();
  746. bp->args = 0; }
  747. else
  748. return;
  749. if (tag) {
  750. if (bp->tag && tag != bp->tag)
  751. retyped_warning(bp->name);
  752. bp->tag = tag; } }
  753. }
  754. void declare_start()
  755. {
  756. register int c;
  757. register bucket *bp;
  758. c = nextc();
  759. if (c == EOF) unexpected_EOF();
  760. if (!isalpha(c) && c != '_' && c != '.' && c != '$')
  761. syntax_error(lineno, line, cptr);
  762. bp = get_name();
  763. if (bp->class == TERM)
  764. terminal_start(bp->name);
  765. if (goal && goal != bp)
  766. restarted_warning();
  767. goal = bp;
  768. }
  769. void read_declarations()
  770. {
  771. register int c, k;
  772. cache_size = 256;
  773. cache = MALLOC(cache_size);
  774. if (cache == 0) no_space();
  775. for (;;) {
  776. c = nextc();
  777. if (c == EOF) unexpected_EOF();
  778. if (c != '%') syntax_error(lineno, line, cptr);
  779. switch (k = keyword()) {
  780. case MARK:
  781. return;
  782. case IDENT:
  783. copy_ident();
  784. break;
  785. case TEXT:
  786. copy_text();
  787. break;
  788. case UNION:
  789. copy_union();
  790. break;
  791. case TOKEN:
  792. case LEFT:
  793. case RIGHT:
  794. case NONASSOC:
  795. declare_tokens(k);
  796. break;
  797. case TYPE:
  798. declare_types();
  799. break;
  800. case START:
  801. declare_start();
  802. break; } }
  803. }
  804. void initialize_grammar()
  805. {
  806. nitems = 4;
  807. maxitems = 300;
  808. pitem = NEW2(maxitems, bucket *);
  809. if (pitem == 0) no_space();
  810. pitem[0] = 0;
  811. pitem[1] = 0;
  812. pitem[2] = 0;
  813. pitem[3] = 0;
  814. nrules = 3;
  815. maxrules = 100;
  816. plhs = NEW2(maxrules, bucket *);
  817. if (plhs == 0) no_space();
  818. plhs[0] = 0;
  819. plhs[1] = 0;
  820. plhs[2] = 0;
  821. rprec = NEW2(maxrules, Yshort);
  822. if (rprec == 0) no_space();
  823. rprec[0] = 0;
  824. rprec[1] = 0;
  825. rprec[2] = 0;
  826. rassoc = NEW2(maxrules, char);
  827. if (rassoc == 0) no_space();
  828. rassoc[0] = TOKEN;
  829. rassoc[1] = TOKEN;
  830. rassoc[2] = TOKEN;
  831. }
  832. void expand_items()
  833. {
  834. maxitems += 300;
  835. pitem = RENEW(pitem, maxitems, bucket *);
  836. if (pitem == 0) no_space();
  837. }
  838. void expand_rules()
  839. {
  840. maxrules += 100;
  841. plhs = RENEW(plhs, maxrules, bucket *);
  842. if (plhs == 0) no_space();
  843. rprec = RENEW(rprec, maxrules, Yshort);
  844. if (rprec == 0) no_space();
  845. rassoc = RENEW(rassoc, maxrules, char);
  846. if (rassoc == 0) no_space();
  847. }
  848. /* set in copy_args and incremented by the various routines that will rescan
  849. ** the argument list as appropriate */
  850. static int rescan_lineno;
  851. static char *copy_args(int *alen)
  852. {
  853. struct mstring *s = msnew();
  854. int depth = 0, len = 1, c;
  855. char quote = 0;
  856. int a_lineno = lineno;
  857. char *a_line = dup_line();
  858. char *a_cptr = a_line + (cptr - line - 1);
  859. rescan_lineno = lineno;
  860. while ((c = *cptr++) != ')' || depth || quote) {
  861. if (c == ',' && !quote && !depth) {
  862. len++;
  863. mputc(s, 0);
  864. continue; }
  865. mputc(s, c);
  866. if (c == '\n') {
  867. get_line();
  868. if (!line) {
  869. if (quote)
  870. unterminated_string(a_lineno, a_line, a_cptr);
  871. else
  872. unterminated_arglist(a_lineno, a_line, a_cptr); } }
  873. else if (quote) {
  874. if (c == quote) quote = 0;
  875. else if (c == '\\') {
  876. if (*cptr != '\n') mputc(s, *cptr++); } }
  877. else {
  878. if (c == '(') depth++;
  879. else if (c == ')') depth--;
  880. else if (c == '\"' || c == '\'') quote = c; } }
  881. if (alen) *alen = len;
  882. FREE(a_line);
  883. return msdone(s);
  884. }
  885. static char *parse_id(char *p, char **save)
  886. {
  887. char *b;
  888. while (isspace(*p)) if (*p++ == '\n') rescan_lineno++;
  889. if (!isalpha(*p) && *p != '_') return 0;
  890. b = p;
  891. while (isalnum(*p) || *p == '_' || *p == '$') p++;
  892. if (save) {
  893. *save = cache_tag(b, p-b); }
  894. return p;
  895. }
  896. static char *parse_int(char *p, int *save)
  897. {
  898. int neg=0, val=0;
  899. while (isspace(*p)) if (*p++ == '\n') rescan_lineno++;
  900. if (*p == '-') {
  901. neg=1;
  902. p++; }
  903. if (!isdigit(*p)) return 0;
  904. while (isdigit(*p))
  905. val = val*10 + *p++ - '0';
  906. if (neg) val = -val;
  907. if (save) *save = val;
  908. return p;
  909. }
  910. static void parse_arginfo(bucket *a, char *args, int argslen)
  911. {
  912. char *p=args, *tmp;
  913. int i, redec=0;
  914. if (a->args >= 0) {
  915. if (a->args != argslen)
  916. error(rescan_lineno, 0, 0, "number of arguments of %s does't "
  917. "agree with previous declaration", a->name);
  918. redec = 1; }
  919. else {
  920. if (!(a->args = argslen)) return;
  921. if (!(a->argnames = NEW2(argslen, char *)) ||
  922. !(a->argtags = NEW2(argslen, char *)))
  923. no_space(); }
  924. if (!args) return;
  925. for (i=0; i<argslen; i++) {
  926. while (isspace(*p)) if (*p++ == '\n') rescan_lineno++;
  927. if (*p++ != '$') bad_formals();
  928. while (isspace(*p)) if (*p++ == '\n') rescan_lineno++;
  929. if (*p == '<') {
  930. havetags = 1;
  931. if (!(p = parse_id(p+1, &tmp))) bad_formals();
  932. while (isspace(*p)) if (*p++ == '\n') rescan_lineno++;
  933. if (*p++ != '>') bad_formals();
  934. if (redec) {
  935. if (a->argtags[i] != tmp)
  936. error(rescan_lineno, 0, 0, "type of argument %d to %s "
  937. "doesn't agree with previous declaration", i+1,
  938. a->name); }
  939. else
  940. a->argtags[i] = tmp; }
  941. else if (!redec)
  942. a->argtags[i] = 0;
  943. if (!(p = parse_id(p, &a->argnames[i]))) bad_formals();
  944. while (isspace(*p)) if (*p++ == '\n') rescan_lineno++;
  945. if (*p++) bad_formals(); }
  946. free(args);
  947. }
  948. static char *compile_arg(char **theptr, char *yyvaltag)
  949. {
  950. char *p = *theptr;
  951. struct mstring *c = msnew();
  952. int i, j, n;
  953. Yshort *offsets=0, maxoffset;
  954. bucket **rhs;
  955. maxoffset = n = 0;
  956. for (i = nitems - 1; pitem[i]; --i) {
  957. n++;
  958. if (pitem[i]->class != ARGUMENT)
  959. maxoffset++; }
  960. if (maxoffset > 0) {
  961. offsets = NEW2(maxoffset+1, Yshort);
  962. if (offsets == 0) no_space(); }
  963. for (j=0, i++; i<nitems; i++)
  964. if (pitem[i]->class != ARGUMENT)
  965. offsets[++j] = i - nitems + 1;
  966. rhs = pitem + nitems - 1;
  967. if (yyvaltag)
  968. msprintf(c, "yyval.%s = ", yyvaltag);
  969. else
  970. msprintf(c, "yyval = ");
  971. while (*p) {
  972. if (*p == '$') {
  973. char *tag = 0;
  974. if (*++p == '<')
  975. if (!(p = parse_id(++p, &tag)) || *p++ != '>')
  976. illegal_tag(rescan_lineno, 0, 0);
  977. if (isdigit(*p) || *p == '-') {
  978. int val;
  979. if (!(p = parse_int(p, &val)))
  980. dollar_error(rescan_lineno, 0, 0);
  981. if (val <= 0) i = val - n;
  982. else if (val > maxoffset) {
  983. dollar_warning(rescan_lineno, val);
  984. i = val - maxoffset;
  985. } else {
  986. i = offsets[val];
  987. if (!tag && !(tag = rhs[i]->tag) && havetags)
  988. untyped_rhs(val, rhs[i]->name);
  989. }
  990. msprintf(c, "yyvsp[%d]", i);
  991. if (tag) msprintf(c, ".%s", tag);
  992. else if (havetags)
  993. unknown_rhs(val);
  994. } else if (isalpha(*p) || *p == '_') {
  995. char *arg;
  996. if (!(p = parse_id(p, &arg)))
  997. dollar_error(rescan_lineno, 0, 0);
  998. for (i=plhs[nrules]->args-1; i>=0; i--)
  999. if (arg == plhs[nrules]->argnames[i]) break;
  1000. if (i<0)
  1001. error(rescan_lineno, 0, 0, "unknown argument $%s", arg);
  1002. if (!tag)
  1003. tag = plhs[nrules]->argtags[i];
  1004. msprintf(c, "yyvsp[%d]", i - plhs[nrules]->args + 1 - n);
  1005. if (tag) msprintf(c, ".%s", tag);
  1006. else if (havetags)
  1007. error(rescan_lineno, 0, 0, "untyped argument $%s", arg); }
  1008. else
  1009. dollar_error(rescan_lineno, 0, 0);
  1010. } else {
  1011. if (*p == '\n') rescan_lineno++;
  1012. mputc(c, *p++);
  1013. }
  1014. }
  1015. *theptr = p;
  1016. if (maxoffset > 0) FREE(offsets);
  1017. return msdone(c);
  1018. }
  1019. #define ARG_CACHE_SIZE 1024
  1020. static struct arg_cache {
  1021. struct arg_cache *next;
  1022. char *code;
  1023. int rule;
  1024. } *arg_cache[ARG_CACHE_SIZE];
  1025. static int lookup_arg_cache(char *code)
  1026. {
  1027. struct arg_cache *entry;
  1028. entry = arg_cache[strnshash(code) % ARG_CACHE_SIZE];
  1029. while (entry) {
  1030. if (!strnscmp(entry->code, code)) return entry->rule;
  1031. entry = entry->next; }
  1032. return -1;
  1033. }
  1034. static void insert_arg_cache(char *code, int rule)
  1035. {
  1036. struct arg_cache *entry = NEW(struct arg_cache);
  1037. int i;
  1038. if (!entry) no_space();
  1039. i = strnshash(code) % ARG_CACHE_SIZE;
  1040. entry->code = code;
  1041. entry->rule = rule;
  1042. entry->next = arg_cache[i];
  1043. arg_cache[i] = entry;
  1044. }
  1045. static void clean_arg_cache(void)
  1046. {
  1047. struct arg_cache *e, *t;
  1048. int i;
  1049. for (i=0; i<ARG_CACHE_SIZE; i++) {
  1050. for (e=arg_cache[i]; (t=e); e=e->next, FREE(t))
  1051. free(e->code);
  1052. arg_cache[i] = 0; }
  1053. }
  1054. void advance_to_start()
  1055. {
  1056. register int c;
  1057. register bucket *bp;
  1058. char *s_cptr;
  1059. int s_lineno;
  1060. char *args = 0;
  1061. int argslen = 0;
  1062. for (;;) {
  1063. c = nextc();
  1064. if (c != '%') break;
  1065. s_cptr = cptr;
  1066. switch (keyword()) {
  1067. case MARK:
  1068. no_grammar();
  1069. case TEXT:
  1070. copy_text();
  1071. break;
  1072. case START:
  1073. declare_start();
  1074. break;
  1075. default:
  1076. syntax_error(lineno, line, s_cptr); } }
  1077. c = nextc();
  1078. if (!isalpha(c) && c != '_' && c != '.' && c != '$')
  1079. syntax_error(lineno, line, cptr);
  1080. bp = get_name();
  1081. if (goal == 0) {
  1082. if (bp->class == TERM)
  1083. terminal_start(bp->name);
  1084. goal = bp; }
  1085. s_lineno = lineno;
  1086. c = nextc();
  1087. if (c == EOF) unexpected_EOF();
  1088. if (c == '(') {
  1089. ++cptr;
  1090. args = copy_args(&argslen);
  1091. if (args == 0) no_space();
  1092. c = nextc(); }
  1093. if (c != ':') syntax_error(lineno, line, cptr);
  1094. start_rule(bp, s_lineno);
  1095. parse_arginfo(bp, args, argslen);
  1096. ++cptr;
  1097. }
  1098. void start_rule(bucket *bp, int s_lineno)
  1099. {
  1100. if (bp->class == TERM)
  1101. terminal_lhs(s_lineno);
  1102. bp->class = NONTERM;
  1103. if (!bp->index) bp->index = nrules;
  1104. if (nrules >= maxrules)
  1105. expand_rules();
  1106. plhs[nrules] = bp;
  1107. rprec[nrules] = UNDEFINED;
  1108. rassoc[nrules] = TOKEN;
  1109. }
  1110. void end_rule()
  1111. {
  1112. register int i;
  1113. if (!last_was_action && plhs[nrules]->tag) {
  1114. for (i = nitems - 1; pitem[i]; --i) continue;
  1115. if (pitem[i+1] == 0 || pitem[i+1]->tag != plhs[nrules]->tag)
  1116. default_action_warning(); }
  1117. last_was_action = 0;
  1118. if (nitems >= maxitems) expand_items();
  1119. pitem[nitems] = 0;
  1120. ++nitems;
  1121. ++nrules;
  1122. }
  1123. void insert_empty_rule()
  1124. {
  1125. register bucket *bp, **bpp;
  1126. assert(cache);
  1127. sprintf(cache, "$$%d", ++gensym);
  1128. bp = make_bucket(cache);
  1129. last_symbol->next = bp;
  1130. last_symbol = bp;
  1131. bp->tag = plhs[nrules]->tag;
  1132. bp->class = ACTION;
  1133. bp->args = 0;
  1134. if ((nitems += 2) > maxitems)
  1135. expand_items();
  1136. bpp = pitem + nitems - 1;
  1137. *bpp-- = bp;
  1138. while ((bpp[0] = bpp[-1])) --bpp;
  1139. if (++nrules >= maxrules)
  1140. expand_rules();
  1141. plhs[nrules] = plhs[nrules-1];
  1142. plhs[nrules-1] = bp;
  1143. rprec[nrules] = rprec[nrules-1];
  1144. rprec[nrules-1] = 0;
  1145. rassoc[nrules] = rassoc[nrules-1];
  1146. rassoc[nrules-1] = TOKEN;
  1147. }
  1148. static char *insert_arg_rule(char *arg, char *tag)
  1149. {
  1150. int lineno = rescan_lineno;
  1151. char *code = compile_arg(&arg, tag);
  1152. int rule = lookup_arg_cache(code);
  1153. FILE *f = action_file;
  1154. if (rule<0) {
  1155. rule = nrules;
  1156. insert_arg_cache(code, rule);
  1157. fprintf(f, "case %d:\n", rule - 2);
  1158. if (!lflag)
  1159. fprintf(f, line_format, lineno, (inc_file?inc_file_name:input_file_name));
  1160. fprintf(f, "%s;\n", code);
  1161. fprintf(f, "break;\n");
  1162. insert_empty_rule();
  1163. plhs[rule]->tag = tag;
  1164. plhs[rule]->class = ARGUMENT; }
  1165. else {
  1166. if (++nitems > maxitems)
  1167. expand_items();
  1168. pitem[nitems-1] = plhs[rule];
  1169. free(code); }
  1170. return arg+1;
  1171. }
  1172. void add_symbol()
  1173. {
  1174. register int c;
  1175. register bucket *bp;
  1176. int s_lineno = lineno;
  1177. char *args = 0;
  1178. int argslen = 0;
  1179. c = *cptr;
  1180. if (c == '\'' || c == '"')
  1181. bp = get_literal();
  1182. else
  1183. bp = get_name();
  1184. c = nextc();
  1185. if (c == '(') {
  1186. ++cptr;
  1187. args = copy_args(&argslen);
  1188. if (args == 0) no_space();
  1189. c = nextc(); }
  1190. if (c == ':') {
  1191. end_rule();
  1192. start_rule(bp, s_lineno);
  1193. parse_arginfo(bp, args, argslen);
  1194. ++cptr;
  1195. return; }
  1196. if (last_was_action)
  1197. insert_empty_rule();
  1198. last_was_action = 0;
  1199. if (bp->args < 0)
  1200. bp->args = argslen;
  1201. if (argslen == 0 && bp->args > 0 && pitem[nitems-1] == 0) {
  1202. int i;
  1203. if (plhs[nrules]->args != bp->args)
  1204. error(lineno, line, cptr, "Wrong number of default arguments "
  1205. "for %s", bp->name);
  1206. for (i=bp->args-1; i>=0; i--)
  1207. if (plhs[nrules]->argtags[i] != bp->argtags[i])
  1208. error(lineno, line, cptr, "Wrong type for default argument "
  1209. "%d to %s", i+1, bp->name); }
  1210. else if (bp->args != argslen)
  1211. error(lineno, line, cptr, "wrong number of arguments for %s",
  1212. bp->name);
  1213. if (args != 0) {
  1214. char *ap;
  1215. int i;
  1216. for (ap=args, i=0; i<argslen; i++)
  1217. ap = insert_arg_rule(ap, bp->argtags[i]);
  1218. free(args); }
  1219. if (++nitems > maxitems)
  1220. expand_items();
  1221. pitem[nitems-1] = bp;
  1222. }
  1223. void copy_action()
  1224. {
  1225. register int c;
  1226. register int i, j, n;
  1227. int depth;
  1228. int trialaction = 0;
  1229. int haveyyval = 0;
  1230. char *tag;
  1231. register FILE *f = action_file;
  1232. int a_lineno = lineno;
  1233. char *a_line = dup_line();
  1234. char *a_cptr = a_line + (cptr - line);
  1235. Yshort *offsets=0, maxoffset;
  1236. bucket **rhs;
  1237. if (last_was_action)
  1238. insert_empty_rule();
  1239. last_was_action = 1;
  1240. fprintf(f, "case %d:\n", nrules - 2);
  1241. if (*cptr != '[')
  1242. fprintf(f, " if (!yytrial)\n");
  1243. else
  1244. trialaction = 1;
  1245. if (!lflag)
  1246. fprintf(f, line_format, lineno, (inc_file?inc_file_name:input_file_name));
  1247. if (*cptr == '=') ++cptr;
  1248. maxoffset = n = 0;
  1249. for (i = nitems - 1; pitem[i]; --i) {
  1250. n++;
  1251. if (pitem[i]->class != ARGUMENT)
  1252. maxoffset++; }
  1253. if (maxoffset > 0) {
  1254. offsets = NEW2(maxoffset+1, Yshort);
  1255. if (offsets == 0) no_space(); }
  1256. for (j=0, i++; i<nitems; i++)
  1257. if (pitem[i]->class != ARGUMENT)
  1258. offsets[++j] = i - nitems + 1;
  1259. rhs = pitem + nitems - 1;
  1260. depth = 0;
  1261. loop:
  1262. c = *cptr;
  1263. if (c == '$') {
  1264. if (cptr[1] == '<') {
  1265. int d_lineno = lineno;
  1266. char *d_line = dup_line();
  1267. char *d_cptr = d_line + (cptr - line);
  1268. ++cptr;
  1269. tag = get_tag();
  1270. c = *cptr;
  1271. if (c == '$') {
  1272. fprintf(f, "yyval.%s", tag);
  1273. ++cptr;
  1274. FREE(d_line);
  1275. goto loop; }
  1276. else if (isdigit(c)) {
  1277. i = get_number();
  1278. if (i > maxoffset) {
  1279. dollar_warning(d_lineno, i);
  1280. fprintf(f, "yyvsp[%d].%s", i - maxoffset, tag); }
  1281. else
  1282. fprintf(f, "yyvsp[%d].%s", offsets[i], tag);
  1283. FREE(d_line);
  1284. goto loop; }
  1285. else if (c == '-' && isdigit(cptr[1])) {
  1286. ++cptr;
  1287. i = -get_number() - n;
  1288. fprintf(f, "yyvsp[%d].%s", i, tag);
  1289. FREE(d_line);
  1290. goto loop; }
  1291. else if (isalpha(c) || c == '_') {
  1292. char *arg = scan_id();
  1293. for (i=plhs[nrules]->args-1; i>=0; i--)
  1294. if (arg == plhs[nrules]->argnames[i]) break;
  1295. if (i<0)
  1296. error(d_lineno,d_line,d_cptr,"unknown argument %s",arg);
  1297. fprintf(f, "yyvsp[%d].%s", i-plhs[nrules]->args+1-n, tag);
  1298. FREE(d_line);
  1299. goto loop; }
  1300. else
  1301. dollar_error(d_lineno, d_line, d_cptr); }
  1302. else if (cptr[1] == '$') {
  1303. if (havetags) {
  1304. tag = plhs[nrules]->tag;
  1305. if (tag == 0) untyped_lhs();
  1306. fprintf(f, "yyval.%s", tag); }
  1307. else
  1308. fprintf(f, "yyval");
  1309. cptr += 2;
  1310. haveyyval = 1;
  1311. goto loop; }
  1312. else if (isdigit(cptr[1])) {
  1313. ++cptr;
  1314. i = get_number();
  1315. if (havetags) {
  1316. if (i <= 0 || i > maxoffset)
  1317. unknown_rhs(i);
  1318. tag = rhs[offsets[i]]->tag;
  1319. if (tag == 0)
  1320. untyped_rhs(i, rhs[offsets[i]]->name);
  1321. fprintf(f, "yyvsp[%d].%s", offsets[i], tag); }
  1322. else {
  1323. if (i > n) {
  1324. dollar_warning(lineno, i);
  1325. fprintf(f, "yyvsp[%d]", i - maxoffset); }
  1326. else
  1327. fprintf(f, "yyvsp[%d]", offsets[i]); }
  1328. goto loop; }
  1329. else if (cptr[1] == '-') {
  1330. cptr += 2;
  1331. i = get_number();
  1332. if (havetags)
  1333. unknown_rhs(-i);
  1334. fprintf(f, "yyvsp[%d]", -i - n);
  1335. goto loop; }
  1336. else if (isalpha(cptr[1]) || cptr[1] == '_') {
  1337. char *arg;
  1338. ++cptr;
  1339. arg = scan_id();
  1340. for (i=plhs[nrules]->args-1; i>=0; i--)
  1341. if (arg == plhs[nrules]->argnames[i]) break;
  1342. if (i<0)
  1343. error(lineno, line, cptr, "unknown argument %s", arg);
  1344. tag = plhs[nrules]->argtags[i];
  1345. fprintf(f, "yyvsp[%d]", i - plhs[nrules]->args + 1 - n);
  1346. if (tag) fprintf(f, ".%s", tag);
  1347. else if (havetags)
  1348. error(lineno, 0, 0, "untyped argument $%s", arg);
  1349. goto loop; } }
  1350. if (isalpha(c) || c == '_' || c == '$') {
  1351. do {
  1352. putc(c, f);
  1353. c = *++cptr;
  1354. } while (isalnum(c) || c == '_' || c == '$');
  1355. goto loop; }
  1356. ++cptr;
  1357. if (trialaction && c == '[' && depth == 0) {
  1358. ++depth;
  1359. putc('{', f);
  1360. goto loop; }
  1361. if (trialaction && c == ']' && depth == 1) {
  1362. --depth;
  1363. putc('}', f);
  1364. c = nextc();
  1365. if (c == '[' && !haveyyval) {
  1366. goto loop; }
  1367. else if (c == '{' && !haveyyval) {
  1368. fprintf(f, "\n");
  1369. if (!lflag) fprintf(f, "#\n");
  1370. fprintf(f, " if (!yytrial)\n");
  1371. if (!lflag)
  1372. fprintf(f, line_format, lineno, (inc_file?inc_file_name:input_file_name));
  1373. trialaction = 0;
  1374. goto loop; }
  1375. else {
  1376. fprintf(f, "\n");
  1377. if (!lflag) fprintf(f, "#\n");
  1378. fprintf(f, "break;\n");
  1379. FREE(a_line);
  1380. if (maxoffset > 0) FREE(offsets);
  1381. return; } }
  1382. putc(c, f);
  1383. switch (c) {
  1384. case '\n':
  1385. get_line();
  1386. if (line) goto loop;
  1387. unterminated_action(a_lineno, a_line, a_cptr);
  1388. case ';':
  1389. if (depth > 0) goto loop;
  1390. fprintf(f, "\n");
  1391. if (!lflag) fprintf(f, "#\n");
  1392. fprintf(f, "break;\n");
  1393. FREE(a_line);
  1394. if (maxoffset > 0) FREE(offsets);
  1395. return;
  1396. case '[':
  1397. ++depth;
  1398. goto loop;
  1399. case ']':
  1400. --depth;
  1401. goto loop;
  1402. case '{':
  1403. ++depth;
  1404. goto loop;
  1405. case '}':
  1406. if (--depth > 0) goto loop;
  1407. c = nextc();
  1408. if (c == '[' && !haveyyval) {
  1409. trialaction = 1;
  1410. goto loop; }
  1411. else if (c == '{' && !haveyyval) {
  1412. fprintf(f, "\n");
  1413. if (!lflag) fprintf(f, "#\n");
  1414. fprintf(f, " if (!yytrial)\n");
  1415. if (!lflag)
  1416. fprintf(f, line_format, lineno, (inc_file?inc_file_name:input_file_name));
  1417. goto loop; }
  1418. else {
  1419. fprintf(f, "\n");
  1420. if (!lflag) fprintf(f, "#\n");
  1421. fprintf(f, "break;\n");
  1422. FREE(a_line);
  1423. if (maxoffset > 0) FREE(offsets);
  1424. return; }
  1425. case '\'':
  1426. case '"':
  1427. copy_string(c, f, 0);
  1428. goto loop;
  1429. case '/':
  1430. copy_comment(f, 0);
  1431. goto loop;
  1432. default:
  1433. goto loop; }
  1434. }
  1435. int mark_symbol()
  1436. {
  1437. register int c;
  1438. register bucket *bp;
  1439. c = cptr[1];
  1440. if (c == '%' || c == '\\') {
  1441. cptr += 2;
  1442. return (1); }
  1443. if (c == '=')
  1444. cptr += 2;
  1445. else if ((c == 'p' || c == 'P') &&
  1446. ((c = cptr[2]) == 'r' || c == 'R') &&
  1447. ((c = cptr[3]) == 'e' || c == 'E') &&
  1448. ((c = cptr[4]) == 'c' || c == 'C') &&
  1449. ((c = cptr[5], !IS_IDENT(c))))
  1450. cptr += 5;
  1451. else
  1452. syntax_error(lineno, line, cptr);
  1453. c = nextc();
  1454. if (isalpha(c) || c == '_' || c == '.' || c == '$')
  1455. bp = get_name();
  1456. else if (c == '\'' || c == '"')
  1457. bp = get_literal();
  1458. else {
  1459. syntax_error(lineno, line, cptr);
  1460. /*NOTREACHED*/
  1461. return 0;}
  1462. if (rprec[nrules] != UNDEFINED && bp->prec != rprec[nrules])
  1463. prec_redeclared();
  1464. rprec[nrules] = bp->prec;
  1465. rassoc[nrules] = bp->assoc;
  1466. return (0);
  1467. }
  1468. void read_grammar()
  1469. {
  1470. register int c;
  1471. initialize_grammar();
  1472. advance_to_start();
  1473. for (;;) {
  1474. c = nextc();
  1475. if (c == EOF) break;
  1476. if (isalpha(c) || c == '_' || c == '.' || c == '$' || c == '\'' ||
  1477. c == '"')
  1478. add_symbol();
  1479. else if (c == '{' || c == '=' || c == '[')
  1480. copy_action();
  1481. else if (c == '|') {
  1482. end_rule();
  1483. start_rule(plhs[nrules-1], 0);
  1484. ++cptr; }
  1485. else if (c == '%') {
  1486. if (mark_symbol()) break; }
  1487. else
  1488. syntax_error(lineno, line, cptr); }
  1489. end_rule();
  1490. if (goal->args > 0)
  1491. error(0, 0, 0, "start symbol %s requires arguments", goal->name);
  1492. }
  1493. void free_tags()
  1494. {
  1495. register int i;
  1496. if (tag_table == 0) return;
  1497. for (i = 0; i < ntags; ++i) {
  1498. assert(tag_table[i]);
  1499. FREE(tag_table[i]); }
  1500. FREE(tag_table);
  1501. }
  1502. void pack_names()
  1503. {
  1504. register bucket *bp;
  1505. register char *p, *s, *t;
  1506. name_pool_size = 13; /* 13 == sizeof("$end") + sizeof("$accept") */
  1507. for (bp = first_symbol; bp; bp = bp->next)
  1508. name_pool_size += strlen(bp->name) + 1;
  1509. name_pool = MALLOC(name_pool_size);
  1510. if (name_pool == 0) no_space();
  1511. strcpy(name_pool, "$accept");
  1512. strcpy(name_pool+8, "$end");
  1513. t = name_pool + 13;
  1514. for (bp = first_symbol; bp; bp = bp->next) {
  1515. p = t;
  1516. s = bp->name;
  1517. while ((*t++ = *s++)) continue;
  1518. FREE(bp->name);
  1519. bp->name = p; }
  1520. }
  1521. void check_symbols()
  1522. {
  1523. register bucket *bp;
  1524. if (goal->class == UNKNOWN)
  1525. undefined_goal(goal->name);
  1526. for (bp = first_symbol; bp; bp = bp->next) {
  1527. if (bp->class == UNKNOWN) {
  1528. undefined_symbol_warning(bp->name);
  1529. bp->class = TERM; } }
  1530. }
  1531. void pack_symbols()
  1532. {
  1533. register bucket *bp;
  1534. register bucket **v;
  1535. register int i, j, k, n;
  1536. nsyms = 2;
  1537. ntokens = 1;
  1538. for (bp = first_symbol; bp; bp = bp->next) {
  1539. ++nsyms;
  1540. if (bp->class == TERM) ++ntokens; }
  1541. start_symbol = ntokens;
  1542. nvars = nsyms - ntokens;
  1543. symbol_name = NEW2(nsyms, char *);
  1544. if (symbol_name == 0) no_space();
  1545. symbol_value = NEW2(nsyms, Yshort);
  1546. if (symbol_value == 0) no_space();
  1547. symbol_prec = NEW2(nsyms, Yshort);
  1548. if (symbol_prec == 0) no_space();
  1549. symbol_assoc = MALLOC(nsyms);
  1550. if (symbol_assoc == 0) no_space();
  1551. v = NEW2(nsyms, bucket *);
  1552. if (v == 0) no_space();
  1553. v[0] = 0;
  1554. v[start_symbol] = 0;
  1555. i = 1;
  1556. j = start_symbol + 1;
  1557. for (bp = first_symbol; bp; bp = bp->next)
  1558. {
  1559. if (bp->class == TERM)
  1560. v[i++] = bp;
  1561. else
  1562. v[j++] = bp;
  1563. }
  1564. assert(i == ntokens && j == nsyms);
  1565. for (i = 1; i < ntokens; ++i)
  1566. v[i]->index = i;
  1567. goal->index = start_symbol + 1;
  1568. k = start_symbol + 2;
  1569. while (++i < nsyms)
  1570. if (v[i] != goal) {
  1571. v[i]->index = k;
  1572. ++k; }
  1573. goal->value = 0;
  1574. k = 1;
  1575. for (i = start_symbol + 1; i < nsyms; ++i) {
  1576. if (v[i] != goal) {
  1577. v[i]->value = k;
  1578. ++k; } }
  1579. k = 0;
  1580. for (i = 1; i < ntokens; ++i) {
  1581. n = v[i]->value;
  1582. if (n > 256) {
  1583. for (j = k++; j > 0 && symbol_value[j-1] > n; --j)
  1584. symbol_value[j] = symbol_value[j-1];
  1585. symbol_value[j] = n; } }
  1586. if (v[1]->value == UNDEFINED)
  1587. v[1]->value = 256;
  1588. j = 0;
  1589. n = 257;
  1590. for (i = 2; i < ntokens; ++i) {
  1591. if (v[i]->value == UNDEFINED) {
  1592. while (j < k && n == symbol_value[j]) {
  1593. while (++j < k && n == symbol_value[j]) continue;
  1594. ++n; }
  1595. v[i]->value = n;
  1596. ++n; } }
  1597. symbol_name[0] = name_pool + 8;
  1598. symbol_value[0] = 0;
  1599. symbol_prec[0] = 0;
  1600. symbol_assoc[0] = TOKEN;
  1601. for (i = 1; i < ntokens; ++i) {
  1602. symbol_name[i] = v[i]->name;
  1603. symbol_value[i] = v[i]->value;
  1604. symbol_prec[i] = v[i]->prec;
  1605. symbol_assoc[i] = v[i]->assoc; }
  1606. symbol_name[start_symbol] = name_pool;
  1607. symbol_value[start_symbol] = -1;
  1608. symbol_prec[start_symbol] = 0;
  1609. symbol_assoc[start_symbol] = TOKEN;
  1610. for (++i; i < nsyms; ++i) {
  1611. k = v[i]->index;
  1612. symbol_name[k] = v[i]->name;
  1613. symbol_value[k] = v[i]->value;
  1614. symbol_prec[k] = v[i]->prec;
  1615. symbol_assoc[k] = v[i]->assoc; }
  1616. FREE(v);
  1617. }
  1618. void pack_grammar()
  1619. {
  1620. register int i, j;
  1621. int assoc, prec;
  1622. ritem = NEW2(nitems, Yshort);
  1623. if (ritem == 0) no_space();
  1624. rlhs = NEW2(nrules, Yshort);
  1625. if (rlhs == 0) no_space();
  1626. rrhs = NEW2(nrules+1, Yshort);
  1627. if (rrhs == 0) no_space();
  1628. rprec = RENEW(rprec, nrules, Yshort);
  1629. if (rprec == 0) no_space();
  1630. rassoc = RENEW(rassoc, nrules, char);
  1631. if (rassoc == 0) no_space();
  1632. ritem[0] = -1;
  1633. ritem[1] = goal->index;
  1634. ritem[2] = 0;
  1635. ritem[3] = -2;
  1636. rlhs[0] = 0;
  1637. rlhs[1] = 0;
  1638. rlhs[2] = start_symbol;
  1639. rrhs[0] = 0;
  1640. rrhs[1] = 0;
  1641. rrhs[2] = 1;
  1642. j = 4;
  1643. for (i = 3; i < nrules; ++i) {
  1644. if (plhs[i]->args > 0) {
  1645. if (plhs[i]->argnames) {
  1646. FREE(plhs[i]->argnames);
  1647. plhs[i]->argnames = 0;
  1648. }
  1649. if (plhs[i]->argtags) {
  1650. FREE(plhs[i]->argtags);
  1651. plhs[i]->argtags = 0;
  1652. }
  1653. }
  1654. rlhs[i] = plhs[i]->index;
  1655. rrhs[i] = j;
  1656. assoc = TOKEN;
  1657. prec = 0;
  1658. while (pitem[j]) {
  1659. ritem[j] = pitem[j]->index;
  1660. if (pitem[j]->class == TERM) {
  1661. prec = pitem[j]->prec;
  1662. assoc = pitem[j]->assoc; }
  1663. ++j; }
  1664. ritem[j] = -i;
  1665. ++j;
  1666. if (rprec[i] == UNDEFINED) {
  1667. rprec[i] = prec;
  1668. rassoc[i] = assoc; } }
  1669. rrhs[i] = j;
  1670. FREE(plhs);
  1671. FREE(pitem);
  1672. clean_arg_cache();
  1673. }
  1674. void print_grammar()
  1675. {
  1676. register int i, j, k;
  1677. int spacing = 0;
  1678. register FILE *f = verbose_file;
  1679. if (!vflag) return;
  1680. k = 1;
  1681. for (i = 2; i < nrules; ++i) {
  1682. if (rlhs[i] != rlhs[i-1]) {
  1683. if (i != 2) fprintf(f, "\n");
  1684. fprintf(f, "%4d %s :", i - 2, symbol_name[rlhs[i]]);
  1685. spacing = strlen(symbol_name[rlhs[i]]) + 1; }
  1686. else {
  1687. fprintf(f, "%4d ", i - 2);
  1688. j = spacing;
  1689. while (--j >= 0) putc(' ', f);
  1690. putc('|', f); }
  1691. while (ritem[k] >= 0) {
  1692. fprintf(f, " %s", symbol_name[ritem[k]]);
  1693. ++k; }
  1694. ++k;
  1695. putc('\n', f); }
  1696. }
  1697. extern int read_errs;
  1698. void reader() {
  1699. write_section("banner");
  1700. create_symbol_table();
  1701. read_declarations();
  1702. read_grammar();
  1703. if(read_errs) done(1);
  1704. free_symbol_table();
  1705. free_tags();
  1706. pack_names();
  1707. check_symbols();
  1708. pack_symbols();
  1709. pack_grammar();
  1710. free_symbols();
  1711. print_grammar();
  1712. }