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

The Archive Base Latest Questions

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

I understand how the difference between the two, how ambiguity means that there is

  • 0

I understand how the difference between the two, how ambiguity means that there is at least one string with 2 distinct parse trees while there is only one in an unambiguous tree. But I can’t seem to convert one to the other.

How would I convert the following ambiguous grammar to an unambiguous one?

S -> aSb
S -> abS
S -> lambda

Edit: Ok, my stab at this would be something like

S -> aSb | lambda
b -> abS | lambda

any thoughts?

  • 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-25T21:14:07+00:00Added an answer on May 25, 2026 at 9:14 pm

    The grammar is ambiguous not only because there are two rules that match ‘a’ as the next token – but because ‘ab’ can be matched either by the first or second rule (substituting using the third for S in each).

    There is such a thing as an inherently ambiguous grammar, but this isn’t one.

    Focusing on this specific example, I started by enumerating the strings that would parse. I numbered the rules 1,2 and 3 – and considered all the sequences in which rules 1 and 2 could appear in the parse (these being the two rules that generate terminals.) N.B. I assumed “lambda” denoted the empty production.

    1,2 => ab
    11,12 => abab
    21,22 => aabb
    111,112 => ababab
    121,122 => abaabb
    211,212 => aababb
    221,222 => aaabbb
    1111,1112 => abababab
    1121,1122 => ababaabb
    1211,1212 => abaababb
    1221,1222 => abaaabbb
    2111,2112 => aabababb
    2121,2122 => aabaabbb
    2211,2212 => aaababbb
    2221,2222 => aaaabbbb
    

    From this exercise, it is obvious that we’re matching even length strings of ‘a and b’ where the number of ‘a’ terminals exactly matches the number of ‘b’ terminals… Further, the concatenation of two strings that match only results in another matching string if the prefix matched using the second rule.

    From this analysis, I drew up some new productions.

    S -> a a X
    S -> a b S
    S -> lambda
    X -> S b b
    

    This new grammar is not ambiguous, but it matches the same strings as the ambiguous grammar. It achieves this by introducing a new non-terminal X. When this CFG is used with a push-down automata, the additional state-information arising from using both S and X is sufficient to avoid ambiguity.

    If this problem arose in the context of using something like Yacc or Bison, the ambiguity is often an indication that you’ve made a poor choice of terminal tokens. If you’d picked ‘aa’, ‘ab’ and ‘bb’ as terminals – you’d not have run into difficulty. When using (F)lex as a tokenizer, as a rule of thumb, it’s a good idea to make the tokens it matches as big as makes sense… as it’s quicker to match a regular expression (in theory at least) than a context free grammar – and this might have yielded the two-character token approach as a matter of course.

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

Sidebar

Related Questions

Is there any substantial difference between those two terms?. I understand that JDK stands
I've been researching the difference between these two patterns. I understand that facade encapsulates
I am trying to understand the difference between these two links: The first one
I am trying to understand difference between those two and really need a explanatory
I'm really trying to understand the difference between OpenID and OAuth? Maybe they're two
I understand the difference between String and StringBuilder ( StringBuilder being mutable) but is
What is the difference between the two architectures: Model-View-Presenter and Three-Tier? I understand the
I don't think I really understand Java generics. What's the difference between these two
I don't quite understand the difference between these two methods. In what situation would
I cannot effectively understand the difference between these two queries. select * from person

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.