| 1 | package FLAT; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 6 | ▶ | use FLAT::Regex; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 6 | ▶ | use FLAT::DFA; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 6 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 6 | our $VERSION = q{1.0.2}; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | FLAT - Formal Language & Automata Toolkit | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | =head2 Name Change Possibility | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | Future releases of this module may very well reflect a name change | ||||||||||||||||||||
| 18 | that is considered to me more I<normal> for Perl modules. When this was | ||||||||||||||||||||
| 19 | originally written (2006), the original author was not very well versed | ||||||||||||||||||||
| 20 | in the idiomatic aspects of C<PERL>. | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | FLAT.pm is the base class of all regular language objects. For more | ||||||||||||||||||||
| 25 | information, see other POD pages. | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | This module provides an interface for manipulating the formal language | ||||||||||||||||||||
| 30 | concept of Regular Expressions, which are used to describe Regular | ||||||||||||||||||||
| 31 | Languages, and their equivalent forms of Automata. | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | It's notable that this module supports, in addition to the traditional | ||||||||||||||||||||
| 34 | Regular Expression operators, the C<shuffle> operator (see [1]). This | ||||||||||||||||||||
| 35 | is expressed as an ampersand, C<&>. In addition to this, logical symbols | ||||||||||||||||||||
| 36 | may be multiple characters. This leads to some interesting applications. | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | While the module can do a lot, i.e.: | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | =over 4 | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | =item * parse a regular expression (RE) (of the formal regular language | ||||||||||||||||||||
| 43 | variety) | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | =item * convert a RE to a NFA (and similarly, a I<shuffle> of two regular | ||||||||||||||||||||
| 46 | languages to a I<parallel> NFA (PFA)) | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =item * convert a PFA to a NFA (note, PFAs are equivalent to PetriNets | ||||||||||||||||||||
| 49 | (see [2], L<Graph::PetriNet>) | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =item * convert a NFA to a DFA | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | =item * convert a DFA to a minimal DFA | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =item * generate strings that may be accepted by a DFA | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | =back | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | It is still missing some capabilities that one would expect: | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | =over 4 | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | =item * generate equivalent REs from a NFA or DFA | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | =item * provide targeted conversion of PFAs, NFAs, DFAs to their more | ||||||||||||||||||||
| 66 | explicit state forms; this is particularly interested to have in the case | ||||||||||||||||||||
| 67 | of the PFA. | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =item * provide targeted serialization of PREs (REs with a shuffle) | ||||||||||||||||||||
| 70 | using direct, explicit manuplation of the AST produced by the parser | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =item * provide other interesting graph-based manipulations that might | ||||||||||||||||||||
| 73 | prove useful, particular when applied to a graph that represents some | ||||||||||||||||||||
| 74 | form of a finite automata (FA) | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =back | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | In addition to the above deficiencies, application of this toolkit in | ||||||||||||||||||||
| 79 | interesting areas would naturally generate ideas for new and interesting | ||||||||||||||||||||
| 80 | capabilities. | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =head2 Sequential Consistency and PREs | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | Valid strings accepted by the shuffle of one or more regular languages is | ||||||||||||||||||||
| 85 | necessarily I<sequentially consistent>. This results from the conversions | ||||||||||||||||||||
| 86 | to a DFA that may be traversed inorder to discover valid string paths | ||||||||||||||||||||
| 87 | necessarily obeys the total ordering constraints of each constituent | ||||||||||||||||||||
| 88 | language of the two being shuffled; and the partial ordering that results | ||||||||||||||||||||
| 89 | among valid string accepted by both (see [2] for more on how PetriNets | ||||||||||||||||||||
| 90 | fit in). | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | =head1 USAGE | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | All regular language objects in FLAT implement the following methods. | ||||||||||||||||||||
| 95 | Specific regular language representations (regex, NFA, DFA) may implement | ||||||||||||||||||||
| 96 | additional methods that are outlined in the repsective POD pages. | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =cut | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | ## let subclasses implement a minimal set of closure properties. | ||||||||||||||||||||
| 101 | ## they can override these with more efficient versions if they like. | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | sub as_dfa { | ||||||||||||||||||||
| 104 | 0 | ▶ | my @params = @_; | ||||||||||||||||||
|
Subroutine
as_dfa: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 105 | 0 | return $params[0]->as_nfa->as_dfa; | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | sub as_min_dfa { | ||||||||||||||||||||
| 109 | 0 | ▶ | my @params = @_; | ||||||||||||||||||
|
Subroutine
as_min_dfa: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 110 | 0 | return $params[0]->as_dfa->as_min_dfa; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | sub is_infinite { | ||||||||||||||||||||
| 114 | 0 | ▶ | my @params = @_; | ||||||||||||||||||
|
Subroutine
is_infinite: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 115 | 0 | return !$params[0]->is_finite; | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | sub star { | ||||||||||||||||||||
| 119 | 0 | ▶ | my @params = @_; | ||||||||||||||||||
|
Subroutine
star: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 120 | 0 | return $params[0]->kleene | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | sub difference { | ||||||||||||||||||||
| 124 | 6 | ▶ | my @params = @_; | ||||||||||||||||||
|
Subroutine
difference: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 125 | 6 | return $params[0]->intersect($params[1]->complement); | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | sub symdiff { | ||||||||||||||||||||
| 129 | 6 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
symdiff: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 130 | 6 | ▶ | return $self if not @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | 3 | my $next = shift()->symdiff(@_); | |||||||||||||||||||
| 132 | 3 | return ($self->difference($next))->union($next->difference($self)); | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | sub equals { | ||||||||||||||||||||
| 136 | 3 | ▶ | my @params = @_; | ||||||||||||||||||
|
Subroutine
equals: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 137 | 3 | return $params[0]->symdiff($params[1])->is_empty(); | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | sub is_subset_of { | ||||||||||||||||||||
| 141 | 0 | ▶ | my @params = @_; | ||||||||||||||||||
|
Subroutine
is_subset_of: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 142 | 0 | return $params[0]->difference($params[1])->is_empty; | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | BEGIN { | ||||||||||||||||||||
| 146 | 6 | ▶ | for my $method ( | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=9.2
| |||||||||||||||||||||
| 147 | qw[ as_nfa as_regex union intersect complement concat | ||||||||||||||||||||
| 148 | kleene reverse is_empty is_finite ] | ||||||||||||||||||||
| 149 | ) { | ||||||||||||||||||||
| 150 | 6 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 151 | *$method = sub { | ||||||||||||||||||||
| 152 | 0 | ▶ | my $pkg = ref $_[0] || $_[0]; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||
| 153 | 0 | carp "$pkg does not (yet) implement $method"; | |||||||||||||||||||
| 154 | 60 | }; | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 6 | 1; | |||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | __END__ | ||||||||||||||||||||