Context Free Grammar Archives - https://www.theoryofcomputation.co/tag/context-free-grammar/ Science of Computer Mon, 10 Sep 2018 19:00:22 +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 Grammar Archives - https://www.theoryofcomputation.co/tag/context-free-grammar/ 32 32 149926143 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
Translating Between Context-Free Grammars and Pushdown Automata https://www.theoryofcomputation.co/context-free-grammars-and-pushdown-automata/ Sun, 19 Aug 2018 10:29:46 +0000 https://www.theoryofcomputation.in/?p=159 Context-free Grammar to Pushdown Automata Each derivation or sequence of production rules that results in a given string is made up of intermediate strings (which are made at each step of the derivation). The pushdown automata’s nondeterminism helps it to guess the sequence of steps in the derivation that will result in the desired string. So at...

The post Translating Between Context-Free Grammars and Pushdown Automata appeared first on .

]]>
Context-free Grammar to Pushdown Automata

Each derivation or sequence of production rules that results in a given string is made up of intermediate strings (which are made at each step of the derivation).

The pushdown automata’s nondeterminism helps it to guess the sequence of steps in the derivation that will result in the desired string. So at each step in the derivation, one of the production rules for a given variable is selected nondeterministically and substituted in for the variable.

The pushdown automata begins by pushing a symbol onto the stack and then goes through the series of intermediate strings until it arrives at a string that contains only the terminal symbols (this will happen if the string is actually in the grammar, otherwise it will reject).

Read More: Definition of Pushdown Automata

Here’s what to do

  • Push the start symbol, $, to the stack.

Then the following steps are repeated until the automaton finishes:

  • If there is a variable X on top of the stack, nondeterministically pick one of the production rules for  X and substitute X  with the string on the right-hand side of the production rule.
  • If there is a terminal variable a  on the input, read the next symbol from the input and compare it to a . If they are the same, repeat and if they are not, reject on this branch of the nondeterminism.
  • If it is the end of the input and the top of the stack has the start symbol, $, then accept

Can you come up with a diagram and formal description of a pushdown automaton that recognizes strings containing only parentheses and accepts on strings that have matched parentheses? 

Σ = {(,)}

Γ = {$,Χ} note:, where the Χ could be any symbol you want

Q = { A, B, C, D }

F = {D}

q0 = A

Z = $

δ = {(A,ε, ε, A, $), (A,(,$,B,X), (B, (, X,B,X), (B,),X,C,ε) , (C,),X,C,ε), (C, ε, $, D, ε) }

Context Free Grammer to PushDown Automata

[the_ad_group id=”24″]

The post Translating Between Context-Free Grammars and Pushdown Automata appeared first on .

]]>
159