When analyzing the text, I often need to implement mini-state machines in the general form as follows.
Is there a CPAN module that is considered โbest practiceโ and is well suited for state logic logic like this in a simple and elegant way?
I would prefer the solutions to be less complicated than Parse::RecDescent
, but if they do not exist and Parse::RecDescent
much easier to apply to this problem than I thought, I really want to consider this instead of skating on my own, before so far.
An example of a common parsing code:
my $state = 1; while (my $token = get_next_token()) { # Usually next line if ($state == 1) { do_state1_processing(); if (token_matches_transition_1_to_2($token)) { do_state_1_to_2_transition_processing(); $state == 2; next; } elsif (token_matches_transition_1_to_4($token)) { do_state_1_to_4_transition_processing(); $state == 4; next; } else { do_state1_continuation(); next; } } elsif ($state == 5) { do_state5_processing(); if (token_matches_transition_5_to_6($token)) { do_state_5_to_6_transition_processing(); $state == 6; next; } elsif (token_matches_transition_5_to_4($token)) { do_state_5_to_4_transition_processing(); $state == 4; next; } else { do_state5_continuation(); next; } } else { } }
source share