File: rxanal.c

package info (click to toggle)
rplay 3.3.2-8
  • links: PTS
  • area: main
  • in suites: sarge, woody
  • size: 3,136 kB
  • ctags: 3,519
  • sloc: ansic: 42,159; makefile: 1,242; perl: 742; tcl: 345; sh: 312; java: 220
file content (732 lines) | stat: -rw-r--r-- 16,816 bytes parent folder | download | duplicates (15)
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*	Copyright (C) 1995, 1996 Tom Lord
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library 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 Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330, 
 * Boston, MA 02111-1307, USA. 
 */



#include "rxall.h"
#include "rxanal.h"
#include "rxbitset.h"
#include "rxsuper.h"




#ifdef __STDC__
int
rx_posix_analyze_rexp (struct rexp_node *** subexps,
		       size_t * re_nsub,
		       struct rexp_node * node,
		       int id)
#else
int
rx_posix_analyze_rexp (subexps, re_nsub, node, id)
     struct rexp_node *** subexps;
     size_t * re_nsub;
     struct rexp_node * node;
     int id;
#endif
{
  if (node)
    {
      size_t this_subexp;
      if (node->type == r_parens)
	{
	  if (node->params.intval >= 0)
	    {
	      this_subexp = *re_nsub;
	      ++*re_nsub;
	      if (!*subexps)
		*subexps = (struct rexp_node **)malloc (sizeof (struct rexp_node *) * *re_nsub);
	      else
		*subexps = (struct rexp_node **)realloc (*subexps,
							 sizeof (struct rexp_node *) * *re_nsub);
	    }
	}
      if (node->params.pair.left)
	id = rx_posix_analyze_rexp (subexps, re_nsub, node->params.pair.left, id);
      if (node->params.pair.right)
	id = rx_posix_analyze_rexp (subexps, re_nsub, node->params.pair.right, id);
      switch (node->type)
	{
	case r_cset:
	  node->len = 1;
	  node->observed = 0;
	  break;
 	case r_string:
 	  node->len = node->params.cstr.len;
 	  node->observed = 0;
 	  break;
	case r_cut:
	  node->len = 0;
	  node->observed = 0;
	  break;
	case r_concat:
	case r_alternate:
	  {
	    int lob, rob;
	    int llen, rlen;
	    lob = (!node->params.pair.left ? 0 : node->params.pair.left->observed);
	    rob = (!node->params.pair.right ? 0 : node->params.pair.right->observed);
	    llen = (!node->params.pair.left ? 0 : node->params.pair.left->len);
	    rlen = (!node->params.pair.right ? 0 : node->params.pair.right->len);
	    node->len = ((llen >= 0) && (rlen >= 0)
			 ? ((node->type == r_concat)
			    ? llen + rlen
			    : ((llen == rlen) ? llen : -1))
			 : -1);
	    node->observed = lob || rob;
	    break;
	  }
	case r_opt:
	case r_star:
	case r_plus:
	  node->len = -1;
	  node->observed = (node->params.pair.left
			    ? node->params.pair.left->observed
			    : 0);
	  break;

	case  r_interval:
	  node->len = -1;
	  node->observed = 1;
	  break;

	case r_parens:
	  if (node->params.intval >= 0)
	    {
	      node->observed = 1;
	      (*subexps)[this_subexp] = node;
	    }
	  else
	    node->observed = (node->params.pair.left
			      ? node->params.pair.left->observed
			      : 0);
	  node->len = (node->params.pair.left
		       ? node->params.pair.left->len
		       : 0);
	  break;
	case r_context:
	  switch (node->params.intval)
	    {
	    default:
	      node->observed = 1;
	      node->len = -1;
	      break;
	    case '^':
	    case '$':
	    case '=':
	    case '<':
	    case '>':
	    case 'b':
	    case 'B':
	    case '`':
	    case '\'':
	      node->observed = 1;
	      node->len = 0;
	      break;
	    }
	  break;
	}
      if (node->observed)
	node->id = id++;
      return id;
    }
  return id;
}

/* Returns 0 unless the pattern can match the empty string. */
#ifdef __STDC__
int
rx_fill_in_fastmap (int cset_size, unsigned char * map, struct rexp_node * exp)
#else
int
rx_fill_in_fastmap (cset_size, map, exp)
     int cset_size;
     unsigned char * map;
     struct rexp_node * exp;
#endif
{
  if (!exp)
    {
    can_match_empty:
      {
	int x;
	for (x = 0; x < cset_size; ++x)
	  map[x] = 1;
      }
      return 1;
    }
  
  switch (exp->type)
    {
    case r_cset:
      {
	int x;
	int most;
	
	most = exp->params.cset_size;
	for (x = 0; x < most; ++x)
	  if (RX_bitset_member (exp->params.cset, x))
	    map[x] = 1;
      }
      return 0;

    case r_string:
      if (exp->params.cstr.len)
 	{
	  map[exp->params.cstr.contents[0]] = 1;
	  return 0;
 	}
      else
	return 1;

    case r_cut:
      return 1;
      

    case r_concat:
      return rx_fill_in_fastmap (cset_size, map, exp->params.pair.left);

      /* Why not the right branch?  If the left branch
       * can't be empty it doesn't matter.  If it can, then
       * the fastmap is already saturated, and again, the
       * right branch doesn't matter.
       */


    case r_alternate:
      return (  rx_fill_in_fastmap (cset_size, map, exp->params.pair.left)
	      | rx_fill_in_fastmap (cset_size, map, exp->params.pair.right));

    case r_parens:
    case r_plus:
      return rx_fill_in_fastmap (cset_size, map, exp->params.pair.left);

    case r_opt:
    case r_star:
      goto can_match_empty;
      /* Why not the subtree?  These operators already saturate
       * the fastmap. 
       */

    case r_interval:
      if (exp->params.intval == 0)
	goto can_match_empty;
      else
	return rx_fill_in_fastmap (cset_size, map, exp->params.pair.left);
      
    case r_context:
      goto can_match_empty;
    }

  /* this should never happen but gcc seems to like it */
  return 0;
  
}


#ifdef __STDC__
int
rx_is_anchored_p (struct rexp_node * exp)
#else
int
rx_is_anchored_p (exp)
     struct rexp_node * exp;
#endif
{
  if (!exp)
    return 0;

  switch (exp->type)
    {
    case r_opt:
    case r_star:
    case r_cset:
    case r_string:
    case r_cut:
      return 0;

    case r_parens:
    case r_plus:
    case r_concat:
      return rx_is_anchored_p (exp->params.pair.left);

    case r_alternate:
      return (   rx_is_anchored_p (exp->params.pair.left)
	      && rx_is_anchored_p (exp->params.pair.right));


    case r_interval:
      if (exp->params.intval == 0)
	return 0;
      else
	return rx_is_anchored_p (exp->params.pair.left);
      
    case r_context:
      return (exp->params.intval == '^');
    }

  /* this should never happen but gcc seems to like it */
  return 0;
}



#ifdef __STDC__
enum rx_answers
rx_start_superstate (struct rx_classical_system * frame)
#else
enum rx_answers
rx_start_superstate (frame)
     struct rx_classical_system * frame;
#endif
{
  struct rx_superset * start_contents;
  struct rx_nfa_state_set * start_nfa_set;

  if (frame->rx->start_set)
    start_contents = frame->rx->start_set;
  else
    {
      {
	struct rx_possible_future * futures;
	futures = rx_state_possible_futures (frame->rx, frame->rx->start_nfa_states);

	if (!futures)
	  return rx_bogus;

	if (futures->next)
	  return rx_start_state_with_too_many_futures;

	start_nfa_set = futures->destset;
      }
      
      start_contents =
	rx_superstate_eclosure_union (frame->rx,
				      rx_superset_cons (frame->rx, 0, 0),
				      start_nfa_set);
      
      if (!start_contents)
	return rx_bogus;
	    
      start_contents->starts_for = frame->rx;
      frame->rx->start_set = start_contents;
    }

  if (   start_contents->superstate
      && (start_contents->superstate->rx_id == frame->rx->rx_id))
    {
      if (frame->state)
	{
	  rx_unlock_superstate (frame->rx, frame->state);
	}
      frame->state = start_contents->superstate;
      /* The cached superstate may be in a semifree state.
       * We need to lock it and preserve the invariant
       * that a locked superstate is never semifree.
       * So refresh it.
       */
      rx_refresh_this_superstate (frame->rx->cache, frame->state);
      rx_lock_superstate (frame->rx, frame->state);
      return rx_yes;
    }
  else
    {
      struct rx_superstate * state;

      rx_protect_superset (frame->rx, start_contents);
      state = rx_superstate (frame->rx, start_contents);
      rx_release_superset (frame->rx, start_contents);
      if (!state)
	return rx_bogus;
      if (frame->state)
	{
	  rx_unlock_superstate (frame->rx, frame->state);
	}
      frame->state = state;
      rx_lock_superstate (frame->rx, frame->state);
      return rx_yes;
    }
}




#ifdef __STDC__
enum rx_answers
rx_fit_p (struct rx_classical_system * frame, unsigned const char * burst, int len)
#else
enum rx_answers
rx_fit_p (frame, burst, len)
     struct rx_classical_system * frame;
     unsigned const char * burst;
     int len;
#endif
{
  struct rx_inx * inx_table;
  struct rx_inx * inx;

  if (!frame->state)
    return rx_bogus;

  if (!len)
    {
      frame->final_tag = frame->state->contents->is_final;
      return (frame->state->contents->is_final
	      ? rx_yes
	      : rx_no);
    }

  inx_table = frame->state->transitions;
  rx_unlock_superstate (frame->rx, frame->state);

  while (len--)
    {
      struct rx_inx * next_table;

      inx = inx_table + *burst;
      next_table = (struct rx_inx *)inx->data;
      while (!next_table)
	{
	  struct rx_superstate * state;
	  state = ((struct rx_superstate *)
		   ((char *)inx_table
		    - ((unsigned long)
		       ((struct rx_superstate *)0)->transitions)));

	  switch ((int)inx->inx)
	    {
	    case rx_backtrack:
	      /* RX_BACKTRACK means that we've reached the empty
	       * superstate, indicating that match can't succeed
	       * from this point.
	       */
	      frame->state = 0;
	      return rx_no;
	    
	    case rx_cache_miss:
	      /* Because the superstate NFA is lazily constructed,
	       * and in fact may erode from underneath us, we sometimes
	       * have to construct the next instruction from the hard way.
	       * This invokes one step in the lazy-conversion.
	       */
	      inx = 
		rx_handle_cache_miss
		  (frame->rx, state, *burst, inx->data_2);

	      if (!inx)
		{
		  frame->state = 0;
		  return rx_bogus;
		}
	      next_table = (struct rx_inx *)inx->data;
	      continue;
		

	      /* No other instructions are legal here.
	       * (In particular, this function does not handle backtracking
	       * or the related instructions.)
	       */
	    default:
	      frame->state = 0;
	      return rx_bogus;
	  }
	}
      inx_table = next_table;
      ++burst;
    }

  if (inx->data_2)		/* indicates a final superstate */
    {
      frame->final_tag = (int)inx->data_2;
      frame->state = ((struct rx_superstate *)
		      ((char *)inx_table
		       - ((unsigned long)
			  ((struct rx_superstate *)0)->transitions)));
      rx_lock_superstate (frame->rx, frame->state);
      return rx_yes;
    }
  frame->state = ((struct rx_superstate *)
		  ((char *)inx_table
		   - ((unsigned long)
		      ((struct rx_superstate *)0)->transitions)));
  rx_lock_superstate (frame->rx, frame->state);
  return rx_no;
}




#ifdef __STDC__
enum rx_answers
rx_advance (struct rx_classical_system * frame, unsigned const char * burst, int len)
#else
enum rx_answers
rx_advance (frame, burst, len)
     struct rx_classical_system * frame;
     unsigned const char * burst;
     int len;
#endif
{
  struct rx_inx * inx_table;

  if (!frame->state)
    return rx_bogus;

  if (!len)
    return rx_yes;

  inx_table = frame->state->transitions;
  rx_unlock_superstate (frame->rx, frame->state);

  while (len--)
    {
      struct rx_inx * inx;
      struct rx_inx * next_table;

      inx = inx_table + *burst;
      next_table = (struct rx_inx *)inx->data;
      while (!next_table)
	{
	  struct rx_superstate * state;
	  state = ((struct rx_superstate *)
		   ((char *)inx_table
		    - ((unsigned long)
		       ((struct rx_superstate *)0)->transitions)));

	  switch ((int)inx->inx)
	    {
	    case rx_backtrack:
	      /* RX_BACKTRACK means that we've reached the empty
	       * superstate, indicating that match can't succeed
	       * from this point.
	       */
	      frame->state = 0;
	      return rx_no;
	    
	    case rx_cache_miss:
	      /* Because the superstate NFA is lazily constructed,
	       * and in fact may erode from underneath us, we sometimes
	       * have to construct the next instruction from the hard way.
	       * This invokes one step in the lazy-conversion.
	       */
	      inx = 
		rx_handle_cache_miss
		  (frame->rx, state, *burst, inx->data_2);

	      if (!inx)
		{
		  frame->state = 0;
		  return rx_bogus;
		}
	      next_table = (struct rx_inx *)inx->data;
	      continue;
		

	      /* No other instructions are legal here.
	       * (In particular, this function does not handle backtracking
	       * or the related instructions.)
	       */
	    default:
	      frame->state = 0;
	      return rx_bogus;
	  }
	}
      inx_table = next_table;
      ++burst;
    }
  
  frame->state = ((struct rx_superstate *)
		  ((char *)inx_table
		   - ((unsigned long)
		      ((struct rx_superstate *)0)->transitions)));
  rx_lock_superstate (frame->rx, frame->state);
  return rx_yes;
}



#ifdef __STDC__
int
rx_advance_to_final (struct rx_classical_system * frame, unsigned const char * burst, int len)
#else
int
rx_advance_to_final (frame, burst, len)
     struct rx_classical_system * frame;
     unsigned const char * burst;
     int len;
#endif
{
  int initial_len;
  struct rx_inx * inx_table;
  struct rx_superstate * this_state;

  if (!frame->state)
    return 0;

  if (!len)
    {
      frame->final_tag = frame->state->contents->is_final;
      return 0;
    }

  inx_table = frame->state->transitions;

  initial_len = len;

  this_state = frame->state;

  while (len--)
    {
      struct rx_inx * inx;
      struct rx_inx * next_table;

      /* this_state holds the state for the position we're
       * leaving.  this_state is locked. 
       */
      inx = inx_table + *burst;
      next_table = (struct rx_inx *)inx->data;

      while (!next_table)
	{
	  struct rx_superstate * state;

	  state = ((struct rx_superstate *)
		   ((char *)inx_table
		    - ((unsigned long)
		       ((struct rx_superstate *)0)->transitions)));
	  
	  switch ((int)inx->inx)
	    {
	    case rx_backtrack:
	      /* RX_BACKTRACK means that we've reached the empty
	       * superstate, indicating that match can't succeed
	       * from this point.
	       *
	       * Return to the state for the position prior to what
	       * we failed at, and return that position.
	       */
	      frame->state = this_state;
	      frame->final_tag = this_state->contents->is_final;
	      return (initial_len - len) - 1;
	    
	    case rx_cache_miss:
	      /* Because the superstate NFA is lazily constructed,
	       * and in fact may erode from underneath us, we sometimes
	       * have to construct the next instruction from the hard way.
	       * This invokes one step in the lazy-conversion.
	       */
	      inx = rx_handle_cache_miss
		(frame->rx, state, *burst, inx->data_2);

	      if (!inx)
		{
		  rx_unlock_superstate (frame->rx, this_state);
		  frame->state = 0;
		  return -1;
		}
	      next_table = (struct rx_inx *)inx->data;
	      continue;
		

	      /* No other instructions are legal here.
	       * (In particular, this function does not handle backtracking
	       * or the related instructions.)
	       */
	    default:
	      rx_unlock_superstate (frame->rx, this_state);
	      frame->state = 0;
	      return -1;
	  }
	}

      /* Release the superstate for the preceeding position: */
      rx_unlock_superstate (frame->rx, this_state);

      /* Compute the superstate for the new position: */
      inx_table = next_table;
      this_state = ((struct rx_superstate *)
		    ((char *)inx_table
		     - ((unsigned long)
			((struct rx_superstate *)0)->transitions)));
      
      /* Lock it (see top-of-loop invariant): */
      rx_lock_superstate (frame->rx, this_state);
      
      /* Check to see if we should stop: */
      if (this_state->contents->is_final)
	{
	  frame->final_tag = this_state->contents->is_final;
	  frame->state = this_state;
	  return (initial_len - len);
	}
      
      ++burst;
    }

  /* Consumed all of the characters. */
  frame->state = this_state;
  frame->final_tag = this_state->contents->is_final;

  /* state already locked (see top-of-loop invariant) */
  return initial_len;
}




#ifdef __STDC__
void
rx_terminate_system (struct rx_classical_system * frame)
#else
void
rx_terminate_system (frame)
     struct rx_classical_system * frame;
#endif
{
  if (frame->state)
    {
      rx_unlock_superstate (frame->rx, frame->state);
      frame->state = 0;
    }
}









#ifdef __STDC__
void
rx_init_system (struct rx_classical_system * frame, struct rx * rx)
#else
void
rx_init_system (frame, rx)
     struct rx_classical_system * frame;
     struct rx * rx;
#endif
{
  frame->rx = rx;
  frame->state = 0;
}