0

i am trying to understand Context free grammar and generate a CFG for any given language.

when you're given a language , what is the best way to generate a CFG from it? are there any steps to follow to help you create a CFG for any language? is there ways of breaking down the language to make it simpler so that it helps you generate the CFG.

for example if i was given L={a^n (ba)^m a^n | n,m >=0 }

does that mean i need to have equal number of a's on both side, and have zero or more ba in between the a's ?

S---> aSa |ε

X---> baX |ε

any helps is appreciated.

1 Answers1

0

The reference question "How to prove that a language is context-free?" has some examples to build a context-free grammar using a simple toolbox. As is mentioned there one needs a "nesting structure" to make this work.

You ask about the difference between language like $L_1 = \{ab^na^nc \mid \dots \}$ and $L_2 = \{a^n (ab)^m a^n \mid \dots \}$. In both cases the parts that are iterated the same $n$ number of times have to be generated in parallel. Productions like $Y\to b Y a$ are good for that.

In case of $L_1$ the $a\dots c$ pair is around the repeat $b^nc^n$ and can be generated before $S\to a X c$, but also independently as $S\to AXC, A\to a, C\to c$ . In case of $L_2$ the $(ab)^m$ is inside the repeat $a^n\dots a^n$ and has to be generated after, like your own solution $S\to aSa, S\to X, X\to abX\mid \varepsilon$.

Hendrik Jan
  • 31,459
  • 1
  • 54
  • 109