0% found this document useful (0 votes)
86 views37 pages

Automata

The document provides context-free grammars (CFGs) for various languages over alphabets containing 'a' and 'b'. It designs CFGs for languages such as: strings with equal numbers of 'a's and 'b's, strings with more 'a's than 'b's, strings of even length not of the form 'ww', and strings with twice as many 'a's as 'b's. It also completes exercises on proving a PDA accepts by final state and empty stack if it accepts by final state alone, and gives decision procedures to determine properties of languages generated by regular expressions, context-free grammars and regular grammars.

Uploaded by

mdalstavolch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views37 pages

Automata

The document provides context-free grammars (CFGs) for various languages over alphabets containing 'a' and 'b'. It designs CFGs for languages such as: strings with equal numbers of 'a's and 'b's, strings with more 'a's than 'b's, strings of even length not of the form 'ww', and strings with twice as many 'a's as 'b's. It also completes exercises on proving a PDA accepts by final state and empty stack if it accepts by final state alone, and gives decision procedures to determine properties of languages generated by regular expressions, context-free grammars and regular grammars.

Uploaded by

mdalstavolch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Context-Free Languages and Pushdown Automata

1. Design context-free grammars (CFGs) for the following languages.

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:

For L(G) = { ω∊Σ* | S ⇒+ω}

Let the tuple G = (V, , R, S) be a context-free grammar that generates L, where:

V is a finite set of variables

Σ is a finite set of terminals (disjoint from V)

R is set of production rules of the form A → W, where A∈V and W∈(V∪Σ)*

S ∈ V is the start variable.

Two substitution rules (productions) → Variables = {S}, Terminals = {0,1}

We have

G, Σ = {0,1}

S→0S1

S→0

Basis: the language contains 01.

Induction: if w is in the language, then 0w1 is also in the language.

(b) {aibjck | i = j or j = k }

ANSWER:

L(G) = { ω∊Σ* | S ⇒+ω}


CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 3

Let the tuple G = (V, , R, S) be a context-free grammar that generates L, where:

V is a finite set of variables

Σ is a finite set of terminals (disjoint from V)

R is set of production rules of the form A → W, where A∈V and W∈(V∪Σ)*

S ∈ V is the start variable.

This language L is simply the union of L1 and L2, where

L1 = {aibjck | i = j}, and

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

L(G) = { ω∊Σ* | S ⇒+ω}

Let G = (V, , R, S) be a context-free grammar that generates L. With set of

variables V, where S is the start variable;

(d) The set of strings of a’s and b’s with an equal number of a’s and b’s.

ANSWER

Let the tuple G = (V, , R, S) be a context-free grammar that generates L, where:

V is a finite set of variables

Σ is a finite set of terminals (disjoint from V)

R is set of production rules of the form A → W, where A∈V and W∈(V∪Σ)*

S ∈ V is the start variable.


CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 5

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:

If S ⇒* α, then a(α) = b(α); so L(G) ⊆ L.

Thus, any string in L can be derived by this grammar. Consider the derivation of the

string aababb: S ⇒ aSbS ⇒ aSb ⇒ aaSbSb ⇒ aabSb ⇒ aabaSbSb ⇒2 aababb.

(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.

For L = {w: w has more a’s than b’s}.


CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 6

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

Since no strings of odd length can be of the form ww,

Let the terminal symbols A and B be used to generate all odd-length strings where the center

characters are a and b, respectively.

A builds an odd-length string, say of length 2k + 1, where k characters precede an a which

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 +

2m + 2: an even length string-where the middle a of the portion generated by A is k + m + 1

characters away from the central b generated by B. That means that the first k + m + 1 characters

cannot be fully replicated in the final k + m + 1 characters.

S → AB | BA | A | B

A → aAa | aAb | bAa | bAb | a

B → aBa | aBb | bBa | bBb | 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

L = {w: w has twice as many a’s as b’s}.

S → SS | aaSb | bSaa | aSbSa| ε

(Cain, n.d.; Collins, n.d.; Rich, 2007; Stanford, n.d.)

2. Complete Exercise #6 at the end of chapter 12 of the text.

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

empty stack (our definition) where L(M) = L(M).

SOLUTION

PDA M that Accepts by Final State and Empty Stack


CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 8

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,

we focus on the second requirement.

Thus, the construction proceeds as follows by building a new PDA P from P.

I. Initially, let P = P.

II. Add a new accepting state A to P.

III. From every original accepting state in P, add an epsilon transition to A.

IV. Make A the only accepting state in P.

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

3. Complete Exercise #1 at the end of chapter 14 of the text.


QUESTION 2
Give a decision procedure to answer each of the following questions:

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?

c) Given a context-free grammar G, does G generate at least three strings?

d) Given a context-free grammar G, does G generate any even length strings?


CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 10

e) Given a regular grammar G, is L(G) context-free?

SOLUTION
a) Given a regular expression  and a PDA M, is the language accepted by M a subset of

the language generated by ?

The only condition for this to be true is if L(M)  L() = . Thus, we apply the algorithm is

as follows:

Build an FSM M* from , such that L(M*) = L().

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**)

If L(M***) is empty, return True

Else return False.

b) Given a context-free grammar G and two strings s1 and s2, does G generate s1s2?

We follow the procedure as follows:

Convert G to Chomsky Normal Form.

Check all derivations in G of length up to 2|s1s2|.

If any of the derivations generates s1s2, return True

Else return False.

c) Given a context-free grammar G, does G generate at least three strings?

Using the same notion used as the basis of the Pumping Theorem, we construct a decision

procedure as follows.

Let b be the branching factor of G

Let n be the number of nonterminals of G.


CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 11

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::

Determine b and n by examining G.

Initialize count to 0.

For every string w in G* such that |w|  bn do:

If decideCFL(G, w) then count = count + 1.

If count = 0 then return False.

If count  3 then return True.

For every string w in G* such that bn < |w|  bn +bn+1 do:

If decide CFL(G, w) then return True.

Return False.

d) Given a context-free grammar G, does G generate any even length strings?

Build a PDA P that accepts L(G), by using CFGtoPDAtopdown(G).

Build an FSM E that accepts all even length strings over the alphabet G.

Build a PDA P* that accepts L(G)  L(E), by using intersectPDAandFSM(P, E).

If decideCFLempty(P*) returns True then return False.


CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 12

Else, return True.

e) Given a regular grammar G, is L(G) context-free?

L(G) is context-free given that every regular language is context-free, thus:


Return True.

(Rich, 2007; Nakayama, 2006; Schulz, 2018)

The essay topic is:

Find two examples of context-free grammars in each of the following programs: C,

Java, and Visual Basic for a total of six examples. Specify what languages they

define.

Example 1:

Context-Free Grammar and Parsing. While loop in C language:


CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 13

iteration-statement → while (expression) statement statement, in turn, is often a list enclosed in

braces:

statement → compound-statement

compound-statement → { block-item-list opt }

where

block-item-list opt → block-item-list

or

block-item-list opt → ε

and

block-item-list → block-item

block-item-list → block-item-list block-item

block-item → declaration

block-item → statement

Example 2:

A Context-Free Grammar which defines the syntax of C language compound statements:

<compound stmt> → { <stmt list> }

<stmt list> → <stmt> <stmt list> | epsilon

<stmt> → <compound stmt>


CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 14

<stmt> → id : <stmt>

<stmt> → if ( <expr> ) <stmt>

<stmt> → if ( <expr> ) <stmt> else <stmt>

<stmt> → while ( <expr> ) <stmt>

<stmt> → do <stmt> while ( <expr> ) ;

<stmt> → for ( <stmt> <expr> ; <expr> ) <stmt>

<stmt> → switch ( <expr> ) <compound stmt>

<stmt> → case <expr> : <stmt> | default: <stmt>

<stmt> → break ; | continue ; | ;

<stmt> → return <expr> ; | goto <id> ;

Example 3: CFG for Pushdown Automaton in Java. String a a b b $ belongs to the language

generated by the automaton.

Q = {p, q, r}

Σ = {a, b}

τ = {A, S}

s=p

z=S

F = {r}
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 15

δ:

(p, a, S) → (p, [A])

(p, b, A) → (q, [ε])

(p, a, A) → (p, [A, A])

(q, b, A) → (q, [ε])

Enter the tape input >

aabb

-----------------------------------------------------------------------------

| used transition | state | word (ω) | stack | transitions

-----------------------------------------------------------------------------

|- |p |aabb$ | [S] | (p, a, S) → (p, [A])

| (p, a, S) → (p, [A]) |p |abb$ | [A] | (p, a, A) → (p, [A, A])

| (p, a, A) → (p, [A, A]) | p |bb$ | [A, A] | (p, b, A) → (q, [ε])

| (p, b, A) → (q, [ε]) |q |b$ | [A] | (q, b, A) → (q, [ε])

| (p, a, S) → (p, [A]) |q |$ | [] |ω∈L

Example 4:

# grammar.cfg

S -> NP VP (0.8) # specify probability between 0 and 1 by appending between parentheses

S -> NP (0.2)
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 16

NP -> Det N # probability defaults to 1.0

NP -> Det Nom

Nom -> Adj N

VP → V # Use '->' or '→'

Det → the # probability defaults to 1.0

N → heavy (0.2)

Adj → heavy (0.8)

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):

'"S" ["NP VP", "Interj NP VP"],


'"NP": ["Det N", "Det N that VP", "Det Adj N", "Det N PP"],
'"PP": ["Prep NP"],
'"Prep": ["in", "on", "over", "against"],
'"VP": ["Vtrans NP", "Vintr"],
'"Interj": ["oh,", "my,", "wow,", "damn,"],
'"Det": ["this", "that", "the"],
'"N": ["amoeba", "dichotomy", "seagull", "trombone", "corsage", "restaurant", "suburb"],
'"Adj": ["bald", "smug", "important", "tame", "overstaffed", "luxurious", "blue"],
'"Vtrans": ["computes", "examines", "foregrounds", "prefers", "interprets", "spins"],
'"Vintr": ["coughs", "daydreams", "whines", "slobbers", "vocalizes", "sneezes"]
'"Vdintr": ["GAVE",]
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 17

Public Class ContextFreeGrammar


''' <summary>
''' Rule set / Grammar
''' </summary>
Public Structure Rule
''' <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>
''' <returns>Populated ruleset</returns>
Public Shared Function AddRuleStr(ByRef Rules As List(Of Rule), ByRef Str As String,
ByRef PartOfSpeech As String) As List(Of Rule)
For Each item In Rules
If item.SentenceType.PartOfSpeech = PartOfSpeech = True Then
item.SentenceStr.Add(Str)
Else
End If
Next
Return Rules
End Function
''' <summary>
''' Gramar parameter
''' </summary>
Public Structure SentenceComponent
''' <summary>
''' Grammar rule
''' </summary>
Public PartOfSpeech As String
End Structure
''' <summary>
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 18

''' Grammar Rule parameter


''' </summary>
Public SentenceType As SentenceComponent
''' <summary>
''' List of associated Words
''' </summary>
Public SentenceStr As List(Of String)
End Structure

#Region "iNTERNAL GRAMMAR"


''' <summary>
''' Standard grammar
''' </summary>
Private BASIC_GRAMMAR As List(Of Rule) = AddRules()
''' <summary>
''' USED AS A STANDARD TEMPLATE FOR CREATING GRAMAR-SET ;
''' THIS IS A UNPOPULATED SET, EACH TERM REQUIRES AT LEAST 2 WORD
STRINGS
''' </summary>
''' <returns></returns>
Public ReadOnly Property StandardGrammar As List(Of Rule)
Get
Return BASIC_GRAMMAR
End Get
End Property
#End Region

#Region "Internal Grammar"


''' <summary>
''' Adds Internal Grammar
''' </summary>
''' <returns></returns>
Private Function AddRules() As List(Of Rule)
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 19

AddRules = New List(Of Rule)


AddRules.AddRange(AddVerbs)
AddRules.AddRange(AddNouns)
'AddRules.AddRange(AddDitransitiveVerb)
'AddRules.AddRange(AddTransitiveVerb)
'AddRules.AddRange(AddInTransitiveVerb)
'AddRules.AddRange(AddConjunctions)
'AddRules.AddRange(AddInterjections)
'AddRules.AddRange(AddAuxVerbs)
AddRules.AddRange(AddSentences)
AddRules.AddRange(AddPunctuation)
'AddRules.AddRange(AddAdjectives)
AddRules.AddRange(AddDeterminers)
'AddRules.AddRange(AddNounPhrases)
AddRules.AddRange(AddPrepositionalPhrases)
'AddRules.AddRange(AddPrepositions)
AddRules.AddRange(AddVerbPhrases)
'AddRules.AddRange(AddAdVerb)
'AddRules.AddRange(AddParticipleVerb)
'AddRules.AddRange(AddProperNouns)
'AddRules.AddRange(AddProperNoun_Name)
'AddRules.AddRange(AddProperNoun_Place)

'AddRules.AddRange(AddHow)
'AddRules.AddRange(AddWhich)
'AddRules.AddRange(AddWhere)
'AddRules.AddRange(AddWhat)
'AddRules.AddRange(AddWhy)
'AddRules.AddRange(AddWhen)
'AddRules.AddRange(AddWho)
End Function

#Region "Sentence components"


CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 20

''' <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

#Region "Prime Grammar components"


''' <summary>
''' Adjectives to be added (Toy) grammar
''' </summary>
''' <returns></returns>
Private Function AddAdjectives() As List(Of Rule)
Dim NewRule As New Rule
AddAdjectives = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$ADJ$"
NewRule.SentenceStr = New List(Of String)
AddAdjectives.Add(NewRule)
End Function
#Region "NOUNS"
''' <summary>
''' Adds Nouns to toy grammar
''' </summary>
''' <returns></returns>
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 24

Private Function AddNouns() As List(Of Rule)


Dim NewRule As New Rule
AddNouns = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$N$"
NewRule.SentenceStr = New List(Of String)
AddNouns.Add(NewRule)
End Function
''' <summary>
''' NounPhrase to be added (Toy) grammar
''' </summary>
''' <returns></returns>
Private Function AddNounPhrases() As List(Of Rule)
Dim NewRule As New Rule
AddNounPhrases = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$NP$"
NewRule.SentenceStr = New List(Of String) From {
"$DET$ $N$",
"$DET$ $PN$",
"$PNN$",
"$DET$ $PNP$",
"$DET$ $ADJ$ $N$",
"$N$ $CONJ$ $N$,"
}

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

Dim NewRule As New Rule


AddParticipleVerb = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$VPV$"
NewRule.SentenceStr = New List(Of String) From {
"$V$"
}
AddParticipleVerb.Add(NewRule)
End Function

#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

Private Function AddWhen() As List(Of Rule)


Dim NewRule As New Rule
AddWhen = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$WHEN$"
NewRule.SentenceStr = New List(Of String) From {
"When"
}
AddWhen.Add(NewRule)
End Function
Private Shared Function AddWho() As List(Of Rule)
Dim NewRule As New Rule
AddWho = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$WHO$"
NewRule.SentenceStr = New List(Of String) From {
"Who"
}
AddWho.Add(NewRule)
End Function
Private Shared Function AddWhere() As List(Of Rule)
Dim NewRule As New Rule
AddWhere = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$WHERE$"
NewRule.SentenceStr = New List(Of String) From {
"Where"
}
AddWhere.Add(NewRule)
End Function
Private Shared Function AddHow() As List(Of Rule)
Dim NewRule As New Rule
AddHow = New List(Of Rule)
NewRule.SentenceType.PartOfSpeech = "$HOW$"
NewRule.SentenceStr = New List(Of String) From {
"How"
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 31

}
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)

If StartStr.Contains(iRule.SentenceType.PartOfSpeech) = True Then


'If StartStr = iRule.SentenceType.PartOfSpeech = True Then
Randomize()
Dim PickStr As String = GetRandomfromListStr(iRule.SentenceStr)

StartStr = StartStr.Replace(iRule.SentenceType.PartOfSpeech, PickStr)

For Each item In StartStr.Split(" ")


Expand(StartStr, Expansion)
Next

Console.WriteLine(Expansion)
Else
Expansion = StartStr
End If
Next
Return Expansion
End Function
''' <summary>
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 33

''' Uses Custom Grammar


''' </summary>
''' <param name="CustomRules">Custome Ruleset</param>
''' <param name="StartStr">Sentence Indicator Rule</param>
''' <param name="Expansion">Final String</param>
''' <returns></returns>
Public Function Expand(ByRef CustomRules As List(Of Rule), ByRef StartStr As String,
ByRef Expansion As String) As String
For i = 0 To CustomRules.Count - 1
Dim iRule = CustomRules(i)

If StartStr.Contains(iRule.SentenceType.PartOfSpeech) = True Then


'If StartStr = iRule.SentenceType.PartOfSpeech = True Then
Randomize()
Dim PickStr As String = GetRandomfromListStr(iRule)
StartStr = StartStr.Replace(iRule.SentenceType.PartOfSpeech, PickStr)

For Each item In StartStr.Split(" ")


Expand(StartStr, Expansion)
Next

Console.WriteLine(Expansion)
Else
Expansion = StartStr
End If
Next
Return Expansion
End Function

Private Shared Function GetRandomfromListStr(iRule As Rule) As String


Return GetRandomfromListStr(iRule.SentenceStr)
End Function
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 34

#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 = New List(Of Rule)


AddRules.AddRange(AddHow)
AddRules.AddRange(AddWhich)
AddRules.AddRange(AddWhere)
AddRules.AddRange(AddWhat)
AddRules.AddRange(AddWhy)
AddRules.AddRange(AddWhen)
AddRules.AddRange(AddWho)
AddRules.AddRange(AddVerbs)
AddRules.AddRange(AddNouns)
AddRules.AddRange(AddDitransitiveVerb)
AddRules.AddRange(AddTransitiveVerb)
AddRules.AddRange(AddInTransitiveVerb)
AddRules.AddRange(AddConjunctions)
AddRules.AddRange(AddInterjections)
AddRules.AddRange(AddAuxVerbs)
AddRules.AddRange(AddSentences)
AddRules.AddRange(AddPunctuation)
AddRules.AddRange(AddAdjectives)
AddRules.AddRange(AddDeterminers)
AddRules.AddRange(AddNounPhrases)
AddRules.AddRange(AddPrepositionalPhrases)
AddRules.AddRange(AddPrepositions)
AddRules.AddRange(AddVerbPhrases)
AddRules.AddRange(AddAdVerb)
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 35

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

''' <returns>Populated ruleset</returns>


Public Shared Function AddRuleStr(ByRef Rules As List(Of Rule), ByRef Str As String,
ByRef PartOfSpeech As String) As List(Of Rule)
For Each item In Rules
If item.SentenceType.PartOfSpeech = PartOfSpeech = True Then
item.SentenceStr.Add(Str)
Else
End If
Next
Return Rules
End Function
#End Region
End Class
(Abrante, 2018; Digitalheir, 2017; Franchitti, 2009; rochester, 2001; Spydaz, 2019)

References

Abrante, C. (2018). PushdownAutomaton: Implementation of a pushdown automaton (PDA) in


Java for learning purposes. Retrieved June 15, 2020, from
https://github.com/CristianAbrante/PushdownAutomaton
Cain, J. (n.d.). CFG. Retrieved June 16, 2020, from https://www.unf.edu/~wkloster/3210/511c.txt
Collins, M. (n.d.). Context Free Grammars Context Free Languages (CFL) •.
CONTEXT-FREE LANGUAGES AND PUSHDOWN AUTOMATA 37

Digitalheir. (2017). java-probabilistic-earley-parser: 🎲 Efficient Java implementation of the


probabilistic Earley algorithm to parse Stochastic Context Free Grammars (SCFGs).
Retrieved June 15, 2020, from https://github.com/digitalheir/java-probabilistic-earley-parser
Franchitti, J.-C. (2009). Adapted from course textbook resources Programming Language
Pragmatics.
Nakayama, M. (2006). Foundations of Computer Science II. In Theory of Computation.
https://doi.org/10.1007/1-84628-477-5_69
Rich, E. (2007). Automata, Computability and Complexity: Theory and Applications. Retrieved
from http://www.cs.utexas.edu/~ear/
rochester. (2001). Grammars. Retrieved June 16, 2020, from
https://www.cs.rochester.edu/u/scott/courses/173/notes/05_grammars
Schulz, S. (2018). CSC519 Programming Languages. In Department of Computer Science
University of Miami, Lecture Notes. Retrieved from
http://www.cs.miami.edu/home/schulz/CSC519.html
Spydaz. (2019). TestCFG. Retrieved June 16, 2020, from
https://github.com/spydaz/TestCFG/commit/ca95a6a3e41f1e74ffd82b4eb452f14cc4903dbb
Stanford. (n.d.). Context-Free Grammars. Retrieved from
http://infolab.stanford.edu/~ullman/ialc/spr10/slides/cfl1.pdf

You might also like