summaryrefslogtreecommitdiff
path: root/Homework/cs5300/homework-two/compilers_assn_2.org
blob: 186303ded24e12c48f6fc8a748e0d0f0bcbe3b36 (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
#+TITLE: Assignment Two
#+AUTHOR: Logan Hunt
#+STARTUP: entitiespretty fold inlineimages
#+LATEX_HEADER: \notindent \notga \usepackage{ dsfont } \usepackage{amsmath}
#+LATEX: \setlength\parindent{0pt}
#+OPTIONS: toc:nil

* Question One
** a
There are four productions.
** b
The terminals of this grammar are:

\begin{verbatim}
a b c d
\end{verbatim}
** c
N_1, N_2, N_3, N_4 are all nonterminals.
** d
The start symbol is N_1.
** e
The symbols N_1, N_2, N_3, N_4 are present in the production heads.
** f
N_2, N_3, N_4, "a", "b", "c", "d" are present in the production bodies.
* Question Two
** a
There are fifteen productions.
** b
The terminals of this grammar are:
\begin{verbatim}
0 1 2 3 4 5 6 7 8 9 + -
\end{verbatim}
** c
The nonterminals are "list" and "digit".
** d
The start symbol is "list".
** e
"list" and "digit" are present in the production heads.
** f
"list", "digit", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, $+$, $-$ are present in the production bodies.
* Question Three
\begin{verbatim}
x -> A (3)
x -> A (3)
xx* -> AA* (2)
(xx*)x+ -> AA+ (1)
((xx*)x+)x+ -> AA+ (1)
\end{verbatim}
* Question Four
** a
Examples:
1. "xy"
2. "xxyy"
3. "xxxyyy"
4. "xxxxyyyy"

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.

** b
Example:
1. "x"
2. "-xx"
3. "+-xx-xx"
4. "-x-xx"

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

The grammar is unambiguous.

** c
Example:
1. ""
2. "()"
3. "()()"
4. "(())"

This grammar generates balanced sets of potentially nested parentheses.

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

#+attr_latex: :width 250px
[[./4c.png]]

** d
1. ""
2. "xyyx"
3. "xyxy"
4. "xy"

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:

#+attr_latex: :width 250px
[[./4d.png]]

** e
1. "(x)**"
2. "x+x"
3. "x*"
4. "xx"

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

This grammar is unambiguous.

* Question Five
** a
#+attr_latex: :width 250px
[[./5a.png]]

** b
#+attr_latex: :width 250px
[[./5b.png]]

* Question Six
\begin{verbatim}
expr -> expr expr op
expr -> factor
op -> + | - | / | *
factor -> 0
factor -> 1
factor ... 9
\end{verbatim}
* Question Seven
\begin{verbatim}
sentence -> character
sentence -> character,sentence
character -> a
character -> b
character -> ...
\end{verbatim}
* Question Eight
\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}
* Question Nine
#+attr_latex: :width 250px
[[./9.png]]
* Question Ten
\begin{verbatim}
expr -> {print '+'} expr + term
expr -> {print '-'} expr - term
expr -> term                     
term -> 0 {print '0'}
term -> 1 {print '1'}          
term -> ... {print ...}
\end{verbatim}
* Question Eleven
** 3-1+2
#+attr_latex: :width 250px
[[./11-1.png]]
** 2+3-1
#+attr_latex: :width 250px
[[./11-2.png]]

* Question Twelve
Predictive parsing is a special kind of recursive descent parsing where the "lookahead unambiguously determines the flow of control".
* Question Thirteen
None of the productions have the same start symbol (disjoint ~FIRST~ sets), and it is not left-recursive.
* Question Fourteen
See ~Infix.java~.
* Question Fifteen
\begin{verbatim}
A -> w R
R -> x y z R | \epsilon
\end{verbatim}
* Question Sixteen
\begin{verbatim}
S -> w R
R -> x y z R
R -> g h R
R -> \epsilon
\end{verbatim}
* Question Seventeen
[[./17.png]]
* Question Eighteen
#+attr_latex: :width 250px
[[./18.png]]