yaccpars 4.6 KB

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