An Entertaining Example of Using the Concepts of Context-Free Grammar and Pushdown Automation

A formal-linguistic approach for solving an entertaining task is made in this paper. The well-known task of the Hanoi towers is discussed in relation to some concepts of discrete mathematics. A context-free grammar which generate an algorithm for solving this task is described. A deterministic pushdown automation which in its work imitates the work of monks in solving the task of the Hanoi towers is built.


Introduction
Task 1 (The Task of the Hanoi Towers [2]) The Hanoi Towers are made up of three vertical columns. On the first is hung a series of N discs. The discs are all different, but ordered by size with the largest being on the bottom and the smallest on top. The task is to move the discs from the first to the third column, using the second column as an assistant. There are several conditions to completing this exercise: only one disc may be moved at one time and while one disc is being moved, all other discs must be on one of the columns and also during this time it is prohibited a larger disc to be on smaller.
The task of the Hanoi Towers is a classic example used to teach recursion in programming [10,2]. In this paper, we will look at this Task from the standpoint of mathematical linguistics, i.e. as a part of the discipline of "discrete mathematics" and "discrete structures" studies by students in Informatics and Computer courses at university [3,4,8,9].
The algebraic properties of context-free grammars and languages are discussed in detail in [5,7]. Several practical applications of formal grammars and languages and pushdown automata are discussed in detail in [1,6].

Context-free grammars and languages
Let V be a finite and non-empty set. The elements of this set we will call letters, and the whole set V -Alphabet.
A word over the alphabet V we will call each finite string of letters by V . The word that does not contain any letter is called the empty word that we will mark with ε. V * denotes the set of all words over V , including empty set. Bythe length of a word refers to the number of letters in it. The length of the word α will be expressed with |α|.
Let α and β be two words over the Alphabet V . By concatenation (multiplication, adhesion) αβ of both words we will mean the word obtained by successive completion of the letters of β after the last letter of α.
Let V be alphabet. Each subset L of V * is called formal language (or only language) over alphabet V .
By generative grammar (or only grammar) Γ we will understand the four ordered tuples Γ = V, W, S, P , where V is a finite set (Alphabet) from terminal symbols, W -set of non -terminal symbols, S-start symbol of the grammar, which is an element of W , and P is set of ordered pairs (α, β), where α, β ∈ (V ∪ W ) * , as in α there is at least one non -terminal symbol. In a number of sources (see references in the end) and an additional condition is placed while sets W and P to be finite. For our needs this condition is not necessary, it is enough that these sets are countable. The elements of P are called productions. If (α, β) ∈ P , then it means α → β, as the symbol " → " does not belong to V ∪ W .
If ω 0 , ω 1 , . . . , ω n is a word over V ∪ W , for which ω 0 ⊢ ω n , we will say that number of words is the derivation of ω n from ω 1 in Γ which we denote with ω 0 Γ |= ω n or only ω 0 |= ω n , if Γ is default. The count n of immediate derivations ω i Γ ⊢ ω i+1 will be called the length of derivation.
where V and W are alphabets respectively with terminal and nonterminal symbols.
Task 2 For a given positive integer N a context-free grammar Γ N should be built with terminal alphabet encoding possible displacements and if ω ∈ L(Γ N ), then ω describes algorithm that solve the Task 1. Prove that for each positive integer N language L(Γ N ) is not empty, i.e for each positive integer N there is an algorithm solves the task of the Hanoi towers. 1 .
The meaning of p ij is "Move top disk from the i − th column of the j − th column". In this way, if ω = π 1 π 2 . . . π k , where π i ∈ V, i = 1, 2, . . . , k, then ω describes algorithm for moving consecutively of k discs in the three columns.
Let ω ∈ L(Γ N ), i.e. we assume that the derivation h 13 (N ) |= ω 2 with lengths less then s, where ω 1 , ω 2 ∈ V * and ω = ω 1 p 13 ω 2 . According to induction assumption ω 1 describes algorithm for moving of N − 1 discs from first to second column, using third one as assistant, and ω 2 describes algorithm for moving of N − 1 discs from second to third column, using first one as assistant and pay attention to constraints from Task 1. Then ω = ω 1 p 13 ω 2 describes the following algorithm: first move while considering with constraints from Task 1 the top number t of discs from first column to second then move the largest bottom disk from the first column of the empty third and finally move t number of discs from second column to third one. Therefore ω = ω 1 p 13 ω 2 (if existing ) describe the solution of the task of the Hanoi towers.
Let prove for each positive integer N language L(Γ N ) is not empty. When N = 1 the only production of P which can be applied is h 13 → p 13 and therefore L(Γ 1 ) = {p 13 }, i.e. L(Γ 1 ) is not empty language. Let's assume that for each positive integer t ≤ N the languages L(Γ t ) are not empty and put N = t + 1. Let consider the context-free grammar Γ ′ t = V, W, h 12 (t), P and Γ ′′ t = V, W, h 23 (t), P . Apparently Γ ′ t and Γ ′′ t work by analogy of Γ t and according to the above proven if ω ′ ∈ L(Γ ′ t ), then ω ′ describes algorithm for moving of t discs from first to second column, using third one as assistant according to constraints described in Task 1, and if ω ′′ ∈ L(Γ ′′ t ), then ω ′′ describes the algorithm for moving of t discs from second column to third one using first one as assistant. According to induction assumption ω ′ and ω ′′ exist. Then in Γ t+1 derivation exist h 13 (t + 1) Therefore ω ∈ L(Γ t+1 ), i.e. L(Γ t+1 ) is non empty language.
When N = 5 next word is produced p 13 p 12 p 32 p 13 p 21 p 23 p 13 p 12 p 32 p 31 p 21 p 32 p 13 p 12 p 32 p 13 p 21 p 23 p 13 p 21 p 32 p 31 p 21 p 23 p 13 p 12 p 32 p 13 p 21 p 23 p 13 We can verify the correctness of the algorithm using the example about the five consecutive cards.
It is easy to prove (eg. using induction on N ) following In other words, for each a positive integer N grammar Γ N generates exactly one word that describes an algorithm for solving the task of the Hanoi towers with exactly 2 N − 1 displacements of the disks from one column to another.

Pushdown automata
By nondeterministic pushdown automation one will understand each ordered septenary M = K, V, W, δ, q 0 , z 0 , F , where: -K is a finite set of states of automaton (states of alphabet); -V is a finite set of entry letters (entry alphabet); -W is a finite, non empty set of stack symbols (stack alphabet); δ : K × (V ∪ {ε}) × W → P(K × W * ) 2 is a transition function; q 0 ∈ K is a start state of automaton; z 0 ∈ W is a start stack symbol; -F ⊆ K is a set of accepting states.
2 As usual with P(A) is denoted the set of all subsets of the set A, including the empty.
Immediately after inclusion of M N , before being submitted as any input signal, the automation replaces the start stack symbol z 0 with word h 12 (N − 1)p 13 h 23 (N −1) according to (1) and after a number of actions depending on the current stack symbol (2), (3), or (4). Moreover, we assume that automation M N is designed so that after reading of stack symbol of the type p ij i, j ∈ {1, 2, 3}, i = j, simultaneously according the action(4) another action carried out namely the removal of top disk i-th column on j-th. Transient function is defined so that after a finite number of beats the stack is empty and stops. This is because if the current stack symbol of the kind p ij , then M N deletes it, and if the current stack symbol is of the type h ij (n), then at the next beat of the parameter n decrease with one unit, if n > 1, or h ij (n) passes into the symbol p ij at n = 1, then that symbol is deleted.
We will prove that 1 ≤ s < N the stack M N of configuration q 0 , ε, h ij (s)γ where i, j ∈ {1, 2, 3}, i = j, γ ∈ W * as a result of their work reaches a configuration q 0 , ε, γ and when that transfer according to the restrictions of Task 1 s discs from column i to column j. When s = 1 we have q 0 , ε, h ij (1)γ ⊢ q 0 , ε, p ij γ ⊢ q 0 , ε, γ , i.e. the assertion is met. Assume that the assertion is fulfilled for any t, such that 1 ≤ t < s < N and let t = s. Then in i, j, k ∈ {1, 2, 3}, i = j, j = k, k = i we have q 0 , ε, h ij (t)γ ⊢ q 0 , ε, h ik (t − 1)p ij h kj (t − 1)γ . According to induction assumption M N reaches the configuration q 0 , ε, p ij h kj γ moving t − 1 discs from i-th column of k-th column then passed in a configuration q 0 , ε, h kj (t − 1)γ moving next disc from column i to column j and again according to the induction moves t − 1 discs (as obviously all are smaller size) on this disk column j taken from column k. Therefore the assertion is true for any s = 1, 2, . . . , N − 1.
According the assertion that has just proved we have: q 0 , ε, z 0 ⊢ q 0 , ε, h 12 (N − 1)p 13 h 23 (N − 1) ⊢ . . . ⊢ q 0 , ε, p 13 h 23 (N − 1) ⊢ q 0 , ε, h 23 (N − 1) ⊢ . . . ⊢ q 0 , ε, ε , while the stack moves at the upper N − 1 discs from first to second column, then move biggest on the bottom from the first to third column and finally moving discs (N −1 numbers from the second to third column while observed the restrictions described in the Task 1. Therefore pushdown automation M N solve the task of the Hanoi towers.