π Lecture 35 β Theory of Automata
π Overview: This lecture covers the elimination of null productions, nullable productions, and unit productions from Context-Free Grammars (CFGs), and introduces Chomsky Normal Form (CNF). These transformations are essential for simplifying grammars while preserving language recognition, a critical step in parsing and compiler design.
ποΈ Topics Covered
The lecture begins with examples of building Transition Graphs (TGs) corresponding to Regular Grammars. It then defines and demonstrates the removal of null productions (of the form N β Ξ), followed by nullable productions (nonterminals that can derive Ξ indirectly). The process for eliminating unit productions (nonterminal β nonterminal) is shown with a detailed example. Finally, Chomsky Normal Form is defined as a grammar containing only productions of the form nonterminal β two nonterminals or nonterminal β one terminal.
π Lecture Summary
Examples of building TG's corresponding to the Regular Grammar
The lecture provides two examples of converting CFGs into TGs. In the first example, the CFG S β aA|bB, A β aS|a, B β bS|b corresponds to a TG with states S-, A, B, and a + state, producing the RE (aa+bb)+. The second example shows the CFG S β aaS|bbS|abX|baX|Ξ, X β aaX|bbX|abS|baS generating a TG where the S- and X states loop with aa,bb and ab,ba respectively, and the language is EVEN-EVEN.
π Definition β Regular Grammar to TG: A direct construction where each production nonterminal β string is represented as a transition between states in a Transition Graph.
Null Production
Null productions are productions of the form nonterminal β Ξ. For example, in the CFG S β aA|bB|Ξ, A β aa|Ξ, B β aS, both S β Ξ and A β Ξ are null productions.
To eliminate null productions from a CFG while preserving the language (except for the word Ξ itself), follow this method: Delete all null productions, and for each production containing the nullable nonterminal, add new productions where the nonterminal is omitted in all possible combinations. For example, given X β aNbNa and N β Ξ, delete N β Ξ and add X β aNba, X β abNa, and X β aba, keeping the original X β aNbNa.
Nullable Production
A nullable production is either a null production (N β Ξ) or a production where there exists a derivation from the nonterminal N that eventually leads to Ξ, i.e., Nβ β Nβ, Nβ β Nβ, ... Nβ β Ξ.
π‘ Why this matters: Nullable productions can indirectly produce Ξ through a chain of productions, and removing them requires handling all intermediate nonterminals.
The method for removing nullable productions is the same as for null productions, but applied to all nullable nonterminals. For the CFG: S β XaY|YY|aX|ZYX, X β Za|bZ|ZZ|Yb, Y β Ya|XY|Ξ, Z β aX|YYY, the nullable productions are S β YY, X β ZZ, Z β YYY, and the null production is Y β Ξ. The resulting CFG without null productions includes all combinations of omitting nullable nonterminals, e.g., S β XaY|Xa|aY|a|YY|Y|aX|ZYX|YX|ZX|ZY|X|Z.
π Example: Given S β XY, X β Zb, Y β bW, Z β AB, W β Z, A β aA|bA|Ξ, B β Ba|Bb|Ξ. Here A β Ξ and B β Ξ are null productions, while Z β AB and W β Z are nullable productions. The new CFG becomes: S β XY, X β Zb|b, Y β bW|b, Z β AB|A|B, W β Z, A β aA|a|bA|b, B β Ba|a|Bb|b.
Unit production
Unit productions are productions of the form nonterminal β one nonterminal. For example, in S β A|bb, A β B|b, B β S|a, the unit productions are S β A, A β B, and B β S.
To eliminate unit productions, separate them from nonunit productions. Then, for each unit production chain (e.g., S β A, A β B), add the right-hand sides of the nonunit productions reachable from the original nonterminal. The unit productions are then deleted.
π Example: From the unit productions S β A, A β B, B β S, and nonunit productions S β bb, A β b, B β a:
- S β A gives S β b (using A β b)
- S β A β B gives S β a (using B β a)
- A β B gives A β a (using B β a)
- A β B β S gives A β bb (using S β bb)
- B β S gives B β bb (using S β bb)
- B β S β A gives B β b (using A β b) The new CFG is S β a|b|bb, A β a|b|bb, B β a|b|bb, generating the finite language {a, b, bb}.
Chomsky Normal Form (Definition)
A CFG is in Chomsky Normal Form (CNF) if every production is of the form:
- nonterminal β string of two nonterminals, or
- nonterminal β one terminal
π‘ Why this matters: CNF is a restricted form that simplifies parsing algorithms, such as the CYK algorithm, and is a standard step in many compiler and language processing applications.
β Key Takeaways
The lecture presents three essential grammar transformations: null/ nullable production removal and unit production elimination, with detailed examples for each. Null and nullable productions are removed by replacing nullable nonterminals with all possible combinations of their presence or absence, while unit productions are eliminated by substituting the chain of nonterminals with their terminal or nonunit derivations. Chomsky Normal Form is defined as a grammar restricted to productions of two nonterminals or one terminal, forming a foundational structure for efficient parsing algorithms.
π§ Quick Revision Questions
- What is the difference between a null production and a nullable production?
- In the CFG S β XaY, X β Za, Y β Ξ, Z β aX, which productions are nullable and which are null?
- How would you eliminate the unit production S β A from a CFG where A β bB|a and B β c?
- Why must the word Ξ be treated separately when removing null productions from a CFG?
- What two forms must all productions take for a CFG to be in Chomsky Normal Form?
π Lecture 36 β Chomsky Normal Form
π Overview: This lecture introduces the Chomsky Normal Form (CNF) and proves that any context-free grammar can be converted into CNF to generate all nonnull words of the corresponding CFL. It also covers the construction of FAs from regular CFGs, leftmost and rightmost derivations, and sets the stage for a new, more powerful class of machines accepting CFLs.
ποΈ Topics Covered
The lecture covers the definition of Chomsky Normal Form (CNF) and the theorem that all nonnull words of a CFL can be generated by a CFG in CNF. It provides detailed examples of converting CFGs to CNF, including handling null and unit productions. It also presents an example of an FA corresponding to a regular CFG, defines leftmost and rightmost derivations with examples and a theorem, and introduces the concept of a new format for FAs to accept CFLs.
π Lecture Summary
Chomsky Normal Form (CNF)
If a CFG has only productions of the form nonterminal β string of two nonterminals or nonterminal β one terminal, then the CFG is said to be in Chomsky Normal Form (CNF).
π‘ Why this matters: CNF is a standardized form that simplifies the analysis of CFGs, especially for proving properties about CFLs.
Note: Any CFG can be converted to be in CNF, if the null productions and unit productions are removed. Also, if a CFG contains nullable productions, then the corresponding new productions are also to be added.
Theorem
All NONNULL words of the CFL can be generated by the corresponding CFG which is in CNF, i.e., the grammar in CNF will generate the same language except the null string.
Example (PALINDROME)
Consider the following CFG: S β aSa | bSb | a | b | aa | bb
To convert to CNF, introduce new productions A β a, B β b, then the new CFG will be: S β ASA | BSB | AA | BB | a | b A β a B β b
Introduce nonterminals Rβ and Rβ so that: S β ARβ | BRβ | AA | BB | a | b Rβ β SA Rβ β SB A β a B β b
which is in CNF.
π Definition β Chomsky Normal Form (CNF): A CFG is in CNF if all productions are of the form nonterminal β two nonterminals or nonterminal β one terminal.
π Procedure: Convert CFG to CNF by:
- Remove null productions
- Remove unit productions
- Introduce new nonterminals for terminals
- Break productions with more than two nonterminals into binary form
π Example: The CFG S β aSa | bSb | a | b | aa | bb generates NONNULLPALINDROME. After conversion to CNF, it uses S β ARβ | BRβ | AA | BB | a | b, Rβ β SA, Rβ β SB, A β a, B β b.
Example (with null productions)
Consider the following CFG: S β ABAB A β a | Π B β b | Π
Here, S β ABAB is a nullable production and A β Π, B β Π are null productions.
Step 1: Removing null productions A β Π and B β Π, and introducing new productions: S β BAB | AAB | ABB | ABA | AA | AB | BA | BB | A | B
Step 2: Now S β A and S β B are unit productions to be eliminated:
- S β A gives S β a (using A β a)
- S β B gives S β b (using B β b)
The new resultant CFG takes the form: S β BAB | AAB | ABB | ABA | AA | AB | BA | BB | a | b A β a B β b
Step 3: Introduce the nonterminal C where C β AB, so that: S β BC | AC | CB | CA | AA | C | BA | BB | a | b A β a B β b C β AB is the CFG in CNF.
Example of an FA corresponding to Regular CFG
To construct an FA that accepts the grammar: S β abA A β baB B β aA | bb
The language can be identified by the three words generated:
Derivation 1: S β abA β abbaB (using AβbaB) β abbabb (using Bβbb)
Derivation 2: S β abA β abbaB (using AβbaB) β abbaaA (using BβaA) β abbaabaB (using AβbaB) β abbaababb (using Bβbb)
Derivation 3: S β abA β abbaB (using AβbaB) β abbaaA (using BβaA) β abbaabaB (using AβbaB) β abbaabaaA (using BβaA) β abbaabaabaB (using AβbaB) β abbaabaababb (using Bβbb)
This shows that the corresponding language has RE: *abba(aba)bb
The FA accepting the given CFG may be: [S-] --a--> [A] --b--> [B] --a--> [A] --b--> [+] [B] --b--> [+] [B] --a--> [A]
Leftmost Derivation
Definition: The derivation of a word w, generated by a CFG, such that at each step, a production is applied to the leftmost nonterminal in the working string, is said to be leftmost derivation.
π Definition β Leftmost nonterminal: The nonterminal that occurs first from the left in the working string.
Example
Consider the following CFG: S β XY X β XX | a Y β YY | b
Following are two leftmost derivations of aaabb:
Derivation 1: S β XY β XXY β aXY β aXXY β aaXY β aaaY β aaaYY β aaabY β aaabb
Derivation 2: S β XY β XXY β XXXY β aXXY β aaXY β aaaY β aaaYY β aaabY β aaabb
Theorem
Any word that can be generated by a certain CFG has also a leftmost derivation.
Note: The above theorem can be stated for rightmost derivation as well.
Example (Leftmost vs Rightmost)
Consider the following CFG: S β YX X β XX | b Y β YY | a
Following are the leftmost and rightmost derivations of abbbb:
Leftmost derivation: S β YX β aX β aXX β abX β abXX β abbX β abbXX β abbbX β abbbb
Rightmost derivation: S β YX β YXX β YXb β YXXb β YXbb β YXXbb β YXbbb β Ybbbb β abbbb
A New Format for FAs
A class of machines (FAs) has been discussed accepting the regular language. It has also been discussed that there is a CFG corresponding to regular language, and CFGs also define some nonregular languages.
There is a question: is there a class of machines accepting the CFLs? The answer is yes. The new machines which are to be defined are more powerful and can be constructed with the help of FAs with a new format.
π‘ Why this matters: This sets the foundation for Pushdown Automata (PDA) which accept CFLs.
β Key Takeaways
A student must remember that Chomsky Normal Form (CNF) requires all productions to be either nonterminal β two nonterminals or nonterminal β one terminal, and any CFG can be converted to CNF by removing null and unit productions. The leftmost derivation applies a production to the leftmost nonterminal at each step, and the theorem states that any word generated by a CFG has a leftmost derivation. The example of an FA from a regular CFG shows the correspondence between regular grammars and finite automata. Finally, the new format for FAs introduces the idea that more powerful machines are needed to accept CFLs, leading to the concept of Pushdown Automata (PDA) .
π§ Quick Revision Questions
- What are the two allowed production forms in Chomsky Normal Form (CNF)?
- According to the theorem, what type of words does a CFG in CNF generate?
- In the example converting
S β aSa|bSb|a|b|aa|bbto CNF, what is the role of the new nonterminalsRβandRβ? - What is the definition of a leftmost derivation?
- Based on the example with grammar
S β abA, A β baB, B β aA|bb, what is the regular expression for the language generated?
π Lecture 37 β New Format for FAs, PDA
π Overview: This lecture introduces a new format for Finite Automata (FAs) and extends it to create Pushdown Automata (PDAs), which are machines capable of accepting Context-Free Languages (CFLs). It explains the components of this new format, including the input TAPE, START, ACCEPT, REJECT, and READ states, and then introduces the PUSHDOWN STACK with PUSH and POP operations. The lecture culminates with an example of a PDA that recognizes the non-regular language {aβΏbβΏ : n β₯ 0}, demonstrating the increased power of this new machine class.
ποΈ Topics Covered
The lecture begins by questioning whether there is a class of machines accepting CFLs, answering with the introduction of Pushdown Automata (PDAs) built from FAs in a new format. It defines the components of this new FA format: the Input TAPE (partitioned into cells for input letters), START state, ACCEPT state, REJECT state, and READ state. Two examples illustrate the new format for FAs accepting (a+b)*a and (a+b)bb(a+b). Then, the PUSHDOWN STACK is introduced as a memory structure, along with PUSH and POP operations. An example PDA accepting the language {aβΏbβΏ : n β₯ 0} is presented with a step-by-step walkthrough of processing the string aaabbb.
π Lecture Summary
New format for FAs
A new class of machines, more powerful than simple FAs, is needed to accept Context-Free Languages (CFLs). These machines are built by first defining a new format for FAs. The components of this new format are:
- Input TAPE: The part of an FA where the input string is placed before it is run. The input TAPE is partitioned into cells, so each letter of the input string can be placed in each cell. The input string abbaa is shown in the following input TAPE: Cell i (a), Cell ii (b), Cell iii (b), Cell iv (a), Cell v (a), then β (blank). The character β indicates a blank in the TAPE. It is assumed that when the first β is read, the rest of the TAPE is supposed to be blank.
- The START state: This state is like the initial state of an FA and is represented by a small circle.
- An ACCEPT state: This state is like a final state of an FA and is expressed by a circle with a dot inside.
- A REJECT state: This state is like a dead-end non-final state and is expressed by an open circle.
- π‘ Why this matters: The ACCEPT and REJECT states are called the halt states.
- A READ state: This state is to read an input letter and lead to some other state. The READ state is expressed by a rectangle with a downward arrow on the left side.
Example
Consider an FA along with its new format. The FA accepts the language of strings expressed by (a+b)*a. The new format of this FA is: a START state leads to a READ state (with a self-loop for b). The READ state has an edge labeled 'a' to a second READ state (with a self-loop for b). The first READ state also has an edge labeled β (blank) to a REJECT state. The second READ state has an edge labeled β to an ACCEPT state. The β edge should not be confused with a Ξ-labeled edge. β-edges start only from READ boxes and lead to halt states.
Example
Another example shows an FA that accepts the language expressed by (a+b)bb(a+b). This FA, in its new format, would have START, READ states, and edges labeled with a, b, and β leading to ACCEPT or REJECT states.
PUSHDOWN STACK or PUSHDOWN STORE
PUSHDOWN STACK is a place where the input letters can be placed until these letters are referred again. It can store as many letters as one can in a long column. Initially the STACK is supposed to be empty, i.e., each of its storage locations contains a blank (β).
- PUSH: A PUSH operator adds a new letter at the top of STACK. For example, if the letters a, b, c, and d are pushed to the STACK that was initially blank, the STACK can be shown as d (top), c, b, a, β (bottom). The PUSH state is expressed by a rectangle with "PUSH" and the letter being pushed. When a letter is pushed, it replaces the existing letter and pushes it one position below.
- POP and STACK: POP is an operation that takes out a letter from the top of the STACK. The rest of the letters are moved one location up. The POP state is expressed by a rectangle with "POP" and a letter (or variable). Note that popping an empty STACK is like reading an empty TAPE, i.e., popping a blank character β.
- π‘ Why this matters: When the new format of an FA contains PUSH and POP states, it is called PUSHDOWN Automata or PDAs. Adding the PUSHDOWN STACK (the memory structure) to an FA increases its language-recognizing capabilities considerably.
Example
Consider the following PDA that accepts the language {aβΏbβΏ : n = 0, 1, 2, ...}. The PDA has states: START, READβ, PUSH a, POPβ, READβ, POPβ, ACCEPT, and REJECT. The transitions are:
- START β READβ
- READβ: reading 'a' β PUSH a; reading β β POPβ
- PUSH a β READβ
- READβ: reading 'b' β POPβ
- POPβ: popping 'a' β READβ; popping β β REJECT
- READβ: reading 'b' β POPβ; reading β β POPβ
- POPβ: popping β β ACCEPT; popping 'a' β REJECT
The string aaabbb is run on this machine.
- The string is placed on the TAPE (a a a b b b β β ...) and the STACK is empty (β at top).
- Reading the first 'a' from the TAPE, we move from READβ to PUSH a state. This causes the letter 'a' to be deleted from the TAPE and added to the top of the STACK.
- Reading the next two 'a's successively will delete further two 'a's from the TAPE and add these letters to the top of the STACK. The STACK now has: a (top), a, a, β, β, ...
- Reading the next letter, which is 'b', from the TAPE will lead to the POPβ state. The top letter at the STACK is 'a', which is popped out, and READβ state is entered. The STACK now has: a (top), a, β, ...
- Reading the next two 'b's successively will delete two 'b's from the TAPE, will lead to the POPβ state, and these 'b's will remove the 'a's from the STACK one by one. The STACK now has: β (top), β, ...
- Now there is only the blank character β left to be read from the TAPE, which leads to POPβ state. While only blank characters are left in the STACK to be popped out, the ACCEPT state is entered, showing that the string aaabbb is accepted by this PDA. It may be observed that the above PDA accepts the language {aβΏbβΏ: n = 0, 1, 2, ...}. Since the null string is like a blank character, reading β at state READβ leads to POPβ state, and POPβ state contains only β, hence it leads to ACCEPT state, and the null string is accepted.
π Definition β PDA (Pushdown Automaton): An FA equipped with a PUSHDOWN STACK and PUSH/POP operations, capable of accepting Context-Free Languages.
π Formula: L = {aβΏbβΏ : n β₯ 0} β A non-regular language that is a CFL, accepted by a PDA by pushing 'a's and popping them with corresponding 'b's.
π Example: Running the string aaabbb on the PDA.
- Initial State: TAPE = [a, a, a, b, b, b, β, β,...], STACK = [β, β, β,...] (empty)
- Step 1: Read 'a' from TAPE β PUSH 'a' β STACK = [a, β, β,...]
- Step 2: Read 'a' from TAPE β PUSH 'a' β STACK = [a, a, β,...]
- Step 3: Read 'a' from TAPE β PUSH 'a' β STACK = [a, a, a, β,...]
- Step 4: Read 'b' from TAPE β POP 'a' from STACK β STACK = [a, a, β,...]
- Step 5: Read 'b' from TAPE β POP 'a' from STACK β STACK = [a, β, β,...]
- Step 6: Read 'b' from TAPE β POP 'a' from STACK β STACK = [β, β, β,...]
- Step 7: Read β from TAPE β POP β from STACK β ACCEPT.
β Key Takeaways
The most critical point is that adding a stack memory structure transforms an FA into a Pushdown Automaton (PDA), which is a fundamentally more powerful machine capable of accepting Context-Free Languages. You must understand the new format for FAs, including the Input TAPE and the START, ACCEPT, REJECT, and READ states, as this is the foundation for the PDA. The PUSHDOWN STACK, with its PUSH and POP operations, provides the mechanism to count and match symbols, which is essential for recognizing languages like {aβΏbβΏ}. The example of the PDA accepting {aβΏbβΏ} is a classic and must be understood process-wise, as it demonstrates the core concept of pushing and popping. Finally, remember that β represents a blank on both the TAPE and the STACK, and that reading or popping a blank leads to halt states.
π§ Quick Revision Questions
- What is the new component that is added to an FA to create a PDA, and what two operations does it support?
- In the new format for an FA, what is the difference between an ACCEPT state and a REJECT state, and what are they collectively called?
- Using the PDA from the lecture, explain step-by-step how the string "aabb" would be processed to reach an ACCEPT state.
- What would happen if the string "aab" were run on the PDA designed for {aβΏbβΏ}? Draw the final state.
- Why is a PDA considered more powerful than a standard FA? Give an example of a language that a PDA can recognize but an FA cannot.
π Lecture 38 β Theory of Automata
π Overview: This lecture covers Pushdown Automata (PDA) in detail, including deterministic and nondeterministic varieties. It provides worked examples of running strings through PDAs, introduces the formal definition of PDA, and demonstrates how PDAs correspond to context-free languages (CFLs).
ποΈ Topics Covered
The lecture begins with a table-based trace of running the string aaabbb through a PDA that accepts the language {aβΏbβΏ : n = 0,1,2,3,...}. It then presents an equivalent PDA representation, followed by an example of a PDA corresponding to the EVEN-EVEN language FA. Nondeterministic PDA is introduced with the EVENPALINDROME example, followed by the complete formal definition of Pushdown Automaton. Finally, an example PDA is given for the CFG S β S+S | S*S | 4, with a full trace of the string 4+4*4.
π Lecture Summary
Example of PDA with table for running a string
The process of running the string aaabbb through a PDA that accepts {aβΏbβΏ} is expressed in a state-stack-tape table.
The sequence is:
- START, then READβ (with β on stack,
aaabbbβ...on tape) - PUSH a β stack becomes
aβ - READβ again, PUSH a β stack becomes
aaβ - READβ again, PUSH a β stack becomes
aaaβ - READβ β POPβ β stack becomes
aaβ - READβ β POPβ β stack becomes
aβ - READβ β POPβ β stack becomes
β - READβ β POPβ β stack remains
β - ACCEPT
π Definition β Language accepted: The above PDA accepts the language {aβΏbβΏ : n = 0,1,2,3,...} β strings with equal numbers of a's followed by b's.
π Observation: The TAPE alphabet Ξ£ and STACK alphabet Ξ may be different in general.
Equivalent PDA
The PDA equivalent to the one accepting {aβΏbβΏ} is presented with a diagram showing:
- START β READβ (loop: PUSH a on
a, stay at READβ) - READβ β POPβ (on
b) - POPβ β loop on
a(PUSH a) andb(POPβ), or go to READβ onβ - READβ β loop on POPβ and POPβ, leading to ACCEPT on
β
PDA for EVEN-EVEN Language
An FA for the EVEN-EVEN language is given (languages with an even number of a's and even number of b's).
The corresponding PDA is constructed from this FA, with states:
- START β READ states with transitions for
aandb - Leading to ACCEPT
Each transition in the FA corresponds to a READ state in the PDA.
Nondeterministic PDA
Like TGs and NFAs, if a PDA has more than one outgoing edge at READ or POP states with the same label, this creates nondeterminism. Such a PDA is called a nondeterministic PDA.
π Key properties:
- No edge is labeled by a string of terminals or nonterminals
- If there is no edge for a letter to be read, the machine crashes and the string is rejected
- A string may trace more than one path
- If at least one path leads to ACCEPT, the string is accepted; otherwise rejected
π Example: Nondeterminism occurs at state READβ in the PDA for EVENPALINDROME.
π Example β EVENPALINDROME: The nondeterministic PDA accepts the language
EVENPALINDROME = {w reverse(w) : w β {a,b}*}
= {Ξ, aa, bb, aaaa, abba, baab, bbbb, ...}
Definition of PUSHDOWN AUTOMATON (PDA) β including nondeterminism
Pushdown Automaton (PDA) consists of:
- An alphabet Ξ£ of input letters
- An input TAPE with infinitely many locations in one direction. Initially the input string is placed starting from the first cell; the remaining part is empty
- An alphabet Ξ of STACK characters
- A pushdown STACK which is initially empty, with infinitely many locations in one direction. Initially the STACK contains blanks
- One START state with only one out-edge and no in-edge
- Two halt states: ACCEPT and REJECT states, with in-edges and no out-edges
- A PUSH state that introduces characters onto the top of the STACK
- A POP state that reads the top character of the STACK (may contain more than one out-edges with the same label)
- A READ state that reads the next unused letter from the TAPE (may contain more than one out-edges with the same label)
Example of Non-Deterministic PDA β CFG S β S+S | S*S | 4
Consider the CFG:
S β S+S | S*S | 4
The PDA accepting the corresponding CFL has the following structure:
- START β PUSHβ S
- POP β loops back to:
- PUSHβ S (followed by PUSHβ +, then PUSHβ S) β back to POP
- PUSHβ S (followed by PUSHβ *, then PUSHβ S) β back to POP
- READβ (on β)
- READβ β loop to POP (on any letter), or go to READβ, READβ, READβ
- Final transitions lead to ACCEPT
π Example β String 4+4*4 trace:
| STATE | STACK | TAPE |
|---|---|---|
| START | β | 4+4*4 |
| PUSHβ S | S | 4+4*4 |
| POP | β | 4+4*4 |
| PUSHβ S | S | 4+4*4 |
| PUSHβ + | +S | 4+4*4 |
| PUSHβ S | S+S | 4+4*4 |
| POP | +S | 4+4*4 |
| READβ | +S | +4*4 |
| POP | S | +4*4 |
| READβ | S | 4*4 |
| POP | β | 4*4 |
| PUSHβ S | S | 4*4 |
| PUSHβ * | *S | 4*4 |
| PUSHβ S | S*S | 4*4 |
| POP | *S | 4*4 |
| READβ | *S | *4 |
| POP | S | *4 |
| READβ | S | 4 |
| POP | β | 4 |
| READβ | β | β |
| POP | β | β |
| READβ | β | β |
| ACCEPT | β | β |
π‘ Why this matters: The choice of path at POP state can be determined by the leftmost derivation of the string belonging to the CFL. Letters are deleted from the TAPE instead of underlined.
β Key Takeaways
- A PDA for {aβΏbβΏ} uses the stack to count a's (push) and match b's (pop), accepting when stack is empty at end of input.
- Nondeterministic PDAs have multiple outgoing edges from READ or POP states with the same label; a string is accepted if any path leads to ACCEPT.
- The formal PDA definition includes an input TAPE alphabet Ξ£, a STACK alphabet Ξ, START, ACCEPT, REJECT, PUSH, POP, and READ states.
- PDAs can be constructed from CFGs where each production corresponds to a PUSH sequence, and the POP-READ loop simulates leftmost derivation.
- The EVENPALINDROME language demonstrates nondeterminism β the PDA must guess when the middle of the palindrome is reached.
π§ Quick Revision Questions
- How does a deterministic PDA accept the language {aβΏbβΏ : n β₯ 0}? Describe the stack operations.
- What creates nondeterminism in a PDA, and how is acceptance determined in a nondeterministic PDA?
- List all nine components of the formal definition of a Pushdown Automaton.
- For the CFG S β S+S | S*S | 4, what is the leftmost derivation of the string
4+4*4? - How does the PDA for EVENPALINDROME differ from the PDA for {aβΏbβΏ}?
π Lecture 39 β PDA corresponding to CFG, Examples of PDA corresponding to CFG
π Overview: This lecture proves the theorem that for any Context-Free Grammar (CFG), there exists a Pushdown Automaton (PDA) that accepts the language generated by that CFG. It demonstrates the construction algorithm using examples of CFGs in Chomsky Normal Form (CNF) and shows step-by-step how the PDA processes input words through leftmost derivations.
ποΈ Topics Covered
The lecture covers the theorem stating that corresponding to any CFG there exists a PDA accepting the language generated by the CFG. It explains how to construct the PDA when the CFG is in CNF, shows a detailed example with the word "aab" including the complete state-transition table, and provides a second example converting a CFG with terminals into CNF before constructing its corresponding PDA.
π Lecture Summary
PDA corresponding to CFG
Theorem: Corresponding to any CFG there exists a PDA accepting the language generated by the CFG.
Since an algorithm has already been discussed to convert the CFG in CNF (Chomsky Normal Form), so the PDA can be constructed corresponding to the CFG. As the CFG in CNF generates all the nonnull words of the corresponding CFL (Context-Free Language), accepting the null string (if it is contained in the CFL) can be managed separately.
π‘ Why this matters: This theorem establishes the equivalence between CFGs and PDAs, showing that these two different models of computation describe exactly the same class of languages.
Example 1
Consider the following CFG which is in CNF and does not generate the null string:
S β SB | AB A β CC B β b C β a
The corresponding PDA will have:
- STACK alphabet Ξ = {S, A, B, C}
- TAPE alphabet Ξ£ = {a, b}
The PDA structure includes:
- START state
- PUSH S state
- POP state
- PUSH A, PUSH B, PUSH C states
- READβ, READβ, READβ states
- ACCEPT state
Note: It may be noted that when the POP state is entered either a nonterminal is replaced by two nonterminals at the top of the STACK accommodating a production, or a nonterminal is popped out from the top of the stack and a READ state is entered to read a specified letter from the TAPE or else the machine crashes.
The choice of path taken at POP state to accommodate the word belonging to the CFL can be determined by the leftmost derivation of the word.
Consider the word aab with its leftmost derivation:
| Working-String Generation | Production Used |
|---|---|
| S β AB | S β AB (step 1) |
| β CCB | A β CC (step 2) |
| β aCB | C β a (step 3) |
| β aaB | C β a (step 4) |
| β aab | B β b (step 5) |
Step-by-step execution of the PDA for "aab":
-
START state is entered β STACK: β, TAPE: aabβ
-
PUSH S state is entered β STACK: S, TAPE: aabβ
-
POP state is entered β to accommodate production S β AB, PUSH B and PUSH A states are entered β STACK: AB, TAPE: aabβ
-
POP state is entered β to accommodate production A β CC, PUSH C, PUSH C states are entered β STACK: CCB, TAPE: aab
-
POP state is entered β to accommodate production C β a, READβ is entered and the letter a is read from the TAPE β STACK: CB, TAPE: aab
-
POP state is entered β to accommodate production C β a, READβ state is entered and the letter a is read from the TAPE β STACK: B, TAPE: aab
-
POP state is entered β to accommodate production B β b, READβ state is entered and the letter b is read from the TAPE β STACK: β, TAPE: aab
-
The β shown in the STACK indicates that there are no nonterminals in the working string and β is read from the STACK which leads to READβ state where the β is read from the TAPE and the ACCEPT state is entered which shows that the word aab is accepted by the PDA.
Complete table for word "aab":
| Leftmost Derivation | STATE | STACK | TAPE |
|---|---|---|---|
| START | β | aab | |
| S | PUSH S | S | aab |
| POP | β | aab | |
| PUSH B | B | aab | |
| βAB | PUSH A | AB | aab |
| POP | B | aab | |
| PUSH C | CB | aab | |
| βCCB | PUSH C | CCB | aab |
| POP | CB | aab | |
| βaCB | READβ | CB | aab |
| POP | B | aab | |
| βaaB | READβ | B | aab |
| POP | β | aab | |
| βaab | READβ | β | aab |
| POP | β | aab | |
| READβ | β | aab | |
| ACCEPT | β | aab |
Example 2
Consider the following CFG:
S β XY X β aX | bX | a Y β Ya | Yb | a
First of all, converting the CFG to be in CNF, introduce the nonterminals A and B as:
A β a B β b
The following CFG is in CNF:
S β XY X β AX | BX | a Y β YA | YB | a A β a B β b
The PDA corresponding to this CFG includes:
- START state
- PUSH S state
- POP state
- PUSH X, PUSH A, PUSH B, PUSH Y states
- READβ, READβ, READβ, READβ, READβ states
- ACCEPT state
The word aaab can be generated as:
| Working-String Generation | Production Used |
|---|---|
| S β XY | S β XY (step 1) |
| β AXY | X β AX (step 2) |
| β aXY | A β a (step 3) |
| β aaY | X β a (step 4) |
| β aaYB | Y β YB (step 5) |
| β aaaB | Y β a (step 6) |
| β aaab | B β b (step 7) |
Step-by-step execution table for "aaab":
| STATE | STACK | TAPE |
|---|---|---|
| START | β | aaab |
| PUSH S | S | aaab |
| POP | β | aaab |
| PUSH Y | Y | aaab |
| PUSH X | XY | aaab |
| POP | Y | aaab |
| PUSH X | XY | aaab |
| PUSH A | AXY | aaab |
| POP | XY | aaab |
| READβ | XY | aaab |
| POP | Y | aaab |
| READβ | Y | aaab |
| POP | β | aabb |
| PUSH B | B | aabb |
| PUSH Y | YB | aabb |
| POP | B | aaab |
| READβ | B | aaab |
| POP | β | aaab |
| READβ | β | aaab |
| POP | β | β |
| READβ | ||
| ACCEPT |
β Key Takeaways
The critical concept is that any CFG can be converted into a PDA that accepts exactly the language generated by that grammar. The construction algorithm requires the CFG to be in Chomsky Normal Form first, then builds a PDA that simulates leftmost derivations by pushing and popping nonterminals on the stack while reading input letters. The PDA uses its stack to hold the working string during derivation, and each production is applied by pushing the right-hand side symbols (in reverse order) onto the stack when a nonterminal is popped. The acceptance condition occurs when both the stack and the input tape contain only the end-marker β, confirming that the entire word has been derived.
π§ Quick Revision Questions
-
What is the STACK alphabet and TAPE alphabet in the first example's PDA corresponding to the CFG S β SB|AB, A β CC, B β b, C β a?
-
How does the PDA determine which path to take at the POP state when accommodating a word belonging to the CFL?
-
In the execution of the PDA for word "aab", which production is applied when the STACK contains "CCB" and the POP state is entered?
-
Why must the CFG be converted to Chomsky Normal Form before constructing the corresponding PDA?
-
In the second example, what two nonterminals are introduced to convert the original CFG into CNF, and what terminals do they represent?