yacc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. # define ID 257
  2. # define CON 258
  3. # define UNARYMINUS 259
  4. #define yyclearin yychar = -1
  5. #define yyerrok yyerrflag = 0
  6. extern int yychar;
  7. extern short yyerrflag;
  8. #ifndef YYMAXDEPTH
  9. #define YYMAXDEPTH 150
  10. #endif
  11. #ifndef YYSTYPE
  12. #define YYSTYPE int
  13. #endif
  14. YYSTYPE yylval, yyval;
  15. # define YYERRCODE 256
  16. #include <stdio.h>
  17. # define U(x) x
  18. # define NLSTATE yyprevious=YYNEWLINE
  19. # define BEGIN yybgin = yysvec + 1 +
  20. # define INITIAL 0
  21. # define YYLERR yysvec
  22. # define YYSTATE (yyestate-yysvec-1)
  23. # define YYOPTIM 1
  24. # define YYLMAX 200
  25. # define output(c) (void)putc(c,yyout)
  26. # define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==10?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)
  27. # define unput(c) {yytchar= (c);if(yytchar=='\n')yylineno--;*yysptr++=yytchar;}
  28. # define yymore() (yymorfg=1)
  29. # define ECHO fprintf(yyout, "%s",yytext)
  30. # define REJECT { nstr = yyreject(); goto yyfussy;}
  31. int yyleng; extern char yytext[];
  32. int yymorfg;
  33. extern char *yysptr, yysbuf[];
  34. int yytchar;
  35. FILE *yyin ={stdin}, *yyout ={stdout};
  36. extern int yylineno;
  37. struct yysvf {
  38. struct yywork *yystoff;
  39. struct yysvf *yyother;
  40. int *yystops;};
  41. struct yysvf *yyestate;
  42. extern struct yysvf yysvec[], *yybgin;
  43. # define YYNEWLINE 10
  44. yylex(){
  45. int nstr; extern int yyprevious;
  46. while((nstr = yylook()) >= 0)
  47. yyfussy: switch(nstr){
  48. case 0:
  49. if(yywrap()) return(0); break;
  50. case 1:
  51. return ID;
  52. break;
  53. case 2:
  54. return CON;
  55. break;
  56. case 3:
  57. ;
  58. break;
  59. case 4:
  60. return yytext[0];
  61. break;
  62. case -1:
  63. break;
  64. default:
  65. fprintf(yyout,"bad switch yylook %d",nstr);
  66. } return(0); }
  67. /* end of yylex */
  68. int yyvstop[] ={
  69. 0,
  70. 4,
  71. 0,
  72. 3,
  73. 4,
  74. 0,
  75. 2,
  76. 4,
  77. 0,
  78. 1,
  79. 4,
  80. 0,
  81. 2,
  82. 0,
  83. 1,
  84. 0,
  85. 0};
  86. # define YYTYPE char
  87. struct yywork { YYTYPE verify, advance; } yycrank[] ={
  88. 0,0, 0,0, 1,3, 0,0,
  89. 0,0, 0,0, 0,0, 0,0,
  90. 0,0, 0,0, 1,4, 1,3,
  91. 0,0, 0,0, 0,0, 0,0,
  92. 0,0, 0,0, 0,0, 0,0,
  93. 0,0, 0,0, 0,0, 0,0,
  94. 0,0, 0,0, 0,0, 0,0,
  95. 0,0, 0,0, 0,0, 0,0,
  96. 0,0, 0,0, 0,0, 0,0,
  97. 0,0, 0,0, 0,0, 0,0,
  98. 0,0, 0,0, 0,0, 0,0,
  99. 0,0, 0,0, 0,0, 0,0,
  100. 0,0, 1,5, 5,7, 5,7,
  101. 5,7, 5,7, 5,7, 5,7,
  102. 5,7, 5,7, 5,7, 5,7,
  103. 0,0, 0,0, 0,0, 0,0,
  104. 0,0, 0,0, 1,6, 6,8,
  105. 6,8, 6,8, 6,8, 6,8,
  106. 6,8, 6,8, 6,8, 6,8,
  107. 6,8, 0,0, 0,0, 0,0,
  108. 0,0, 0,0, 0,0, 0,0,
  109. 6,8, 6,8, 6,8, 6,8,
  110. 6,8, 6,8, 6,8, 6,8,
  111. 6,8, 6,8, 6,8, 6,8,
  112. 6,8, 6,8, 6,8, 6,8,
  113. 6,8, 6,8, 6,8, 6,8,
  114. 6,8, 6,8, 6,8, 6,8,
  115. 6,8, 6,8, 0,0, 0,0,
  116. 0,0, 0,0, 6,8, 0,0,
  117. 6,8, 6,8, 6,8, 6,8,
  118. 6,8, 6,8, 6,8, 6,8,
  119. 6,8, 6,8, 6,8, 6,8,
  120. 6,8, 6,8, 6,8, 6,8,
  121. 6,8, 6,8, 6,8, 6,8,
  122. 6,8, 6,8, 6,8, 6,8,
  123. 6,8, 6,8, 0,0, 0,0,
  124. 0,0};
  125. struct yysvf yysvec[] ={
  126. 0, 0, 0,
  127. yycrank+-1, 0, 0,
  128. yycrank+0, yysvec+1, 0,
  129. yycrank+0, 0, yyvstop+1,
  130. yycrank+0, 0, yyvstop+3,
  131. yycrank+2, 0, yyvstop+6,
  132. yycrank+19, 0, yyvstop+9,
  133. yycrank+0, yysvec+5, yyvstop+12,
  134. yycrank+0, yysvec+6, yyvstop+14,
  135. 0, 0, 0};
  136. struct yywork *yytop = yycrank+141;
  137. struct yysvf *yybgin = yysvec+1;
  138. char yymatch[] ={
  139. 00 ,01 ,01 ,01 ,01 ,01 ,01 ,01 ,
  140. 01 ,011 ,012 ,01 ,01 ,01 ,01 ,01 ,
  141. 01 ,01 ,01 ,01 ,01 ,01 ,01 ,01 ,
  142. 01 ,01 ,01 ,01 ,01 ,01 ,01 ,01 ,
  143. 011 ,01 ,01 ,01 ,01 ,01 ,01 ,01 ,
  144. 01 ,01 ,01 ,01 ,01 ,01 ,01 ,01 ,
  145. '0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,
  146. '0' ,'0' ,01 ,01 ,01 ,01 ,01 ,01 ,
  147. 01 ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,
  148. 'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,
  149. 'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,
  150. 'A' ,'A' ,'A' ,01 ,01 ,01 ,01 ,'A' ,
  151. 01 ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,
  152. 'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,
  153. 'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,
  154. 'A' ,'A' ,'A' ,01 ,01 ,01 ,01 ,01 ,
  155. 0};
  156. char yyextra[] ={
  157. 0,0,0,0,0,0,0,0,
  158. 0};
  159. /* ncform 4.1 83/08/11 */
  160. int yylineno =1;
  161. # define YYU(x) x
  162. # define NLSTATE yyprevious=YYNEWLINE
  163. char yytext[YYLMAX];
  164. struct yysvf *yylstate [YYLMAX], **yylsp, **yyolsp;
  165. char yysbuf[YYLMAX];
  166. char *yysptr = yysbuf;
  167. int *yyfnd;
  168. extern struct yysvf *yyestate;
  169. int yyprevious = YYNEWLINE;
  170. yylook(){
  171. register struct yysvf *yystate, **lsp;
  172. register struct yywork *yyt;
  173. struct yysvf *yyz;
  174. int yych;
  175. struct yywork *yyr;
  176. # ifdef LEXDEBUG
  177. int debug;
  178. # endif
  179. char *yylastch;
  180. /* start off machines */
  181. # ifdef LEXDEBUG
  182. debug = 0;
  183. # endif
  184. if (!yymorfg)
  185. yylastch = yytext;
  186. else {
  187. yymorfg=0;
  188. yylastch = yytext+yyleng;
  189. }
  190. for(;;){
  191. lsp = yylstate;
  192. yyestate = yystate = yybgin;
  193. if (yyprevious==YYNEWLINE) yystate++;
  194. for (;;){
  195. # ifdef LEXDEBUG
  196. if(debug)fprintf(yyout,"state %d\n",yystate-yysvec-1);
  197. # endif
  198. yyt = yystate->yystoff;
  199. if(yyt == yycrank){ /* may not be any transitions */
  200. yyz = yystate->yyother;
  201. if(yyz == 0)break;
  202. if(yyz->yystoff == yycrank)break;
  203. }
  204. *yylastch++ = yych = input();
  205. tryagain:
  206. # ifdef LEXDEBUG
  207. if(debug){
  208. fprintf(yyout,"char ");
  209. allprint(yych);
  210. putchar('\n');
  211. }
  212. # endif
  213. yyr = yyt;
  214. if ( yyt > yycrank){
  215. yyt = yyr + yych;
  216. if (yyt <= yytop && yyt->verify+yysvec == yystate){
  217. if(yyt->advance+yysvec == YYLERR) /* error transitions */
  218. {unput(*--yylastch);break;}
  219. *lsp++ = yystate = yyt->advance+yysvec;
  220. goto contin;
  221. }
  222. }
  223. # ifdef YYOPTIM
  224. else if(yyt < yycrank) { /* r < yycrank */
  225. yyt = yyr = yycrank+(yycrank-yyt);
  226. # ifdef LEXDEBUG
  227. if(debug)fprintf(yyout,"compressed state\n");
  228. # endif
  229. yyt = yyt + yych;
  230. if(yyt <= yytop && yyt->verify+yysvec == yystate){
  231. if(yyt->advance+yysvec == YYLERR) /* error transitions */
  232. {unput(*--yylastch);break;}
  233. *lsp++ = yystate = yyt->advance+yysvec;
  234. goto contin;
  235. }
  236. yyt = yyr + YYU(yymatch[yych]);
  237. # ifdef LEXDEBUG
  238. if(debug){
  239. fprintf(yyout,"try fall back character ");
  240. allprint(YYU(yymatch[yych]));
  241. putchar('\n');
  242. }
  243. # endif
  244. if(yyt <= yytop && yyt->verify+yysvec == yystate){
  245. if(yyt->advance+yysvec == YYLERR) /* error transition */
  246. {unput(*--yylastch);break;}
  247. *lsp++ = yystate = yyt->advance+yysvec;
  248. goto contin;
  249. }
  250. }
  251. if ((yystate = yystate->yyother) && (yyt= yystate->yystoff) != yycrank){
  252. # ifdef LEXDEBUG
  253. if(debug)fprintf(yyout,"fall back to state %d\n",yystate-yysvec-1);
  254. # endif
  255. goto tryagain;
  256. }
  257. # endif
  258. else
  259. {unput(*--yylastch);break;}
  260. contin:
  261. # ifdef LEXDEBUG
  262. if(debug){
  263. fprintf(yyout,"state %d char ",yystate-yysvec-1);
  264. allprint(yych);
  265. putchar('\n');
  266. }
  267. # endif
  268. ;
  269. }
  270. # ifdef LEXDEBUG
  271. if(debug){
  272. fprintf(yyout,"stopped at %d with ",*(lsp-1)-yysvec-1);
  273. allprint(yych);
  274. putchar('\n');
  275. }
  276. # endif
  277. while (lsp-- > yylstate){
  278. *yylastch-- = 0;
  279. if (*lsp != 0 && (yyfnd= (*lsp)->yystops) && *yyfnd > 0){
  280. yyolsp = lsp;
  281. if(yyextra[*yyfnd]){ /* must backup */
  282. while(yyback((*lsp)->yystops,-*yyfnd) != 1 && lsp > yylstate){
  283. lsp--;
  284. unput(*yylastch--);
  285. }
  286. }
  287. yyprevious = YYU(*yylastch);
  288. yylsp = lsp;
  289. yyleng = yylastch-yytext+1;
  290. yytext[yyleng] = 0;
  291. # ifdef LEXDEBUG
  292. if(debug){
  293. fprintf(yyout,"\nmatch ");
  294. sprint(yytext);
  295. fprintf(yyout," action %d\n",*yyfnd);
  296. }
  297. # endif
  298. return(*yyfnd++);
  299. }
  300. unput(*yylastch);
  301. }
  302. if (yytext[0] == 0 /* && feof(yyin) */)
  303. {
  304. yysptr=yysbuf;
  305. return(0);
  306. }
  307. yyprevious = yytext[0] = input();
  308. if (yyprevious>0)
  309. output(yyprevious);
  310. yylastch=yytext;
  311. # ifdef LEXDEBUG
  312. if(debug)putchar('\n');
  313. # endif
  314. }
  315. }
  316. yyback(p, m)
  317. int *p;
  318. {
  319. if (p==0) return(0);
  320. while (*p)
  321. {
  322. if (*p++ == m)
  323. return(1);
  324. }
  325. return(0);
  326. }
  327. /* the following are only used in the lex library */
  328. yyinput(){
  329. return(input());
  330. }
  331. yyoutput(c)
  332. int c; {
  333. output(c);
  334. }
  335. yyunput(c)
  336. int c; {
  337. unput(c);
  338. }
  339. main() {
  340. yyparse();
  341. return 0;
  342. }
  343. /* yyerror - issue error message */
  344. yyerror(s) char *s; {
  345. printf("%s\n", s);
  346. }
  347. short yyexca[] ={
  348. -1, 1,
  349. 0, -1,
  350. -2, 0,
  351. };
  352. # define YYNPROD 15
  353. # define YYLAST 249
  354. short yyact[]={
  355. 12, 2, 9, 8, 17, 11, 25, 17, 15, 18,
  356. 16, 10, 18, 17, 15, 7, 16, 13, 18, 5,
  357. 3, 1, 0, 19, 20, 0, 0, 21, 22, 23,
  358. 24, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  359. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  360. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  361. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  362. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  363. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  364. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  365. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  366. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  367. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  368. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  369. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  370. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  371. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  372. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  373. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  374. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  375. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  376. 0, 0, 0, 0, 0, 0, 0, 6, 14, 0,
  377. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  378. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  379. 0, 0, 0, 0, 0, 0, 0, 4, 6 };
  380. short yypact[]={
  381. -1000, -9,-1000, 5, -7, -59,-1000,-1000,-1000, -40,
  382. -29, -40, -40,-1000,-1000, -40, -40, -40, -40, -38,
  383. -35, -38, -38,-1000,-1000,-1000 };
  384. short yypgo[]={
  385. 0, 21, 20, 17, 11 };
  386. short yyr1[]={
  387. 0, 1, 1, 1, 1, 2, 4, 4, 4, 4,
  388. 4, 4, 4, 4, 3 };
  389. short yyr2[]={
  390. 0, 0, 2, 3, 3, 3, 3, 3, 3, 3,
  391. 2, 3, 1, 1, 1 };
  392. short yychk[]={
  393. -1000, -1, 10, -2, 256, -3, 257, 10, 10, 61,
  394. -4, 45, 40, -3, 258, 43, 45, 42, 47, -4,
  395. -4, -4, -4, -4, -4, 41 };
  396. short yydef[]={
  397. 1, -2, 2, 0, 0, 0, 14, 3, 4, 0,
  398. 5, 0, 0, 12, 13, 0, 0, 0, 0, 10,
  399. 0, 6, 7, 8, 9, 11 };
  400. #ifndef lint
  401. #endif
  402. # define YYFLAG -1000
  403. # define YYERROR goto yyerrlab
  404. # define YYACCEPT return(0)
  405. # define YYABORT return(1)
  406. /* parser for yacc output */
  407. #ifdef YYDEBUG
  408. int yydebug = 0; /* 1 for debugging */
  409. #endif
  410. YYSTYPE yyv[YYMAXDEPTH]; /* where the values are stored */
  411. int yychar = -1; /* current input token number */
  412. int yynerrs = 0; /* number of errors */
  413. short yyerrflag = 0; /* error recovery flag */
  414. yyparse() {
  415. short yys[YYMAXDEPTH];
  416. short yyj, yym;
  417. register YYSTYPE *yypvt;
  418. register short yystate, *yyps, yyn;
  419. register YYSTYPE *yypv;
  420. register short *yyxi;
  421. yystate = 0;
  422. yychar = -1;
  423. yynerrs = 0;
  424. yyerrflag = 0;
  425. yyps= &yys[-1];
  426. yypv= &yyv[-1];
  427. yystack: /* put a state and value onto the stack */
  428. #ifdef YYDEBUG
  429. if( yydebug ) printf( "state %d, char 0%o\n", yystate, yychar );
  430. #endif
  431. if( ++yyps> &yys[YYMAXDEPTH] ) { yyerror( "yacc stack overflow" ); return(1); }
  432. *yyps = yystate;
  433. ++yypv;
  434. *yypv = yyval;
  435. yynewstate:
  436. yyn = yypact[yystate];
  437. if( yyn<= YYFLAG ) goto yydefault; /* simple state */
  438. if( yychar<0 ) if( (yychar=yylex())<0 ) yychar=0;
  439. if( (yyn += yychar)<0 || yyn >= YYLAST ) goto yydefault;
  440. if( yychk[ yyn=yyact[ yyn ] ] == yychar ){ /* valid shift */
  441. yychar = -1;
  442. yyval = yylval;
  443. yystate = yyn;
  444. if( yyerrflag > 0 ) --yyerrflag;
  445. goto yystack;
  446. }
  447. yydefault:
  448. /* default state action */
  449. if( (yyn=yydef[yystate]) == -2 ) {
  450. if( yychar<0 ) if( (yychar=yylex())<0 ) yychar = 0;
  451. /* look through exception table */
  452. for( yyxi=yyexca; (*yyxi!= (-1)) || (yyxi[1]!=yystate) ; yyxi += 2 ) ; /* VOID */
  453. while( *(yyxi+=2) >= 0 ){
  454. if( *yyxi == yychar ) break;
  455. }
  456. if( (yyn = yyxi[1]) < 0 ) return(0); /* accept */
  457. }
  458. if( yyn == 0 ){ /* error */
  459. /* error ... attempt to resume parsing */
  460. switch( yyerrflag ){
  461. case 0: /* brand new error */
  462. yyerror( "syntax error" );
  463. yyerrlab:
  464. ++yynerrs;
  465. case 1:
  466. case 2: /* incompletely recovered error ... try again */
  467. yyerrflag = 3;
  468. /* find a state where "error" is a legal shift action */
  469. while ( yyps >= yys ) {
  470. yyn = yypact[*yyps] + YYERRCODE;
  471. if( yyn>= 0 && yyn < YYLAST && yychk[yyact[yyn]] == YYERRCODE ){
  472. yystate = yyact[yyn]; /* simulate a shift of "error" */
  473. goto yystack;
  474. }
  475. yyn = yypact[*yyps];
  476. /* the current yyps has no shift onn "error", pop stack */
  477. #ifdef YYDEBUG
  478. if( yydebug ) printf( "error recovery pops state %d, uncovers %d\n", *yyps, yyps[-1] );
  479. #endif
  480. --yyps;
  481. --yypv;
  482. }
  483. /* there is no state on the stack with an error shift ... abort */
  484. yyabort:
  485. return(1);
  486. case 3: /* no shift yet; clobber input char */
  487. #ifdef YYDEBUG
  488. if( yydebug ) printf( "error recovery discards char %d\n", yychar );
  489. #endif
  490. if( yychar == 0 ) goto yyabort; /* don't discard EOF, quit */
  491. yychar = -1;
  492. goto yynewstate; /* try again in the same state */
  493. }
  494. }
  495. /* reduction by production yyn */
  496. #ifdef YYDEBUG
  497. if( yydebug ) printf("reduce %d\n",yyn);
  498. #endif
  499. yyps -= yyr2[yyn];
  500. yypvt = yypv;
  501. yypv -= yyr2[yyn];
  502. yyval = yypv[1];
  503. yym=yyn;
  504. /* consult goto table to find next state */
  505. yyn = yyr1[yyn];
  506. yyj = yypgo[yyn] + *yyps + 1;
  507. if( yyj>=YYLAST || yychk[ yystate = yyact[yyj] ] != -yyn ) yystate = yyact[yypgo[yyn]];
  508. switch(yym){
  509. case 4:
  510. { yyerrok; } break;
  511. case 5:
  512. { printf("store\n"); } break;
  513. case 6:
  514. { printf("add\n"); } break;
  515. case 7:
  516. { printf("negate\nadd\n"); } break;
  517. case 8:
  518. { printf("multiply\n"); } break;
  519. case 9:
  520. { printf("divide\n"); } break;
  521. case 10:
  522. { printf("negate\n"); } break;
  523. case 12:
  524. { printf("load\n"); } break;
  525. case 13:
  526. { printf("push %s\n", yytext); } break;
  527. case 14:
  528. { printf("%s\n", yytext); } break;
  529. }
  530. goto yystack; /* stack new state and value */
  531. }
  532. int yywrap() { return 1; }