File: gram.y

package info (click to toggle)
rpncalc 1.31.13
  • links: PTS
  • area: main
  • in suites: woody
  • size: 828 kB
  • ctags: 262
  • sloc: sh: 2,624; ansic: 1,836; lex: 335; sed: 312; yacc: 118; makefile: 88
file content (178 lines) | stat: -rw-r--r-- 5,531 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
%{
/***************************************************************************
 * gram.y -- Grammar for the Reverse Polish Calculator			   *
 *									   *
 * A little RPN (Reverse Polish Notation) calculator, rudimentary          *
 * emulating a HP 28S. 					                   *
 * 								           *
 * rpncalc is (c) David Frey, 1993, 1994, 1995, 1996, 1997, 1998	   *
 *								           * 
 * This program is free software; you can redistribute it and/or modify it *
 * under the terms of the GNU General Public License as published by the   *
 * Free Software Foundation; either version 2 of the License, or (at your  *
 * option) any later version.                                              *
 *									   * 
 * This program is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of              *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
 * General Public License for more details.                                *
 *									   * 
 * You should have received a copy of the GNU General Public License       *
 * along with this program; if not, write to the Free Software Foundation, *
 * Inc., 59 Temple Place Suite 330, Boston, MA 02111-1307 USA.             *
 ***************************************************************************/

/* $Id: gram.y,v 1.4 2000/04/02 16:12:17 david Rel $
 * $Log: gram.y,v $
 * Revision 1.4  2000/04/02 16:12:17  david
 * Added `#include <string.h>', added declaration of `pushtostack'.
 *
 * Revision 1.3  1999/09/12 20:51:45  david
 * Added gettext support.
 * Added variables and functions.
 * Set errno to 0 before function calls.
 *
 * Revision 1.2  1999/03/07 16:35:16  david
 * Simplified grammar, handles now EOF correctly.
 *
 * Revision 1.1  1999/03/06 14:45:27  david
 * Initial revision
 *
 */

#include <stdio.h>
#include <errno.h>
#include <malloc.h>
#include <string.h>
#include <search.h>

#include "cmds.h"
#include "stack.h"
#include "utils.h"

#ifdef HAVE_GETTEXT
#include <libintl.h>

#define _(String) gettext(String)
#else
#define _(String) (String)

#define textdomain(Domain)
#define bindtextdomain(Package, Directory)
#endif
extern int lineno;
extern int pushtostack;

extern void yyerror(const char *s);
extern int yylex(void);

#define MAXNOOFVARS 256

struct var vartab[MAXNOOFVARS];
%}

%union {
	        char    *name;
	        struct cmd *func;
                double   dnum;
		long int inum;
       }

%token <name>   VAR
%token <inum>   INT  
%token <dnum>   REAL
%token <func>   CMD FUNC
%token <name>   HELP

%%

input: /* empty */
     | input { pushtostack=1; } line
     ; 

line: '\n'		     { lineno++; showstack(); }
    | explist	             
    | help
    ;

explist: exp
       | explist exp
       ;
       
exp: CMD		     { double op1=0.0, op2=0.0;
                               errno=0;
                               switch ($1->argno) {
                                 case 0: $1->fnct(); 
                                         break;
                                 case 1: op1=pop(); 
                                         $1->fnct(op1); 
                                         break;
                                 case 2: op2=pop(); op1=pop(); 
                                         $1->fnct(op1,op2); 
                                         break;
                               }
		               if (errno != 0) yyerror(strerror(errno));
                             }
   | FUNC		     { double op1=0.0, op2=0.0, res=0.0;

                               errno=0;
                               switch ($1->argno) {
                                 case 0: res=$1->fnct(); 
                                         break;
                                 case 1: op1=pop(); 
                                         res=$1->fnct(op1); 
                                         break;
                                 case 2: op2=pop(); op1=pop(); 
                                         res=$1->fnct(op1,op2); 
                                         break;
			       }
		               if (errno != 0) yyerror(strerror(errno));
	                       push(res);
                             }
   | VAR		     { struct var s, *r;

			       s.vname=$1;
			       s.value=0.0;
                               r=lfind(&s, &vartab, &noofvars,
				       sizeof(struct var), varcmp);
			       if (r != NULL) {
				 push(r->value);
			       } else {
				 yyerror(_("undefined variable"));
			       }
                               free($1);
                             }
   | VAR '='		     { struct var v, *r;
			     
			       v.vname=xstrdup($1);
			       v.value=pop();
			       v.isconst=0;

                               r=lfind(&v, &vartab, &noofvars,
				       sizeof(struct var), varcmp);
			       if (r != NULL) {
				 if (r->isconst) {
				   yyerror(_("is a constant"));
				 } else {
				   r->value=v.value;
				 }
			       } else {
				 if (noofvars < MAXNOOFVARS) {
				   lsearch(&v, &vartab, &noofvars, 
					   sizeof(struct var), varcmp);
				 } else {
				   yyerror(_("variable table is full"));
				 }
			       }
                               free($1);
                             }
   | INT                     { push($1); }
   | REAL                    { push($1); }
   ;

help: HELP CMD               { help($2); }     
    | HELP FUNC              { help($2); }
    | HELP HELP              { help(NULL); }
    | HELP                   { help(NULL); }
    ;
%%