5

The question is to develop a context free grammar for language containing all strings having more number of As than Bs.

I can't think of a logical solution . Is there a way to approach such problems , what can help me approach such problems better ? Can someone suggest a logical way to analyse such grammar problems ?

nino96
  • 395
  • 3
  • 6
  • 17
  • Can you describe the language you'd like to generate more precisely? For example, is `aabbaba` a valid string? – blazs Apr 01 '16 at 15:50
  • @blazs Yes aabbaba would be a valid string , there is no restriction on the order of a or b . I am able to write grammars for cases when Bs follow As but the generality of the given problem is proving tough – nino96 Apr 01 '16 at 15:52

4 Answers4

15

The following grammar generates all strings over {a,b} that have more a's than b's. I denote by eps the empty string.

S -> Aa | RS | SRA
A -> Aa | eps
R -> RR | aRb | bRa | eps

It's obvious it always generates more a's than b's. It's less obvious it generates all possible strings over {a,b} that have more a's than b's

The production R -> RR | aRb | bRa | eps generates all balanced strings (this is easy to see), and the production A -> Aa generates the language a* (i.e. strings with zero or more a's).

Here's the logic behind the grammar. Notice that if w=c1,c2,c3,...,cn is a string over {a,b} with more a's than b's then we can always decompose it into a concatenation of balanced strings (i.e. equal number of a's and b's, which includes the empty string) and strings of the form a+.

For example, ababaaaba = abab (can be generated by R),aaa (can be generated by A),ba (can be generated by R).

Now notice that the production S -> Aa | RS | SRA generates precisely strings of this form.

It suffices to verify that S covers the following cases (because every other case can be covered by breaking into such subcases, as you should verify):

  • [a][balanced]: use S => SRA => AaR.
  • [balanced][a]: use S => RS => RA => RAa.
  • [balanced][a]balanced]: use S => SRA => RSRA => RAaR.
blazs
  • 4,705
  • 24
  • 38
  • There is a strict inequality , number of As and number of Bs cannot be equal – nino96 Apr 01 '16 at 15:56
  • Yes, thanks, I've just noticed that and corrected the answer. – blazs Apr 01 '16 at 15:57
  • Can you explain the logic and the thought process behind the solution ? That would help a lot . Thanks in advance – nino96 Apr 01 '16 at 15:59
  • Thanks a lot , that really clarifies a lot. I just have one silly doubt , is R -> RR required ? – nino96 Apr 01 '16 at 16:44
  • Yes: you need it to generate `aaabbbaabb`, for example. – blazs Apr 01 '16 at 16:48
  • Ah yes , if it weren't there we'd need matching a's and b's at 0th and n-1th position and 1st and (n-2)the position and so on , now I understand. – nino96 Apr 01 '16 at 17:03
0

S → TAT

T → ATb | bTA | TT | ɛ

A → aA | a

T generates any string where #a >= #b (including the empty string). S ensures there is at least one 'a' more than b's at any position.

  • The string aabababbbbbaabbbaabbababbaa has 12 a's and 15 b's (fewer a's than b's), but (according to JFLAP) it appears that this string is in the language generated by this grammar. – user3134725 Oct 23 '22 at 16:07
  • Something went wrong in your test. I didn't use jflap but https://web.stanford.edu/class/archive/cs/cs103/cs103.1156/tools/cfg/ tool says different. Besides, it is not possible to generate the string you mentioned because every time you generate one 'b', you have to generate at least one 'a'.... plus the additional 'a' at the beginning ensures more a's than b's. – Silvio Bandeira Oct 24 '22 at 17:45
0

Another simple solution I can think for this would be:

S -> XAX|a

A -> aS|Sa

X -> aXb|bXa|e

For e to be epsilon.

Silent
  • 1
-1

Another perhaps simpler solution:

S->A|AAB|BAA|e A->AA | a B->AB | BA | b

Dhruv Ghulati
  • 2,976
  • 3
  • 35
  • 51