How to generate the language for context free grammar?

Data Structure AlgorithmsComputer ScienceComputers


Generate the language for the given context free grammar.

S->0S, S-> λ

S-> A0, A->1A, A-> λ

S->000S, S-> λ


Context free grammar (CFG) is a formal grammar that is used to generate all the possible patterns of strings in a given formal language.

CFG is defined by four tuples



  • T: Set of terminals (lowercase letters) symbols.
  • V: Vertices or non-terminal symbols (Capital letters).
  • P: Production rules.
  • S: Start symbol.

Example 1

The grammar is −

S->0S, S->λ

Case 1 − S->0S


Case 2 − S->0S



Case 3 − S->0S




Therefore, the language generated for the given grammar is −


Example 2

The grammar is

S-> A0, A->1A, A-> λ

Case 1 − S->A0


Case 2 − S->A0

    ->1A0 {A->1A}


Case 3 − S->A0


   -> 11A0


Therefore, the language generated based on given grammar is −


Example 3

The grammar is

S->000S, S-> λ

Case 1 − S->000S


Case 2 − S->000S



Therefore, the language generated based on the given grammar is −

L={ ε,000,000000,……….}

Published on 12-Jun-2021 09:21:40