Context Free Grammar - Automata

Context Free Grammar

Grammar defines a set of rules, and with the help of these rules valid sentences in a language are constructed. A grammar consists of collection of substitution rules, which are also called production rules.

Context Free Grammar have recursive structure. The language that is associated with Context Free Grammar is called Context Free Language. Context Free Grammar has one condition for production rules, i.e., on the left-hand side of each rule there must be only single variable, and on the right-hand side, there may be combination of variables and terminals including ?.

The variables are those symbols that can be replaced with other variables, terminals, both or ?.

The terminals are those symbols which cannot be replaced by anything.

Example of Context free Grammar:

S -> 0S1

S -> 0

S-> ?

The above given grammar can also be written as:

S -> 0S1|0| ?

If the same variable is given more than one on the left-hand side then we can combine their resulting strings in one line using symbol “|”. The sequence of substitutions for obtaining a string is called derivation.

The reverse substitution is not permitted.

Example: S -> 0 is a production, then we can replace S by 0 but not 0 with S.

Formal Definitions of Context Free Grammar

Context Free Grammar has 4 tuples (V, T, P, S)

V: - V is a finite set of variables also called non-terminals.

T: - T is a finite set of symbols called terminals that form the strings of the language generated by the grammar.

P: - P is a finite set of rules called production rules, each rule have a single variable on the left – hand side and strings of variables and terminals on right hand side.

S: - S is a special symbol called the start symbol.

Let us take some examples of Context Free Grammar

Example1:

Construct Context Free Grammar to generate the following language.

L = {wwR: w ? {0, 1}*}

Solution:

The type of strings that language L generates includes {010010, 110011, 01111110 …..}

Let G = (V, T, P, S) be a context free grammar generating given language

Where V = {S}

T = { 0, 1, ? }

S is the start symbol

P is set of production rules defined as follows:

S -> 0S0

S -> 1S1

S-> ?

Now let us check whether the Context Free Grammar generates the strings that belongs to given language. Consider the string 001100 ? L. The derivations of this string are:

S => 0S0

    => 00S00

    => 001S100

    => 001100

Thus 001100 ? L.

Example 2:

Construct Context Free Grammar to generate the following language.

L = {01(1100) n110 (10) n | n>=0}

Solution:

The type of strings that language L generates includes {01110, 01110011010…..}

Let G = (V, T, P, S) be a context free grammar generating given language

Where V = {S, A, B}

T = {0, 1, ?}

S is the start symbol

P is set of production rules defined as follows:

S -> 01A

A -> 11B0

B -> 00A1

B-> ?

Now let us check whether the Context Free Grammar generates the types of strings that belongs to given language. Consider the string 01110011010 ? L. The derivations of this string are:

S => 01A

    => 0111B0

    => 011100A10

    => 01110011B010

    => 01110011010

Thus 01110011010 ? L.

Example 3:

Construct Context Free Grammar that generates strings over {0, 1} which contains at least four consecutive 0’s.

Solution:

The type of strings that language L generates includes {00001, 110000, 0000, 11010001 …..}

Let G = (V, T, P, S) be a context free grammar generating given language.

Where V = {S, A}

T = {0, 1, ?}

S is the start symbol

P is set of production rules defined as follows:

S -> A0000A

A -> 0A

A -> 1A

A -> ?

Now let us check whether the Context Free Grammar generates the types of strings that belongs to given language. Consider the string 11010001 ? L. The derivations of this string are:

S => A0000A

    => 1A0000A

    => 11A0000A

    => 110A0000A

    => 1101A0000A

    => 11010000A

    => 110100001A

    => 110100001

Thus 110100001 ? L.


Related Topics

Finite Automata

Finite Automata Finite state Automata or Finite State Machine is the simplest model used in Automata. Finite state automata accepts regular language. In this, the term finite means it has a...

4 minutes read.

Push down Automata Acceptance

Acceptance of Language by Push down Automata Push down Automata accepts the language, which is called context free language. Push down Automata = Finite Automata + Auxiliary Memory (Stack) Auxiliary Memory helps Push...

3 minutes read.

Ambiguity in Context Free Grammar - Automata

Ambiguity in Context Free Grammar? Context Free Grammar Context Free Grammar has one condition for production rules, which is, on the left-hand side of each rule, there must be only single variable,...

4 minutes read.

Derivation Tree of Context Free Grammar - Automata

Derivation Tree of Context Free Grammar Derivation tree gives a way to show how a string can be derived from context free grammar. It is also called as parse tree, production...

3 minutes read.

Automata Chomsky Hierarchy

Chomsky Hierarchy Formal languages: A language with precise syntax and semantics are called formal languages. Noam Chomsky has defined the Chomsky hierarchy in 1956. He is an American scientist and philosopher, and...

3 minutes read.

Minimization of Finite Automata

Minimization of Finite Automata The term minimization refers to the construction of finite automata with a minimum number of states, which is equivalent to the given finite automata. The number of...

4 minutes read.

Context Free Grammar - Automata

Context Free Grammar Grammar defines a set of rules, and with the help of these rules valid sentences in a language are constructed. A grammar consists of collection of substitution rules,...

3 minutes read.

Equivalence of Regular Grammar and Finite Automata

Equivalence of Regular Grammar and Finite Automata Regular Grammar / type -3 grammar A regular grammar or type-3 defines the language called regular language that is accepted by finite Automata.A Regular Grammar...

3 minutes read.

Pumping Lemma for Context Free Languages

Pumping Lemma for Context Free Languages The Pumping Lemma is made up of two words, in which, the word pumping is used to generate many input strings by pushing the symbol...

3 minutes read.

Chomsky's Normal Form - Automata

Chomsky's Normal Form (CNF) In context free grammar, the left-hand side of production rules contains only one variable, and right side may contain any number of variables or terminals in production...

4 minutes read.

Minimization of Finite Automata with Output

Minimization of Finite Automata with Output The method to minimize the Moore and Mealy machine is very much similar to the method that we have used to minimize the DFA. Methods to...

3 minutes read.

Automata Regular Expressions

Regular Expressions Regular expressions are also referred as rational expressions, which are used to describe the algebraic description of regular languages. It is generally a sequence of characters that is used...

3 minutes read.

Automata Greibach Normal Form

Greibach Normal Form In Greibach Normal Form, there is restriction on the position, in which, terminals and variables can appear on right-hand side of production rules. In Greibach Normal Form, every...

4 minutes read.

Non-deterministic Turing Machine - Automata

A non-deterministic Turing machine (NTM) is a theoretical machine used in computability theory to study the extent to which a machine can "guess" at a solution and yet still be...

4 minutes read.

Undecidable Problem of Turing Machine

Automata Undecidable Problem of Turing Machine Undecidable problems areproblems which cannot be solved by any Turing machine. Example: Post Correspondence Problem is the type of undecidable Problem. Post Correspondence Problem Post Correspondence problem is...

7 minutes read.

Finite Automata with Output

Finite Automata with Output The Finite State machine is similar to Finite Automata, except that it has the additional capacity of producing output. Finite State machine = Finite Automata + Output Capabilities Types...

3 minutes read.

Conversion from Moore Machine to Mealy Machine

Conversion from Moore Machine to Mealy Machine Moore Machine The output of Moore machine depends only on the present state. In this, if machine has N number of states, then it will...

6 minutes read.

Pumping Lemma for Regular Languages - Automata

Pumping Lemma for Regular Languages The language accepted by the finite automata is called Regular Language. If we are given a language L and asked whether it is regular or not?...

4 minutes read.

Automata Tutorial

What is Automata? Automata is a mathematical model and abstract model, which is used to detect string in various languages. In Automata, Regular language is recognized by Finite state AutomataContext Free language...

4 minutes read.

Closure Properties of Regular Languages -Automata

Closure Properties of Regular Languages We use the term “Closure” when we talk about sets of things. If we have two regular languages L1 and L2, and L is obtained by...

3 minutes read.