-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_fac.sh
executable file
·325 lines (296 loc) · 8.32 KB
/
create_fac.sh
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/bin/bash
# Constructs "fac_gen.c"
# shall contain one function for each variation of the main loop.
function printDeltaBody()
{
printf " foll_ql = state->q->%s_set[%s];\n" $SETNAME ${SET_INDEX}
printf " foll_size = state->q->size_%s_set[%s];\n" $SETNAME ${SET_INDEX}
cat<<EOF
for(i = 0; i < foll_size; i++){
found = true;
for(c = 0; c < num_constraints; c++){
constraint constr = re->constraints_r[c]->value.constr;
if(
( foll_ql[i].updates[c] == FOLLOW_INC && constr.upper != 0 && (state->counters[c] + 1 >= (constr.upper)))
|| ( foll_ql[i].updates[c] == FOLLOW_RES && (state->counters[c] + 1 < (constr.lower)))
){
found = false;
break;
}
}
if(found){
state->q = (regexp*) foll_ql[i].next;
for(u = 0; u < num_constraints; u++){
if(foll_ql[i].updates[u] == FOLLOW_INC)
state->counters[u]++;
else if( foll_ql[i].updates[u] == FOLLOW_RES)
state->counters[u] = 0;
}
EOF
cat <<EOF
return state;
}
}
EOF
}
cat <<EOF
/** Constructed automatically by create_fac.sh. Do not edit **/
/* fac_gen.c - Finita Automata with Counters
Copyright 2008 University of Bergen
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 3, 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.,
51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA */
/* Written July, 2008 by Dag Hovland */
#include "regexp.h"
#include "fac.h"
#include <strings.h>
#include <limits.h>
fac_state *q;
// Contains zeroes. Used to initialize the counters in fac_delta_last and fac_delta_qi
unsigned int * zero_counter;
/* If one of the first/last set is an ANY_CHAR, index of this position in the last/first set */
int anychar_first;
int anychar_last;
/** Prints state during verbose matching **/
void fprintf_fac_state(FILE* f, bool qi, regexp* re){
size_t i;
bool first_print = true;
if(qi)
fprintf(f, "\n(qI, {");
else {
fprintf(f, "(<");
fprintf_position(f, &(q->q->tpos));
fprintf(f, ">, {");
}
for(i = 0; i < num_constraints; i++){
if(!first_print)
fprintf(f, ",");
first_print = false;
fprintf(f, "<");
fprintf_position(f, &(re->constraints_r[i]->tpos));
fprintf(f, ">=%i", q->counters[i]);
}
fprintf(f, "})\n");
}
EOF
for DELTA_FUNC in "delta_qi" "delta_last"
do
for VERBOSE in ""
do
if [ ${DELTA_FUNC} = "delta_qi" ] ; then
SIZESET="re->size_first_set"
SETNAME="re->first_set"
FIRST_LAST="first"
else
SIZESET="re->size_last_set"
SETNAME="re->last_set"
FIRST_LAST="last"
fi
cat <<EOF
/**
delta(qi, letter)
This is the delta function applied to initial state and counter
Also initializes the memory structures needed
**/
EOF
if [ ${VERBOSE} ] ; then
printf "fac_state* fac_%s_verbose(regexp* re, char letter, FILE * f){\n" ${DELTA_FUNC}
else
printf "fac_state* fac_%s(regexp* re, char letter){\n" ${DELTA_FUNC}
fi
cat <<EOF
//size_t i;
// memset(q->counters, 0, re->num_constraints_r * sizeof ( int ) );
memcpy(q->counters, zero_counter, re->num_constraints_r * sizeof ( int ) );
// for(i = 0; i < re->num_constraints_r; i++)
// q->counters[i] = 0;
EOF
echo " if(anychar_${FIRST_LAST} >= 0){"
echo " q->q = ${SETNAME}[anychar_${FIRST_LAST}];"
echo " return q;"
echo " }"
echo " if(${FIRST_LAST}StateLetter[(int) letter] != 0){"
echo " q->q = ${FIRST_LAST}StateLetter[(int) letter];"
cat <<EOF
return q;
}
return (fac_state*) 0;
}
EOF
done
done
cat <<EOF
void fac_init(regexp* re){
size_t i;
char let_val;
q = (fac_state*) get_buf(sizeof(fac_state));
q->counters = (unsigned int*) calloc(re->num_constraints_r, sizeof(unsigned int));
zero_counter = (unsigned int*) calloc(re->num_constraints_r, sizeof(unsigned int));
EOF
for FIRSTLAST in "first" "last"
do
echo " ${FIRSTLAST}StateLetter = calloc(SCHAR_MAX - SCHAR_MIN + 1, sizeof(regexp*));"
echo " ${FIRSTLAST}StateLetter -= SCHAR_MIN;"
echo " anychar_${FIRSTLAST} = -1;"
echo " for(i = 0; i < re->size_${FIRSTLAST}_set; i++){"
echo " if(re->${FIRSTLAST}_set[i]->type == RE_ANY_CHAR)"
echo " anychar_${FIRSTLAST} = i;"
echo " else {"
echo " for(let_val = re->${FIRSTLAST}_set[i]->value.let_inf.let_val; let_val <= re->${FIRSTLAST}_set[i]->value.let_inf.upper; let_val++)"
echo " ${FIRSTLAST}StateLetter[(int) let_val] = re->${FIRSTLAST}_set[i];"
echo " }"
echo " }"
done
cat <<EOF
}
EOF
for DELTA_FUNC in "delta" "delta_prec"
do
for VERBOSE in "" "verbose"
do
if [ ${DELTA_FUNC} = "delta" ]; then
SETNAME="follow"
else
SETNAME="prec"
fi
cat <<EOF
/**
This is the delta function
**/
EOF
if [ ${VERBOSE} ] ; then
printf "fac_state* fac_%s_verbose(regexp* re, fac_state* state, char letter, FILE * f){\n" $DELTA_FUNC
else
printf "fac_state* fac_%s(regexp* re, fac_state* state, char letter){\n" $DELTA_FUNC
fi
cat <<EOF
size_t i, c, u;
bool found;
follow_set* foll_ql;
size_t foll_size;
size_t let_no;
EOF
SET_INDEX="0"
printDeltaBody
cat <<EOF
let_no = getCharAlphPos(letter);
if(getCharAlphPos(letter) == 0)
return (fac_state*) 0;
EOF
SET_INDEX="let_no"
printDeltaBody
cat <<EOF
return (fac_state*) 0;
}
EOF
done
done
for FINAL_FUNC in "final" "final_rev"
do
printf "bool fac_%s(regexp* re, fac_state* state){\n" $FINAL_FUNC
if [ ${FINAL_FUNC} = "final" ]; then
SETNAME="last"
else
SETNAME="first"
fi
cat <<EOF
bool final = false;
size_t i, size_set;
regexp** set;
EOF
printf " set = re->%s_set;\n" $SETNAME
printf " size_set = re->size_%s_set;\n" $SETNAME
cat <<EOF
for(i = 0; i < size_set; i++){
if(state->q == set[i]){
final = true;
break;
}
}
if(!final)
return false;
for(i = 0; i < re->num_constraints_r; i++){
if( position_incl ( re->constraints_r[i], state->q ) && ( state->counters[i] + 1 < re->constraints_r[i]->value.constr.lower ) )
return false;
}
return true;
}
EOF
done
for VERBOSE in "" "verbose"
do
if [ "${VERBOSE}" ] ; then
func_name_modifier="_verbose"
func_arg_mod=", outfile"
func_param_mod=", FILE * outfile"
else
func_name_modifier=""
func_arg_mod=""
func_param_mod=""
fi
for func_name in {"word_fac","word_fac_rev","whole_word_rev","whole_word"}
do
if [ "$func_name" = "word_fac" -o "$func_name" = "whole_word" ]
then
delta_qi_func="fac_delta_qi(fac, *(stringstart++) )"
delta_func="fac_delta( fac, fs, *(stringstart++) )"
final_func="fac_final(fac, fs)"
else
delta_qi_func="fac_delta_last(fac, *(--stringend) )"
delta_func="fac_delta_prec( fac, fs, *(--stringend))"
final_func="fac_final_rev(fac, fs)"
fi
if [ "$func_name" = "whole_word" -o "$func_name" = "whole_word_rev" ] ; then
MODIFIER=""
RET_TYPE="bool"
RET_FAIL="false"
else
MODIFIER=" - 1"
RET_TYPE="int"
RET_FAIL="-1"
fi
cat <<EOF
/**
Recognizes a word using the regexp* structure created from it
Exact recognition - the whole word must match
**/
EOF
printf "%s recognize_%s%s(char* stringstart, char* stringend, regexp* fac %s ){\n" "${RET_TYPE}" "${func_name}" "${func_name_modifier}" "${func_param_mod}"
cat <<EOF
fac_state* fs;
if(stringstart == stringend)
EOF
echo " return fac->nullable$MODIFIER;"
echo " fs = $delta_qi_func;"
if [ ${VERBOSE} ] ; then
echo ' fprintf_fac_state(outfile, true, fac);'
fi
echo " while(stringend > stringstart){"
echo " if(fs == 0)"
printf " return %s;\n" ${RET_FAIL}
if [ ${RET_TYPE} == "int" ] ; then
echo " if ( ${final_func} )"
echo " return stringend - stringstart;"
fi
echo " fs = ${delta_func};"
if [ ${VERBOSE} ] ; then
echo ' fprintf_fac_state(outfile, false, fac);'
fi
cat <<EOF
}
if(fs != 0)
EOF
echo " return ${final_func}${MODIFIER};"
echo " return ${RET_FAIL};"
echo "}"
done
done