File: tokenize.l

package info (click to toggle)
mmorph 2.3.4.2-9
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 920 kB
  • ctags: 904
  • sloc: ansic: 4,992; yacc: 1,215; lex: 417; makefile: 295; sh: 48; sed: 33; csh: 26
file content (417 lines) | stat: -rw-r--r-- 10,258 bytes parent folder | download | duplicates (6)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
%{
/*
    mmorph, MULTEXT morphology tool
    Version 2.3, October 1995
    Copyright (c) 1994,1995 ISSCO/SUISSETRA, Geneva, Switzerland
    Dominique Petitpierre, <petitp@divsun.unige.ch>
*/
/*
     tokenizer for user syntax
     Dominique Petitpierre, ISSCO, July 1994

     TODO: better handling of strings: a la lex, without necessity of
	   quoting when possible.
*/

#include "user.h"
#include "parse.h"
#include "y.tab.h"

#define NUL '\0'

//int	yylineno = 1;
int	linepos = 0;
static char	buf_str[MAXSTRLNG];
static char   *pbuf_str;
int	augmenting = FALSE;
int	allow_string_letters = TRUE; /* true until alphabets are declared */

#ifdef yywrap
#undef yywrap
#endif

#define MAX_INCLUDE_DEPTH 10
static FILE    *file_stack[MAX_INCLUDE_DEPTH];
static int	 line_no_stack[MAX_INCLUDE_DEPTH];
char	*file_name_stack[MAX_INCLUDE_DEPTH];
short	stack_index = -1;
static YY_BUFFER_STATE buffer_state_stack[MAX_INCLUDE_DEPTH];

static int	lexstr();
void	initfile();

#ifdef TEST
#define fatal_error \
    (void) printf("file=%s,line=%d\n",file_name_stack[stack_index],yylineno);\
    (void) printf
#endif

%}

isoalphanum	([a-zA-Z0-9_\241-\377.])
blank           ([ \t\240])
sep	        ([ \t\n\240])
notsep	        ([^ \t\n\240])
comment		(\;.*\n)
		
%x QSTRING INCLUDE ASTRING

%%


\?          { linepos += yyleng; return(ANY);}
\|          { linepos += yyleng; return(BAR);}
"<=>"	    { linepos += yyleng; return(BIARROW);}
\*	    { linepos += yyleng; return(CONCATBOUNDARY);}
\+          { linepos += yyleng; return(MORPHEMEBOUNDARY);}
\-          { linepos += yyleng; return(CONTEXTBOUNDARY);}
\~          { linepos += yyleng; return(WORDBOUNDARY);}
\$          { linepos += yyleng; return(DOLLAR);}
\=          { linepos += yyleng; return(EQUAL);}
\<	    { linepos += yyleng; return(LANGLE);}
"<-"	    { linepos += yyleng; return(LARROW);}
"<="	    { linepos += yyleng; return(COERCEARROW);}
\[          { linepos += yyleng; return(LBRA);}
"!="        { linepos += yyleng; return(NOTEQUAL);}
\>	    { linepos += yyleng; return(RANGLE);}
"=>"	    { linepos += yyleng; return(RARROW);}
\]	    { linepos += yyleng; return(RBRA);}
\:          { linepos += yyleng; return(COLON);}
\/          { linepos += yyleng; return(SLASH);}

\@{blank}*Alphabets  { linepos += yyleng; return(ALPHABETS);}
\@{blank}*Attributes { linepos += yyleng; return(ATTRIBUTES);}
\@{blank}*Classes    { linepos += yyleng; return(CLASSES);}
\@{blank}*Grammar    { linepos += yyleng; return(GRAMMAR);}
\@{blank}*Lexicon    { linepos += yyleng; return(LEXICON);}
\@{blank}*Pairs      { linepos += yyleng; return(PAIRS);}
\@{blank}*Spelling   { linepos += yyleng; return(SPELLING);}
\@{blank}*Types      { linepos += yyleng; return(TYPES);}

{isoalphanum}+/({sep}*{comment})*{sep}*: {
    /* declaration identifier name */
    linepos += yyleng;
    return(lexstr(DNAME,yytext));
    }

\'{notsep}+/{sep}({sep}*{comment})*{sep}*: {
    /* declaration identifier name */
    linepos += yyleng;
    return(lexstr(DNAME,yytext+1));
    }

{isoalphanum}+/({sep}*{comment})*{sep}*\[ {
    /* declaration identifier name */
    linepos += yyleng;
    return(lexstr(TNAME,yytext));
    }

\'{notsep}+/{sep}({sep}*{comment})*{sep}*\[ {
    /* declaration identifier name */
    linepos += yyleng;
    return(lexstr(TNAME,yytext+1));
    }

{isoalphanum}+ { /* identifier name */
    linepos += yyleng;
    return(lexstr(NAME,yytext));
    }

\'{notsep}+ { /* quoted identifier name */
    linepos += yyleng;
    return(lexstr(NAME,yytext+1));
    }


\"  { /* start string */
    linepos += yyleng;
    pbuf_str=buf_str;
    if (allow_string_letters 
        || current_letter_map == NULL) /* alphabets not yet declared */
	BEGIN(ASTRING);
    else
	BEGIN(QSTRING);
    }

<QSTRING>\\\n { /* ignore escaped newline */
    yylineno++;
    linepos=0;
    }

<INCLUDE,ASTRING>\\(0[0-7][0-7]*|.) { /* escape notation */

    unsigned int escaped_char;

    linepos += yyleng;
    if ((pbuf_str - buf_str) >= (MAXSTRLNG - 1)) {
        *pbuf_str=NUL;
        fatal_error("string too long: \"%s\"",buf_str);
        }
    switch (yytext[1]) {
        case 'n':
	    *pbuf_str++='\n';
	    break;
        case 't':
	    *pbuf_str++='\t';
	    break;
	case '0':
	    if (sscanf(yytext+1, "%o", &escaped_char) != 1
		|| escaped_char > MAXUCHAR
		|| escaped_char == 0)
		    fatal_error("%s value \"%s\" is not allowed",
		    "error while reading octal code:",
		    yytext);
	    *pbuf_str++ = (unsigned char) escaped_char;
	    break;
        default: 
	    *pbuf_str++=yytext[1];
	    break;
    }
    }

<QSTRING>\\(0[0-7][0-7]*|.) { /* escape notation */

     /* temp storage, because yytext does not like to be changed with \n */
    unsigned int   escaped_char;

    linepos += yyleng;
    if ((pbuf_str - buf_str) >= (MAXSTRLNG - 1)) {
	fatal_error("string too long"); /* TODO: print the string */
	}
    switch (yytext[1]) {
	case 'n':
	    escaped_char='\n';
	    break;
	case 't':
	    escaped_char='\t';
	    break;
	case '0':
	    if (sscanf(yytext+1, "%o", &escaped_char) != 1
		|| escaped_char > MAXUCHAR
		|| escaped_char == 0)
		    fatal_error("%s value \"%s\" is not allowed",
		    "error while reading octal code:",
		    yytext);
	    break;
	default:
	    escaped_char=yytext[1];
	    break;
    }
    if ((*pbuf_str++ = current_letter_map[escaped_char]) == 0 )
	fatal_error("symbol \"%c\" is not a %s",
		    escaped_char,
		    symbol_kind_str[current_letter_kind]);
    }

<ASTRING>\&{isoalphanum}+\; { /* long letter name a la SGML */
    linepos += yyleng;
    fatal_error("Sorry, %s \"%s\"",
		"cannot declare an alphabet symbol with such a name:",
		yytext);
    }

<QSTRING>\&{isoalphanum}+\; { /* long letter name a la SGML */
    t_str letter_name;

    linepos += yyleng;
    if ((pbuf_str - buf_str) >= (MAXSTRLNG - 1)) {
	*pbuf_str=NUL;
	fatal_error("string too long"); /* TODO: print string */
	}
	MY_STRDUP(letter_name, yytext + 1); /* find_letter will free it */
	letter_name[yyleng - 2] = '\0';
	*pbuf_str++ = *find_letter(letter_name,current_letter_kind);
    }

<QSTRING,INCLUDE,ASTRING>\n {
    fatal_error("newline not allowed in string; use \\n if you mean one");
    }

<ASTRING>\" {
    linepos += yyleng;
    *pbuf_str=NUL;
    BEGIN(INITIAL);
    return(lexstr(STRING,buf_str));
    }

<QSTRING>\" {
    linepos += yyleng;
    *pbuf_str=NUL;
    BEGIN(INITIAL);
    return(lexstr(LSTRING,buf_str));
    }

<INCLUDE>\"{blank}*/\n {
    linepos += yyleng;
    *pbuf_str=NUL;
    BEGIN(INITIAL);
    initfile(buf_str);
    }

<INCLUDE,ASTRING>. {
    linepos += yyleng;
    if ((pbuf_str - buf_str) >= (MAXSTRLNG - 1)) {
	*pbuf_str=NUL;
	fatal_error("string too long: \"%s\"",buf_str);
	}
    *pbuf_str++ = *yytext;
    }

<QSTRING>. {
    linepos += yyleng;
    if ((pbuf_str - buf_str) >= (MAXSTRLNG - 1)) {
	fatal_error("string too long"); /* TODO print string */
	}
    if ((*pbuf_str++ = current_letter_map[(unsigned char) *yytext]) == 0 )
	fatal_error("symbol \"%c\" is not a %s",
		    *yytext,
		    symbol_kind_str[current_letter_kind]);
    }

{blank} { /* separator blank */
    linepos += yyleng;
    }

\n  { /* separator newline */
    yylineno++;
    linepos=0;
    }

{comment}    { /* comment */
    yylineno++;
    linepos=0;
    }

\#{blank}*include{blank}+\" {
    linepos += yyleng;
    pbuf_str=buf_str;
    BEGIN(INCLUDE);
    }


.           { /* error */
    linepos += yyleng;
    fatal_error("unexpected character: '%s'",yytext);
    }

<<EOF>>     {
    /*
   if ( ! end_parse)  {
	fatal_error("unexpected end of file");
    */
	yyterminate();
    }

%%


void
initfile(file_name)
char *file_name;

{
    char **including;

    if (stack_index >= MAX_INCLUDE_DEPTH - 1) {
       fatal_error(
	   "cannot include file \"%s\": too many nested include (maximum %u)",
	   file_name,MAX_INCLUDE_DEPTH);
    }
    stack_index++;
    if (file_name == NULL) {
	file_stack[stack_index] = stdin;
	file_name = "standard input";
	file_name_stack[stack_index] = file_name;
    } else
	if ((file_stack[stack_index] = fopen(file_name,"r")) == NULL) {
	    fatal_error("Cannot open file %s",file_name);
	}
	else {
	    MY_STRDUP(file_name_stack[stack_index],file_name);
	}
    for (including = file_name_stack;
         strcmp(file_name,*including) != 0;
	 including++);
    if (including != &file_name_stack[stack_index])
	fatal_error("#include recursion with file \"%s\"",file_name);
    yyin= file_stack[stack_index];
    if (stack_index) {
	line_no_stack[stack_index-1] = yylineno;
	buffer_state_stack[stack_index-1] = YY_CURRENT_BUFFER;
        yy_switch_to_buffer(yy_create_buffer(file_stack[stack_index],
	                                     YY_BUF_SIZE));
    }
    yylineno = 1;
    linepos = 0;
}

/* yywrap is called when an end of file is reached */
int
yywrap()

{
    if (augmenting
	&& strcmp(file_name_stack[stack_index], augment_file_name) == 0) {
	/* end of augment file */
	while(stack_index > 0 ) { /* close all including files */
	    (void) fclose(file_stack[stack_index]);
	    if (file_stack[stack_index] != stdin )
		MY_FREE(file_name_stack[stack_index]);
	    yy_delete_buffer(YY_CURRENT_BUFFER);
	    stack_index--;
	    yy_switch_to_buffer( buffer_state_stack[stack_index] );
	}
    }
    (void) fclose(file_stack[stack_index]);
    if (file_stack[stack_index] != stdin )
	MY_FREE(file_name_stack[stack_index]);
    stack_index--;
    if (stack_index < 0)
	return(1); /* no more files to read from, triggers <<EOF>> */
    else {
	yy_delete_buffer(YY_CURRENT_BUFFER);
	yy_switch_to_buffer( buffer_state_stack[stack_index] );
	yylineno = line_no_stack[stack_index];
	linepos=0;
	return(0); /* continue in enclosing file */
    }
}


#ifdef TEST
main( argc, argv )
int argc;
char **argv;

{
    int r;

    ++argv, --argc;  /* skip over program name */
    if ( argc > 0 )
            initfile(argv[0]);
    else
            initfile(NULL);

    while (r=yylex())
	(void) printf("%d\n",r);
}
#endif

static int
lexstr(token,string)
    int token;
    char *string;
{
#ifdef TEST
    (void) printf("%s\n",string);
#else
    if (token == STRING) {
	MY_STRDUP(yylval.string,string);
    }
    else {
	MY_STRDUP(yylval.letter_string,string);
    }
#endif
    return(token);
}
/* other procedures are in file "main.c" */