calc.tab.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /* A Bison-1.00 parser, made from calc.y */
  2. #define DIGIT 258
  3. #define LETTER 259
  4. #define EXIT 260
  5. #define UMINUS 261
  6. #line 1 "calc.y"
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <strings.h>
  10. #include <ctype.h>
  11. int regs[26];
  12. int base;
  13. int val;
  14. int printflag;
  15. /* forward declaration */
  16. void yyerror (char *s);
  17. #ifndef YYLTYPE
  18. typedef
  19. struct yyltype
  20. {
  21. int timestamp;
  22. int first_line;
  23. int first_column;
  24. int last_line;
  25. int last_column;
  26. char *text;
  27. }
  28. yyltype;
  29. #define YYLTYPE yyltype
  30. #endif
  31. #define YYACCEPT return(0)
  32. #define YYABORT return(1)
  33. #define YYERROR return(1)
  34. #ifndef YYSTYPE
  35. #define YYSTYPE int
  36. #endif
  37. #include <stdio.h>
  38. #ifndef __STDC__
  39. #define const
  40. #endif
  41. #define YYFINAL 31
  42. #define YYFLAG -32768
  43. #define YYNTBASE 17
  44. #define YYTRANSLATE(x) ((unsigned)(x) <= 261 ? yytranslate[x] : 20)
  45. static const char yytranslate[] = { 0,
  46. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  47. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  48. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  49. 2, 2, 2, 2, 2, 2, 12, 7, 2, 15,
  50. 16, 10, 8, 2, 9, 2, 11, 2, 2, 2,
  51. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  52. 14, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  53. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  54. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  55. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  56. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  57. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  58. 2, 2, 2, 6, 2, 2, 2, 2, 2, 2,
  59. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  60. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  61. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  62. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  63. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  64. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  65. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  66. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  67. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  68. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  69. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  70. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  71. 2, 2, 2, 2, 2, 1, 2, 3, 4, 5,
  72. 13
  73. };
  74. static const short yyrline[] = { 0,
  75. 30, 32, 34, 38, 40, 48, 50, 52, 54, 56,
  76. 58, 60, 62, 64, 69, 71
  77. };
  78. static const char * const yytname[] = { 0,
  79. "error","$illegal.","DIGIT","LETTER","EXIT","'|'","'&'","'+'","'-'","'*'",
  80. "'/'","'%'","UMINUS","'='","'('","')'","stmt"
  81. };
  82. static const short yyr1[] = { 0,
  83. 17, 17, 17, 18, 18, 18, 18, 18, 18, 18,
  84. 18, 18, 18, 18, 19, 19
  85. };
  86. static const short yyr2[] = { 0,
  87. 1, 3, 1, 3, 3, 3, 3, 3, 3, 3,
  88. 3, 2, 1, 1, 1, 2
  89. };
  90. static const short yydefact[] = { 0,
  91. 15, 13, 3, 0, 0, 1, 14, 0, 13, 12,
  92. 0, 0, 0, 0, 0, 0, 0, 0, 16, 2,
  93. 4, 10, 11, 5, 6, 7, 8, 9, 0, 0,
  94. 0
  95. };
  96. static const short yydefgoto[] = { 29,
  97. 6, 7
  98. };
  99. static const short yypact[] = { 12,
  100. -32768, -12,-32768, 15, 15, 36, 0, 15,-32768,-32768,
  101. 25, 15, 15, 15, 15, 15, 15, 15,-32768, 36,
  102. -32768, 42, 47, -5, -5,-32768,-32768,-32768, 20, 22,
  103. -32768
  104. };
  105. static const short yypgoto[] = {-32768,
  106. -4,-32768
  107. };
  108. #define YYLAST 59
  109. static const short yytable[] = { 10,
  110. 11, 8, 19, 20, 16, 17, 18, 22, 23, 24,
  111. 25, 26, 27, 28, 1, 2, 3, 1, 9, 30,
  112. 4, 31, 0, 4, 0, 0, 5, 0, 0, 5,
  113. 12, 13, 14, 15, 16, 17, 18, 0, 0, 0,
  114. 21, 12, 13, 14, 15, 16, 17, 18, 13, 14,
  115. 15, 16, 17, 18, 14, 15, 16, 17, 18
  116. };
  117. static const short yycheck[] = { 4,
  118. 5, 14, 3, 8, 10, 11, 12, 12, 13, 14,
  119. 15, 16, 17, 18, 3, 4, 5, 3, 4, 0,
  120. 9, 0, -1, 9, -1, -1, 15, -1, -1, 15,
  121. 6, 7, 8, 9, 10, 11, 12, -1, -1, -1,
  122. 16, 6, 7, 8, 9, 10, 11, 12, 7, 8,
  123. 9, 10, 11, 12, 8, 9, 10, 11, 12
  124. };
  125. #define YYPURE 1
  126. #line 2 "bison.simple"
  127. /* Skeleton output parser for bison-1.00
  128. copyright (C) 1984 Bob Corbett and Richard Stallman
  129. NO WARRANTY
  130. BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  131. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
  132. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  133. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  134. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  135. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  136. FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
  137. AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
  138. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  139. CORRECTION.
  140. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  141. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  142. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  143. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  144. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  145. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  146. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  147. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  148. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  149. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  150. GENERAL PUBLIC LICENSE TO COPY
  151. 1. You may copy and distribute verbatim copies of this source file
  152. as you receive it, in any medium, provided that you conspicuously and
  153. appropriately publish on each copy a valid copyright notice "Copyright
  154. (C) 1985 Free Software Foundation, Inc."; and include following the
  155. copyright notice a verbatim copy of the above disclaimer of warranty
  156. and of this License. You may charge a distribution fee for the
  157. physical act of transferring a copy.
  158. 2. You may modify your copy or copies of this source file or
  159. any portion of it, and copy and distribute such modifications under
  160. the terms of Paragraph 1 above, provided that you also do the following:
  161. a) cause the modified files to carry prominent notices stating
  162. that you changed the files and the date of any change; and
  163. b) cause the whole of any work that you distribute or publish,
  164. that in whole or in part contains or is a derivative of this
  165. program or any part thereof, to be licensed at no charge to all
  166. third parties on terms identical to those contained in this
  167. License Agreement (except that you may choose to grant more extensive
  168. warranty protection to some or all third parties, at your option).
  169. c) You may charge a distribution fee for the physical act of
  170. transferring a copy, and you may at your option offer warranty
  171. protection in exchange for a fee.
  172. Mere aggregation of another unrelated program with this program (or its
  173. derivative) on a volume of a storage or distribution medium does not bring
  174. the other program under the scope of these terms.
  175. 3. You may copy and distribute this program (or a portion or derivative
  176. of it, under Paragraph 2) in object code or executable form under the terms
  177. of Paragraphs 1 and 2 above provided that you also do one of the following:
  178. a) accompany it with the complete corresponding machine-readable
  179. source code, which must be distributed under the terms of
  180. Paragraphs 1 and 2 above; or,
  181. b) accompany it with a written offer, valid for at least three
  182. years, to give any third party free (except for a nominal
  183. shipping charge) a complete machine-readable copy of the
  184. corresponding source code, to be distributed under the terms of
  185. Paragraphs 1 and 2 above; or,
  186. c) accompany it with the information you received as to where the
  187. corresponding source code may be obtained. (This alternative is
  188. allowed only for noncommercial distribution and only if you
  189. received the program in object code or executable form alone.)
  190. For an executable file, complete source code means all the source code for
  191. all modules it contains; but, as a special exception, it need not include
  192. source code for modules which are standard libraries that accompany the
  193. operating system on which the executable file runs.
  194. 4. You may not copy, sublicense, distribute or transfer this program
  195. except as expressly provided under this License Agreement. Any attempt
  196. otherwise to copy, sublicense, distribute or transfer this program is void and
  197. your rights to use the program under this License agreement shall be
  198. automatically terminated. However, parties who have received computer
  199. software programs from you with this License Agreement will not have
  200. their licenses terminated so long as such parties remain in full compliance.
  201. 5. If you wish to incorporate parts of this program into other free
  202. programs whose distribution conditions are different, write to the Free
  203. Software Foundation at 675 Mass Ave, Cambridge, MA 02139. We have not yet
  204. worked out a simple rule that can be stated here, but we will often permit
  205. this. We will be guided by the two goals of preserving the free status of
  206. all derivatives of our free software and of promoting the sharing and reuse of
  207. software.
  208. In other words, you are welcome to use, share and improve this program.
  209. You are forbidden to forbid anyone else to use, share and improve
  210. what you give them. Help stamp out software-hoarding! */
  211. /* This is the parser code that is written into each bison parser
  212. when the %semantic_parser declaration is not specified in the grammar.
  213. It was written by Richard Stallman by simplifying the hairy parser
  214. used when %semantic_parser is specified. */
  215. /* Note: there must be only one dollar sign in this file.
  216. It is replaced by the list of actions, each action
  217. as one case of the switch. */
  218. #define yyerrok (yyerrstatus = 0)
  219. #define yyclearin (yychar = YYEMPTY)
  220. #define YYEMPTY -2
  221. #define YYEOF 0
  222. #define YYFAIL goto yyerrlab;
  223. #define YYTERROR 1
  224. #ifndef YYIMPURE
  225. #define YYLEX yylex()
  226. #endif
  227. #ifndef YYPURE
  228. #define YYLEX yylex(&yylval, &yylloc)
  229. #endif
  230. /* If nonreentrant, generate the variables here */
  231. #ifndef YYIMPURE
  232. int yychar; /* the lookahead symbol */
  233. YYSTYPE yylval; /* the semantic value of the */
  234. /* lookahead symbol */
  235. YYLTYPE yylloc; /* location data for the lookahead */
  236. /* symbol */
  237. int yynerr; /* number of parse errors so far */
  238. #ifdef YYDEBUG
  239. int yydebug = 0; /* nonzero means print parse trace */
  240. #endif
  241. #endif /* YYIMPURE */
  242. /* YYMAXDEPTH indicates the initial size of the parser's stacks */
  243. #ifndef YYMAXDEPTH
  244. #define YYMAXDEPTH 200
  245. #endif
  246. /* YYMAXLIMIT is the maximum size the stacks can grow to
  247. (effective only if the built-in stack extension method is used). */
  248. #ifndef YYMAXLIMIT
  249. #define YYMAXLIMIT 10000
  250. #endif
  251. #line 165 "bison.simple"
  252. int yyparse (void)
  253. {
  254. register int yystate;
  255. register int yyn;
  256. register short *yyssp;
  257. register YYSTYPE *yyvsp;
  258. YYLTYPE *yylsp;
  259. int yyerrstatus; /* number of tokens to shift before error messages enabled */
  260. int yychar1; /* lookahead token as an internal (translated) token number */
  261. short yyssa[YYMAXDEPTH]; /* the state stack */
  262. YYSTYPE yyvsa[YYMAXDEPTH]; /* the semantic value stack */
  263. YYLTYPE yylsa[YYMAXDEPTH]; /* the location stack */
  264. short *yyss = yyssa; /* refer to the stacks thru separate pointers */
  265. YYSTYPE *yyvs = yyvsa; /* to allow yyoverflow to reallocate them elsewhere */
  266. YYLTYPE *yyls = yylsa;
  267. int yymaxdepth = YYMAXDEPTH;
  268. #ifndef YYPURE
  269. int yychar;
  270. YYSTYPE yylval;
  271. YYLTYPE yylloc;
  272. #endif
  273. #ifdef YYDEBUG
  274. extern int yydebug;
  275. #endif
  276. YYSTYPE yyval; /* the variable used to return */
  277. /* semantic values from the action */
  278. /* routines */
  279. int yylen;
  280. #ifdef YYDEBUG
  281. if (yydebug)
  282. fprintf(stderr, "Starting parse\n");
  283. #endif
  284. yystate = 0;
  285. yyerrstatus = 0;
  286. yynerr = 0;
  287. yychar = YYEMPTY; /* Cause a token to be read. */
  288. /* Initialize stack pointers.
  289. Waste one element of value and location stack
  290. so that they stay on the same level as the state stack. */
  291. yyssp = yyss - 1;
  292. yyvsp = yyvs;
  293. yylsp = yyls;
  294. /* Push a new state, which is found in yystate . */
  295. /* In all cases, when you get here, the value and location stacks
  296. have just been pushed. so pushing a state here evens the stacks. */
  297. yynewstate:
  298. *++yyssp = yystate;
  299. if (yyssp >= yyss + yymaxdepth - 1)
  300. {
  301. /* Give user a chance to reallocate the stack */
  302. /* Use copies of these so that the &'s don't force the real ones into memory. */
  303. YYSTYPE *yyvs1 = yyvs;
  304. YYLTYPE *yyls1 = yyls;
  305. short *yyss1 = yyss;
  306. /* Get the current used size of the three stacks, in elements. */
  307. int size = yyssp - yyss + 1;
  308. #ifdef yyoverflow
  309. /* Each stack pointer address is followed by the size of
  310. the data in use in that stack, in bytes. */
  311. yyoverflow("parser stack overflow",
  312. &yyss1, size * sizeof (*yyssp),
  313. &yyvs1, size * sizeof (*yyvsp),
  314. &yyls1, size * sizeof (*yylsp),
  315. &yymaxdepth);
  316. yyss = yyss1; yyvs = yyvs1; yyls = yyls1;
  317. #else /* no yyoverflow */
  318. /* Extend the stack our own way. */
  319. if (yymaxdepth >= YYMAXLIMIT)
  320. yyerror("parser stack overflow");
  321. yymaxdepth *= 2;
  322. if (yymaxdepth > YYMAXLIMIT)
  323. yymaxdepth = YYMAXLIMIT;
  324. yyss = (short *) alloca (yymaxdepth * sizeof (*yyssp));
  325. bcopy ((char *)yyss1, (char *)yyss, size * sizeof (*yyssp));
  326. yyvs = (YYSTYPE *) alloca (yymaxdepth * sizeof (*yyvsp));
  327. bcopy ((char *)yyvs1, (char *)yyvs, size * sizeof (*yyvsp));
  328. #ifdef YYLSP_NEEDED
  329. yyls = (YYLTYPE *) alloca (yymaxdepth * sizeof (*yylsp));
  330. bcopy ((char *)yyls1, (char *)yyls, size * sizeof (*yylsp));
  331. #endif
  332. #endif /* no yyoverflow */
  333. yyssp = yyss + size - 1;
  334. yyvsp = yyvs + size - 1;
  335. #ifdef YYLSP_NEEDED
  336. yylsp = yyls + size - 1;
  337. #endif
  338. #ifdef YYDEBUG
  339. if (yydebug)
  340. fprintf(stderr, "Stack size increased to %d\n", yymaxdepth);
  341. #endif
  342. if (yyssp >= yyss + yymaxdepth - 1)
  343. YYERROR;
  344. }
  345. #ifdef YYDEBUG
  346. if (yydebug)
  347. fprintf(stderr, "Entering state %d\n", yystate);
  348. #endif
  349. /* Do appropriate processing given the current state. */
  350. /* Read a lookahead token if we need one and don't already have one. */
  351. yyresume:
  352. /* First try to decide what to do without reference to lookahead token. */
  353. yyn = yypact[yystate];
  354. if (yyn == YYFLAG)
  355. goto yydefault;
  356. /* Not known => get a lookahead token if don't already have one. */
  357. /* yychar is either YYEMPTY or YYEOF
  358. or a valid token in external form. */
  359. if (yychar == YYEMPTY)
  360. {
  361. #ifdef YYDEBUG
  362. if (yydebug)
  363. fprintf(stderr, "Reading a token: ");
  364. #endif
  365. yychar = YYLEX;
  366. }
  367. /* Convert token to internal form (in yychar1) for indexing tables with */
  368. if (yychar <= 0) /* This means end of input. */
  369. {
  370. yychar1 = 0;
  371. yychar = YYEOF; /* Don't call YYLEX any more */
  372. #ifdef YYDEBUG
  373. if (yydebug)
  374. fprintf(stderr, "Now at end of input.\n");
  375. #endif
  376. }
  377. else
  378. {
  379. yychar1 = YYTRANSLATE(yychar);
  380. #ifdef YYDEBUG
  381. if (yydebug)
  382. fprintf(stderr, "Next token is %d (%s)\n", yychar, yytname[yychar1]);
  383. #endif
  384. }
  385. yyn += yychar1;
  386. if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != yychar1)
  387. goto yydefault;
  388. yyn = yytable[yyn];
  389. /* yyn is what to do for this token type in this state.
  390. Negative => reduce, -yyn is rule number.
  391. Positive => shift, yyn is new state.
  392. New state is final state => don't bother to shift,
  393. just return success.
  394. 0, or most negative number => error. */
  395. if (yyn < 0)
  396. {
  397. if (yyn == YYFLAG)
  398. goto yyerrlab;
  399. yyn = -yyn;
  400. goto yyreduce;
  401. }
  402. else if (yyn == 0)
  403. goto yyerrlab;
  404. if (yyn == YYFINAL)
  405. YYACCEPT;
  406. /* Shift the lookahead token. */
  407. #ifdef YYDEBUG
  408. if (yydebug)
  409. fprintf(stderr, "Shifting token %d (%s), ", yychar, yytname[yychar1]);
  410. #endif
  411. /* Discard the token being shifted unless it is eof. */
  412. if (yychar != YYEOF)
  413. yychar = YYEMPTY;
  414. *++yyvsp = yylval;
  415. #ifdef YYLSP_NEEDED
  416. *++yylsp = yylloc;
  417. #endif
  418. /* count tokens shifted since error; after three, turn off error status. */
  419. if (yyerrstatus) yyerrstatus--;
  420. yystate = yyn;
  421. goto yynewstate;
  422. /* Do the default action for the current state. */
  423. yydefault:
  424. yyn = yydefact[yystate];
  425. if (yyn == 0)
  426. goto yyerrlab;
  427. /* Do a reduction. yyn is the number of a rule to reduce with. */
  428. yyreduce:
  429. yylen = yyr2[yyn];
  430. yyval = yyvsp[1-yylen]; /* implement default value of the action */
  431. #ifdef YYDEBUG
  432. if (yydebug)
  433. {
  434. if (yylen == 1)
  435. fprintf (stderr, "Reducing 1 value via line %d, ",
  436. yyrline[yyn]);
  437. else
  438. fprintf (stderr, "Reducing %d values via line %d, ",
  439. yylen, yyrline[yyn]);
  440. }
  441. #endif
  442. switch (yyn) {
  443. case 1:
  444. #line 31 "calc.y"
  445. { val = yyvsp[0]; printflag = 1; ;
  446. break;}
  447. case 2:
  448. #line 33 "calc.y"
  449. { regs[yyvsp[-2]] = yyvsp[0]; ;
  450. break;}
  451. case 3:
  452. #line 35 "calc.y"
  453. { exit(0); ;
  454. break;}
  455. case 4:
  456. #line 39 "calc.y"
  457. { yyval = yyvsp[-1]; ;
  458. break;}
  459. case 5:
  460. #line 41 "calc.y"
  461. { if (yyvsp[-2] == 69)
  462. {
  463. yyval = 2;
  464. printf ("$1 became %d\n", yyvsp[-2]);
  465. yyvsp[-2] = 69;
  466. }
  467. yyval = yyvsp[-2] + yyvsp[0]; ;
  468. break;}
  469. case 6:
  470. #line 49 "calc.y"
  471. { yyval = yyvsp[-2] - yyvsp[0]; ;
  472. break;}
  473. case 7:
  474. #line 51 "calc.y"
  475. { yyval = yyvsp[-2] * yyvsp[0]; ;
  476. break;}
  477. case 8:
  478. #line 53 "calc.y"
  479. { yyval = yyvsp[-2] / yyvsp[0]; ;
  480. break;}
  481. case 9:
  482. #line 55 "calc.y"
  483. { yyval = yyvsp[-2] % yyvsp[0]; ;
  484. break;}
  485. case 10:
  486. #line 57 "calc.y"
  487. { yyval = yyvsp[-2] | yyvsp[0]; ;
  488. break;}
  489. case 11:
  490. #line 59 "calc.y"
  491. { yyval = yyvsp[-2] & yyvsp[0]; ;
  492. break;}
  493. case 12:
  494. #line 61 "calc.y"
  495. { yyval = - yyvsp[0]; ;
  496. break;}
  497. case 13:
  498. #line 63 "calc.y"
  499. { yyval = regs[yyvsp[0]]; ;
  500. break;}
  501. case 15:
  502. #line 70 "calc.y"
  503. { yyval = yyvsp[0]; base = (yyvsp[0] == 0) ? 8 : 10; ;
  504. break;}
  505. case 16:
  506. #line 72 "calc.y"
  507. { yyval = base * yyvsp[-1] + yyvsp[0]; ;
  508. break;}
  509. }
  510. /* the action file gets copied in in place of this dollarsign */
  511. #line 303 "bison.simple"
  512. yyvsp -= yylen;
  513. yyssp -= yylen;
  514. #ifdef YYLSP_NEEDED
  515. yylsp -= yylen;
  516. #endif
  517. #ifdef YYDEBUG
  518. if (yydebug)
  519. {
  520. short *ssp1 = yyss - 1;
  521. fprintf (stderr, "state stack now", yyssp-yyss);
  522. while (ssp1 != yyssp)
  523. fprintf (stderr, " %d", *++ssp1);
  524. fprintf (stderr, "\n");
  525. }
  526. #endif
  527. *++yyvsp = yyval;
  528. #ifdef YYLSP_NEEDED
  529. yylsp++;
  530. if (yylen == 0)
  531. {
  532. yylsp->first_line = yylloc.first_line;
  533. yylsp->first_column = yylloc.first_column;
  534. yylsp->last_line = (yylsp-1)->last_line;
  535. yylsp->last_column = (yylsp-1)->last_column;
  536. yylsp->text = 0;
  537. }
  538. else
  539. {
  540. yylsp->last_line = (yylsp+yylen-1)->last_line;
  541. yylsp->last_column = (yylsp+yylen-1)->last_column;
  542. }
  543. #endif
  544. /* Now "shift" the result of the reduction.
  545. Determine what state that goes to,
  546. based on the state we popped back to
  547. and the rule number reduced by. */
  548. yyn = yyr1[yyn];
  549. yystate = yypgoto[yyn - YYNTBASE] + *yyssp;
  550. if (yystate >= 0 && yystate <= YYLAST && yycheck[yystate] == *yyssp)
  551. yystate = yytable[yystate];
  552. else
  553. yystate = yydefgoto[yyn - YYNTBASE];
  554. goto yynewstate;
  555. yyerrlab: /* here on detecting error */
  556. if (! yyerrstatus)
  557. /* If not already recovering from an error, report this error. */
  558. {
  559. ++yynerr;
  560. yyerror("parse error");
  561. }
  562. if (yyerrstatus == 3)
  563. {
  564. /* if just tried and failed to reuse lookahead token after an error, discard it. */
  565. /* return failure if at end of input */
  566. if (yychar == YYEOF)
  567. YYERROR;
  568. #ifdef YYDEBUG
  569. if (yydebug)
  570. fprintf(stderr, "Discarding token %d (%s).\n", yychar, yytname[yychar1]);
  571. #endif
  572. yychar = YYEMPTY;
  573. }
  574. /* Else will try to reuse lookahead token
  575. after shifting the error token. */
  576. yyerrstatus = 3; /* Each real token shifted decrements this */
  577. goto yyerrhandle;
  578. yyerrdefault: /* current state does not do anything special for the error token. */
  579. #if 0
  580. /* This is wrong; only states that explicitly want error tokens
  581. should shift them. */
  582. yyn = yydefact[yystate]; /* If its default is to accept any token, ok. Otherwise pop it.*/
  583. if (yyn) goto yydefault;
  584. #endif
  585. yyerrpop: /* pop the current state because it cannot handle the error token */
  586. if (yyssp == yyss) YYERROR;
  587. yyvsp--;
  588. yystate = *--yyssp;
  589. #ifdef YYLSP_NEEDED
  590. yylsp--;
  591. #endif
  592. #ifdef YYDEBUG
  593. if (yydebug)
  594. {
  595. short *ssp1 = yyss - 1;
  596. fprintf (stderr, "Error: state stack now", yyssp-yyss);
  597. while (ssp1 != yyssp)
  598. fprintf (stderr, " %d", *++ssp1);
  599. fprintf (stderr, "\n");
  600. }
  601. #endif
  602. yyerrhandle:
  603. yyn = yypact[yystate];
  604. if (yyn == YYFLAG)
  605. goto yyerrdefault;
  606. yyn += YYTERROR;
  607. if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != YYTERROR)
  608. goto yyerrdefault;
  609. yyn = yytable[yyn];
  610. if (yyn < 0)
  611. {
  612. if (yyn == YYFLAG)
  613. goto yyerrpop;
  614. yyn = -yyn;
  615. goto yyreduce;
  616. }
  617. else if (yyn == 0)
  618. goto yyerrpop;
  619. if (yyn == YYFINAL)
  620. YYACCEPT;
  621. #ifdef YYDEBUG
  622. if (yydebug)
  623. fprintf(stderr, "Shifting error token, ");
  624. #endif
  625. *++yyvsp = yylval;
  626. #ifdef YYLSP_NEEDED
  627. *++yylsp = yylloc;
  628. #endif
  629. yystate = yyn;
  630. goto yynewstate;
  631. }
  632. #line 75 "calc.y"
  633. static int eol;
  634. int yylex (void)
  635. {
  636. int c;
  637. while ( (c=getchar()) == ' ') {}
  638. if (c == '\n')
  639. { eol = 1;
  640. return 0; }
  641. if (c == 'Q' || c == EOF)
  642. return(EXIT);
  643. if (islower(c))
  644. {
  645. yylval = c - 'a';
  646. return (LETTER);
  647. }
  648. if (isdigit(c))
  649. {
  650. yylval = c - '0';
  651. return (DIGIT);
  652. }
  653. return (c);
  654. }
  655. void yyerror (char *s)
  656. {
  657. printf("%s\n", s);
  658. }
  659. int main (int argc, char *argv[])
  660. {
  661. for (;;)
  662. {
  663. eol = 0;
  664. printflag = 0;
  665. if (yyparse()) printflag = 0;
  666. if (printflag) printf("%d\n", val);
  667. while (!eol)
  668. if (yylex() == EOF)
  669. exit(0);
  670. }
  671. return (0);
  672. }