yaccpars 4.5 KB

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