Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7554795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:26:07+00:00 2026-05-30T11:26:07+00:00

When parsing text, I frequently need to implement mini-state-machines, in the generic form following

  • 0

When parsing text, I frequently need to implement mini-state-machines, in the generic form following the code below.

Is there a CPAN module that’s considered “best practice” and well suited to implement state machine logic like this in an easy and elegant way?

I would prefer solutions less complicated than Parse::RecDescent but if none exist and Parse::RecDescent is a lot easier to apply to this problem than I thought, I’m very willing to consider it instead of rolling my own like I’ve been so far.

Example generic 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 {

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:26:08+00:00Added an answer on May 30, 2026 at 11:26 am

    I would recommend taking a look at Marpa and Marpa::XS.

    Just look at this simple calculator.

    my $grammar = Marpa::XS::Grammar->new(
        {   start   => 'Expression',
            actions => 'My_Actions',
            default_action => 'first_arg',
            rules   => [
                { lhs => 'Expression', rhs => [qw'Term'] },
                { lhs => 'Term', rhs => [qw'Factor'] },
                { lhs => 'Factor', rhs => [qw'Number'] },
                { lhs => 'Term', rhs => [qw'Term Add Term'], action => 'do_add' },
                {   lhs    => 'Factor',
                    rhs    => [qw'Factor Multiply Factor'],
                    action => 'do_multiply'
                },
            ],
        }
    );
    

    You will have to implement the tokenizer yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following code, I am trying to do a text parsing by using
Apart from just inserting and parsing text into a blank Word field, is there
Parsing a text file in vb.net and need to locate the latitude and longitude
There are several questions on SO about parsing structured text in Ruby, but none
I'm parsing a text file and I need to remove all terms such this:
in ruby/rails3, I need to do some heavy text parsing to find a certain
I'm parsing text documents and inserting them into PostgreSQL DB. My code is written
I'm parsing text from a file and storing it in a string. The problem
I am parsing text that has a heading and then data that applies to
I've never really done much with parsing text in PHP (or any language). I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.