-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.y
272 lines (245 loc) · 5.94 KB
/
expr.y
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
%{
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <stdarg.h>
#include <cstring>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#undef yyFlexLexer
#define yyFlexLexer expFlexLexer
#include <FlexLexer.h>
#include "common.h"
using namespace std;
#define YYSTYPE char*
expFlexLexer explexer;
string analyze_expression(const string& s);
int expFlexLexer::explex(void)
{
this->yylex();
}
int explex(void)
{
return explexer.yylex();
}
int expparse(void);
void experror(const char*,...);
FILE* expin;
FILE* expout;
stringstream output;
#ifndef yywrap
int yywrap() { return 1; }
#endif
%}
%error-verbose
%locations
%right EQ
%token RPAREN
%token LPAREN
%right KET
%left BRA
%right ASSIGN
%token SEMICOLON
%left COMMA
%right ARROW
%left PIPE
%left COLON
%left AT
%left AND
%token WORD
%token COLLECTED_WORD
%token EXPRINIT
%token RSBRA
%left NOTEQ
%left EQUAL
%left GT
%left LT
%left GTEQ
%left LTEQ
%left MULT
%left DIV
%left MOD
%left PLUS
%left MINUS
%right LOGNOT
%left LIKEOP
%right CONDQUEST
%token NUM
%token VAR
%token NOVAR_WORD
%token END 0 "end of file"
%%
file: base_expr
{
$$ = $1;
output << $$;
}
| END
;
base_expr:
coll_words
{
$$ = $1;
}
| operand_expr
{
$$ = $1;
}
| LPAREN operand_expr RPAREN
{
stringstream ss;
ss << "(" << $2 << ")";
$$ = alloc_string((char*) ss.str().data());
destroy_string($1);
}
;
coll_words:
coll_word
{
$$ = alloc_string($1);
}
| coll_words coll_word
{
stringstream ss;
ss << $1 << " .. ";
char* group = alloc_string((char*)ss.str().data());
$$ = grow_string(group, $2);
destroy_string($1);
destroy_string($2);
}
;
coll_word:
VAR
{
$$ = extract_variable($1);
}
| NUM
{
stringstream ss;
string s = string($1);
if(s.size() > 0 && s[0] != '\"' && s[s.size()-1] != '\"')
ss << "\"" << $1 << "\"";
else
ss << $1;
$$ = alloc_string((char*) ss.str().data());
free($1);
}
| NOVAR_WORD
{
stringstream ss;
string s = string($1);
if((s.size() > 0 && s[0] != '\"' && s[s.size()-1] != '\"') &&
(s.find("=") == string::npos) &&
(s.find(">") == string::npos) &&
(s.find("<") == string::npos) &&
(s.find(">=") == string::npos) &&
(s.find("<=") == string::npos) &&
(s.find("==") == string::npos)
)
ss << "\"" << $1 << "\"";
else
ss << $1;
$$ = alloc_string((char*) ss.str().data());
free($1);
}
;
operand_expr : unary_expr { $$ = $1; }
| binary_expr { $$ = $1; }
| conditional_op { $$ = $1; }
| assign_expr { $$ = $1; }
;
assign_expr: NOVAR_WORD EQ base_expr
{
stringstream ss;
ss << $1 << "=" << $3;
$$ = alloc_string((char*)ss.str().data());
free($1);
destroy_string($3);
}
binary_expr: base_expr binary_op base_expr
{
$$ = extract_binary_expr($1, $2, $3);
destroy_string($1);
destroy_string($2);
destroy_string($3);
};
unary_expr: unary_op base_expr
{
$$ = extract_unary_expr($1, $2);
destroy_string($1);
destroy_string($2);
}
;
conditional_op : base_expr CONDQUEST base_expr COLON base_expr
{
$$ = extract_conditional_op($1, $3, $5);
destroy_string($1);
destroy_string($3);
destroy_string($5);
}
;
binary_op: logical_binary_op
{
$$ = $1;
}
| arith_binary_op
{
$$ = $1;
}
| special_binary_op
{
$$ = $1;
}
;
logical_binary_op : PIPE { $$ = alloc_string((char*)"|"); }
| AND { $$ = alloc_string((char*)"&"); }
| EQUAL { $$ = alloc_string((char*)"=="); }
| NOTEQ { $$ = alloc_string((char*)"!="); }
| LT { $$ = alloc_string((char*)"<"); }
| GT { $$ = alloc_string((char*)">"); }
| GTEQ { $$ = alloc_string((char*)">="); }
| LTEQ { $$ = alloc_string((char*)"<="); }
;
arith_binary_op : PLUS { $$ = alloc_string((char*)"+"); }
| MINUS { $$ = alloc_string((char*)"-"); }
| MULT { $$ = alloc_string((char*)"*"); }
| DIV { $$ = alloc_string((char*)"/"); }
| MOD { $$ = alloc_string((char*)"%"); }
;
special_binary_op : AT
{
$$ = alloc_string((char*) "@");
}
| COLON
{
$$ = alloc_string((char*) ":");
};
unary_op : logical_unary_op { $$ = $1; }
| arith_unary_op { $$ = $1; }
;
logical_unary_op : LOGNOT { $$ = alloc_string((char*) "!"); };
arith_unary_op : MINUS { $$ = alloc_string((char*) "-"); };
%%
string analyze_expression(const string& s)
{
stringstream input;
input << s;
explexer.switch_streams(&input, &output);
expparse();
string to_ret = output.str();
output.str("");
return to_ret;
}
void
experror(const char *s, ...)
{
va_list ap;
va_start(ap, s);
if(explloc.first_line)
fprintf(stderr, "%d.%d-%d.%d: error: ", explloc.first_line, explloc.first_column,
explloc.last_line, explloc.last_column);
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
}