Search code examples
context-free-grammarcontext-free-language

How do I find the language for a context free grammar?


I am having trouble determining the language from a given context free grammar. I've been given a hint that there are 2 parts to the language but cant figure either out.

G= ({S,A,B,C,D,E,Z},(0,1),R,S),

S→E|Z
E→A|C 
A→01B|0A|e
B→1B|10A 
C→10D|1C|e
D→01C|0D 
Z→0Z1|e

e is the empty string by the way. I have figured out that if it is Z the number of 0s and 1s are equal but cant figure out wh if it goes to E


Solution

  • Kind of off topic, but you can pretty easily break down the grammar into subgrammars that you can analyze indepedently.

    Firstly, the E rule only appears in one place on the RHS, so you can trivially substitute it and get rid of it by making the S rule S→A|C|Z. Each of those non-terminals leads to an indepedent sublanguage (language A consisting of just the rules for A and B, language C with just C and D, and language Z with just Z. Your language G is just the union of these three sublanguages.

    Langauge A is trivially regular (due to all the non-terminals being at the end of the RHS of rules), and can be trivially transoformed into a 2 state E-NFA, which reduces to regexp (0|011*10)*

    Language C similarly reduces to the regexp (1|100*01)*

    Language Z is the only non-regular subpart and is the language { 0i1i | i ≥ 0 }

    The union of these three is language G and doesn't really have any particularly good way of describing it other than the grammar.