Automata
Automata
QUESTION 1
(a) { 0n1n | n _ 0 }
(b) {aibjck | i = j or j = k }
(c) {aibjck | i 6= j or j 6= k }
(d) The set of strings of a’s and b’s with an equal number of a’s and b’s.
(e) The set of strings of a’s and b’s with more a’s than b’s.
(f) The set of even-length strings of a’s and b’s that are not of the form ww.
(g) The set of strings of a’s and b’s with twice as many a’s as b’s.
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 2
SOLUTION:
(a) { 0n1n | n ≥ 0 }
ANSWER:
We have
G, Σ = {0,1}
S→0S1
S→0
(b) {aibjck | i = j or j = k }
ANSWER:
L2 = {aibjck | j = k}.
Hence, we can create simple grammars for the separate languages and union them
to L.
Thus, for
{aibjck | i = j}
we have
S1 → aS2bS3|
S2 → aS2b|
S3 → cS3|
For
{aibjck | j = k}
we have
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 4
S4 → aS5bS6|
S5 → aS5b|
S6 → aS6b|
Thus,
{aibjck | i > j = i or j + k}
S0 → S1|S4
(c) {aibjck | i 6 = j or j 6 = k }
ANSWER
(d) The set of strings of a’s and b’s with an equal number of a’s and b’s.
ANSWER
Let Σ = {a, b} and let L = {w ∈ Σ* | w has the same number of a's and b's }
we have
V = {S, A, B, a, b}
Σ = {a, b}
R={
S→
S → SaSbS
S → SbSaS
A→a
B→b
By induction:
Thus, any string in L can be derived by this grammar. Consider the derivation of the
(e) The set of strings of a’s and b’s with more a’s than b’s.
ANSWER
The context-free grammar (CFGs) for L over the alphabet Σ = {a, b} with more a’s than
b’s.
S → TaT
T → T T | aT b | bTa | a | ε
The non-terminal T generates all strings with the same number of a’s as b’s, while S
forces an extra a.
(f) The set of even-length strings of a’s and b’s that are not of the form ww.
ANSWER
Let the terminal symbols A and B be used to generate all odd-length strings where the center
preceds k more characters. Similarly, B generates a string, say of length 2m + 1, where m a's and
b's precede one b preceds m more a's and b's. When concatenated, we get a string of length 2k +
characters away from the central b generated by B. That means that the first k + m + 1 characters
S → AB | BA | A | B
(g) The set of strings of a’s and b’s with twice as many a’s as b’s.
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 7
ANSWER
QUESTION 2
Complete the proof that we started in Example 12.14. Specifically, show that if, M is a PDA that
accepts by accepting state alone, then there exists a PDA M that accepts by accepting state and
SOLUTION
Given a PDA M that accepts by accepting state alone, then there exists a PDA M that accepts by
accepting state and empty stack, where L(M) = L(M). Thus, we extend the definition of the PDA
M to the PDA M = (K, , , , s, A) to accept if it lands in an accepting state and empty stack.
Hence, to show that this model is equivalent to ours, we must show two things: first, for each of
our machines, there exists an equivalent one of these, and, second, for each of these, there exists
an equivalent one of ours. The first requirement has been demonstrated in example 12.14. Here,
I. Initially, let P = P.
III. From every original accepting state in P, add an epsilon transition to A.
V. For every element g of , add the transition ((F, , g), (F, )) to P.
That is, if and only if P accepts, then proceed to A and clear the stack. This means that if and only
if P accepts by accepting state, P will also accept by accepting state and empty stack.
(Rich, 2007)
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 9
a) Given a regular expression and a PDA M, is the language accepted by M a subset of the
language generated by ?
b) Given a context-free grammar G and two strings s1 and s2, does G generate s1s2?
SOLUTION
a) Given a regular expression and a PDA M, is the language accepted by M a subset of
The only condition for this to be true is if L(M) L() = . Thus, we apply the algorithm is
as follows:
Build a new FSM M** from M*, such that L(M**) = L(M*).
Build a PDA M*** from M and M**, that accepts L(M) L(M**)
b) Given a context-free grammar G and two strings s1 and s2, does G generate s1s2?
Using the same notion used as the basis of the Pumping Theorem, we construct a decision
procedure as follows.
Then the length of the longest string that G can generate and assign a parse tree with no
duplicated nonterminals on any path is at most bn. Any string of length greater than bn, that can
be generated by G can be pumped. G generates at least three strings, since an infinite number of
strings are generated. Thus, there must be at least a shorter string due to pumping out from it, for
any long string generated. Where |vxy| bn+1, and since we can keep pumping out until we
obtain a shorter string, G must generate at least one string of length between bn and bn+1, if can
also generate any string of length greater than bn +bn+1. Thus, we apply the algorithm is as
follows::
Initialize count to 0.
For every string w in G* such that bn < |w| bn +bn+1 do:
Return False.
Build an FSM E that accepts all even length strings over the alphabet G.
Java, and Visual Basic for a total of six examples. Specify what languages they
define.
Example 1:
braces:
statement → compound-statement
where
or
block-item-list opt → ε
and
block-item-list → block-item
block-item → declaration
block-item → statement
Example 2:
<stmt> → id : <stmt>
Example 3: CFG for Pushdown Automaton in Java. String a a b b $ belongs to the language
Q = {p, q, r}
Σ = {a, b}
τ = {A, S}
s=p
z=S
F = {r}
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 15
δ:
aabb
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
Example 4:
# grammar.cfg
S -> NP (0.2)
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 16
N → heavy (0.2)
V → heave (0.8)
N → heave (0.2)
Example 5:
Visual Basic CFG for a program that generates context free grammar (sentence generation based
on simple grammar):
'AddRules.AddRange(AddHow)
'AddRules.AddRange(AddWhich)
'AddRules.AddRange(AddWhere)
'AddRules.AddRange(AddWhat)
'AddRules.AddRange(AddWhy)
'AddRules.AddRange(AddWhen)
'AddRules.AddRange(AddWho)
End Function
''' <summary>
''' Adds Toy grammar
''' </summary>
''' <returns></returns>
Private Function AddSentences() As List(Of Rule)
Dim NewRule As New Rule
AddSentences = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$S$"
NewRule.SentenceStr = New List(Of String) From {
"$NP$ $VP$ $PUNCT$",
"$NP$ $CONJ$ $VP$ $PUNCT$",
"$S$ $CONJ$ $S$",
"$NP$ $VAV$ $VP$ $PUNCT$",
"$NP$ $VP$, $PP$ $PUNCT$",
"$NP$ $PP$, $VP$ $PUNCT$",
"$VAV$ $NP$ $VP$ ?",
"$WHO$ $VAV$ $NP$ $VP$ ?",
"$WHAT$ $VAV$ $NP$ $VP$ ?",
"$WHY$ $VAV$ $NP$ $VP$ ?",
"$WHEN$ $VAV$ $NP$ $VP$ ?",
"$WHERE$ $VAV$ $NP$ $VP$ ?",
"$WHICH$ $VAV$ $NP$ $VP$ ?",
"$HOW$ $VAV$ $NP$ $VP$ ?"
}
AddSentences.Add(NewRule)
End Function
''' <summary>
''' Punctuation to be added (Toy) grammar
''' </summary>
''' <returns></returns>
Private Function AddPunctuation() As List(Of Rule)
Dim NewRule As New Rule
AddPunctuation = New List(Of Rule)
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 21
NewRule.SentenceType.PartOfSpeech = "$PUNCT$"
NewRule.SentenceStr = New List(Of String) From {
"."
}
AddPunctuation.Add(NewRule)
End Function
#End Region
#Region "Extra Components"
''' <summary>
''' Prepositional phrases to be added (Toy) grammar
''' </summary>
''' <returns></returns>
Private Function AddPrepositionalPhrases() As List(Of Rule)
Dim NewRule As New Rule
AddPrepositionalPhrases = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$PP$"
'"Prep": ["in", "on", "over", "against"],
NewRule.SentenceStr = New List(Of String) From {
"$PREP$ $NP$"
}
AddPrepositionalPhrases.Add(NewRule)
End Function
''' <summary>
''' Prepositions to be added (Toy) grammar
''' </summary>
''' <returns></returns>
Private Function AddPrepositions() As List(Of Rule)
Dim NewRule As New Rule
AddPrepositions = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$PREP$"
'"Prep": ["in", "on", "over", "against"],
NewRule.SentenceStr = New List(Of String)
AddPrepositions.Add(NewRule)
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 22
End Function
''' <summary>
''' CONJUNCTIONS
''' </summary>
''' <returns></returns>
Private Function AddConjunctions() As List(Of Rule)
Dim NewRule As New Rule
AddConjunctions = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$CONJ$"
NewRule.SentenceStr = New List(Of String)
AddConjunctions.Add(NewRule)
'Standards
AddConjunctions = AddRuleStr(AddConjunctions, "BUT", "$CONJ$")
AddConjunctions = AddRuleStr(AddConjunctions, "AND", "$CONJ$")
AddConjunctions = AddRuleStr(AddConjunctions, "OR", "$CONJ$")
End Function
''' <summary>
''' INTERJECTIONS
''' </summary>
''' <returns></returns>
Private Function AddInterjections() As List(Of Rule)
Dim NewRule As New Rule
AddInterjections = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$INTERJ$"
NewRule.SentenceStr = New List(Of String)
AddInterjections.Add(NewRule)
AddInterjections = AddRuleStr(AddInterjections, "WOW", "$INTERJ$")
End Function
#End Region
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 23
''' <summary>
''' Determiners to be added (Toy) grammar
''' </summary>
''' <returns></returns>
Private Function AddDeterminers() As List(Of Rule)
Dim NewRule As New Rule
AddDeterminers = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$DET$"
NewRule.SentenceStr = New List(Of String)
AddDeterminers.Add(NewRule)
'Default (Singular)
AddDeterminers = AddRuleStr(AddDeterminers, "THE", "$DET$")
AddDeterminers = AddRuleStr(AddDeterminers, "A", "$DET$")
End Function
AddNounPhrases.Add(NewRule)
End Function
''' <summary>
''' Adds Nouns to toy grammar
''' </summary>
''' <returns></returns>
Private Function AddProperNouns() As List(Of Rule)
Dim NewRule As New Rule
AddProperNouns = New List(Of Rule)
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 25
NewRule.SentenceType.PartOfSpeech = "$PN$"
NewRule.SentenceStr = New List(Of String) From {
"$N$"
}
AddProperNouns.Add(NewRule)
End Function
''' <summary>
''' Adds Nouns to toy grammar
''' </summary>
''' <returns></returns>
Private Function AddProperNoun_Name() As List(Of Rule)
Dim NewRule As New Rule
AddProperNoun_Name = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$PNN$"
NewRule.SentenceStr = New List(Of String) From {
"$N$"
}
AddProperNoun_Name.Add(NewRule)
End Function
''' <summary>
''' Adds Nouns to toy grammar
''' </summary>
''' <returns></returns>
Private Function AddProperNoun_Place() As List(Of Rule)
Dim NewRule As New Rule
AddProperNoun_Place = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$PNP$"
NewRule.SentenceStr = New List(Of String) From {
"$N$"
}
AddProperNoun_Place.Add(NewRule)
End Function
#End Region
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 26
#Region "VERBS"
''' <summary>
''' Verbs to be added (Toy) grammar
''' </summary>
''' <returns></returns>
Private Shared Function AddVerbs() As List(Of Rule)
Dim NewRule As New Rule
AddVerbs = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$V$"
NewRule.SentenceStr = New List(Of String)
AddVerbs.Add(NewRule)
End Function
''' <summary>
''' Verbs to be added (Toy) grammar
''' </summary>
''' <returns></returns>
Private Shared Function AddAuxVerbs() As List(Of Rule)
Dim NewRule As New Rule
AddAuxVerbs = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$VAV$"
NewRule.SentenceStr = New List(Of String)
AddAuxVerbs.Add(NewRule)
End Function
''' <summary>
''' verb phrases to be added (Toy) grammar
'''
''' </summary>
''' <returns></returns>
Private Function AddVerbPhrases() As List(Of Rule)
Dim NewRule As New Rule
AddVerbPhrases = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$VP$"
NewRule.SentenceStr = New List(Of String) From {
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 27
"$V$",
"$VTV$ $NP$",
"$VDTRANS$ $NP$",
"$V$ $ADV$",
"$PP$ $V$ ",
"$VP$ $CONJ$ $VP$"
}
AddVerbPhrases.Add(NewRule)
End Function
''' <summary>
''' INTRANSITIVE VERBS
''' </summary>
''' <returns></returns>
Private Function AddInTransitiveVerb() As List(Of Rule)
Dim NewRule As New Rule
AddInTransitiveVerb = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$VITV$"
NewRule.SentenceStr = New List(Of String) From {
"$V$"
}
AddInTransitiveVerb.Add(NewRule)
End Function
''' <summary>
''' ADDVERB VERBS
''' </summary>
''' <returns></returns>
Private Function AddAdVerb() As List(Of Rule)
Dim NewRule As New Rule
AddAdVerb = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$ADV$"
NewRule.SentenceStr = New List(Of String)
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 28
AddAdVerb.Add(NewRule)
End Function
''' <summary>
''' TRANSITIVE VERBS
''' </summary>
''' <returns></returns>
Private Function AddTransitiveVerb() As List(Of Rule)
Dim NewRule As New Rule
AddTransitiveVerb = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$VTV$"
NewRule.SentenceStr = New List(Of String) From {
"$V$"
}
AddTransitiveVerb.Add(NewRule)
End Function
''' <summary>
''' DITRANSITIVE VERB
''' </summary>
''' <returns></returns>
Private Function AddDitransitiveVerb() As List(Of Rule)
Dim NewRule As New Rule
AddDitransitiveVerb = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$VDTRANS$"
NewRule.SentenceStr = New List(Of String) From {
"$V$"
}
AddDitransitiveVerb.Add(NewRule)
End Function
''' <summary>
''' AddParticipleVerb
''' </summary>
''' <returns></returns>
Private Function AddParticipleVerb() As List(Of Rule)
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 29
#End Region
#End Region
#Region "Questions"
'where (place), when (time), why (reason), who (person).
'auxiliary verb + subject + main
'S → Wh-NP VP
Private Function AddWhat() As List(Of Rule)
Dim NewRule As New Rule
AddWhat = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$WHAT$"
NewRule.SentenceStr = New List(Of String) From {
"What"
}
AddWhat.Add(NewRule)
End Function
Private Function AddWhy() As List(Of Rule)
Dim NewRule As New Rule
AddWhy = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$WHY$"
NewRule.SentenceStr = New List(Of String) From {
"Why"
}
AddWhy.Add(NewRule)
End Function
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 30
}
AddHow.Add(NewRule)
End Function
Private Shared Function AddWhich() As List(Of Rule)
Dim NewRule As New Rule
AddWhich = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$WHICH$"
NewRule.SentenceStr = New List(Of String) From {
"Which"
}
AddWhich.Add(NewRule)
End Function
#End Region
#End Region
#Region "MAIN ALGORITHYM"
''' <summary>
''' Gets Random item from list
''' </summary>
''' <param name="Item"></param>
''' <returns></returns>
Public Shared Function GetRandomfromListStr(ByVal Item As List(Of String)) As String
' Initialize the random-number generator.
Randomize()
' Generate random value between 1 and 6.
If Item.Count > 0 Then
Randomize()
Dim value As Integer = CInt(Int((Item.Count * Rnd())))
Return Item(value)
Else
Return ""
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 32
End If
End Function
''' <summary>
''' Expands grammar to Words
''' </summary>
''' <param name="StartStr">Sentence containing gramar to be expanded</param>
''' <param name="Expansion">Expanded Sentence Grammar</param>
''' <returns>Expanded Sentence</returns>
Public Function Expand(ByRef StartStr As String, ByRef Expansion As String) As String
For i = 0 To StandardGrammar.Count - 1
Dim iRule = StandardGrammar(i)
Console.WriteLine(Expansion)
Else
Expansion = StartStr
End If
Next
Return Expansion
End Function
''' <summary>
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 33
Console.WriteLine(Expansion)
Else
Expansion = StartStr
End If
Next
Return Expansion
End Function
#End Region
#Region "FUNCTIONS"
''' <summary>
''' Adds The basic Grammar to given rules
''' </summary>
''' <returns></returns>
Public Function AddRules(ByRef Rules As List(Of Rule)) As List(Of Rule)
AddRules.AddRange(AddParticipleVerb)
AddRules.AddRange(AddProperNouns)
AddRules.AddRange(AddProperNoun_Name)
AddRules.AddRange(AddProperNoun_Place)
Return Rules
End Function
''' <summary>
''' Generates Output from Sentence Grammar String with custome grammar
''' </summary>
''' <param name="CFG">Grammar / Ruleset</param>
''' <returns>Expanded Sentence</returns>
Public Function GenerateRandomSentencefromGramar(ByRef CFG As List(Of Rule)) As
String
Randomize()
Return Expand(CFG, "$S$ ", "")
End Function
''' <summary>
''' generates sentence with Internal Grammar CFG_RULES
''' </summary>
''' <returns></returns>
Public Function GenerateRandomSentence() As String
Randomize()
Return Expand(StandardGrammar, "$S$ ", "")
End Function
''' <summary>
''' Adds a string to a existing rule; Rules must not be duplicated, all rules must have at least 1
string
''' </summary>
''' <param name="Rules">ruleset / grammar</param>
''' <param name="Str">string to be added / Word / Phrase</param>
''' <param name="PartOfSpeech">Part of Speech to be added to (in custom rulesets this can be
a userdefined PartofSpeech)</param>
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 36
References