How to generate the language for context free grammar?

Data Structure AlgorithmsComputer ScienceComputers

Problem

Generate the language for the given context free grammar.

S->0S, S-> λ

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

S->000S, S-> λ

Solution

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

G=(V,T,P,S)

Where,

  • 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

     ->0

Case 2 − S->0S

    ->00S

    ->00

Case 3 − S->0S

   ->00S

   ->000S

   ->000

Therefore, the language generated for the given grammar is −

L={e,0,00,000……..}

Example 2

The grammar is

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

Case 1 − S->A0

    ->0

Case 2 − S->A0

    ->1A0 {A->1A}

    ->10

Case 3 − S->A0

   ->1A0

   -> 11A0

   ->110

Therefore, the language generated based on given grammar is −

L={0,10,110,…………….}

Example 3

The grammar is

S->000S, S-> λ

Case 1 − S->000S

->000

Case 2 − S->000S

->000000S

->000000

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

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

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