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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:49:05+00:00 2026-05-21T03:49:05+00:00

From this wikipedia page: The fundamental difference between context-free grammars and parsing expression grammars

  • 0

From this wikipedia page:

The fundamental difference between
context-free grammars and parsing
expression grammars is that the PEG’s
choice operator is ordered. If the
first alternative succeeds, the second
alternative is ignored. Thus ordered
choice is not commutative, unlike
unordered choice as in context-free
grammars and regular expressions.
Ordered choice is analogous to soft
cut operators available in some logic
programming languages.

Why does PEG’s choice operator short circuits the matching? Is it because to minimize memory usage (due to memoization)?

I’m not sure what the choice operator is in regular expressions but let’s suppose it is this: /[aeiou]/ to match a vowel. So this regex is commutative because I could have written it in any of the 5! (five factorial) permutations of the vowel characters? i.e. /[aeiou]/ behaves the same as /[eiaou]/. What is the advantage of it being commutative? (c.f. PEG’s non-commutativity)

The consequence is that if a CFG is
transliterated directly to a PEG, any
ambiguity in the former is resolved by
deterministically picking one parse
tree from the possible parses. By
carefully choosing the order in which
the grammar alternatives are
specified, a programmer has a great
deal of control over which parse tree
is selected.

Is this saying that PEG’s grammar is superior to CFG’s?

  • 1 1 Answer
  • 3 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-21T03:49:06+00:00Added an answer on May 21, 2026 at 3:49 am

    A CFG grammar is non-deterministic, meaning that some input could result in two or more possible parse-trees. Though most CFG-based parser-generators have restrictions on the determinability of the grammar. It will give a warning or error if it has two or more choices.

    A PEG grammar is deterministic, meaning that any input can only be parsed one way.

    To take a classic example; The grammar

    if_statement := "if" "(" expr ")" statement "else" statement
                  | "if" "(" expr ")" statement;
    

    applied to the input

    if (x1) if (x2) y1 else y2
    

    could either be parsed as

    if_statement(x1, if_statement(x2, y1, y2))
    

    or

    if_statement(x1, if_statement(x2, y1), y2)
    

    A CFG-parser would generate a Shift/Reduce-conflict, since it can’t decide if it should shift (read another token), or reduce (complete the node), when reaching the “else” keyword. Of course, there are ways to get around this problem.

    A PEG-parser would always pick the first choice.

    Which one is better is for you to decide. My opinion is that often PEG-grammars is easier to write, and CFG grammars easier to analyze.

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

Sidebar

Related Questions

I need to pull school names and abbreviations from this page on Wikipedia .
Quote from Wikipedia : A public key token. This is a 64-bit hash of
I got this SVG image from Wikipedia and embedded it into a website using
While browsing I came across this blog post about using the Wikipedia API from
From this page: http://www.doctrine-project.org/documentation/manual/1_2/en/working-with-models#dealing-with-relations:creating-related-records You can see that it says $obj['property']; is the recommended
I try to parse articles from wikipedia. I use the *page-articles.xml file, where they
I'm trying to get a list of link titles from a wikipedia page. I
Consider this simple class that demonstrates RAII in C++ (From the top of my
This is the paragraph on OpenID security from Wikipedia . Are there any new
I would like to fetch the WikiText from a Wikipedia page and display it

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.