282
|
1 /* parser.y -- Bison parser for libexp
|
|
2 *
|
|
3 * Copyright (C) 2001 Janusz Gregorczyk <jgregor@kki.net.pl>
|
|
4 *
|
|
5 * This file is part of xvs.
|
|
6 *
|
|
7 * This program is free software; you can redistribute it and/or modify
|
|
8 * it under the terms of the GNU General Public License as published by
|
|
9 * the Free Software Foundation; either version 2 of the License, or
|
|
10 * (at your option) any later version.
|
|
11 *
|
|
12 * This program is distributed in the hope that it will be useful,
|
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 * GNU General Public License for more details.
|
|
16 *
|
|
17 * You should have received a copy of the GNU General Public License
|
|
18 * along with this program; if not, write to the Free Software
|
|
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
20 */
|
|
21
|
|
22 /* C declarations. */
|
|
23 %{
|
|
24 #include <ctype.h>
|
|
25 #include <glib.h>
|
|
26 #include <locale.h>
|
|
27 #include <math.h>
|
|
28 #include <stdio.h>
|
|
29 #include <string.h>
|
|
30
|
|
31 #include "dict.h"
|
|
32 #include "execute.h"
|
|
33 #include "function.h"
|
|
34 #include "parser.h"
|
|
35 #include "storage.h"
|
|
36
|
|
37 #define YYPARSE_PARAM yyparam
|
|
38 #define YYLEX_PARAM yyparam
|
|
39
|
|
40 static gboolean expr_add_compile (expression_t *expr, symbol_dict_t *dict,
|
|
41 char *str);
|
|
42
|
|
43 #define GENERATE(str) if (!expr_add_compile (((parser_control *)yyparam)->expr, \
|
|
44 ((parser_control *)yyparam)->dict, str)) \
|
|
45 YYABORT;
|
|
46 %}
|
|
47
|
|
48 %pure_parser
|
|
49
|
|
50 /* Data types. */
|
|
51 %union {
|
|
52 char *s_value;
|
|
53 char c_value;
|
|
54 double d_value;
|
|
55 int i_value;
|
|
56 }
|
|
57
|
|
58 /* Terminal symbols. */
|
|
59 %token <s_value> NAME
|
|
60 %token <d_value> NUMBER
|
|
61
|
|
62 /* Precedence rules. */
|
|
63 %right '='
|
|
64 %left '-' '+'
|
|
65 %left '*' '/'
|
|
66 %left NEG
|
|
67 %right '^'
|
|
68
|
|
69 /* Grammar follows */
|
|
70 %%
|
|
71
|
|
72 /* Input consits of a (possibly empty) list of expressions. */
|
|
73 input: /* empty */
|
|
74 | input expression_list
|
|
75 ;
|
|
76
|
|
77 /* expression_list is a ';' separated list of expressions. */
|
|
78 expression_list: /* empty */
|
|
79 | expression
|
|
80 { }
|
|
81 | expression_list ';'
|
|
82 | error ';'
|
|
83 { yyerrok; }
|
|
84
|
|
85 /* argument list is a comma separated list od expressions */
|
|
86 argument_list:
|
|
87 expression
|
|
88 {
|
|
89 }
|
|
90 | argument_list ',' expression
|
|
91 {
|
|
92 }
|
|
93
|
|
94 /* expression is a C-like expression. */
|
|
95 expression: NUMBER
|
|
96 {
|
|
97 char *buf = g_strdup_printf ("c%f:", $1);
|
|
98 GENERATE (buf);
|
|
99 g_free (buf);
|
|
100 }
|
|
101 | NAME
|
|
102 {
|
|
103 char *buf = g_strdup_printf ("l%s:", $1);
|
|
104 GENERATE (buf);
|
|
105 g_free (buf);
|
|
106 }
|
|
107 | NAME '=' expression
|
|
108 {
|
|
109 char *buf = g_strdup_printf ("s%s:", $1);
|
|
110 GENERATE (buf);
|
|
111 g_free (buf);
|
|
112 }
|
|
113 | NAME '(' argument_list ')'
|
|
114 {
|
|
115 char *buf = g_strdup_printf ("f%s:", $1);
|
|
116 GENERATE (buf);
|
|
117 g_free (buf);
|
|
118 }
|
|
119
|
|
120 | expression '>' expression
|
|
121 { GENERATE (">"); }
|
|
122 | expression '<' expression
|
|
123 { GENERATE ("<"); }
|
|
124
|
|
125 | expression '+' expression
|
|
126 { GENERATE ("+"); }
|
|
127 | expression '-' expression
|
|
128 { GENERATE ("-"); }
|
|
129 | expression '*' expression
|
|
130 { GENERATE ("*"); }
|
|
131 | expression '/' expression
|
|
132 { GENERATE ("/"); }
|
|
133 | '-' expression %prec NEG
|
|
134 { GENERATE ("n"); }
|
|
135 | expression '^' expression
|
|
136 { GENERATE ("^"); }
|
|
137 | '(' expression ')'
|
|
138 { }
|
|
139 ;
|
|
140
|
|
141 %%
|
|
142 /* End of grammar */
|
|
143
|
|
144 /* Called by yyparse on error. */
|
|
145 int yyerror (char *s) {
|
|
146 /* Ignore errors, just print a warning. */
|
|
147 g_warning ("%s\n", s);
|
|
148 return 0;
|
|
149 }
|
|
150
|
|
151 int yylex (YYSTYPE *yylval, void *yyparam) {
|
|
152 int c;
|
|
153 parser_control *pc = (parser_control *) yyparam;
|
|
154
|
|
155 /* Ignore whitespace, get first nonwhite character. */
|
|
156 while ((c = fgetc (pc->input)) == ' ' || c == '\t' || c == '\n');
|
|
157
|
|
158 /* End of input ? */
|
|
159 if (c == EOF)
|
|
160 return 0;
|
|
161
|
|
162 /* Char starts a number => parse the number. */
|
|
163 if (isdigit (c)) {
|
|
164 ungetc (c, pc->input);/* Put the char back. */
|
|
165 {
|
|
166 char *old_locale, *saved_locale;
|
|
167
|
|
168 old_locale = setlocale (LC_ALL, NULL);
|
|
169 saved_locale = g_strdup (old_locale);
|
|
170 setlocale (LC_ALL, "C");
|
|
171 fscanf (pc->input, "%lf", &yylval->d_value);
|
|
172 setlocale (LC_ALL, saved_locale);
|
|
173 g_free (saved_locale);
|
|
174 }
|
|
175 return NUMBER;
|
|
176 }
|
|
177
|
|
178 /* Char starts an identifier => read the name. */
|
|
179 if (isalpha (c)) {
|
|
180 GString *sym_name;
|
|
181
|
|
182 sym_name = g_string_new (NULL);
|
|
183
|
|
184 do {
|
|
185 sym_name = g_string_append_c (sym_name, c);
|
|
186
|
|
187 /* Get another character. */
|
|
188 c = fgetc (pc->input);
|
|
189 } while (c != EOF && isalnum (c));
|
|
190
|
|
191 ungetc (c, pc->input);
|
|
192
|
|
193 yylval->s_value = sym_name->str;
|
|
194
|
|
195 g_string_free (sym_name, FALSE);
|
|
196
|
|
197 return NAME;
|
|
198 }
|
|
199
|
|
200 /* Any other character is a token by itself. */
|
|
201 return c;
|
|
202 }
|
|
203
|
|
204 static int load_name (char *str, char **name) {
|
|
205 int count = 0;
|
|
206 GString *new = g_string_new (NULL);
|
|
207
|
|
208 while (*str != 0 && *str != ':') {
|
|
209 g_string_append_c (new, *str++);
|
|
210 count++;
|
|
211 }
|
|
212
|
|
213 *name = new->str;
|
|
214 g_string_free (new, FALSE);
|
|
215
|
|
216 return count;
|
|
217 }
|
|
218
|
|
219 static gboolean expr_add_compile (expression_t *expr, symbol_dict_t *dict,
|
|
220 char *str) {
|
|
221 char op;
|
|
222 double dval;
|
|
223 int i;
|
|
224 char *name;
|
|
225
|
|
226 while ((op = *str++)) {
|
|
227 switch (op) {
|
|
228 case 'c': /* A constant. */
|
|
229 store_byte (expr, 'c');
|
|
230 sscanf (str, "%lf%n", &dval, &i);
|
|
231 str += i;
|
|
232 store_double (expr, dval);
|
|
233 str++; /* Skip ';' */
|
|
234 break;
|
|
235
|
|
236 case 'f': /* A function call. */
|
|
237 store_byte (expr, 'f');
|
|
238 str += load_name (str, &name);
|
|
239 i = function_lookup (name);
|
|
240 if (i < 0) return FALSE; /* Fail on error. */
|
|
241 store_int (expr, i);
|
|
242 g_free (name);
|
|
243 str++; /* Skip ';' */
|
|
244 break;
|
|
245
|
|
246 case 'l': /* Load a variable. */
|
|
247 case 's': /* Store a variable. */
|
|
248 store_byte (expr, op);
|
|
249 str += load_name (str, &name);
|
|
250 i = dict_lookup (dict, name);
|
|
251 store_int (expr, i);
|
|
252 g_free (name);
|
|
253 str++; /* Skip ';' */
|
|
254 break;
|
|
255
|
|
256 default: /* Copy verbatim. */
|
|
257 store_byte (expr, op);
|
|
258 break;
|
|
259 }
|
|
260 }
|
|
261
|
|
262 return TRUE;
|
|
263 }
|
|
264
|
|
265 expression_t *expr_compile_string (const char* str, symbol_dict_t *dict) {
|
|
266 parser_control pc;
|
|
267 FILE *stream;
|
|
268
|
|
269 stream = fmemopen (str, strlen (str), "r");
|
|
270
|
|
271 pc.input = stream;
|
|
272 pc.expr = expr_new ();
|
|
273 pc.dict = dict;
|
|
274
|
|
275 if (yyparse (&pc) != 0) {
|
|
276 /* Check for error. */
|
|
277 expr_free (pc.expr);
|
|
278 pc.expr = NULL;
|
|
279 }
|
|
280
|
|
281 fclose (stream);
|
|
282
|
|
283 return pc.expr;
|
|
284 }
|