~ubuntu-branches/ubuntu/hoary/malaga/hoary

« back to all changes in this revision

Viewing changes to source/patterns.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Bushnell, BSG
  • Date: 2004-08-20 12:58:50 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040820125850-rx9s8bn0ep8jgist
Tags: 6.13-4
This should have been urgency=high, because it is an important and
long-delayed accomodation to new upstream with a bajillion bug fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of Malaga, a system for Natural Language Analysis.
2
 
 * Copyright (C) 1995-1999 Bjoern Beutel
3
 
 *
4
 
 * Bjoern Beutel
5
 
 * Universitaet Erlangen-Nuernberg
6
 
 * Abteilung fuer Computerlinguistik
7
 
 * Bismarckstrasse 12
8
 
 * D-91054 Erlangen
9
 
 * e-mail: malaga@linguistik.uni-erlangen.de 
10
 
 *
11
 
 * This program is free software; you can redistribute it and/or modify
12
 
 * it under the terms of the GNU General Public License as published by
13
 
 * the Free Software Foundation; either version 2 of the License, or
14
 
 * (at your option) any later version.
15
 
 *
16
 
 * This program is distributed in the hope that it will be useful,
17
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 
 * GNU General Public License for more details.
20
 
 *
21
 
 * You should have received a copy of the GNU General Public License
22
 
 * along with this program; if not, write to the Free Software
23
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
24
 
 
25
 
/* description ==============================================================*/
26
 
 
27
 
/* This module contains function to compile and execute pattern matching 
28
 
 * strings. */
29
 
 
30
 
/* includes =================================================================*/
31
 
 
32
 
#include <ctype.h>
33
 
#include <string.h>
34
 
#include <stdio.h>
35
 
#include <stdlib.h>
36
 
#include "basic.h"
37
 
 
38
 
#undef GLOBAL
39
 
#define GLOBAL
40
 
 
41
 
#include "patterns.h"
42
 
 
43
 
/* constants ================================================================*/
44
 
 
45
 
#define SPECIAL_CHARS ".[]-^()*?+|\\" /* characters with a special meaning */
46
 
 
47
 
#define PATTERN_STACK_MAX 50 /* maximum size of pattern stack */
48
 
 
49
 
/* These are the instructions for matching a pattern.
50
 
 *
51
 
 * A pattern is a 0-terminated sequence of CHARs, defined as follows:
52
 
 * C[] is the code vector.
53
 
 * PC means pattern counter (pointer to char following a PAT_ instruction).
54
 
 * S[] is the string to be examined.
55
 
 * I is the index into the string.
56
 
 * CS[] is the code stack and IS[] is the string index stack.
57
 
 * SP is the stack pointer (same for RS[] and IS[]).
58
 
 * VS[] and VE[] are vectors to store the index of start and end of vars. */
59
 
enum
60
 
{
61
 
  /* code 0 is EOS */
62
 
  PAT_JUMP = 1, /* PC += (byte_t) C[PC]; */      
63
 
  PAT_JUMP_NOW, /* SP++; CS[SP] = PC+1; IS[SP] = I; PC += (byte_t) C[PC]; */
64
 
  PAT_JUMP_LATER, /* SP++; CS[SP] = PC + (byte_t) C[PC]; IS[SP] = I; PC++; */
65
 
  PAT_MATCH_ANY, /* if S[I] != EOS
66
 
                  * then I++;
67
 
                  * else fail; */
68
 
  PAT_MATCH_CLASS, /* if (S[I] in {C[PC+1],..,C[PC+C[PC]})
69
 
                    * then I++; PC += C[PC]+1;
70
 
                    * else fail; */ 
71
 
  PAT_MATCH_NOT_CLASS, /* if (S[I] in {C[PC+1],..,C[PC+C[PC]})
72
 
                        * then fail;
73
 
                        * else I++; PC += C[PC]+1; */
74
 
  PAT_START_VAR_0, /* VS[0] = I; */
75
 
  PAT_START_VAR_1, /* VS[1] = I; */
76
 
  PAT_START_VAR_2, /* VS[2] = I; */
77
 
  PAT_START_VAR_3, /* VS[3] = I; */
78
 
  PAT_START_VAR_4, /* VS[4] = I; */
79
 
  PAT_END_VAR_0, /* VE[0] = I; */
80
 
  PAT_END_VAR_1, /* VE[1] = I; */
81
 
  PAT_END_VAR_2, /* VE[2] = I; */
82
 
  PAT_END_VAR_3, /* VE[3] = I; */
83
 
  PAT_END_VAR_4 /* VE[4] = I; */
84
 
};
85
 
 
86
 
/* forwards =================================================================*/
87
 
 
88
 
FORWARD void local_compile_pattern (text_t text, 
89
 
                                    string_t *string_ptr, 
90
 
                                    bool_t *may_be_empty);
91
 
 
92
 
/* functions ================================================================*/
93
 
 
94
 
LOCAL bool_t is_pattern_char (string_t s)
95
 
{
96
 
  return ((s[0] == '\\' && s[1] != EOS && strchr (SPECIAL_CHARS, s[1]) != NULL)
97
 
          || (IS_PRINT (s[0]) && strchr (SPECIAL_CHARS, s[0]) == NULL));
98
 
}
99
 
 
100
 
/*---------------------------------------------------------------------------*/
101
 
 
102
 
LOCAL char pattern_char (string_t *string_ptr)
103
 
/* See if *<string_ptr> points to a valid char or to an escape sequence.
104
 
 * Return the character code. Show an error if not valid. */
105
 
{
106
 
  string_t s = *string_ptr;
107
 
  char c;
108
 
  
109
 
  if (s[0] == '\\' && s[1] != EOS && strchr (SPECIAL_CHARS, s[1]) != NULL)
110
 
  {
111
 
    c = TO_LOWER (s[1]);
112
 
    s += 2;
113
 
  }
114
 
  else if (IS_PRINT (s[0]) && strchr (SPECIAL_CHARS, s[0]) == NULL)
115
 
  {
116
 
    c = TO_LOWER (s[0]);
117
 
    s++;
118
 
  }
119
 
  else if (s[0] == EOS)
120
 
    error ("unexpected end of pattern");
121
 
  else
122
 
    error ("invalid char \"%c\" in pattern", s[0]);
123
 
  
124
 
  *string_ptr = s;
125
 
  return c;
126
 
}
127
 
 
128
 
/*---------------------------------------------------------------------------*/
129
 
 
130
 
LOCAL char offset (int_t offset)
131
 
/* Return <offset> as a char. */
132
 
{
133
 
  /* See if offset can be stored in a char. */
134
 
  if ((char) offset != offset || offset == 0)
135
 
    error ("pattern too complex");
136
 
 
137
 
  return (char) offset;
138
 
}
139
 
 
140
 
/*---------------------------------------------------------------------------*/
141
 
 
142
 
LOCAL void compile_char_class (text_t text, string_t *string_ptr)
143
 
/* Compile a character class at <*string_ptr>.
144
 
 * Save the resulting pattern in <text>. */
145
 
{
146
 
  string_t s = *string_ptr;
147
 
  int_t patch_index;
148
 
 
149
 
  s++;
150
 
  if (*s == '^') 
151
 
  {
152
 
    s++;
153
 
    add_char_to_text (text, PAT_MATCH_NOT_CLASS);
154
 
  } 
155
 
  else
156
 
    add_char_to_text (text, PAT_MATCH_CLASS);
157
 
 
158
 
  patch_index = text_length (text);
159
 
 
160
 
  do /* Read chars and ranges. */
161
 
  {
162
 
    int_t ca, ce, c;
163
 
 
164
 
    if (*s == EOS)
165
 
      error ("missing \"]\" in pattern");
166
 
    
167
 
    ca = ORD (pattern_char (&s));
168
 
    if (*s == '-') 
169
 
    {
170
 
      s++;
171
 
      ce = ORD (pattern_char (&s));
172
 
      if (ca > ce)
173
 
        error ("invalid range \"%c-%c\" in pattern", (char) ca, (char) ce);
174
 
    }
175
 
    else 
176
 
      ce = ca;
177
 
 
178
 
    for (c = ca; c <= ce; c++)
179
 
      add_char_to_text (text, (char) c);
180
 
 
181
 
  } while (*s != ']');
182
 
  
183
 
  *string_ptr = ++s;
184
 
  insert_char_in_text (text, offset (text_length (text) - patch_index), 
185
 
                       patch_index); 
186
 
}
187
 
 
188
 
/*---------------------------------------------------------------------------*/
189
 
 
190
 
LOCAL void compile_atom (text_t text, 
191
 
                         string_t *string_ptr, 
192
 
                         bool_t *may_be_empty)
193
 
/* Compile an atom at <*string_ptr>. 
194
 
 * Save the resulting pattern in <text>. 
195
 
 * <may_be_empty> == TRUE iff the atom may match an empty string. */
196
 
{
197
 
  string_t s = *string_ptr;
198
 
  int_t start, length;
199
 
  bool_t local_may_be_empty;
200
 
 
201
 
  *may_be_empty = TRUE;
202
 
  while (TRUE)
203
 
  {
204
 
    local_may_be_empty = FALSE;
205
 
    start = text_length (text);
206
 
 
207
 
    if (*s == '[') 
208
 
      compile_char_class (text, &s);
209
 
    else if (*s == '.') 
210
 
    {
211
 
      s++;
212
 
      add_char_to_text (text, PAT_MATCH_ANY);
213
 
    } 
214
 
    else if (*s == '(')
215
 
    {
216
 
      s++;
217
 
      local_compile_pattern (text, &s, &local_may_be_empty);
218
 
      if (*s++ != ')')
219
 
        error ("missing \")\" in pattern");
220
 
    }   
221
 
    else if (is_pattern_char (s))
222
 
      add_char_to_text (text, pattern_char (&s)); 
223
 
    else
224
 
      break;
225
 
    
226
 
    length = text_length (text) - start;
227
 
 
228
 
    /* There may be a postfix operator following parentheses. */
229
 
    if (*s == '?')
230
 
    {
231
 
      s++;
232
 
      insert_char_in_text (text, PAT_JUMP_NOW, start);
233
 
      insert_char_in_text (text, offset (2 + length), start + 1);
234
 
    }
235
 
    else if (*s == '*') 
236
 
    {
237
 
      s++;
238
 
      if (local_may_be_empty)
239
 
        error ("pattern argument for \"*\" may be empty");
240
 
      insert_char_in_text (text, PAT_JUMP, start);
241
 
      insert_char_in_text (text, offset (2 + length), start + 1);
242
 
      add_char_to_text (text, PAT_JUMP_LATER);
243
 
      add_char_to_text (text, offset (- length));
244
 
    }
245
 
    else if (*s == '+') 
246
 
    {
247
 
      s++;
248
 
      if (local_may_be_empty)
249
 
        error ("pattern argument for \"+\" may be empty");
250
 
      add_char_to_text (text, PAT_JUMP_LATER);
251
 
      add_char_to_text (text, offset (- length));
252
 
      *may_be_empty = FALSE;
253
 
    }
254
 
    else
255
 
      *may_be_empty &= local_may_be_empty;
256
 
  }
257
 
  
258
 
  *string_ptr = s;
259
 
}
260
 
 
261
 
/*---------------------------------------------------------------------------*/
262
 
 
263
 
LOCAL void local_compile_pattern (text_t text, 
264
 
                                  string_t *string_ptr, 
265
 
                                  bool_t *may_be_empty)
266
 
/* Convert <string> to a pattern to be used as input to "match_pattern".
267
 
 * If <pattern_var_no> != -1, mark the pattern so the string matching this
268
 
 * pattern will be stored in <pattern_var[pattern_var_no]>.
269
 
 * The result pattern must be freed with "free" after usage. */
270
 
{
271
 
  string_t s = *string_ptr;
272
 
  int_t alternative_length, alternative_start;
273
 
  bool_t local_may_be_empty;
274
 
 
275
 
  alternative_start = text_length (text);
276
 
  compile_atom (text, &s, may_be_empty);
277
 
  alternative_length = text_length (text) - alternative_start;
278
 
 
279
 
  while (*s == '|') 
280
 
  {
281
 
    s++;
282
 
 
283
 
    /* Add jump from start of last alternative to start of this alternative. */
284
 
    insert_char_in_text (text, PAT_JUMP_LATER, alternative_start++);
285
 
    insert_char_in_text (text, alternative_length + 4, alternative_start++);
286
 
 
287
 
    alternative_start = text_length (text);
288
 
    compile_atom (text, &s, &local_may_be_empty);
289
 
    alternative_length = text_length (text) - alternative_start;
290
 
    
291
 
    *may_be_empty |= local_may_be_empty;
292
 
 
293
 
    /* Add from end of last alternative to end of this alternative. */
294
 
    insert_char_in_text (text, PAT_JUMP, alternative_start++);
295
 
    if (*s == '|')
296
 
      insert_char_in_text (text, alternative_length + 4, alternative_start++);
297
 
    else
298
 
      insert_char_in_text (text, alternative_length + 2, alternative_start++);
299
 
  }
300
 
 
301
 
  *string_ptr = s;
302
 
}
303
 
 
304
 
/*---------------------------------------------------------------------------*/
305
 
 
306
 
GLOBAL string_t compile_pattern (string_t string, int_t pattern_var_no)
307
 
/* Convert <string> to a pattern to be used as input to "match_pattern".
308
 
 * If <pattern_var_no> != -1, mark the pattern so the string matching this
309
 
 * pattern will be stored in <pattern_var[pattern_var_no]>.
310
 
 * The result pattern must be freed with "free" after usage. */
311
 
{
312
 
  text_t text = new_text ();
313
 
  bool_t may_be_empty;
314
 
 
315
 
  if (pattern_var_no != -1)
316
 
    add_char_to_text (text, PAT_START_VAR_0 + pattern_var_no);
317
 
 
318
 
  local_compile_pattern (text, &string, &may_be_empty);
319
 
  if (*string != EOS)
320
 
    error ("illegal char \"%c\" in pattern", *string);
321
 
 
322
 
  if (pattern_var_no != -1)
323
 
    add_char_to_text (text, PAT_END_VAR_0 + pattern_var_no);
324
 
 
325
 
  return text_to_string (&text);
326
 
}
327
 
 
328
 
/*---------------------------------------------------------------------------*/
329
 
 
330
 
GLOBAL bool_t match_pattern (string_t string, string_t pattern)
331
 
/* Test whether <string> matches <pattern> (a string of chars compiled with
332
 
 * "compile_pattern") and set substrings in <pattern_var>.
333
 
 * The substrings can be freed with "free_mem". */
334
 
{
335
 
  struct {string_t string, pattern;} stack[PATTERN_STACK_MAX]; /* backtrack */
336
 
  struct {string_t start; string_t end;} var[PATTERN_VAR_MAX];
337
 
  int_t sp, i;
338
 
  bool_t found_mismatch;
339
 
 
340
 
  sp = 0;
341
 
  found_mismatch = FALSE;
342
 
 
343
 
  for (i = 0; i < PATTERN_VAR_MAX; i++)
344
 
    var[i].start = var[i].end = NULL;
345
 
 
346
 
  while (! found_mismatch) 
347
 
  {
348
 
    char code = *pattern++;
349
 
    
350
 
    switch (code) 
351
 
    {
352
 
    case EOS:
353
 
      if (*string == EOS)
354
 
      {
355
 
        for (i = 0; i < PATTERN_VAR_MAX; i++)
356
 
        {
357
 
          if (var[i].start != NULL && var[i].end != NULL)
358
 
          {
359
 
            free_mem (pattern_var + i);
360
 
            pattern_var[i] = new_string (var[i].start, var[i].end);
361
 
          }
362
 
        }
363
 
        return TRUE;
364
 
      }
365
 
      else
366
 
        found_mismatch = TRUE;
367
 
      break;
368
 
 
369
 
    case PAT_JUMP:
370
 
      pattern += (byte_t) *pattern - 1;
371
 
      break;
372
 
      
373
 
    case PAT_JUMP_NOW:
374
 
      if (sp == PATTERN_STACK_MAX)
375
 
        error ("match pattern is too complex");
376
 
      
377
 
      stack[sp].string = string;
378
 
      stack[sp].pattern = pattern + 1;
379
 
      sp++;
380
 
      pattern += (byte_t) *pattern - 1;
381
 
      break;
382
 
      
383
 
    case PAT_JUMP_LATER:
384
 
      if (sp == PATTERN_STACK_MAX)
385
 
        error ("match pattern is too complex");
386
 
      
387
 
      stack[sp].string = string;
388
 
      stack[sp].pattern = pattern + (byte_t) *pattern - 1;
389
 
      sp++;
390
 
      pattern++;
391
 
      break;
392
 
      
393
 
    case PAT_MATCH_ANY:
394
 
      if (*string++ == EOS)
395
 
        found_mismatch = TRUE;
396
 
      break;
397
 
      
398
 
    case PAT_MATCH_CLASS:
399
 
      if (*string == EOS)
400
 
        found_mismatch = TRUE;
401
 
      else 
402
 
      {
403
 
        string_t index;
404
 
        
405
 
        index = pattern + 1;
406
 
        pattern += (byte_t) *pattern + 1;
407
 
        while (index < pattern && TO_LOWER (*string) != *index)
408
 
          index++;
409
 
        string++;
410
 
        
411
 
        if (index >= pattern)
412
 
          found_mismatch = TRUE;
413
 
      }
414
 
      break;
415
 
      
416
 
    case PAT_MATCH_NOT_CLASS:
417
 
      if (*string == EOS)
418
 
        found_mismatch = TRUE;
419
 
      else
420
 
      {
421
 
        string_t index;
422
 
        
423
 
        index = pattern + 1;
424
 
        pattern += (byte_t) *pattern + 1;
425
 
        while (index < pattern && TO_LOWER (*string) != *index)
426
 
          index++;
427
 
        string++;
428
 
        
429
 
        if (index < pattern)
430
 
          found_mismatch = TRUE;
431
 
      }
432
 
      break;
433
 
 
434
 
    case PAT_START_VAR_0:
435
 
    case PAT_START_VAR_1:
436
 
    case PAT_START_VAR_2:
437
 
    case PAT_START_VAR_3:
438
 
    case PAT_START_VAR_4:
439
 
      var[code - PAT_START_VAR_0].start = string;
440
 
      break;
441
 
      
442
 
    case PAT_END_VAR_0:
443
 
    case PAT_END_VAR_1:
444
 
    case PAT_END_VAR_2:
445
 
    case PAT_END_VAR_3:
446
 
    case PAT_END_VAR_4:
447
 
      var[code - PAT_END_VAR_0].end = string;
448
 
      break;
449
 
      
450
 
    default:
451
 
      if (code != TO_LOWER (*string))
452
 
        found_mismatch = TRUE;
453
 
      string++;
454
 
      break;
455
 
    }
456
 
    
457
 
    /* If this path was not successful and there is another path, try it. */
458
 
    if (found_mismatch && sp > 0) 
459
 
    {
460
 
      sp--;
461
 
      string = stack[sp].string;
462
 
      pattern = stack[sp].pattern;
463
 
      found_mismatch = FALSE;
464
 
    }
465
 
  }
466
 
  
467
 
  return FALSE;
468
 
}
469
 
 
470
 
/* end of file ==============================================================*/