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

  • SEARCH
  • Home
  • 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 8686269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:48:05+00:00 2026-06-12T22:48:05+00:00

Here is a starting point for a grammar: %%{ machine xo; char = x

  • 0

Here is a starting point for a grammar:

%%{
  machine xo;

  char = "x" | "o";
  group = "(" char* ")";
  main := group;
}%%

It handles (xxxx(oo)()xx), for example. How do I extend it to allow nested groups; e.g. (xxxx(o(x)o)()xx?

I know recursion is not, generally, supported by one Ragel machine. So this won’t work:

group = "(" ( char | group )* ")";

From the Ragel State Machine Compiler User Guide (PDF): (bold text added for emphasis):

“In general Ragel cannot handle recursive structures because the grammar is interpreted as a regular language. However, depending on what needs to be parsed it is sometimes practical to implement the recursive parts using manual coding techniques. This often works in cases where the recursive structures are simple and easy to recognize, such as in the balancing of parentheses.”

“One approach to parsing recursive structures is to use actions that increment and decrement counters or otherwise recognize the entry to and exit from recursive structures and then jump to the appropriate machine defnition using fcall and fret. Alternatively, semantic conditions can be used to test counter variables.

“A more traditional approach is to call a separate parsing function (expressed in the host lan- guage) when a recursive structure is entered, then later return when the end is recognized.”

From a mailing list discussion on nested brackets, the same three approaches are mentioned:

  1. Specify a growable stack using prepush and postpop and then use fcall
    and fret.

  2. Counting and then verify in actions or conditions.

  3. Call a new parsing function (in the host language) when the recursive
    structure is entered.

Can you point me to an example of each — preferably using my example above — in Ruby? Thanks!

  • 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-06-12T22:48:08+00:00Added an answer on June 12, 2026 at 10:48 pm

    A general pattern using fcall/fret is like:

    balanced = [^(){}\[\]] |
                   '(' @{ fcall balancedTokensParen; } |
                   '[' @{ fcall balancedTokensBracket; } |
                   '{' @{ fcall balancedTokensBrace; };
    balancedTokensParen   := balanced* ')' @{ fret; };
    balancedTokensBracket := balanced* ']' @{ fret; };
    balancedTokensBrace   := balanced* '}' @{ fret; };
    

    So your case could be handled as

      char = [xo];
      group = '(' @{ fcall group_rest; };
      group_rest := (char|group)* ')' @{ fret; };
    
      main := group;
    

    the lexer function should contain the stack array, and you have to manually to check the top to ensure there isn’t unclosed ‘(‘:

    stack = []
    %% write init;
    %% write exec;
    if top > 0 
        cs = %%{ write error; }%%
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the problem: I have to find a path from the starting point
I am using the RayWenderlich OpenGL tutorial found here as a starting point for
I have these functions here that will record the time between starting point and
I'm looking for a starting point here, so no code to post I'm afraid
Here is a graphic representation of the problem: http://i.imgur.com/aBG3p.jpg Given a starting point (x1,y1)
So here is the starting point CREATE TABLE #Data ( ID INT IDENTITY(1,1), MyData
Okay. Tough to find the best starting point, here. The error XCode (4.3.2) in
I'm staring blank on this error, so I hope someone here can point out
Here's the code: //check if the starting row variable was passed in the URL
Here is my Fiddle: http://jsfiddle.net/LLFpp/ I'm just starting to learn CSS and Divs (moving

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.