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

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.

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.

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.

Converting Finite Automata to Regular Expression using Arden’s Theorem

Converting Finite Automata to Regular Expression using Arden’s Theorem The Arden’s Theorem can be applied to find the regular expression recognized by the given transition diagram. This theorem can be applied...

4 minutes read.

What is Automata Theory?

What is Automata Theory? Automata theory is used to study the abstract machine and automata (the self-acting machine).  Abstract machine is a theoretical model of a computer used in Automata theory. Automata...

4 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.

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.

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.

Simplification of Context Free Grammar - Automata

Simplification of Context Free Grammar Context Free Grammar has recursive structure. The languages that are accepted with Context Free Grammar are called Context Free Languages. Context Free Grammar has one condition...

3 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.

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.

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.

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.

Recursive Language and Recursive Enumerable Language

Recursive Language and Recursive Enumerable Language Before discussing Recursive language and Recursive Enumerable language, let us see what Turing machine responds for some input string. If we have the Turing machine T...

4 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.

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.

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.

Conversion from NFA to DFA

Conversion from NFA to DFA A Non-deterministic Finite Automata (NFA) is a finite state machine, in which, the move from one state to another is not fully deterministic, i.e., for a...

4 minutes read.

Conversion from Mealy Machine to Moore Machine

Conversion from Mealy Machine to Moore Machine In this topic, we will see different ways that are used for the conversion of Mealy Machine to Moore Machine. Moore Machine The output of Moore...

4 minutes read.