summaryrefslogtreecommitdiff
path: root/Homework/cs5300/homework-two/compilers_assn_2.tex
blob: 408558b325d3265c15511b578e06e7892b7172ae (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
% Created 2023-01-27 Fri 20:04
% Intended LaTeX compiler: pdflatex
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\notindent \notga \usepackage{ dsfont } \usepackage{amsmath}
\author{Logan Hunt}
\date{\today}
\title{Assignment Two}
\hypersetup{
 pdfauthor={Logan Hunt},
 pdftitle={Assignment Two},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 28.2 (Org mode 9.6.1)}, 
 pdflang={English}}
\begin{document}

\maketitle
\setlength\parindent{0pt}

\section{Question One}
\label{sec:org87beba7}
\subsection{a}
\label{sec:orgf2add9e}
There are four productions.
\subsection{b}
\label{sec:orgc7248f7}
The terminals of this grammar are:

\begin{verbatim}
a b c d
\end{verbatim}
\subsection{c}
\label{sec:orgf876127}
N\textsubscript{1}, N\textsubscript{2}, N\textsubscript{3}, N\textsubscript{4} are all nonterminals.
\subsection{d}
\label{sec:orgf042bc3}
The start symbol is N\textsubscript{1}.
\subsection{e}
\label{sec:orgd668266}
The symbols N\textsubscript{1}, N\textsubscript{2}, N\textsubscript{3}, N\textsubscript{4} are present in the production heads.
\subsection{f}
\label{sec:org34be0af}
N\textsubscript{2}, N\textsubscript{3}, N\textsubscript{4}, "a", "b", "c", "d" are present in the production bodies.
\section{Question Two}
\label{sec:org4b362f4}
\subsection{a}
\label{sec:orga65ff15}
There are fifteen productions.
\subsection{b}
\label{sec:org3c23122}
The terminals of this grammar are:
\begin{verbatim}
0 1 2 3 4 5 6 7 8 9 + -
\end{verbatim}
\subsection{c}
\label{sec:org899f753}
The nonterminals are "list" and "digit".
\subsection{d}
\label{sec:org3be2a40}
The start symbol is "list".
\subsection{e}
\label{sec:org19adb89}
"list" and "digit" are present in the production heads.
\subsection{f}
\label{sec:org6764041}
"list", "digit", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, \(+\), \(-\) are present in the production bodies.
\section{Question Three}
\label{sec:orgcef93d4}
\begin{verbatim}
x -> A (3)
x -> A (3)
xx* -> AA* (2)
(xx*)x+ -> AA+ (1)
((xx*)x+)x+ -> AA+ (1)
\end{verbatim}
\section{Question Four}
\label{sec:orge97ecd2}
\subsection{a}
\label{sec:org8eb6283}
Examples:
\begin{enumerate}
\item "xy"
\item "xxyy"
\item "xxxyyy"
\item "xxxxyyyy"
\end{enumerate}

This grammar generates a string of x's prepended to a string of y's with the same amount of characters.

The grammar is unambiguous.

\subsection{b}
\label{sec:org3b9ad4f}
Example:
\begin{enumerate}
\item "x"
\item "-xx"
\item "+-xx-xx"
\item "-x-xx"
\end{enumerate}

This grammar generates addition and subtraction prefix expressions on "x" or chains of "x".

The grammar is unambiguous.

\subsection{c}
\label{sec:orgb960951}
Example:
\begin{enumerate}
\item ""
\item "()"
\item "()()"
\item "(())"
\end{enumerate}

This grammar generates balanced sets of potentially nested parentheses.

The grammar is ambiguous. For example, the string "()()" can be parsed multiple ways:

\begin{center}
\includegraphics[width=250px]{./4c.png}
\end{center}

\subsection{d}
\label{sec:org13f5216}
\begin{enumerate}
\item ""
\item "xyyx"
\item "xyxy"
\item "xy"
\end{enumerate}

This grammar generates jumbled combinations of the same number of x's and y's.

The grammar is ambiguous.

For example, the string "xyxy" has two parse trees:

\begin{center}
\includegraphics[width=250px]{./4d.png}
\end{center}

\subsection{e}
\label{sec:org88fd9c7}
\begin{enumerate}
\item "(x)**"
\item "x+x"
\item "x*"
\item "xx"
\end{enumerate}

This grammar generates combinations of + / * expressions on x in any nested amount of balanced parentheses.

This grammar is unambiguous.

\section{Question Five}
\label{sec:org85d516c}
\subsection{a}
\label{sec:orga24c671}
\begin{center}
\includegraphics[width=250px]{./5a.png}
\end{center}

\subsection{b}
\label{sec:org2837306}
\begin{center}
\includegraphics[width=250px]{./5b.png}
\end{center}

\section{Question Six}
\label{sec:orgfa68e73}
\begin{verbatim}
expr -> expr expr op
expr -> factor
op -> + | - | / | *
factor -> 0
factor -> 1
factor ... 9
\end{verbatim}
\section{Question Seven}
\label{sec:org6a6c81c}
\begin{verbatim}
sentence -> character
sentence -> character,sentence
character -> a
character -> b
character -> ...
\end{verbatim}
\section{Question Eight}
\label{sec:orgc9de4dd}
\begin{verbatim}
expr -> expr - term
expr -> expr + term
expr -> expr * term
expr -> expr / term
expr -> term
term -> +num
term -> -num
term -> num
num -> Integer
num -> Ident
\end{verbatim}
\section{Question Nine}
\label{sec:org0c5cd84}
\begin{center}
\includegraphics[width=250px]{./9.png}
\end{center}
\section{Question Ten}
\label{sec:org51dec25}
\begin{verbatim}
expr -> {print '+'} expr + term
expr -> {print '-'} expr - term
expr -> term                     
term -> 0 {print '0'}
term -> 1 {print '1'}          
term -> ... {print ...}
\end{verbatim}
\section{Question Eleven}
\label{sec:orgfe9e241}
\subsection{3-1+2}
\label{sec:org327f033}
\begin{center}
\includegraphics[width=250px]{./11-1.png}
\end{center}
\subsection{2+3-1}
\label{sec:org9170442}
\begin{center}
\includegraphics[width=250px]{./11-2.png}
\end{center}

\section{Question Twelve}
\label{sec:org24eacf8}
Predictive parsing is a special kind of recursive descent parsing where the "lookahead unambiguously determines the flow of control".
\section{Question Thirteen}
\label{sec:org23d57f5}
None of the productions have the same start symbol (disjoint \texttt{FIRST} sets), and it is not left-recursive.
\section{Question Fourteen}
\label{sec:org71b8b64}
See \texttt{Infix.java}.
\section{Question Fifteen}
\label{sec:org1569332}
\begin{verbatim}
A -> w R
R -> x y z R | \epsilon
\end{verbatim}
\section{Question Sixteen}
\label{sec:org626e3a3}
\begin{verbatim}
S -> w R
R -> x y z R
R -> g h R
R -> \epsilon
\end{verbatim}
\section{Question Seventeen}
\label{sec:org8167863}
\begin{center}
\includegraphics[width=.9\linewidth]{./17.png}
\end{center}
\section{Question Eighteen}
\label{sec:org2d4c8ce}
\begin{center}
\includegraphics[width=250px]{./18.png}
\end{center}
\end{document}