Pushdown Automata Archives - https://www.theoryofcomputation.co/tag/pushdown-automata/ Science of Computer Thu, 06 Sep 2018 19:18:39 +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 Pushdown Automata Archives - https://www.theoryofcomputation.co/tag/pushdown-automata/ 32 32 149926143 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
Context Free Grammars https://www.theoryofcomputation.co/context-free-grammars/ Thu, 23 Aug 2018 18:21:25 +0000 https://www.theoryofcomputation.in/?p=163 Context free grammars (CFGs) are used to describe context-free languages. A context-free grammar is a set of recursive rules used to generate patterns of strings. A context-free grammar can describe all regular languages and more, but they cannot describe all possible languages. Context-free grammars are studied in fields of theoretical computer science, compiler design, and linguistics. CFG’s are used to describe programming languages and parser...

The post Context Free Grammars appeared first on .

]]>
Context free grammars (CFGs) are used to describe context-free languages. A context-free grammar is a set of recursive rules used to generate patterns of strings. A context-free grammar can describe all regular languages and more, but they cannot describe all possible languages.

Context-free grammars are studied in fields of theoretical computer science, compiler design, and linguistics. CFG’s are used to describe programming languages and parser programs in compilers can be generated automatically from context-free grammars.

Context Free Grammars
Two parse trees that describe CFGs that generate the string “x + y * z”. Source: Context-free grammar wikipedia page.

Context Free Grammars:

Context-free grammars can generate context-free languages. They do this by taking a set of variables which are defined recursively, in terms of one another, by a set of production rules. Context-free grammars are named as such because any of the production rules in the grammar can be applied regardless of context—it does not depend on any other symbols that may or may not be around a given symbol that is having a rule applied to it.

Context-free grammars have the following components:
    • A set of terminal symbols which are the characters that appear in the language/strings generated by the grammar. Terminal symbols never appear on the left-hand side of the production rule and are always on the right-hand side.
    • A set of nonterminal symbols (or variables) which are placeholders for patterns of terminal symbols that can be generated by the nonterminal symbols. These are the symbols that will always appear on the left-hand side of the production rules, though they can be included on the right-hand side. The strings that a CFG produces will contain only symbols from the set of nonterminal symbols.
    • A set of production rules which are the rules for replacing nonterminal symbols. Production rules have the following form: variable  string of variables and terminals.
    • A start symbol which is a special nonterminal symbol that appears in the initial string generated by the grammar.
      
      

For comparison, a context-sensitive grammar can have production rules where both the left-hand and right-hand sides may be surrounded by a context of terminal and nonterminal symbols.

To create a string from a context-free grammar, follow these steps:
    • Begin the string with a start symbol.
    • Apply one of the production rules to the start symbol on the left-hand side by replacing the start symbol with the right-hand side of the production.
    • Repeat the process of selecting nonterminal symbols in the string, and replacing them with the right-hand side of some corresponding production, until all nonterminals have been replaced by terminal symbols. Note, it could be that not all production rules are used.

 

Formal Definition

A context-free grammar can be described by a four-element tuple (V, Σ, R, S) , where

  • V is a finite set of variables (which are non-terminal)
  • Σ is a finite set (disjoint from V) of terminal symbols
  • R is a set of production rules where each production rule maps a variable to a string  s ∈ (V ∪ Σ) *
  • S (which is in V ) which is a start symbol.
Example:
Come up with a grammar that will generate the context-free (and also regular) language that contains all strings with matched parentheses.

There are many grammars that can do this task. This solution is one way to do it, but should give you a good idea of if your (possibly different) solution works too.

Starting symbol -> S
Non-terminal variables = {(,)}
Production rules:
    • S -> ( )
    • S -> SS
    • S -> (S).

 

A way to condense production rules is as follows:

We can take

S->()
S->SS
S->(S)

and translate them into a single line: S ->  ( ) | SS | (S) | ε where ε is an empty string.

Context-free grammars can be modeled as parse trees. The nodes of the tree represent the symbols and the edges represent the use of production rules. The leaves of the tree are the end result (terminal symbols) that make up the string the grammar is generating with that particular sequence of symbols and production rules.

The parse trees below represent two ways to generate the string “a + a – a” with the grammar

 

Context Free Grammars
Example of an ambiguous grammar—one that can have multiple ways of generating the same string

Because this grammar can be implemented with multiple parse trees to get the same resulting string, this is said to be ambiguous.

Relationship with other Computation Models

A context-free grammar can be generated by pushdown automata just as regular languages can be generated by finite state machines. Since all regular languages can be generated by CFGs, all regular languages can too be generated by pushdown automata.

Any language that can be generated using regular expressions can be generated by a context-free grammar.

The way to do this is to take the regular language, determine its finite state machine and write production rules that follow the transition functions.

The post Context Free Grammars appeared first on .

]]>
163
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
Definition of Pushdown Automata https://www.theoryofcomputation.co/definition-pushdown-automata/ Fri, 17 Aug 2018 19:52:10 +0000 https://www.theoryofcomputation.in/?p=130 Definition of Pushdown Automata Pushdown Automata is a finite automata with extra memory called stack which helps Pushdown automata to recognize Context Free Languages. A Pushdown Automata (PDA) can be defined as : Q is the set of states ∑ is the set of input symbols Γ is the set of pushdown symbols (which can be...

The post Definition of Pushdown Automata appeared first on .

]]>
Definition of Pushdown Automata

Pushdown Automata is a finite automata with extra memory called stack which helps Pushdown automata to recognize Context Free Languages.

A Pushdown Automata (PDA) can be defined as :

  • Q is the set of states
  • ∑ is the set of input symbols
  • Γ is the set of pushdown symbols (which can be pushed and popped from stack)
  • q0 is the initial state
  • Z is the initial pushdown symbol (which is initially present in stack)
  • F is the set of final states
  • δ is a transition function which maps Q x { ∑ ∪ ɛ } x Γ into Q x Γ *. In a given state, PDA will read input symbol and stack symbol (top of the stack) and move to a new state and change the symbol of stack.

Pushdown automata are nondeterministic finite state machines augmented with additional memory in the form of a stack, which is why the term “pushdown” is used, as elements are pushed down onto the stack.

Pushdown automata are computational models — theoretical computer-like machines — that can do more than a finite state machine, but less than a Turing machine.

Pushdown automata accept context-free languages, which include the set of regular languages. The language that describes strings that have matching parentheses is a context-free language.

Say that a programmer has written some code, and in order for the code to be valid, any parentheses must be matched.

One way to do this would be to feed the code (as strings) into a pushdown automaton programmed with transition functions that implement the context-free grammar for the language of balanced parentheses.

If the code is valid, and all parentheses are matched, the pushdown automata will “accept” the code. If there are unbalanced parentheses, the pushdown automaton will be able to return to the programmer that the code is not valid. This is one of the more theoretical ideas behind computer parsers and compilers.

Pushdown automata can be useful when thinking about parser design and any area where context-free grammars are used, such as in computer language design.

Since pushdown automata are equal in power to context-free languages, there are two ways of proving that a language is context-free: provide the context-free grammar or provide a pushdown automaton for the language.

Pushdown Automata Theory of Computation
δ represents transition functions (the program of the pushdown automaton), is the stack symbol, is the tape symbol, and represents the state

A stack can be thought of as a stack of plates, one is stacked on top of the other, and plates can be taken off of the top of the stack. To get to the bottom of the stack of plates, all others must be removed first. Stacks are a last-in-first-out, or LIFO data structure. In pushdown automata, state transitions include adding a symbol to the string generated, like in FSMs, but state transitions can also include instructions about pushing and popping elements to and from the stack.

One can walk through the pushdown automata diagram to see what kinds of strings can be produced by the transition functions describing the language the pushdown automata generates, or you can feed it an input string and verify that there exists a set of transitions that end in an accepting state that creates the input string.

At each transition, a pushdown automaton can push a symbol to the stack, pop a symbol from the stack, do both, or do no operations to the stack. This transition symbol is ɛ. ɛ also represents the empty string and can be used as a symbol. If the instructions say that ɛ is the symbol read, this means that the stack/input is empty. If the instructions say to replace the symbol on top of the stack with an ɛ  this means to delete the symbol on top of the stack (this is popping).

The pushdown automaton starts with an empty stack and accepts if it ends in an accepting state at the end. The contents of the stack at the end do not matter unless the problem specifies that the stack must be empty at the end. If no transition from the current state can be made, reject. For example, if the transition from state A to state B requires popping an x  from the stack, if there is no x on the top of the stack to pop, reject.

Pushdown automata can be modeled as a state machine diagram with added instructions about the stack. Where in the finite state machine, the arrows between states were labeled with a symbol that represented the input symbol from the string, a pushdown automaton includes information in the form of input symbol followed by the symbol that is currently at the top of the stack, followed by the symbol to replace the top of the stack with. These instructions are sometimes separated by commas, slashes, or arrows.

Education on Pushdown Automata

The exception to the “replace with this symbol” command is during the first step after we write the $ symbol, we do not overwrite (i.e. pop/delete) the $ symbol. We need to keep this so that as we reach the end of the string, we know when we’ve reached the bottom of our stack. Instead of overwritting this symbol, simply place the next stack symbol on top of the $.

For this example, assume that s5 and s6 are the accepting states. This pushdown automaton only shows instructions for the stack, usually, the pushdown automata diagrams will also contain information about which symbols are needed to move from one state to another, but let’s use this example to get a feel for how the stack works. Assume the stack starts off empty, with the symbol , which indicates the bottom of the stack: so the stack is initially set to [$].

Transition of State in Pushdown Automata

What does the stack look like after following these transitions:  s1 to s2  to s3 ?

The push down automaton pushes “a”, pushes “b”, and then pushes another “b” so the stack at this point is [$,a,b,b].

Starting with the empty stack, what does the stack look like after the transitions s1 to s2  to s3  to s3  to s4  to s4 ?

The push down automaton pushes “a”, pushes “b”, pushes “b”, pushes “b”, pops “b”, and pops “b”, so the stack looks like [$, a, b].

Try It Yourself:

Given the following pushdown automata and input string 00001111, what does the PDA’s stack look like once it has gotten through and read the second 1 of the string (i.e. the substring 000011 has been read).

PushDown Automata Question

It is easy give your answer in comments.

 

What’s the point of a stack

A stack allows pushdown automata a limited amount of memory. A pushdown automaton can read from, push (add) to, or pop (remove) the stack. The transition functions that describe the pushdown automaton (usually represented by labels on the arrows between the state circles) tell the automaton what to do.

Pushdown automata accept context-free languages. This means that a context-free language can be represented by a pushdown automaton or a context-free grammar.

For example, the language containing all strings of 0’s followed by an equal number of 1’s is a context-free language, and it was proved on the regular languages page that this language is not a regular language, and so it is possible to represent this language using a pushdown automaton.

Here is a push down automaton that accepts strings in the language L = {0,1 | 0^n1^n for n >= 0 }.

PushDown Automata Question

Note: in the transition from A to B, do not overwrite the $ symbol with an empty string (i.e. don’t remove the $) just write the new symbol on top of that.

 

The post Definition of Pushdown Automata appeared first on .

]]>
130