| 1 | package FLAT::CMD; | ||
| 2 | use FLAT; | ||
| 3 | use FLAT::Regex; | ||
| 4 | use FLAT::NFA; | ||
| 5 | use FLAT::DFA; | ||
| 6 | use Carp; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | CMD - Commandline interface for the Formal Language & Automata Toolkit | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | CMD.pm is provides an interface to the C<fash> commandline utility that offers | ||
| 15 | certain features implemented in FLAT. Consequently, this interface is also | ||
| 16 | available using the C<perl -MFLAT::CMD -e func> paradigm, but C<fash> makes | ||
| 17 | things a lot more convenient. | ||
| 18 | |||
| 19 | =head1 USAGE | ||
| 20 | |||
| 21 | All regular language objects in FLAT implement the following methods. | ||
| 22 | Specific regular language representations (regex, NFA, DFA) may implement | ||
| 23 | additional methods that are outlined in the repsective POD pages. | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | # Support for perl one liners - like what CPAN.pm uses #<- should move all to another file | ||
| 28 | use parent 'Exporter'; #instead of: use Exporter (); @ISA = 'Exporter'; | ||
| 29 | use vars qw(@EXPORT $AUTOLOAD); | ||
| 30 | |||
| 31 | @EXPORT = qw(compare dump dfa2jflap nfa2jflap dfa2gv nfa2gv pfa2gv dfa2undgv nfa2undgv pfa2undgv dfa2digraph | ||
| 32 | nfa2digraph pfa2digraph dfa2undirected nfa2undirected pfa2undirected random_pre random_re | ||
| 33 | savedfa test help | ||
| 34 | ); | ||
| 35 | |||
| 36 | sub AUTOLOAD { | ||
| 37 | my ($l) = $AUTOLOAD; | ||
| 38 | $l =~ s/.*:://; | ||
| 39 | my (%EXPORT); | ||
| 40 | @EXPORT{@EXPORT} = ''; | ||
| 41 | if ( exists $EXPORT{$l} ) { | ||
| 42 | FLAT::CMD->$l(@_); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | sub help { | ||
| 47 | print <<END; | ||
| 48 | __________ .__ ___________.____ ___________ | ||
| 49 | \______ \ ___________| | \_ _____/| | _____\__ ___/ | ||
| 50 | | ___// __ \_ __ \ | | __) | | \__ \ | | | ||
| 51 | | | \ ___/| | \/ |__ | \ | |___ / __ \| | | ||
| 52 | |____| \___ >__| |____/ \___ / |_______ (____ /____| | ||
| 53 | \/ \/ \/ \/ | ||
| 54 | |||
| 55 | Everything is wrt parallel regular expressions, i.e., | ||
| 56 | with the addtional shuffle operator, "&". All this | ||
| 57 | means is that you can use the ambersand (&) as a symbol | ||
| 58 | in the regular expressions you submit because it will be | ||
| 59 | detected as an operator.That said, if you avoid using | ||
| 60 | the "&" operator, you can forget about all that shuffle | ||
| 61 | business. | ||
| 62 | |||
| 63 | %perl -MFLAT::CMD -e | ||
| 64 | "somestrings" 're1' # creates all valid strings via acyclic path, no cycles yet | ||
| 65 | "compare 're1','re2'" # comares 2 regexs | see note [2] | ||
| 66 | "dump 're1'" # dumps parse trees | see note[1] | ||
| 67 | "dfa2jflap 're1'" # dumps JFLAT XML desc | see note[1] | ||
| 68 | "nfa2jflap 're1'" # dumps JFLAT XML desc | see note[1] | ||
| 69 | "dfa2gv 're1'" # dumps graphviz digraph desc | see note[1] | ||
| 70 | "nfa2gv 're1'" # dumps graphviz digraph desc | see note[1] | ||
| 71 | "pfa2gv 're1'" # dumps graphviz digraph desc | see note[1] | ||
| 72 | "dfa2undgv 're1'" # dumps graphviz undirected graph desc | see note[1] | ||
| 73 | "nfa2undgv 're1'" # dumps graphviz undirected graph desc | see note[1] | ||
| 74 | "pfa2undgv 're1'" # dumps graphviz undirected graph desc | see note[1] | ||
| 75 | "dfa2digraph 're1'" # dumps directed graph without transitions | ||
| 76 | "nfa2digraph 're1'" # dumps directed graph without transitions | ||
| 77 | "pfa2digraph 're1'" # dumps directed graph without transitions | ||
| 78 | "dfa2undirected 're1'" # dumps undirected graph without transitions | ||
| 79 | "nfa2undirected 're1'" # dumps undirected graph without transitions | ||
| 80 | "pfa2undirected 're1'" # dumps undirected graph without transitions | ||
| 81 | random_pre | ||
| 82 | random_re | ||
| 83 | "savedfa 're1'" # converts PRE to min dfa, then serializes to disk | ||
| 84 | "test 'regex' 'string1'" # give a regex, reports if subsequent strings are valid | ||
| 85 | help | ||
| 86 | |||
| 87 | NOTES: | ||
| 88 | [1] This means you could presumably do something like the following: | ||
| 89 | %perl -MFLAT -e command < text_file_with_1_regex_per_line.txt | ||
| 90 | ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 91 | [2] This command compares the minimal DFAs of each regular expression; | ||
| 92 | if there exists a exact 1-1 mapping of symbols, states, and | ||
| 93 | transitions then the DFAs are considered equal. This means that | ||
| 94 | "abc" will be equal to "def" To make matters more confusing, "ab+ac" | ||
| 95 | would be equivalent to "xy+xz"; or worse yet, "z(x+y)". So to the | ||
| 96 | 'compare' command, "ab+ac" == "xy+xz" == "z(x+y)". This however | ||
| 97 | does not translate into the situation where "ab+ac" will accept | ||
| 98 | the same LITERAL strings as "z(x+y)" because the symbols are obviously | ||
| 99 | different. | ||
| 100 | |||
| 101 | CREDITS: | ||
| 102 | Blockhead, CPAN.pm (for the example of how to implement these one liners), | ||
| 103 | and #perl on irc.freenode.net for pointing out something I missed when | ||
| 104 | trying to copy CPAN one liner majik. | ||
| 105 | |||
| 106 | Perl FLAT and all included modules are released under the same terms as Perl | ||
| 107 | itself. Cheers. | ||
| 108 | |||
| 109 | SEE: | ||
| 110 | http://www.0x743.com/flat | ||
| 111 | |||
| 112 | END | ||
| 113 | } | ||
| 114 | |||
| 115 | # save to a dat file | ||
| 116 | sub savedfa { | ||
| 117 | my $PRE = shift; | ||
| 118 | |||
| 119 | # neat a better way to get input via stdin | ||
| 120 | if ( !$PRE ) { | ||
| 121 | while (<>) { | ||
| 122 | chomp; | ||
| 123 | $PRE = $_; | ||
| 124 | last; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | use FLAT::Regex::WithExtraOps; | ||
| 128 | use FLAT::PFA; | ||
| 129 | use FLAT::NFA; | ||
| 130 | use FLAT::DFA; | ||
| 131 | use Storable; | ||
| 132 | |||
| 133 | # caches results, loads them in if detexted | ||
| 134 | my $dfa = FLAT::Regex::WithExtraOps->new($PRE)->as_pfa->as_nfa->as_dfa->as_min_dfa->trim_sinks; | ||
| 135 | store $dfa, "$PRE.dat"; | ||
| 136 | } | ||
| 137 | |||
| 138 | # dumps directed graph using Kundu notation | ||
| 139 | # Usage: | ||
| 140 | # perl -MFLAT -e "pfa2directed('a&b&c&d*e*')" | ||
| 141 | sub test { | ||
| 142 | use FLAT::Regex::WithExtraOps; | ||
| 143 | use FLAT::PFA; | ||
| 144 | use FLAT::NFA; | ||
| 145 | use FLAT::DFA; | ||
| 146 | |||
| 147 | # handles multiple strings; first is considered the regex | ||
| 148 | if (@_) { | ||
| 149 | my $FA = FLAT::Regex::WithExtraOps->new( shift @_ )->as_pfa()->as_nfa->as_dfa(); | ||
| 150 | foreach (@_) { | ||
| 151 | if ( $FA->is_valid_string($_) ) { | ||
| 152 | print "(+): $_\n"; | ||
| 153 | } | ||
| 154 | else { | ||
| 155 | print "(-): $_\n"; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | } | ||
| 159 | else { | ||
| 160 | my $FA; | ||
| 161 | while (<STDIN>) { | ||
| 162 | chomp; | ||
| 163 | if ( $. == 1 ) { #<-- uses first line as regex! | ||
| 164 | $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa->as_dfa(); | ||
| 165 | } | ||
| 166 | else { | ||
| 167 | if ( $FA->is_valid_string($_) ) { | ||
| 168 | print "(+): $_\n"; | ||
| 169 | } | ||
| 170 | else { | ||
| 171 | print "(-): $_\n"; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | } | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | # dumps parse tree | ||
| 179 | # Usage: | ||
| 180 | # perl -MFLAT -e "dump('re1','re2',...,'reN')" | ||
| 181 | # perl -MFLAT -e dump < list_of_regexes.dat | ||
| 182 | sub dump { | ||
| 183 | use FLAT::Regex::WithExtraOps; | ||
| 184 | use Data::Dumper; | ||
| 185 | if (@_) { | ||
| 186 | foreach (@_) { | ||
| 187 | my $PRE = FLAT::Regex::WithExtraOps->new($_); | ||
| 188 | print Dumper($PRE); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | else { | ||
| 192 | while (<STDIN>) { | ||
| 193 | chomp; | ||
| 194 | my $PRE = FLAT::Regex::WithExtraOps->new($_); | ||
| 195 | print Dumper($PRE); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | # dumps jflap XML notation | ||
| 201 | # Usage: | ||
| 202 | # perl -MFLAT -e "dfa2jflap('a&b&c&d*e*')" | ||
| 203 | sub dfa2jflap { | ||
| 204 | use FLAT::Regex::WithExtraOps; | ||
| 205 | use FLAT::DFA; | ||
| 206 | use FLAT::NFA; | ||
| 207 | use FLAT::PFA; | ||
| 208 | if (@_) { | ||
| 209 | foreach (@_) { | ||
| 210 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa()->as_dfa()->as_min_dfa()->trim_sinks(); | ||
| 211 | print $FA->as_jflap; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | else { | ||
| 215 | while (<STDIN>) { | ||
| 216 | chomp; | ||
| 217 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa()->as_dfa->as_min_dfa()->trim_sinks(); | ||
| 218 | print $FA->as_jflap; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | # dumps jflap XML notation | ||
| 224 | # Usage: | ||
| 225 | # perl -MFLAT -e "nfa2jflap('a&b&c&d*e*')" | ||
| 226 | sub nfa2jflap { | ||
| 227 | use FLAT::Regex::WithExtraOps; | ||
| 228 | use FLAT::DFA; | ||
| 229 | use FLAT::NFA; | ||
| 230 | use FLAT::PFA; | ||
| 231 | if (@_) { | ||
| 232 | foreach (@_) { | ||
| 233 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa(); | ||
| 234 | print $FA->as_jflap; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | else { | ||
| 238 | while (<STDIN>) { | ||
| 239 | chomp; | ||
| 240 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa(); | ||
| 241 | print $FA->as_jflap; | ||
| 242 | } | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | # dumps graphviz notation | ||
| 247 | # Usage: | ||
| 248 | # perl -MFLAT -e "nfa2jflap('a&b&c&d*e*')" | ||
| 249 | sub dfa2gv { | ||
| 250 | use FLAT::Regex::WithExtraOps; | ||
| 251 | use FLAT::DFA; | ||
| 252 | use FLAT::NFA; | ||
| 253 | use FLAT::PFA; | ||
| 254 | if (@_) { | ||
| 255 | foreach (@_) { | ||
| 256 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa(); | ||
| 257 | print $FA->as_jflap; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | else { | ||
| 261 | while (<STDIN>) { | ||
| 262 | chomp; | ||
| 263 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa(); | ||
| 264 | print $FA->as_jflap; | ||
| 265 | } | ||
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | # dumps graphviz notation | ||
| 270 | # Usage: | ||
| 271 | # perl -MFLAT -e "nfa2gv('a&b&c&d*e*')" | ||
| 272 | sub nfa2gv { | ||
| 273 | use FLAT::Regex::WithExtraOps; | ||
| 274 | use FLAT::DFA; | ||
| 275 | use FLAT::NFA; | ||
| 276 | use FLAT::PFA; | ||
| 277 | if (@_) { | ||
| 278 | foreach (@_) { | ||
| 279 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa(); | ||
| 280 | print $FA->as_graphviz; | ||
| 281 | } | ||
| 282 | } | ||
| 283 | else { | ||
| 284 | while (<STDIN>) { | ||
| 285 | chomp; | ||
| 286 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa(); | ||
| 287 | print $FA->as_graphviz; | ||
| 288 | } | ||
| 289 | } | ||
| 290 | } | ||
| 291 | |||
| 292 | # dumps graphviz notation | ||
| 293 | # Usage: | ||
| 294 | # perl -MFLAT -e "pfa2gv('a&b&c&d*e*')" | ||
| 295 | sub pfa2gv { | ||
| 296 | use FLAT::Regex::WithExtraOps; | ||
| 297 | use FLAT::PFA; | ||
| 298 | if (@_) { | ||
| 299 | foreach (@_) { | ||
| 300 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa(); | ||
| 301 | print $FA->as_graphviz; | ||
| 302 | } | ||
| 303 | } | ||
| 304 | else { | ||
| 305 | while (<STDIN>) { | ||
| 306 | chomp; | ||
| 307 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa(); | ||
| 308 | print $FA->as_graphviz; | ||
| 309 | } | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | #as_undirected_graphviz | ||
| 314 | |||
| 315 | # dumps graphviz notation | ||
| 316 | # Usage: | ||
| 317 | # perl -MFLAT -e "dfa2undgv('a&b&c&d*e*')" | ||
| 318 | sub dfa2undgv { | ||
| 319 | use FLAT::Regex::WithExtraOps; | ||
| 320 | use FLAT::DFA; | ||
| 321 | use FLAT::NFA; | ||
| 322 | use FLAT::PFA; | ||
| 323 | if (@_) { | ||
| 324 | foreach (@_) { | ||
| 325 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa()->as_dfa()->as_min_dfa()->trim_sinks(); | ||
| 326 | print $FA->as_undirected_graphviz; | ||
| 327 | } | ||
| 328 | } | ||
| 329 | else { | ||
| 330 | while (<STDIN>) { | ||
| 331 | chomp; | ||
| 332 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa()->as_dfa->as_min_dfa()->trim_sinks(); | ||
| 333 | print $FA->as_undirected_graphviz; | ||
| 334 | } | ||
| 335 | } | ||
| 336 | } | ||
| 337 | |||
| 338 | # dumps graphviz notation | ||
| 339 | # Usage: | ||
| 340 | # perl -MFLAT -e "nfa2undgv('a&b&c&d*e*')" | ||
| 341 | sub nfa2undgv { | ||
| 342 | use FLAT::Regex::WithExtraOps; | ||
| 343 | use FLAT::DFA; | ||
| 344 | use FLAT::NFA; | ||
| 345 | use FLAT::PFA; | ||
| 346 | if (@_) { | ||
| 347 | foreach (@_) { | ||
| 348 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa(); | ||
| 349 | print $FA->as_undirected_graphviz; | ||
| 350 | } | ||
| 351 | } | ||
| 352 | else { | ||
| 353 | while (<STDIN>) { | ||
| 354 | chomp; | ||
| 355 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa(); | ||
| 356 | print $FA->as_undirected_graphviz; | ||
| 357 | } | ||
| 358 | } | ||
| 359 | } | ||
| 360 | |||
| 361 | # dumps graphviz notation | ||
| 362 | # Usage: | ||
| 363 | # perl -MFLAT -e "pfa2undgv('a&b&c&d*e*')" | ||
| 364 | sub pfa2undgv { | ||
| 365 | use FLAT::Regex::WithExtraOps; | ||
| 366 | use FLAT::PFA; | ||
| 367 | if (@_) { | ||
| 368 | foreach (@_) { | ||
| 369 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa(); | ||
| 370 | print $FA->as_undirected_graphviz; | ||
| 371 | } | ||
| 372 | } | ||
| 373 | else { | ||
| 374 | while (<STDIN>) { | ||
| 375 | chomp; | ||
| 376 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa(); | ||
| 377 | print $FA->as_undirected_graphviz; | ||
| 378 | } | ||
| 379 | } | ||
| 380 | } | ||
| 381 | |||
| 382 | # dumps directed graph using Kundu notation | ||
| 383 | # Usage: | ||
| 384 | # perl -MFLAT -e "dfa2directed('a&b&c&d*e*')" | ||
| 385 | sub dfa2digraph { | ||
| 386 | use FLAT::Regex::WithExtraOps; | ||
| 387 | use FLAT::DFA; | ||
| 388 | use FLAT::NFA; | ||
| 389 | use FLAT::PFA; | ||
| 390 | |||
| 391 | # trims sink states from min-dfa since transitions are gone | ||
| 392 | if (@_) { | ||
| 393 | foreach (@_) { | ||
| 394 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa()->as_dfa->as_min_dfa->trim_sinks(); | ||
| 395 | print $FA->as_digraph; | ||
| 396 | } | ||
| 397 | } | ||
| 398 | else { | ||
| 399 | while (<STDIN>) { | ||
| 400 | chomp; | ||
| 401 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa()->as_dfa->as_min_dfa->trim_sinks(); | ||
| 402 | print $FA->as_digraph; | ||
| 403 | } | ||
| 404 | } | ||
| 405 | print "\n"; | ||
| 406 | } | ||
| 407 | |||
| 408 | # dumps directed graph using Kundu notation | ||
| 409 | # Usage: | ||
| 410 | # perl -MFLAT -e "nfa2directed('a&b&c&d*e*')" | ||
| 411 | sub nfa2digraph { | ||
| 412 | use FLAT::Regex::WithExtraOps; | ||
| 413 | use FLAT::DFA; | ||
| 414 | use FLAT::NFA; | ||
| 415 | use FLAT::PFA; | ||
| 416 | if (@_) { | ||
| 417 | foreach (@_) { | ||
| 418 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa(); | ||
| 419 | print $FA->as_digraph; | ||
| 420 | } | ||
| 421 | } | ||
| 422 | else { | ||
| 423 | while (<STDIN>) { | ||
| 424 | chomp; | ||
| 425 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa(); | ||
| 426 | print $FA->as_digraph; | ||
| 427 | } | ||
| 428 | } | ||
| 429 | print "\n"; | ||
| 430 | } | ||
| 431 | |||
| 432 | # dumps directed graph using Kundu notation | ||
| 433 | # Usage: | ||
| 434 | # perl -MFLAT -e "pfa2directed('a&b&c&d*e*')" | ||
| 435 | sub pfa2digraph { | ||
| 436 | use FLAT::Regex::WithExtraOps; | ||
| 437 | use FLAT::PFA; | ||
| 438 | if (@_) { | ||
| 439 | foreach (@_) { | ||
| 440 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa(); | ||
| 441 | print $FA->as_digraph; | ||
| 442 | } | ||
| 443 | } | ||
| 444 | else { | ||
| 445 | while (<STDIN>) { | ||
| 446 | chomp; | ||
| 447 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa(); | ||
| 448 | print $FA->as_digraph; | ||
| 449 | } | ||
| 450 | } | ||
| 451 | print "\n"; | ||
| 452 | } | ||
| 453 | |||
| 454 | # dumps undirected graph using Kundu notation | ||
| 455 | # Usage: | ||
| 456 | # perl -MFLAT -e "dfa2undirected('a&b&c&d*e*')" | ||
| 457 | sub dfa2undirected { | ||
| 458 | use FLAT::Regex::WithExtraOps; | ||
| 459 | use FLAT::DFA; | ||
| 460 | use FLAT::NFA; | ||
| 461 | use FLAT::PFA; | ||
| 462 | |||
| 463 | # trims sink states from min-dfa since transitions are gone | ||
| 464 | if (@_) { | ||
| 465 | foreach (@_) { | ||
| 466 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa()->as_dfa->as_min_dfa->trim_sinks(); | ||
| 467 | print $FA->as_undirected; | ||
| 468 | } | ||
| 469 | } | ||
| 470 | else { | ||
| 471 | while (<STDIN>) { | ||
| 472 | chomp; | ||
| 473 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa()->as_dfa->as_min_dfa->trim_sinks(); | ||
| 474 | print $FA->as_undirected; | ||
| 475 | } | ||
| 476 | } | ||
| 477 | print "\n"; | ||
| 478 | } | ||
| 479 | |||
| 480 | # dumps undirected graph using Kundu notation | ||
| 481 | # Usage: | ||
| 482 | # perl -MFLAT -e "nfa2undirected('a&b&c&d*e*')" | ||
| 483 | sub nfa2undirected { | ||
| 484 | use FLAT::Regex::WithExtraOps; | ||
| 485 | use FLAT::DFA; | ||
| 486 | use FLAT::NFA; | ||
| 487 | use FLAT::PFA; | ||
| 488 | if (@_) { | ||
| 489 | foreach (@_) { | ||
| 490 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa(); | ||
| 491 | print $FA->as_undirected; | ||
| 492 | } | ||
| 493 | } | ||
| 494 | else { | ||
| 495 | while (<STDIN>) { | ||
| 496 | chomp; | ||
| 497 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa()->as_nfa(); | ||
| 498 | print $FA->as_undirected; | ||
| 499 | } | ||
| 500 | } | ||
| 501 | print "\n"; | ||
| 502 | } | ||
| 503 | |||
| 504 | # dumps undirected graph using Kundu notation | ||
| 505 | # Usage: | ||
| 506 | # perl -MFLAT -e "pfa2undirected('a&b&c&d*e*')" | ||
| 507 | sub pfa2undirected { | ||
| 508 | use FLAT::Regex::WithExtraOps; | ||
| 509 | use FLAT::PFA; | ||
| 510 | if (@_) { | ||
| 511 | foreach (@_) { | ||
| 512 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa(); | ||
| 513 | print $FA->as_undirected; | ||
| 514 | } | ||
| 515 | } | ||
| 516 | else { | ||
| 517 | while (<STDIN>) { | ||
| 518 | chomp; | ||
| 519 | my $FA = FLAT::Regex::WithExtraOps->new($_)->as_pfa(); | ||
| 520 | print $FA->as_undirected; | ||
| 521 | } | ||
| 522 | } | ||
| 523 | print "\n"; | ||
| 524 | } | ||
| 525 | |||
| 526 | # compares 2 give PREs | ||
| 527 | # Usage: | ||
| 528 | # perl -MFLAT -e "compare('a','a&b&c&d*e*')" #<-- no match, btw | ||
| 529 | sub compare { | ||
| 530 | use FLAT::Regex::WithExtraOps; | ||
| 531 | use FLAT::DFA; | ||
| 532 | use FLAT::PFA; | ||
| 533 | my $PFA1 = FLAT::Regex::WithExtraOps->new(shift)->as_pfa(); | ||
| 534 | my $PFA2 = FLAT::Regex::WithExtraOps->new(shift)->as_pfa(); | ||
| 535 | my $DFA1 = $PFA1->as_nfa->as_min_dfa; | ||
| 536 | my $DFA2 = $PFA2->as_nfa->as_min_dfa; | ||
| 537 | if ( $DFA1->equals($DFA2) ) { | ||
| 538 | print "Yes\n"; | ||
| 539 | } | ||
| 540 | else { | ||
| 541 | print "No\n"; | ||
| 542 | } | ||
| 543 | } | ||
| 544 | |||
| 545 | # prints random PRE | ||
| 546 | # Usage: | ||
| 547 | # perl -MFLAT -e random_pre | ||
| 548 | sub random_pre { | ||
| 549 | my $and_chance = shift; | ||
| 550 | |||
| 551 | # skirt around deep recursion warning annoyance | ||
| 552 | local $SIG{__WARN__} = sub { $_[0] =~ /^Deep recursion/ or warn $_[0] }; | ||
| 553 | srand $$; | ||
| 554 | my %CMDLINEOPTS = (); | ||
| 555 | |||
| 556 | # Percent chance of each operator occuring | ||
| 557 | $CMDLINEOPTS{LENGTH} = 32; | ||
| 558 | $CMDLINEOPTS{OR} = 6; | ||
| 559 | $CMDLINEOPTS{STAR} = 10; | ||
| 560 | $CMDLINEOPTS{OPEN} = 5; | ||
| 561 | $CMDLINEOPTS{CLOSE} = 0; | ||
| 562 | $CMDLINEOPTS{n} = 1; | ||
| 563 | $CMDLINEOPTS{AND} = 10; #<-- default | ||
| 564 | $CMDLINEOPTS{AND} = $and_chance if ( $and_chance == 0 ); #<-- to make it just an re (no shuffle) | ||
| 565 | |||
| 566 | my $getRandomChar = sub { | ||
| 567 | my $ch = ''; | ||
| 568 | |||
| 569 | # Get a random character between 0 and 127. | ||
| 570 | do { | ||
| 571 | $ch = int( rand 2 ); | ||
| 572 | } while ( $ch !~ m/[a-zA-Z0-9]/ ); | ||
| 573 | return $ch; | ||
| 574 | }; | ||
| 575 | |||
| 576 | my $getRandomRE = sub { | ||
| 577 | my $str = ''; | ||
| 578 | my @closeparens = (); | ||
| 579 | for ( 1 .. $CMDLINEOPTS{LENGTH} ) { | ||
| 580 | $str .= $getRandomChar->(); | ||
| 581 | |||
| 582 | # % chance of an "or" | ||
| 583 | if ( int( rand 100 ) < $CMDLINEOPTS{OR} ) { | ||
| 584 | $str .= "|1"; | ||
| 585 | } | ||
| 586 | elsif ( int( rand 100 ) < $CMDLINEOPTS{AND} ) { | ||
| 587 | $str .= "&0"; | ||
| 588 | } | ||
| 589 | elsif ( int( rand 100 ) < $CMDLINEOPTS{STAR} ) { | ||
| 590 | $str .= "*1"; | ||
| 591 | } | ||
| 592 | elsif ( int( rand 100 ) < $CMDLINEOPTS{OPEN} ) { | ||
| 593 | $str .= "("; | ||
| 594 | push( @closeparens, '0101)' ); | ||
| 595 | } | ||
| 596 | elsif ( int( rand 100 ) < $CMDLINEOPTS{CLOSE} && @closeparens ) { | ||
| 597 | $str .= pop(@closeparens); | ||
| 598 | } | ||
| 599 | } | ||
| 600 | |||
| 601 | # empty out @closeparens if there are still some left | ||
| 602 | if (@closeparens) { | ||
| 603 | $str .= join( '', @closeparens ); | ||
| 604 | } | ||
| 605 | return $str; | ||
| 606 | }; | ||
| 607 | |||
| 608 | for ( 1 .. $CMDLINEOPTS{n} ) { | ||
| 609 | print $getRandomRE->(), "\n"; | ||
| 610 | } | ||
| 611 | } | ||
| 612 | |||
| 613 | # prints random RE (no & operator) | ||
| 614 | # Usage: | ||
| 615 | # perl -MFLAT -e random_re | ||
| 616 | sub random_re { | ||
| 617 | shift->random_pre(0); | ||
| 618 | } | ||
| 619 | |||
| 620 | 1; | ||
| 621 | |||
| 622 | __END__ |