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 6364989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:14:11+00:00 2026-05-25T00:14:11+00:00

I was adding zero-or-more and one-or-more modifiers to my PEG parser, which is straightforward

  • 0

I was adding zero-or-more and one-or-more modifiers to my PEG parser, which is straightforward since there is so little backtracking in PEG. Earlier iterations are never reconsidered, so a simple while loop suffices.

However, in other contexts, zero-or-more and one-or-more modifiers do require backtracking. For example, take the following regular expression:

(aa|aaa)+

This expression should be able to greedily match a string of seven a‘s: there are several ways to add up 2 and 3 to get 7. But to get there, reconsidering earlier iterations is necessary. For instance, if the expression matches three a‘s the first time and three a‘s the second time, only one a remains, which cannot be matched. Backtrack the last three a‘s and match two a‘s instead, however, and five a‘s are matched. Then the last two a‘s can be matched too (i.e., 3 + 2 + 2 = 7).

Fortunately, the regular expression quits its search once it has matched the string. But what about an EBNF parser? If the grammar is ambiguous, the parser is to use backtracking to find all possible syntax trees! If we have the production

( "aa" | "aaa" )*

and a string of seven a‘s, a fully backtracking parser would return all possible ways of expressing 7 in terms of 2 and 3. And that’s just for seven a‘s: match a slightly longer string, and the N-ary tree of possibilities grows another level. Consider N = 6:

S : ( T )*
  ;

T : A
  | B
  | C
  | D
  | E
  | F
  ;

A terrifying combinatorial explosion!

Could this really be the case, though? Are there no restrictions on the zero-or-more and one-or-more modifiers in EBNF? Implementing them as described would be a lot more work than the plain while() loop of the PEG parser, so I have to wonder …

  • 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-25T00:14:12+00:00Added an answer on May 25, 2026 at 12:14 am

    yes; backtracking can give you a lot of results. i’m the author of lepl, which is a recursive decent parser that will happily backtrack and produce a “parse forest” of all possible ASTs. and there is no restriction in EBNF (which is just a specification language and not tied to any particular parser implementation).

    but not all parsing algorithms backtrack. many implementations of regular expressions do so, but it is not always necessary. in fact, for a “simple” regular expression (one that really is confined to regular grammars) it is possible to match without backtracking at all – the trick is to, in a sense, run things in parallel.

    there are two (equivalent) ways of doing this – either by “compiling” the regular expression (working out what the expression would be if the work in parallel was explicit), or by juggling parallel matches at runtime. the compilation approach translates the regular expression to a DFA (deterministic finite automaton). more exactly, a NFA (non-deterministic…) is vaguely like a graph version of the regex, and is probably how you imagine regular expressions work; matching with a NFA does require backtracking, but you can translate the NFA into a DFA which does not.

    however, doing this at runtime is easier to understand (and tends to be more useful in practice) and is explained in three awesome articles which you should really read if you want to understand this better: http://swtch.com/~rsc/regexp/regexp3.html and the links at the start of that.

    i cannot emphasize this enough – you need to read those articles…

    ps vaguely related – you can make backtracking more efficient by caching results that you might need again later (when you end up arriving at the same text and expression via a different route). this is called “packrat parsing” when applied to recursive decent parsing (although to be honest it’s not worth a separate name – it really is just using a cache). the caching avoids exponential runtimes – there’s a paper somewhere by norvig (the guy at google, but this was written way before) that explains that: http://acl.ldc.upenn.edu/J/J91/J91-1004.pdf

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

Sidebar

Related Questions

Adding source files more than one directory away (e.g. ../../source.cpp or ../../../somewhere_else/source.cpp, vs. just
Adding more than one child to a WPF StatusBar results in poor layout with
just adding a zero like below [assembly: AssemblyVersion(2.03.406.2)] results in 2.3.406.2 which is not
Adding the row one by one in the button click.. (i tried but its
When adding an EditItemTemplate of some complexity (mulitple fields in one template), and then
UPDATE: Please see the updates below, as I'm adding more information to the bottom
I'm interested in adding to some of my sites the official Tweet button which
Adding an element to the head of an alist (Associative list) is simple enough:
Adding Exceptional Handling is a good practise, but I have a doubt, In our
Adding functionality to a class can be done by adding a method or by

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.