blob: f89a7740fb73137ed727371ef4eac7687ddb6521 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
#+TITLE: Assignment Five
#+AUTHOR: Lizzy Hunt
#+STARTUP: entitiespretty fold inlineimages
#+LATEX_HEADER: \notindent \notga \usepackage{ dsfont } \usepackage[utf8]{inputenc} \usepackage{amsmath} \usepackage{fontspec} \usepackage[a4paper,margin=1in,portrait]{geometry} \usepackage{fontspec} \setmonofont{DejaVu Sans Mono}
#+LATEX: \setlength\parindent{0pt}
#+LATEX_COMPILER: lualatex
#+OPTIONS: toc:nil
* Question One
\begin{verbatim}
N -> N a N b N
-> ε a N b N (using N -> ε)
-> ε a N a N b N b N (using N -> N a N b N)
-> ε a N a N b N a N b N b N (using N -> N a N b N)
-> ε a ε a N b N a N b N b N (using N -> ε)
-> ε a ε a ε b N a N b N b N (using N -> ε)
-> ε a ε a ε b ε a N b N b N (using N -> ε)
-> ε a ε a ε b ε a ε b N b N (using N -> ε)
-> ε a ε a ε b ε a ε b ε b N (using N -> ε)
-> ε a ε a ε b ε a ε b ε b ε (using N -> ε)
-> aababb (remove epsilons for clarity)
\end{verbatim}
#+attr_latex: :width 225px
[[./1.excalidraw.png]]
* Question Two
\begin{verbatim}
N -> N N
-> N * N (using N -> N *)
-> (N) * N (using N -> (N))
-> (N + N) * N (using N -> N + N)
-> (a + N) * N (using N -> a)
-> (a + a) * N (using N -> a)
-> (a + a) * a (using N -> a)
\end{verbatim}
* Question Three
\begin{verbatim}
N -> b N a N
-> b ε a N (using N -> ε)
-> b ε a a N b N (using N -> a N b N)
-> b ε a a a N b N b N (using N -> a N b N)
-> b ε a a a ε b N b N (using N -> ε)
-> b ε a a a ε b ε b N (using N -> ε)
-> b ε a a a ε b ε b ε (using N -> ε)
\end{verbatim}
* Question Four
\begin{verbatim}
N -> aNbN
N -> bNaN
N -> ε
\end{verbatim}
* Question Five
$count(a) \neq count(b) \Rightarrow count(a) > count(b) \vee count(a) < count(b)$
+ More A's than B's = G
+ Less A's than B's = L
\begin{verbatim}
S -> G
S -> L
G -> GG
G -> EAE
L -> LL
L -> EBE
E -> aEaE
E -> bEaE
E -> ε
A -> aA
A -> a
B -> bB
B -> b
\end{verbatim}
* Question Six
This one we can actually create from a DFA!
#+attr_latex: :width 150px
[[./6.excalidraw.png]]
\begin{verbatim}
S -> aN
N -> aM
N -> bS
M -> MM
M -> a
\end{verbatim}
* Question Seven
A grammar, S, with regular expressions in its production's bodies, after replacing any extensions on regular expressions with their corresponding algebraic equivalents as given in 3.3.5, then, we can transform S into an equivalent grammar by the following rules for nonterminals or terminals:
** Kleene Closure
If we have a match in a production body corresponding to Kleene Closure, then we can transform it into two new productions:
\begin{verbatim}
B*
\end{verbatim}
is mapped to
\begin{verbatim}
A -> ε
A -> BA
\end{verbatim}
** Union
If we have a match in a production body corresponding to the union operator, then we can rewrite it as two new productions:
\begin{verbatim}
B | C
\end{verbatim}
is mapped to
\begin{verbatim}
A -> B
A -> C
\end{verbatim}
** Example
Consider the grammar:
\begin{verbatim}
N -> (A | B)*
A -> aA
A -> ε
B -> bB
B -> ε
\end{verbatim}
then we can form our first transformation by replacing N (with no need to rename since it's already the start symbol):
\begin{verbatim}
N -> ε
N -> (A | B)N
A -> aA
A -> ε
B -> bB
B -> ε
\end{verbatim}
which is then further reduced when we transform (A | B)
\begin{verbatim}
N -> ε
N -> DN
D -> A
D -> B
A -> aA
A -> ε
B -> bB
B -> ε
\end{verbatim}
|