-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathputzer.l
317 lines (268 loc) · 7.35 KB
/
putzer.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
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
/* -*- coding: iso-8859-1; -*-
putzer.l : clean files, i.e. remove double spaces, spaces at
beginning or end of line, double newlines, overlong words etc.
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 2, 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 2004
by Sebastian Nagel, CIS Uni München */
%{
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "LC_ISOlatin1.h"
#define tolower(c) tolower_ISOlatin1_tab[c]
unsigned char NEWLINE = '\n';
unsigned char HSPACE = ' ';
typedef unsigned int BOOL;
enum { FALSE = 0, TRUE = 1 };
unsigned int stateINITIAL; /* faked state for INITIAL according to options */
struct option {
/* don't use bit-maps: costs are 1% of time, saves almost no space */
/* because option is frequently consulted in yylex */
BOOL hyphCont;
BOOL printHyphen;
BOOL convCase;
BOOL quiet;
BOOL set; /* options set in Lexer (do it only once) */
unsigned int maxWordLength;
unsigned int language; /* reserved */
} option;
void
echo_char (unsigned char c)
{
if (option.convCase == TRUE) /* case conversion */
fputc(tolower((unsigned char)c), yyout);
else
fputc(c, yyout);
}
void
echo_yytext ()
{
int i;
if (option.convCase == TRUE) /* case conversion */
{
for (i = 0; yytext[i] != '\0'; i++)
{
fputc(tolower((unsigned char)yytext[i]), yyout);
}
}
else
{
fputs(yytext, yyout);
}
}
void
max_word_control ()
{
if ( option.maxWordLength && ( yyleng > option.maxWordLength ) )
{
if ( ! option.quiet )
{
fprintf(stderr, "Word too long, stripping: \n ");
fputs(yytext, stderr);
fprintf(stderr, "\n =>\n ");
}
yytext[option.maxWordLength] = '\0';
if ( ! option.quiet )
{
fputs(yytext, stderr);
fputc('\n', stderr);
}
echo_yytext();
}
else
{
echo_yytext();
}
}
%}
%option 8bit batch ecs noyywrap noreject
LETTER [A-Za-z\xc0-\xd6\xd8-\xf6\xf7-\xff]
LETTER_UC [A-Z\xc0-\xd6\xd8-\xde]
LETTER_LC [a-z\xdf-\xf6\xf7-\xff]
DIGIT [0-9]
HSPACE [\x20\t\xa0]
/* normalize also non-breaking space */
VSPACE [\n\r\f\v]
CONTR [\x00-\x1f\x7f-\x9f]
SPACE ({HSPACE}|{VSPACE}|{CONTR})
SOFTHYPHEN \xad
HYPHEN [-{SOFTHYPHEN]
%s contHyph noCaseConvMaxWords maxWords
%%
%{
/* for processing options fake initial state */
if (! option.set) {
if (option.hyphCont == TRUE)
stateINITIAL = contHyph;
else if (option.maxWordLength != 0)
stateINITIAL = maxWords;
else if (option.convCase == FALSE)
stateINITIAL = noCaseConvMaxWords;
option.set = TRUE;
BEGIN(stateINITIAL);
}
%}
/**** hyphenated words: ****/
<contHyph>{
{LETTER}+{SOFTHYPHEN}/{LETTER} {
/* because there is no function to unput
the prefix of yytext (like yyless() for
a suffix), we must unput each character
separately */
int i;
int in_word = 0;
char *yycopy = strdup(yytext);
for( i = yyleng - 1; i >= 0; --i ) {
if ( in_word )
unput( yycopy[i] );
if ( yycopy[i] == '\xad' )
in_word = 1;
}
free(yycopy);
}
{LETTER}+{HYPHEN}{HSPACE}*{VSPACE}{HSPACE}*/{LETTER_LC} {
/* because there is no function to unput
the prefix of yytext (like yyless() for
a suffix), we must unput each character
separately */
int i;
int in_word = 0;
char *yycopy = strdup(yytext);
for( i = yyleng - 1; i >= 0; --i ) {
if ( in_word )
unput( yycopy[i] );
if ( (yycopy[i] == '-') || (yycopy[i] == '\xad') )
in_word = 1;
}
free(yycopy);
}
}
/*** spaces, newlines, control chars ***/
{HSPACE}*{VSPACE}{HSPACE}*{VSPACE}+{HSPACE}* { /* paragraph break */
fputc(NEWLINE, yyout);
fputc(NEWLINE, yyout);
}
{HSPACE}*{VSPACE}{HSPACE}* {
fputc(NEWLINE, yyout);
/* skip also preceeding or following hor. spaces */
}
^{HSPACE}+ ;
{HSPACE}+ fputc(HSPACE, yyout);
{CONTR}+ ;
/*** letters ***/
<noCaseConvMaxWords>{
{LETTER}+ ECHO;
}
<INITIAL>{
{LETTER_UC} echo_char(yytext[0]);
{LETTER_LC}+ ECHO;
}
<maxWords,contHyph>{
{LETTER}+ max_word_control();
}
/*** other chars ***/
. ECHO;
%%
void
help ()
{
fprintf(stdout,
"\n"
"putzer -- clean files: remove double spaces, spaces at beginning or end of line,\n"
" double newlines and ... (see options)\n"
"\n"
" putzer [options] [files]\n"
"\n"
" options:\n"
" -i <filen> input filename\n"
" -o <filen> output filename\n"
" -c combine continuation: hyphenated words on line breaks\n"
" will be put together\n"
" -l convert to lowercase (latin-1)\n"
" -m <int> maximal word length in chars:\n"
" longer words will be stripped\n"
" -q quiet: don't report errors\n"
" -h | -? print this help and exit\n"
" Other arguments will be read as input filenames.\n"
" If no input files are given, input is read from stdin.\n"
" If no output file is given, the tokenized text is written to stdout\n"
"\n");
printf("putzer, Sebastian Nagel ([email protected])\n");
exit(1);
}
int
main (int argc, char **argv)
{
int c;
while (1) {
c = getopt(argc, argv, "cli:o:m:qh?");
if (c == -1)
break;
switch (c)
{
case 'h':
case '?':
help();
break;
case 'q':
option.quiet = TRUE;
break;
case 'c':
option.hyphCont = TRUE;
break;
case 'l':
option.convCase = TRUE;
break;
case 'm':
if ( ! sscanf(optarg, "%d", &option.maxWordLength) )
fprintf(stderr, "Argument of -m must be an integer not \"%s\"!\n", optarg);
break;
case 'o':
if (optarg != NULL && (yyout = fopen(optarg, "w")) == NULL)
{
fprintf(stderr, "Can't open %s for writing!\n", optarg);
perror(optarg);
exit(1);
}
break;
case 'i':
if (optarg != NULL && (yyin = fopen(optarg, "r")) == NULL)
{
fprintf(stderr, "Can't read from %s!\n", optarg);
perror(optarg);
exit(1);
}
break;
}
}
if (optind < argc) /* remaing ARGVs are filenames */
{
while (optind < argc)
{
if (argv[optind] != NULL && (yyin = fopen(argv[optind], "r")) == NULL)
{
fprintf(stderr, "Can't read from %s!\n", argv[optind]);
perror(argv[optind]);
exit(1);
}
yylex();
optind++;
}
}
else /* default: read yyin/stdin, when no input-files are given */
{
yylex();
}
return 0;
}