summaryrefslogtreecommitdiff
path: root/lib/FLAT/CMD.pm
blob: 5bd0e2d94589ae5cb90f7119a64b456b5ffd4a11 (plain)
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
package FLAT::CMD;
use FLAT;
use FLAT::Regex;
use FLAT::NFA;
use FLAT::DFA;
use Carp;

=head1 NAME

CMD - Commandline interface for the Formal Language & Automata Toolkit

=head1 SYNOPSIS

CMD.pm is provides an interface to the C<fash> commandline utility that offers
certain features implemented in FLAT.  Consequently, this interface is also
available using the C<perl -MFLAT::CMD -e func> paradigm, but C<fash> makes
things a lot more convenient.

=head1 USAGE

All regular language objects in FLAT implement the following methods.
Specific regular language representations (regex, NFA, DFA) may implement
additional methods that are outlined in the repsective POD pages.

=cut

# Support for perl one liners - like what CPAN.pm uses #<- should move all to another file
use base 'Exporter'; #instead of: use Exporter (); @ISA = 'Exporter';
use vars qw(@EXPORT $AUTOLOAD);

@EXPORT = qw(compare dump dfa2gv nfa2gv pfa2gv dfa2undgv nfa2undgv pfa2undgv dfa2digraph
	     nfa2digraph pfa2digraph dfa2undirected nfa2undirected pfa2undirected random_pre random_re
             savedfa test help
	     );

sub AUTOLOAD {
    my($l) = $AUTOLOAD;
    $l =~ s/.*:://;
    my(%EXPORT);
    @EXPORT{@EXPORT} = '';
    if (exists $EXPORT{$l}){
	FLAT::CMD->$l(@_);
    }
}

sub help {
print <<END
__________             .__    ___________.____         ___________
\______   \ ___________|  |   \_   _____/|    |   _____\__    ___/
 |     ___// __ \_  __ \  |    |    __)  |    |   \__  \ |    |   
 |    |   \  ___/|  | \/  |__  |     \   |    |___ / __ \|    |   
 |____|    \___  >__|  |____/  \___  /   |_______ (____  /____|   
               \/                  \/            \/    \/    
	       
  Everything is wrt parallel regular expressions, i.e., 
  with the addtional shuffle operator, "&".  All this 
  means is that you can use the ambersand (&) as a symbol
  in the regular expressions you submit because it will be 
  detected as an operator.That said, if you avoid using
  the "&" operator, you can forget about all that shuffle
  business.

%perl -MFLAT::CMD -e
    "somestrings" 're1'       # creates all valid strings via acyclic path, no cycles yet
    "compare  're1','re2'"   # comares 2 regexs | see note [2] 
    "dump     're1'"         # dumps parse trees | see note[1]	   
    "dfa2gv  're1'"          # dumps graphviz digraph desc | see note[1]  
    "nfa2gv  're1'"          # dumps graphviz digraph desc | see note[1]  
    "pfa2gv  're1'"          # dumps graphviz digraph desc | see note[1]  
    "dfa2undgv  're1'"       # dumps graphviz undirected graph desc | see note[1]  
    "nfa2undgv  're1'"       # dumps graphviz undirected graph desc | see note[1]  
    "pfa2undgv  're1'"       # dumps graphviz undirected graph desc | see note[1]  
    "dfa2digraph 're1'"      # dumps directed graph without transitions
    "nfa2digraph 're1'"      # dumps directed graph without transitions
    "pfa2digraph 're1'"      # dumps directed graph without transitions
    "dfa2undirected 're1'"   # dumps undirected graph without transitions
    "nfa2undirected 're1'"   # dumps undirected graph without transitions
    "pfa2undirected 're1'"   # dumps undirected graph without transitions
     random_pre 
     random_re
    "savedfa 're1'"          # converts PRE to min dfa, then serializes to disk
    "test 'regex' 'string1'" # give a regex, reports if subsequent strings are valid
     help

NOTES:
[1] This means you could presumably do something like the following:
    %perl -MFLAT -e command < text_file_with_1_regex_per_line.txt
                    ^^^^^^^   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[2] This command compares the minimal DFAs of each regular expression;
    if there exists a exact 1-1 mapping of symbols, states, and 
    transitions then the DFAs are considered equal.  This means that 
    "abc" will be equal to "def"  To make matters more confusing, "ab+ac" 
    would be equivalent to "xy+xz"; or worse yet, "z(x+y)". So to the 
    'compare' command, "ab+ac" == "xy+xz" == "z(x+y)". This however 
    does not translate into the situation where "ab+ac" will accept 
    the same LITERAL strings as "z(x+y)" because the symbols are obviously
    different.	    
		   
CREDITS:
Blockhead, CPAN.pm (for the example of how to implement these one liners), 
and #perl on irc.freenode.net for pointing out something I missed when 
trying to copy CPAN one liner majik.

Perl FLAT and all included modules are released under the same terms as Perl
itself.  Cheers.

SEE:
http://www.0x743.com/flat

END
}

# save to a dat file
sub savedfa {
    my $PRE = shift;
    # neat a better way to get input via stdin
    if (!$PRE) {
      while (<>) {
        chomp;
        $PRE = $_;
        last;
      }
    } 
    use FLAT::Regex::WithExtraOps;
    use FLAT::PFA;
    use FLAT::NFA;
    use FLAT::DFA;
    use Storable;
    # caches results, loads them in if detexted
    my $dfa = FLAT::Regex::WithExtraOps->new($PRE)->as_pfa->as_nfa->as_dfa->as_min_dfa->trim_sinks;
    store $dfa, "$PRE.dat";
}

# dumps directed graph using Kundu notation
# Usage:
# perl -MFLAT -e "pfa2directed('a&b&c&d*e*')"
sub test {
  use FLAT::Regex::WithExtraOps;
  use FLAT::PFA;
  use FLAT::NFA;
  use FLAT::DFA;
  # handles multiple strings; first is considered the regex
  if (@_) 
  { my $FA = FLAT::Regex::WithExtraOps->new(shift @_)->as_pfa()->as_nfa->as_dfa(); 
    foreach (@_)
    { if ($FA->is_valid_string($_)) {
        print "(+): $_\n";
      } else {
        print "(-): $_\n";
      }     
    } 
  } else {
    my $FA;
    while (<STDIN>) {
      chomp;
      if ($. == 1) { #<-- uses first line as regex!
        $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa->as_dfa();
      } else {
	if ($FA->is_valid_string($_)) {
	  print "(+): $_\n";
        } else {
	  print "(-): $_\n";
	}      
      }
    }
  }
}

# dumps parse tree
# Usage:
# perl -MFLAT -e "dump('re1','re2',...,'reN')"
# perl -MFLAT -e dump < list_of_regexes.dat
sub dump {
  use FLAT::Regex::WithExtraOps;
  use Data::Dumper;
  if (@_) 
  { foreach (@_)
    { my $PRE = FLAT::Regex::WithExtraOps->new($_);
      print Dumper($PRE); }} 
  else    
  { while (<STDIN>) 
     { chomp;
       my $PRE = FLAT::Regex::WithExtraOps->new($_);
       print Dumper($PRE); }
  }
}

# dumps graphviz notation
# Usage:
# perl -MFLAT -e "dfa2gv('a&b&c&d*e*')"
sub dfa2gv {
  use FLAT::Regex::WithExtraOps;
  use FLAT::DFA;
  use FLAT::NFA;
  use FLAT::PFA;  
  if (@_) 
  { foreach (@_)
    { my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa()->as_dfa()->as_min_dfa()->trim_sinks();
      print $FA->as_graphviz;} }
  else    
  { while (<STDIN>) 
     { chomp;
       my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa()->as_dfa->as_min_dfa()->trim_sinks();
       print $FA->as_graphviz;} 
  }
}

# dumps graphviz notation
# Usage:
# perl -MFLAT -e "nfa2gv('a&b&c&d*e*')"
sub nfa2gv {
  use FLAT::Regex::WithExtraOps;
  use FLAT::DFA;
  use FLAT::NFA;
  use FLAT::PFA;  
  if (@_) 
  { foreach (@_)
    { my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa();
      print $FA->as_graphviz;} }
  else    
  { while (<STDIN>) 
     { chomp;
       my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa();
       print $FA->as_graphviz;} 
  }
}

# dumps graphviz notation
# Usage:
# perl -MFLAT -e "pfa2gv('a&b&c&d*e*')"
sub pfa2gv {
  use FLAT::Regex::WithExtraOps;
  use FLAT::PFA;
  if (@_) 
  { foreach (@_)
    { my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa();
      print $FA->as_graphviz;} }
  else    
  { while (<STDIN>) 
     { chomp;
       my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa();
       print $FA->as_graphviz;} 
  }
}

#as_undirected_graphviz

# dumps graphviz notation
# Usage:
# perl -MFLAT -e "dfa2undgv('a&b&c&d*e*')"
sub dfa2undgv {
  use FLAT::Regex::WithExtraOps;
  use FLAT::DFA;
  use FLAT::NFA;
  use FLAT::PFA;  
  if (@_) 
  { foreach (@_)
    { my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa()->as_dfa()->as_min_dfa()->trim_sinks();
      print $FA->as_undirected_graphviz;} }
  else    
  { while (<STDIN>) 
     { chomp;
       my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa()->as_dfa->as_min_dfa()->trim_sinks();
       print $FA->as_undirected_graphviz;} 
  }
}

# dumps graphviz notation
# Usage:
# perl -MFLAT -e "nfa2undgv('a&b&c&d*e*')"
sub nfa2undgv {
  use FLAT::Regex::WithExtraOps;
  use FLAT::DFA;
  use FLAT::NFA;
  use FLAT::PFA;  
  if (@_) 
  { foreach (@_)
    { my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa();
      print $FA->as_undirected_graphviz;} }
  else    
  { while (<STDIN>) 
     { chomp;
       my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa();
       print $FA->as_undirected_graphviz;} 
  }
}

# dumps graphviz notation
# Usage:
# perl -MFLAT -e "pfa2undgv('a&b&c&d*e*')"
sub pfa2undgv {
  use FLAT::Regex::WithExtraOps;
  use FLAT::PFA;
  if (@_) 
  { foreach (@_)
    { my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa();
      print $FA->as_undirected_graphviz;} }
  else    
  { while (<STDIN>) 
     { chomp;
       my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa();
       print $FA->as_undirected_graphviz;} 
  }
}

# dumps directed graph using Kundu notation
# Usage:
# perl -MFLAT -e "dfa2directed('a&b&c&d*e*')"
sub dfa2digraph {
  use FLAT::Regex::WithExtraOps;
  use FLAT::DFA;
  use FLAT::NFA;
  use FLAT::PFA;  
  # trims sink states from min-dfa since transitions are gone 
  if (@_) 
  { foreach (@_)
    { my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa()->as_dfa->as_min_dfa->trim_sinks(); 
       print $FA->as_digraph;} }
  else    
  { while (<STDIN>) 
     { chomp;
       my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa()->as_dfa->as_min_dfa->trim_sinks();
       print $FA->as_digraph;} 
  }
  print "\n";
}

# dumps directed graph using Kundu notation
# Usage:
# perl -MFLAT -e "nfa2directed('a&b&c&d*e*')"
sub nfa2digraph {
  use FLAT::Regex::WithExtraOps;
  use FLAT::DFA;
  use FLAT::NFA;
  use FLAT::PFA;  
  if (@_) 
  { foreach (@_)
    { my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa(); 
       print $FA->as_digraph;} }
  else    
  { while (<STDIN>) 
     { chomp;
       my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa();
       print $FA->as_digraph;} 
  }
  print "\n";
}

# dumps directed graph using Kundu notation
# Usage:
# perl -MFLAT -e "pfa2directed('a&b&c&d*e*')"
sub pfa2digraph {
  use FLAT::Regex::WithExtraOps;
  use FLAT::PFA;
  if (@_) 
  { foreach (@_)
    { my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa(); 
       print $FA->as_digraph;} }
  else    
  { while (<STDIN>) 
     { chomp;
       my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa();
       print $FA->as_digraph;} 
  }
  print "\n";
}

# dumps undirected graph using Kundu notation
# Usage:
# perl -MFLAT -e "dfa2undirected('a&b&c&d*e*')"
sub dfa2undirected {
  use FLAT::Regex::WithExtraOps;
  use FLAT::DFA;
  use FLAT::NFA;
  use FLAT::PFA;  
  # trims sink states from min-dfa since transitions are gone 
  if (@_) 
  { foreach (@_)
    { my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa()->as_dfa->as_min_dfa->trim_sinks(); 
       print $FA->as_undirected;} }
  else    
  { while (<STDIN>) 
     { chomp;
       my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa()->as_dfa->as_min_dfa->trim_sinks();
       print $FA->as_undirected;} 
  }
  print "\n";
}

# dumps undirected graph using Kundu notation
# Usage:
# perl -MFLAT -e "nfa2undirected('a&b&c&d*e*')"
sub nfa2undirected {
  use FLAT::Regex::WithExtraOps;
  use FLAT::DFA;
  use FLAT::NFA;
  use FLAT::PFA;  
  if (@_) 
  { foreach (@_)
    { my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa(); 
       print $FA->as_undirected;} }
  else    
  { while (<STDIN>) 
     { chomp;
       my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa();
       print $FA->as_undirected;} 
  }
  print "\n";
}

# dumps undirected graph using Kundu notation
# Usage:
# perl -MFLAT -e "pfa2undirected('a&b&c&d*e*')"
sub pfa2undirected {
  use FLAT::Regex::WithExtraOps;
  use FLAT::PFA;
  if (@_) 
  { foreach (@_)
    { my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa(); 
       print $FA->as_undirected;} }
  else    
  { while (<STDIN>) 
     { chomp;
       my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa();
       print $FA->as_undirected;} 
  }
  print "\n";
}

# compares 2 give PREs
# Usage:
# perl -MFLAT -e "compare('a','a&b&c&d*e*')" #<-- no match, btw
sub compare {
  use FLAT::Regex::WithExtraOps;
  use FLAT::DFA;
  use FLAT::PFA;
  my $PFA1 = FLAT::Regex::WithExtraOps->new(shift)->as_pfa();
  my $PFA2 = FLAT::Regex::WithExtraOps->new(shift)->as_pfa();
  my $DFA1 = $PFA1->as_nfa->as_min_dfa;
  my $DFA2 = $PFA2->as_nfa->as_min_dfa;
  if ($DFA1->equals($DFA2)) {
    print "Yes\n";
  } else {
    print "No\n";
  }
}

# prints random PRE
# Usage:
# perl -MFLAT -e random_pre
sub random_pre {
  my $and_chance = shift;
  # skirt around deep recursion warning annoyance
  local $SIG{__WARN__} = sub { $_[0] =~ /^Deep recursion/ or warn $_[0] };
  srand $$;
  my %CMDLINEOPTS = ();
  # Percent chance of each operator occuring
  $CMDLINEOPTS{LENGTH} = 32;
  $CMDLINEOPTS{OR} = 6;
  $CMDLINEOPTS{STAR} = 10;
  $CMDLINEOPTS{OPEN} = 5;
  $CMDLINEOPTS{CLOSE} = 0;
  $CMDLINEOPTS{n} = 1;
  $CMDLINEOPTS{AND} = 10; #<-- default    
  $CMDLINEOPTS{AND} = $and_chance if ($and_chance == 0); #<-- to make it just an re (no shuffle)
  

  my $getRandomChar = sub {
    my $ch = '';
    # Get a random character between 0 and 127.
    do {
      $ch = int(rand 2);
    } while ($ch !~ m/[a-zA-Z0-9]/);  
    return $ch;
  };

  my $getRandomRE = sub {
    my $str = '';
    my @closeparens = ();
    for (1..$CMDLINEOPTS{LENGTH}) {
      $str .= $getRandomChar->();  
      # % chance of an "or"
      if (int(rand 100) < $CMDLINEOPTS{OR}) {
	$str .= "|1";
      } elsif (int(rand 100) < $CMDLINEOPTS{AND}) {
	$str .= "&0";
      } elsif (int(rand 100) < $CMDLINEOPTS{STAR}) {
	$str .= "*1";     
      } elsif (int(rand 100) < $CMDLINEOPTS{OPEN}) {
	$str .= "(";
	push(@closeparens,'0101)');
      } elsif (int(rand 100) < $CMDLINEOPTS{CLOSE} && @closeparens) {
	$str .= pop(@closeparens);
      }
    }
    # empty out @closeparens if there are still some left
    if (@closeparens) {
      $str .= join('',@closeparens);  
    }
    return $str;
  };

  for (1..$CMDLINEOPTS{n}) {
    print $getRandomRE->(),"\n";  
  } 
}

# prints random RE (no & operator)
# Usage:
# perl -MFLAT -e random_re
sub random_re {
  shift->random_pre(0);
}

1;

__END__

=head1 AUTHORS & ACKNOWLEDGEMENTS

FLAT is written by Mike Rosulek E<lt>mike at mikero dot comE<gt> and 
Brett Estrade E<lt>estradb at gmail dot comE<gt>.

The initial version (FLAT::Legacy) by Brett Estrade was work towards an 
MS thesis at the University of Southern Mississippi.

Please visit the Wiki at http://www.0x743.com/flat

=head1 LICENSE

This module is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.