yaccpar 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #define YYFLAG -1000
  2. #define yyclearin yychar = -1
  3. #define yyerrok yyerrflag = 0
  4. #ifdef yydebug
  5. #include "y.debug"
  6. #else
  7. #define yydebug 0
  8. char* yytoknames[1]; /* for debugging */
  9. char* yystates[1]; /* for debugging */
  10. #endif
  11. /* parser for yacc output */
  12. int yynerrs = 0; /* number of errors */
  13. int yyerrflag = 0; /* error recovery flag */
  14. extern int fprint(int, char*, ...);
  15. extern int sprint(char*, char*, ...);
  16. char*
  17. yytokname(int yyc)
  18. {
  19. static char x[16];
  20. if(yyc > 0 && yyc <= sizeof(yytoknames)/sizeof(yytoknames[0]))
  21. if(yytoknames[yyc-1])
  22. return yytoknames[yyc-1];
  23. sprint(x, "<%d>", yyc);
  24. return x;
  25. }
  26. char*
  27. yystatname(int yys)
  28. {
  29. static char x[16];
  30. if(yys >= 0 && yys < sizeof(yystates)/sizeof(yystates[0]))
  31. if(yystates[yys])
  32. return yystates[yys];
  33. sprint(x, "<%d>\n", yys);
  34. return x;
  35. }
  36. long
  37. yylex1(void)
  38. {
  39. long yychar;
  40. long *t3p;
  41. int c;
  42. yychar = yylex();
  43. if(yychar <= 0) {
  44. c = yytok1[0];
  45. goto out;
  46. }
  47. if(yychar < sizeof(yytok1)/sizeof(yytok1[0])) {
  48. c = yytok1[yychar];
  49. goto out;
  50. }
  51. if(yychar >= YYPRIVATE)
  52. if(yychar < YYPRIVATE+sizeof(yytok2)/sizeof(yytok2[0])) {
  53. c = yytok2[yychar-YYPRIVATE];
  54. goto out;
  55. }
  56. for(t3p=yytok3;; t3p+=2) {
  57. c = t3p[0];
  58. if(c == yychar) {
  59. c = t3p[1];
  60. goto out;
  61. }
  62. if(c == 0)
  63. break;
  64. }
  65. c = 0;
  66. out:
  67. if(c == 0)
  68. c = yytok2[1]; /* unknown char */
  69. if(yydebug >= 3)
  70. fprint(2, "lex %.4lux %s\n", yychar, yytokname(c));
  71. return c;
  72. }
  73. int
  74. yyparse(void)
  75. {
  76. struct
  77. {
  78. YYSTYPE yyv;
  79. int yys;
  80. } yys[YYMAXDEPTH], *yyp, *yypt;
  81. short *yyxi;
  82. int yyj, yym, yystate, yyn, yyg;
  83. long yychar;
  84. YYSTYPE save1, save2;
  85. int save3, save4;
  86. save1 = yylval;
  87. save2 = yyval;
  88. save3 = yynerrs;
  89. save4 = yyerrflag;
  90. yystate = 0;
  91. yychar = -1;
  92. yynerrs = 0;
  93. yyerrflag = 0;
  94. yyp = &yys[-1];
  95. goto yystack;
  96. ret0:
  97. yyn = 0;
  98. goto ret;
  99. ret1:
  100. yyn = 1;
  101. goto ret;
  102. ret:
  103. yylval = save1;
  104. yyval = save2;
  105. yynerrs = save3;
  106. yyerrflag = save4;
  107. return yyn;
  108. yystack:
  109. /* put a state and value onto the stack */
  110. if(yydebug >= 4)
  111. fprint(2, "char %s in %s", yytokname(yychar), yystatname(yystate));
  112. yyp++;
  113. if(yyp >= &yys[YYMAXDEPTH]) {
  114. yyerror("yacc stack overflow");
  115. goto ret1;
  116. }
  117. yyp->yys = yystate;
  118. yyp->yyv = yyval;
  119. yynewstate:
  120. yyn = yypact[yystate];
  121. if(yyn <= YYFLAG)
  122. goto yydefault; /* simple state */
  123. if(yychar < 0)
  124. yychar = yylex1();
  125. yyn += yychar;
  126. if(yyn < 0 || yyn >= YYLAST)
  127. goto yydefault;
  128. yyn = yyact[yyn];
  129. if(yychk[yyn] == yychar) { /* valid shift */
  130. yychar = -1;
  131. yyval = yylval;
  132. yystate = yyn;
  133. if(yyerrflag > 0)
  134. yyerrflag--;
  135. goto yystack;
  136. }
  137. yydefault:
  138. /* default state action */
  139. yyn = yydef[yystate];
  140. if(yyn == -2) {
  141. if(yychar < 0)
  142. yychar = yylex1();
  143. /* look through exception table */
  144. for(yyxi=yyexca;; yyxi+=2)
  145. if(yyxi[0] == -1 && yyxi[1] == yystate)
  146. break;
  147. for(yyxi += 2;; yyxi += 2) {
  148. yyn = yyxi[0];
  149. if(yyn < 0 || yyn == yychar)
  150. break;
  151. }
  152. yyn = yyxi[1];
  153. if(yyn < 0)
  154. goto ret0;
  155. }
  156. if(yyn == 0) {
  157. /* error ... attempt to resume parsing */
  158. switch(yyerrflag) {
  159. case 0: /* brand new error */
  160. yyerror("syntax error");
  161. yynerrs++;
  162. if(yydebug >= 1) {
  163. fprint(2, "%s", yystatname(yystate));
  164. fprint(2, "saw %s\n", yytokname(yychar));
  165. }
  166. case 1:
  167. case 2: /* incompletely recovered error ... try again */
  168. yyerrflag = 3;
  169. /* find a state where "error" is a legal shift action */
  170. while(yyp >= yys) {
  171. yyn = yypact[yyp->yys] + YYERRCODE;
  172. if(yyn >= 0 && yyn < YYLAST) {
  173. yystate = yyact[yyn]; /* simulate a shift of "error" */
  174. if(yychk[yystate] == YYERRCODE)
  175. goto yystack;
  176. }
  177. /* the current yyp has no shift onn "error", pop stack */
  178. if(yydebug >= 2)
  179. fprint(2, "error recovery pops state %d, uncovers %d\n",
  180. yyp->yys, (yyp-1)->yys );
  181. yyp--;
  182. }
  183. /* there is no state on the stack with an error shift ... abort */
  184. goto ret1;
  185. case 3: /* no shift yet; clobber input char */
  186. if(yydebug >= 2)
  187. fprint(2, "error recovery discards %s\n", yytokname(yychar));
  188. if(yychar == YYEOFCODE)
  189. goto ret1;
  190. yychar = -1;
  191. goto yynewstate; /* try again in the same state */
  192. }
  193. }
  194. /* reduction by production yyn */
  195. if(yydebug >= 2)
  196. fprint(2, "reduce %d in:\n\t%s", yyn, yystatname(yystate));
  197. yypt = yyp;
  198. yyp -= yyr2[yyn];
  199. yyval = (yyp+1)->yyv;
  200. yym = yyn;
  201. /* consult goto table to find next state */
  202. yyn = yyr1[yyn];
  203. yyg = yypgo[yyn];
  204. yyj = yyg + yyp->yys + 1;
  205. if(yyj >= YYLAST || yychk[yystate=yyact[yyj]] != -yyn)
  206. yystate = yyact[yyg];
  207. switch(yym) {
  208. $A
  209. }
  210. goto yystack; /* stack new state and value */
  211. }