0

What could be the correct Context free grammar for the Language L= {a^i b^j a^k | j< i + k} ?

I want to know about the correctness of following CFG for this-

S-> aaSbA | A | ^
A-> bAa | a

Are there any standard rules for getting strings satisfying j < i + k?

Please help

Raj Chauhan
  • 15
  • 1
  • 8

1 Answers1

1

The usual approach is by breaking it down into simpler languages. Start with the simple repeating languages:

La = { ai } -> ε | Laa
Lab = { aibi } -> ε | aLabb

Now you use those to build up your language. The core of your language is LabLba. That gives you the same pattern, but with j = i + k. So to get <, you need to add at least one a either before or after. So you end up with

S -> aLaLabLbaLa | LaLabLbaLaa

Now this grammar is ambiguous, so if you care about that sort of thing, you can refactor it to remove (or at least reduce) ambiguities, but that is another question.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • Then is this the corresponding unambiguous CFG G: S -> aA | Aa A -> BCDB B-> aB | ^ C -> aCb | ^ D -> bDa | ^ – Raj Chauhan Apr 07 '18 at 03:30
  • @RajChauhan: That's the same CFG -- its ambiguous, but correct for the language. – Chris Dodd Apr 07 '18 at 20:24
  • There are, unfortunately, no standard rules for reducing or eliminating ambiguity in a grammar -- ambiguity itself is undecidable, so its a hard problem. One trick that sometimes works is trying to convert the grammar to LR(1) or LR(k) -- if that works then the grammar is deterministic and unambiguous. But it doesn't always work. – Chris Dodd Apr 09 '18 at 17:42