fst-compiler.yy
10.8 KB
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
326
327
328
329
330
331
332
%{
/*******************************************************************/
/* */
/* FILE fst-compiler.yy */
/* MODULE fst-compiler */
/* PROGRAM SFST */
/* AUTHOR Helmut Schmid, IMS, University of Stuttgart */
/* */
/*******************************************************************/
#include <stdio.h>
#include "make-compact.h"
#include "interface.h"
using std::cerr;
extern int yylineno;
extern char *yytext;
void yyerror(char *text);
void warn(char *text);
void warn2(char *text, char *text2);
int yylex( void );
int yyparse( void );
static int Switch=0;
Transducer *Result;
%}
%union {
int number;
Twol_Type type;
Repl_Type rtype;
char *name;
char *value;
unsigned char uchar;
unsigned int longchar;
Character character;
Transducer *expression;
Range *range;
Ranges *ranges;
Contexts *contexts;
}
%token <number> NEWLINE ALPHA COMPOSE PRINT POS INSERT SWITCH
%token <type> ARROW
%token <rtype> REPLACE
%token <name> SYMBOL VAR SVAR RVAR RSVAR
%token <value> STRING STRING2 UTF8CHAR
%token <uchar> CHARACTER
%type <longchar> LCHAR
%type <character> CODE
%type <expression> RE
%type <range> RANGE VALUE VALUES
%type <ranges> RANGES
%type <contexts> CONTEXT CONTEXT2 CONTEXTS CONTEXTS2
%left PRINT INSERT
%left ARROW REPLACE
%left COMPOSE
%left '|'
%left '-'
%left '&'
%left SEQ
%left '!' '^' '_'
%left '*' '+'
%%
ALL: ASSIGNMENTS RE NEWLINES { Result=result($2, Switch); }
;
ASSIGNMENTS: ASSIGNMENTS ASSIGNMENT {}
| ASSIGNMENTS NEWLINE {}
| /* nothing */ {}
;
ASSIGNMENT: VAR '=' RE { if (def_var($1,$3)) warn2("assignment of empty transducer to",$1); }
| RVAR '=' RE { if (def_rvar($1,$3)) warn2("assignment of empty transducer to",$1); }
| SVAR '=' VALUES { if (def_svar($1,$3)) warn2("assignment of empty symbol range to",$1); }
| RSVAR '=' VALUES { if (def_svar($1,$3)) warn2("assignment of empty symbol range to",$1); }
| RE PRINT STRING { write_to_file($1, $3); }
| ALPHA RE { def_alphabet($2); }
;
RE: RE ARROW CONTEXTS2 { $$ = restriction($1,$2,$3,0); }
| RE '^' ARROW CONTEXTS2 { $$ = restriction($1,$3,$4,1); }
| RE '_' ARROW CONTEXTS2 { $$ = restriction($1,$3,$4,-1); }
| RE REPLACE CONTEXT2 { $$ = replace_in_context(minimise(explode($1)),$2,$3,false); }
| RE REPLACE '?' CONTEXT2 { $$ = replace_in_context(minimise(explode($1)),$2,$4,true);}
| RE REPLACE '(' ')' { $$ = replace(minimise(explode($1)), $2, false); }
| RE REPLACE '?' '(' ')' { $$ = replace(minimise(explode($1)), $2, true); }
| RE RANGE ARROW RANGE RE { $$ = make_rule($1,$2,$3,$4,$5); }
| RE RANGE ARROW RANGE { $$ = make_rule($1,$2,$3,$4,NULL); }
| RANGE ARROW RANGE RE { $$ = make_rule(NULL,$1,$2,$3,$4); }
| RANGE ARROW RANGE { $$ = make_rule(NULL,$1,$2,$3,NULL); }
| RE COMPOSE RE { $$ = composition($1, $3); }
| '{' RANGES '}' ':' '{' RANGES '}' { $$ = make_mapping($2,$6); }
| RANGE ':' '{' RANGES '}' { $$ = make_mapping(add_range($1,NULL),$4); }
| '{' RANGES '}' ':' RANGE { $$ = make_mapping($2,add_range($5,NULL)); }
| RE INSERT CODE ':' CODE { $$ = freely_insert($1, $3, $5); }
| RE INSERT CODE { $$ = freely_insert($1, $3, $3); }
| RANGE ':' RANGE { $$ = new_transducer($1,$3); }
| RANGE { $$ = new_transducer($1,$1); }
| VAR { $$ = var_value($1); }
| RVAR { $$ = rvar_value($1); }
| RE '*' { $$ = repetition($1); }
| RE '+' { $$ = repetition2($1); }
| RE '?' { $$ = optional($1); }
| RE RE %prec SEQ { $$ = catenate($1, $2); }
| '!' RE { $$ = negation($2); }
| SWITCH RE { $$ = switch_levels($2); }
| '^' RE { $$ = upper_level($2); }
| '_' RE { $$ = lower_level($2); }
| RE '&' RE { $$ = conjunction($1, $3); }
| RE '-' RE { $$ = subtraction($1, $3); }
| RE '|' RE { $$ = disjunction($1, $3); }
| '(' RE ')' { $$ = $2; }
| STRING { $$ = read_words($1); }
| STRING2 { $$ = read_transducer($1); }
;
RANGES: RANGE RANGES { $$ = add_range($1,$2); }
| { $$ = NULL; }
;
RANGE: '[' VALUES ']' { $$=$2; }
| '[' '^' VALUES ']' { $$=complement_range($3); }
| '[' RSVAR ']' { $$=rsvar_value($2); }
| '.' { $$=NULL; }
| CODE { $$=add_value($1,NULL); }
;
CONTEXTS2: CONTEXTS { $$ = $1; }
| '(' CONTEXTS ')' { $$ = $2; }
;
CONTEXTS: CONTEXT ',' CONTEXTS { $$ = add_context($1,$3); }
| CONTEXT { $$ = $1; }
;
CONTEXT2: CONTEXT { $$ = $1; }
| '(' CONTEXT ')' { $$ = $2; }
;
CONTEXT : RE POS RE { $$ = make_context($1, $3); }
| POS RE { $$ = make_context(NULL, $2); }
| RE POS { $$ = make_context($1, NULL); }
;
VALUES: VALUE VALUES { $$=append_values($1,$2); }
| VALUE { $$ = $1; }
;
VALUE: LCHAR '-' LCHAR { $$=add_values($1,$3,NULL); }
| SVAR { $$=svar_value($1); }
| LCHAR { $$=add_value(character_code($1),NULL); }
| CODE { $$=add_value($1,NULL); }
;
LCHAR: CHARACTER { $$=$1; }
| UTF8CHAR { $$=utf8toint($1); free($1); }
| '.' { $$='.'; }
| '!' { $$='!'; }
| '?' { $$='?'; }
| '{' { $$='{'; }
| '}' { $$='}'; }
| ')' { $$=')'; }
| '(' { $$='('; }
| '&' { $$='&'; }
| '|' { $$='|'; }
| '*' { $$='*'; }
| '+' { $$='+'; }
| ':' { $$=':'; }
| ',' { $$=','; }
| '=' { $$='='; }
| '_' { $$='_'; }
| '^' { $$='^'; }
| '-' { $$='-'; }
;
CODE: CHARACTER { $$=character_code($1); }
| UTF8CHAR { $$=symbol_code($1); }
| SYMBOL { $$=symbol_code($1); }
;
NEWLINES: NEWLINE NEWLINES {}
| /* nothing */ {}
;
%%
extern FILE *yyin;
static int Compact=0;
static int LowMem=0;
/*******************************************************************/
/* */
/* yyerror */
/* */
/*******************************************************************/
void yyerror(char *text)
{
cerr << "\n" << FileName << ":" << yylineno << ": " << text << " at: ";
cerr << yytext << "\naborted.\n";
exit(1);
}
/*******************************************************************/
/* */
/* warn */
/* */
/*******************************************************************/
void warn(char *text)
{
cerr << "\n" << FileName << ":" << yylineno << ": warning: " << text << "!\n";
}
/*******************************************************************/
/* */
/* warn2 */
/* */
/*******************************************************************/
void warn2(char *text, char *text2)
{
cerr << "\n" << FileName << ":" << yylineno << ": warning: " << text << ": ";
cerr << text2 << "\n";
}
/*******************************************************************/
/* */
/* get_flags */
/* */
/*******************************************************************/
void get_flags( int *argc, char **argv )
{
for( int i=1; i<*argc; i++ ) {
if (strcmp(argv[i],"-c") == 0) {
Compact = 1;
argv[i] = NULL;
}
else if (strcmp(argv[i],"-l") == 0) {
LowMem = 1;
argv[i] = NULL;
}
else if (strcmp(argv[i],"-q") == 0) {
Verbose = 0;
argv[i] = NULL;
}
else if (strcmp(argv[i],"-s") == 0) {
Switch = 1;
argv[i] = NULL;
}
}
// remove flags from the argument list
int k;
for( int i=k=1; i<*argc; i++)
if (argv[i] != NULL)
argv[k++] = argv[i];
*argc = k;
}
/*******************************************************************/
/* */
/* main */
/* */
/*******************************************************************/
int main( int argc, char *argv[] )
{
FILE *file;
get_flags(&argc, argv);
if (argc < 3) {
fprintf(stderr,"\nUsage: %s [options] infile outfile\n", argv[0]);
fprintf(stderr,"\nOPTIONS:\n");
fprintf(stderr,"-c\tStore the transducer in fst-infl2 format.\n");
fprintf(stderr,"-l\tStore the transducer in fst-infl3 format.\n");
fprintf(stderr,"-s\tSwitch the upper and lower levels producing a transducer for generation rather than recognition.\n");
fprintf(stderr,"-q\tquiet mode\n\n");
exit(1);
}
if ((file = fopen(argv[1],"rt")) == NULL) {
fprintf(stderr,"\nError: Cannot open grammar file \"%s\"\n\n", argv[1]);
exit(1);
}
FileName = argv[1];
Result = NULL;
TheAlphabet.utf8 = UTF8;
yyin = file;
try {
yyparse();
Result->alphabet.utf8 = UTF8;
if (Verbose)
cerr << "\n";
if (Result->is_empty())
warn("resulting transducer is empty");
if ((file = fopen(argv[2],"wb")) == NULL) {
fprintf(stderr,"\nError: Cannot open output file %s\n\n", argv[2]);
exit(1);
}
if (Compact) {
MakeCompactTransducer ca(*Result);
delete Result;
ca.store(file);
}
else if (LowMem)
Result->store_lowmem(file);
else
Result->store(file);
fclose(file);
}
catch(const char* p) {
cerr << "\n" << p << "\n\n";
exit(1);
}
}