-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.l
213 lines (153 loc) · 2.59 KB
/
Parser.l
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
%{
#include <assert.h>
#include "ParserExport.h"
typedef struct parser_stack_ {
int top;
lex_data_t data[MAX_MEXPR_LEN];
} parser_stack_t;
extern "C" int yylex();
extern unsigned char lex_buffer[MAX_STRING_SIZE];
extern parser_stack_t undo_stack;
unsigned char lex_buffer[MAX_STRING_SIZE] = {0};
unsigned char *curr_ptr = lex_buffer;
parser_stack_t undo_stack = {-1, {0,0,0}};
static void
lex_push(lex_data_t lex_data) {
assert(undo_stack.top < MAX_MEXPR_LEN - 1);
undo_stack.data[++undo_stack.top] = lex_data;
}
static lex_data_t
lex_pop() {
assert(undo_stack.top > -1);
lex_data_t res = undo_stack.data[undo_stack.top];
undo_stack.top--;
return res;
}
// user specific header files
#include "MexprEnums.h"
%}
%%
"(" {
return MATH_BRACKET_START;
}
")" {
return MATH_BRACKET_END;
}
"<=" {
return MATH_LESS_THAN_EQ;
}
"<" {
return MATH_LESS_THAN;
}
">" {
return MATH_GREATER_THAN;
}
"=" {
return MATH_EQ;
}
"!=" {
return MATH_NOT_EQ;
}
"and" {
return MATH_AND;
}
"or" {
return MATH_OR;
}
"*" {
return MATH_MUL;
}
"+" {
return MATH_PLUS;
}
"-" {
return MATH_MINUS;
}
"/" {
return MATH_DIV;
}
"," {
return MATH_COMMA;
}
"sqrt" {
return MATH_SQRT;
}
"sqr" {
return MATH_SQR;
}
"mmax" {
return MATH_MAX;
}
"mmin" {
return MATH_MIN;
}
"sin" {
return MATH_SIN;
}
"cos" {
return MATH_COS;
}
"pow" {
return MATH_POW;
}
\n {
return PARSER_EOL;
}
"\\\n" {
}
[ ] {
/* Ignore
process_white_space(1);
*/
}
[\t] {
/*ignore
process_white_space(4);
*/
}
"\\q" {
return PARSER_QUIT;
}
0|-?[1-9][0-9]* {
return MATH_INTEGER_VALUE;
}
-?[0-9]*\.[0-9]+ {
return MATH_DOUBLE_VALUE;
}
[a-zA-Z0-9_]+ {
return MATH_IDENTIFIER;
}
[a-zA-Z0-9_]+\.[a-zA-Z0-9_]+ {
return MATH_IDENTIFIER_IDENTIFIER;
}
'[^']*' {
// string enclosed within single quotes
return MATH_STRING_VALUE;
}
\"[^"]*\" {
// string enclosed within double quotes
return MATH_STRING_VALUE;
}
. {
// ignore any other character
}
%%
int main(int argc, char** argv) {
/*
while(1) {
printf("-----------\nInput -> ");
fgets(lex_buffer, sizeof(lex_buffer), stdin);
if(lex_buffer[0] == '\n') {
lex_buffer[0] = 0;
continue;
}
yy_scan_string(lex_buffer);
int token_code;
token_code = yylex();
while(token_code != PARSER_EOL) {
printf("token_code = %d, token = %s, token_len = %d\n", token_code, yytext, yyleng);
token_code = yylex();
}
}
*/
}