Context Free Languages Archives - https://www.theoryofcomputation.co/tag/context-free-languages/ Science of Computer Wed, 19 Sep 2018 17:58:32 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 https://i0.wp.com/www.theoryofcomputation.co/wp-content/uploads/2018/08/cropped-favicon-512x512-2.png?fit=32%2C32&ssl=1 Context Free Languages Archives - https://www.theoryofcomputation.co/tag/context-free-languages/ 32 32 149926143 What is Chomsky Hierarchy in Theory of Computation https://www.theoryofcomputation.co/what-is-chomsky-hierarchy-in-theory-of-computation/ Wed, 19 Sep 2018 16:54:42 +0000 https://www.theoryofcomputation.in/?p=206 What is Chomsky Hierarchy? Noam Chomsky categorised regular and other languages which called as Chomsky Hierarchy. Language Class Grammar Automaton 3 Regular NFA or DFA 2 Context-Free Push-Down Automaton 1 Context-Sensitive Linear-Bounded Automaton 0 Unrestricted (or Free) Turing Machine This is a hierarchy, so every language of type 3 is also of types 2, 1 and 0;...

The post What is Chomsky Hierarchy in Theory of Computation appeared first on .

]]>
What is Chomsky Hierarchy?

Noam Chomsky categorised regular and other languages which called as Chomsky Hierarchy.

Language Class Grammar Automaton
3 Regular NFA or DFA
2 Context-Free Push-Down Automaton
1 Context-Sensitive Linear-Bounded Automaton
0 Unrestricted (or Free) Turing Machine

What is Chomsky Hierarchy

This is a hierarchy, so every language of type 3 is also of types 2, 1 and 0; every language of type 2 is also of types 1 and 0 etc.

The distinction between languages can be seen by examining the structure of the production rules of their corresponding grammar, or the nature of the automata which can be used to identify them.

Type 3 – Regular Languages

A regular language is one which can be represented by a regular grammar, described using a regular expression, or accepted using an NFA or a DFA.

Type 2 – Context-Free Languages

A Context-Free Grammar (CFG) is one whose production rules are of the form: A -> α , where A is any single non-terminal, and α is any combination of terminals and non-terminals.

A NFA/DFA cannot recognise strings from this type of language since we must be able to “remember” information somehow. Instead we use a Push-Down Automaton which is like a DFA except that we are also allowed to use a stack.

Type 1 – Context-Sensitive Languages

Context-Sensitive grammars may have more than one symbol on the left-hand-side of their production rules (provided that at least one of them is a non-terminal). However, the production rules must now obey the following:

CS1
The number of symbols on the left-hand-side must not exceed the number of symbols on the right-hand-side
CS2
We do not allow rules of the form A → ε unless A is the start symbol and does not occur on the right-hand-side of any rule.

Since we allow more than one symbol on the left-hand-side, we refer to those symbols other than the one we are replacing as the context of the replacement.

The automaton which recognises a context-sensitive language is called a linear-bounded automaton: this is basically a NFA/DFA which can store symbols in a list.

Conditions CS1 and CS2 above mean that the sentential form in any derivation must always increase in length every time a production rule is applied. This basically means that the size of a sentential form is bounded by the length of the sentence (ie. word) we are deriving.

Since the sentinel form cannot thus grow infinitely large before deriving a sentence, a linear-bounded automaton always uses a finitely-long list as its store.

Type 0 – Unrestricted (Free) Languages

Free grammars have absolutely no restrictions on their grammar rules, (except, of course, that there must be at least one non-terminal on the left-hand-side).

The type of automata which can recognise such a language is basically a NFA/DFA with an infinitely-long list at its disposal to use as a store; this is called a Turing machine.

 

The post What is Chomsky Hierarchy in Theory of Computation appeared first on .

]]>
206
The Pumping Lemma for Context-Free Languages https://www.theoryofcomputation.co/the-pumping-lemma-for-context-free-languages/ Mon, 10 Sep 2018 18:57:45 +0000 https://www.theoryofcomputation.in/?p=190 The Pumping Lemma for Context-Free Languages (CFL) Proving that something is not a context-free language requires either finding a context-free grammar to describe the language or using another proof technique (though the pumping lemma is the most commonly used one). A common lemma to use to prove that a language is not context-free is the Pumping...

The post The Pumping Lemma for Context-Free Languages appeared first on .

]]>
The Pumping Lemma for Context-Free Languages (CFL)

Proving that something is not a context-free language requires either finding a context-free grammar to describe the language or using another proof technique (though the pumping lemma is the most commonly used one). A common lemma to use to prove that a language is not context-free is the Pumping Lemma for Context-Free Languages.

Theorem
The pumping lemma for context-free languages states that if a language L is 
context-free, there exists some integer length p ≥ 1 such that every string s ε L 
has a length of a p or more symbols, |s| ≥ p, that can written s = uvwxy where 
u, v, w, x and y are substrings of s such that:
    • |vwx| ≤ p
    • |vx| ≥ 1
    • uvnwxny ∈ ∀  n ≥ 0

All context-free languages are “pumpable” meaning that the pumping lemma constraints hold true for all context-free languages. If a language is not pumpable, then it is not a context-free language. However, if a language is pumpable, it is not necessarily a context-free language. Because the set of regular languages is contained in the set of context-free languages, all regular languages must be pumpable too.

Essentially, the pumping lemma holds that arbitrarily long strings can be pumped without ever producing a new string that is not in the language .

To prove that a language is not context-free, use proof by contradiction and the pumping lemma. Set up a proof that claims that is context-free​, and show that a contradiction of the pumping lemma’s constraints occurs in at least one of the three constraints listed above.

Basically, the idea behind the pumping lemma for context-free languages is that there are certain constraints a language must adhere to in order to be a context-free language. You can use the pumping lemma to test if all of these constraints hold for a particular language, and if they do not, you can prove with contradiction that the language is not context-free.

Example

Use the Pumping Lemma to prove that L = { anbncn|n>0 } is not a context-free language.

Assume, for the sake of contradiction, that L = {anbncn |n > 0  } is a context-free
language. By the pumping lemma, there exists an integer pumping length p for L. 
We need a string s that is longer than or equal to the length of p. Certainly 
s = apbpcp is longer than p, so we choose this for the s string. This s is in L since 
it has p a's , p b's and p c's.

Now by the pumping lemma, |vwx| ≤ p. There are five possible places in the string that 
we can assign to be vwx:
    • vwx = aj for some j ≤ p. This means that vwx is contained purely in the a’s section.
    • vwx = ajbk for some and  where j+k ≤ p. This means that the vwx segment is contained somewhere in the a’s and b’s section.
    • vwx = bj for some j ≤ p. This means that vwx is contained purely in the b’s section.
    • vwx = bjck for some and  where j+k ≤ p. This means that the vwx segment is contained somewhere in the b’s and c’s section.
    • vwx = cj for some j ≤ p. This means that vwx is contained purely in the c’s section.

In any of these five cases, we can easily verify that the third constraint for the pumping lemma, that uvnwxny ∈ L ∀ n ≥ 0, does not hold. In other words, for any of these five choices of vwx, the string cannot be pumped in a way that results in a string that has an equal number of a’s, b’s and c’s (the definition of the language L).

Read Also: Context Free Languages

Let’s take a short example string described by a5b5c5 = aaaaabbbbbccccc and p = 3.

In the first case, there will be more a’s than there are b’s and c’s, making the resulting, pumped string, not a member of L. If we pump this region, we will get the string aaaaaaaabbbbbccccc: a string with 8 a’s, 5 b’s and 5 c’s. Clearly this is not in the language. A similar proof can be checked for the third and fifth case, just pump the b and c region, respectively, and the results will be symmetrical.

For the second and fourth case, we do something similar. If we pump anywhere in the a and b region only, we will have a resulting string with more a’s and b’s than c’s (for the second case) and more b’s and c’s than a’s (in the fifth case). For the second case, if we take a5b5c5 = aaaaabbbbbccccc and p = 3 and pump the last a in the a section and the first two b’s in the b section, we get this string: aaaaaabbbbbbbccccc — a string with six a’s, seven b’s, and five c’s. The fifth case has a symmetrical example.

The post The Pumping Lemma for Context-Free Languages appeared first on .

]]>
190
Context Free Languages https://www.theoryofcomputation.co/context-free-languages/ Thu, 06 Sep 2018 19:18:28 +0000 https://www.theoryofcomputation.in/?p=185 Context-free languages (CFLs) are generated by context-free grammars. The set of all context-free languages is identical to the set of languages accepted by pushdown automata, and the set of regular languages is a subset of context-free languages. An inputed language is accepted by a computational model if it runs through the model and ends in an accepting final state. All regular languages...

The post Context Free Languages appeared first on .

]]>
Context-free languages (CFLs) are generated by context-free grammars. The set of all context-free languages is identical to the set of languages accepted by pushdown automata, and the set of regular languages is a subset of context-free languages.

An inputed language is accepted by a computational model if it runs through the model and ends in an accepting final state. All regular languages are context-free languages, but not all context-free languages are regular. Most arithmetic expressions are generated by context-free grammars, and are therefore, context-free languages.

Context-free languages and context-free grammars have applications in computer science and linguistics such as natural language processing and computer language design.

Context Free Languages

Context Free Languages Definition

In formal language theory, a language is defined as a set of strings of symbols that may be constrained by specific rules. Similarly, the written English language is made up of groups of letters (words) separated by spaces. A valid (accepted) sentence in the language must follow particular rules, the grammar.

A context-free language is a language generated by a context-free grammar. They are more general (and include) regular languages. The same context-free language might be generated by multiple context-free grammars.

The set of all context-free languages is identical to the set of languages that are accepted by pushdown  automata (PDA).

Here is an example of a language that is not regular (proof here) but is context-free:

{ anbn | n ≥ 0}.  This is the language of all strings that have an equal number of a’s and b’s.

In this notation,a4b4 can be expanded out too aaaabbbb, where there are four a’s and then four b’s. (So this isn’t exponentiation, through the notation is similar).

Read Also: Context Free Grammars

Closure Properties

Context-free languages have the following closure properties. A set is closed under an operation if doing the operation on a given set always produces a member of the same set. This means that if one of these closed operations is applied to a context-free language the result will also be a context-free language.

  • Union: Context-free languages are closed under the union operation. This means that if  are both context-free languages, then  is also a context-free language.
Proof:

Here is a proof that context-free grammars are closed under union
  1. Let L and P be generated by the context-free grammars, GL = (VL, ΣL, RL, SL) and GP = (VP, ΣP, RP, SP), respectively.
  2. Without loss of generality, subscript each nonterminal symbol in GL with an L, and each nonterminal of GP with a P such that VL ∩ VP = ∅.
  3. Define the CFG, G, that generates LP as follows: G=(VL ∪ VP ∪ {S}, ΣL ∪ ΣP, RL ∪ RP ∪ {S -> SL|SP}, S).
  • Concatenation: If L and are both context-free languages, then LP is also context free. The concatenation of a string is defined as follows: S1S2 = vw: v ∈ S1 and w ∈ S2.
Proof:

Here is a proof that context-free grammars are closed under union
    1. Let L and P be generated by the context-free grammars, GL = (VL, ΣL, RL, SL) and GP = (VP, ΣP, RP, SP), respectively.
    2. Without loss of generality, subscript each nonterminal symbol in GL with an L, and each nonterminal of GP with a P such that VL ∩ VP = ∅.
    3. Define the CFG, G, that generates LP as follows: G=(VL ∪ VP ∪ {S}, ΣL ∪ ΣP, RL ∪ RP ∪ {S -> SLSP}, S).
Every word that G generates is a word L followed by a word P, which is the definition of concatenation.
  •  Kleene Star: If  is a context-free language, then L ∗  is also context free. The Kleene star can repeat the string or symbol it is attached to any number of times (including zero times). The Kleene star basically performs a recursive concatenation of a string with itself. For example, {a,b}∗ = {ε, a, b, ab, aab, aaab, abb, ….} and so on. We’ve already proved that CFLs are closed under concatenation.

Context-free languages are not closed under complement or intersection.

If CFL’s  were closed under intersection then there would be CFLs that violate the pumping lemma for context-free languages which cannot be.

Please wait for our next post on Pumping Lemma.

Please Like Our Post on Facebook

Also see: Definition of Pushdown Automata

The post Context Free Languages appeared first on .

]]>
185