-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbak0016.rkt
348 lines (330 loc) · 8.33 KB
/
bak0016.rkt
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#lang plai
;;Bryant Kelley bak0016
(require racket/path)
;;creates global list variable called tokens
;;reads in every line from tokens file and saves
;;each token/lexeme pair as a separate list item
;(define tokens (file->lines (string->path "D:\\Dropbox\\auburn_faculty\\RacketPrograms\\tokens")));;school computer
(define tokens (file->lines (string->path "/Users/bryant/Documents/COMP 3220/Assignment 3/tokens.txt")));;home computer
(define error_count 0)
;;helper function
;;retrieves the first token from the list of tokens
(define current_token
(lambda ()
(regexp-split #px" " (car tokens))
);end lambda
);end current_token
;;helper function
;;removes one token from the tokens list
;;so that current_token function always extracts
;;the next available token
(define next_token
(lambda ()
(set! tokens (cdr tokens))
(cond
((null? tokens)
(display "No more tokens to parse!")
(newline)
(display "Exiting prematurely, no eof found")
(newline)
(display "Errors found: ")
(display error_count)
(newline)
(exit));end null tokens
(else
(cond
((equal? (car (current_token)) "whitespace")
(next_token);call yourself again
)
);end cond
);end else
);end cond
);end lambda
);end next_token
;;checks ID rule
;;if current_token is an id token
;;displays message found + lexeme that was encountered
;;in addition, also displays when entering or leaving
;;function (parse trace)
;;if current_token is not id, displays an error message
(define id
(lambda ()
(display "Entering <id>")
(newline)
(cond
((equal? (car (current_token)) "id")
(display "Found ")
(display (second (current_token)))
(newline)
(next_token)
(display "Leaving <id>")
(newline)
);end first equality check in condition
(else
(display "Not an id token: Error")
(newline)
(+ error_count 1)
);end else statement
);end condition block
);end lambda
);end id
;;checks int rule
;;if current_token is an int token
;;displays message found + lexeme that was encountered
;;in addition, also displays when entering or leaving
;;function (parse trace)
;;if current_token is not int, displays an error message
(define int
(lambda ()
(display "Entering <int>")
(newline)
(cond
((equal? (car (current_token)) "int")
(display "Found ")
(display (second (current_token)))
(newline)
(next_token)
(display "Leaving <int>")
(newline)
);end first equality check in condition
(else
(display "Not an int token: Error")
(newline)
(+ error_count 1)
);end else statement
);end condition block
);end lambda
);end int
;;checks factor rule
;;prints terminals along the way, calling the appropriate function
;;for non-terminals
;;displays message found + lexeme that was encountered
;;in addition, also displays when entering or leaving
;;function (parse trace)
;;if current_token is not (
(define factor
(lambda ()
(display "Entering <factor>")
(newline)
(cond
((equal? (car (current_token)) "(")
(display "Found ")
(display (second (current_token)));should display a (
(newline)
(next_token)
(exp)
(cond
((equal? (car (current_token)) ")")
(display "Found ")
(display (second (current_token)));should display a )
(newline)
(next_token)
(display "Leaving <factor>")
(newline)
);end
(else
(display "Not a ( token: Error")
(newline)
(+ error_count 1)
);end else
);end cond
);end first equality check in condition
((equal? (car (current_token)) "id")
(id)
(display "Leaving <factor>")
(newline)
);end second equality check in condition
((equal? (car (current_token)) "int")
(int)
(display "Leaving <factor>")
(newline)
);end third equality check in condition
(else
(display "Not an factor token: Error")
(newline)
(+ error_count 1)
);end else statement
);end condition block
);end lambda
);end define factor
;;checks exp rule
;;prints terminals along the way, calling the appropriate function
;;for non-terminals
;;displays message found + lexeme that was encountered
;;in addition, also displays when entering or leaving
;;function (parse trace)
(define exp
(lambda ()
(display "Entering <exp>")
(newline)
(term)
(etail)
(display "Leaving <exp>")
(newline)
);end lambda
);end define exp
(define assign
(lambda ()
(display "Entering <assign>")
(newline)
(id)
(cond
((equal? (car (current_token)) "=")
(display "Found ")
(display (second (current_token)));should display a =
(newline)
(next_token)
(exp)
(display "Leaving <assign>")
(newline)
);end first equality check
(else
(display "Not a = token: Error")
(newline)
(+ error_count 1)
);end else statement
);end condition block
);end lambda
);end define assign
(define term
(lambda ()
(display "Entering <term>")
(newline)
(factor)
(ttail)
(display "Leaving <term>")
(newline)
);end lambda
);end define term
(define etail
(lambda ()
(display "Entering <etail>")
(newline)
(cond
((equal? (car (current_token)) "+")
(display "Found ")
(display (second (current_token)));should display a +
(newline)
(next_token)
(factor)
(etail)
(display "Leaving <etail>")
(newline)
);end first cond check
((equal? (car (current_token)) "-")
(display "Found ")
(display (second (current_token)));should display a -
(newline)
(next_token)
(factor)
(etail)
(display "Leaving <etail>")
(newline)
);end second cond check
(else
(display "Not an etail token.")
(newline)
(display "Leaving <etail>")
(newline)
);end else
);end cond
);end lambda
);end define etail
(define ttail
(lambda ()
(display "Entering <ttail>")
(newline)
(cond
((equal? (car (current_token)) "*")
(display "Found ")
(display (second (current_token)));should display a *
(newline)
(next_token)
(factor)
(ttail)
(display "Leaving <ttail>")
(newline)
);end first cond check
((equal? (car (current_token)) "/")
(display "Found ")
(display (second (current_token)));should display a /
(newline)
(next_token)
(factor)
(ttail)
(display "Leaving <ttail>")
(newline)
);end second cond check
(else
(display "Not a ttail token.")
(newline)
(display "Leaving <ttail>")
(newline)
);end else
);end cond
);end lambda
);end define etail
(define stmt
(lambda ()
(display "Entering <stmt>")
(newline)
(cond
((equal? (car (current_token)) "print")
(display "Found ")
(display (second (current_token)));should display print
(newline)
(next_token)
(exp)
(display "Leaving <stmt>")
(newline)
(stmt)
)
((equal? (car (current_token)) "id")
(assign)
(display "Leaving <stmt>")
(newline)
(stmt)
)
((equal? (car (current_token)) "eof")
(display "Found ")
(display (second (current_token)))
(display "Leaving <stmt>")
(newline)
)
(else
(display "Not a <stmt>: Error")
(newline)
(+ error_count 1)
);end else
);end cond
);end lambda
);end define stmt
(define pgm
(lambda ()
(display "Entering <pgm>")
(newline)
(stmt)
(cond
((equal? (car (current_token)) "eof")
(display "End of File")
(newline)
)
)
(display "Leaving <pgm>")
(newline)
(display "Errors found: ")
(display error_count)
(newline)
)
)
;;test function to test whether or not next_token
;;will actually terminate program when it encounters
;;eof
(define tt
(lambda ()
(display (current_token))
(newline)
(next_token)
(tt)
)
)