In this code I take non-looping FSAs and process the description using XML::Twig and search the defined paths looking for a solution.

here is a pictorial representation of the paths

(A) --B--> (C) --1--> (R) --L--> (F) (A) --D--> (D) --2--> (Q) --3--> (F) (A) --P--> (M) --4--> (P) --9--> (F)
and here is the XML graph description
<fsm initial_state="A" final_state="F"> <delta><start>A</start><input>B</input><rest>C</rest></delta> <delta><start>C</start><input>1</input><rest>R</rest></delta> <delta><start>R</start><input>L</input><rest>F</rest></delta> <delta><start>A</start><input>P</input><rest>M</rest></delta> <delta><start>M</start><input>4</input><rest>P</rest></delta> <delta><start>P</start><input>9</input><rest>F</rest></delta> <delta><start>A</start><input>Q</input><rest>D</rest></delta> <delta><start>D</start><input>2</input><rest>Q</rest></delta> <delta><start>Q</start><input>3</input><rest>F</rest></delta> </fsm>
use XML::Twig; my $twig = XML::Twig->new; my @input = qw(Q 2 3); $twig->parsefile("fsa.xml"); my $root = $twig->root; my $I = $root->att('initial_state'); my $F = $root->att('final_state'); my @delta = $root->children; sub bfs { my ($type, $search_text, @nodes) = @_; grep { $_->first_child($type)->text eq $search_text } @nodes ; } my @start_candidate = bfs('start', 'A', @delta); &DFS(\@start_candidate,\@input); sub DFS { my ($start_candidate,$input) = @_; for my $path (@$start_candidate) { my $current_position = $path->next_elt('start')->text; warn "currently at: $current_position"; return 1 if $F eq $current_position and !@$input; my @input = @$input; if ($path->next_elt('input')->text eq shift(@input)) { my $next = $path->next_elt('rest'); my @new_paths = bfs('start', $next->text, @delta); return DFS(\@new_paths, \@input); } else { next; } } return 0; }

In reply to Finite State Automata with XML::Twig by princepawn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.