-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchemeParser.rkt
434 lines (400 loc) · 11.3 KB
/
SchemeParser.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#lang plai
(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 "/Users/donaldtran/Documents/AU Course Work and Info/2017/Spring 2017/COMP 3220/Assignment 1 - Ruby/KEY/tokens")));;home computer
;;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")
(inc-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")
(inc-count)
(newline)
);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")
(inc-count)
(newline)
);end else statement
);end condition block
);end lambda
);end int
;;checks whitespace rule
;;if current_token is a whitespace token
;;displays message found + lexeme that was encountered
;;in addition, also displays when entering or leaving
;;function (parse trace)
;;if current_token is not whitespace, displays an error message
(define whitespace
(lambda ()
(display "Entering <whitespace>")
(newline)
(cond
((equal? (car (current_token)) " ")
(display "Found ")
(display (second (current_token)))
(newline)
(next_token)
(display "Leaving <whitespace>")
(newline)
);end first equality check in condition
((equal? (car (current_token)) "\n")
(display "Found ")
(display (second (current_token)))
(newline)
(next_token)
(display "Leaving <whitespace>")
(newline)
);end second equality check in condition
(else
(display "Error: Not a whitespace token")
(inc-count)
(newline)
);end else statement
);end condition block
);end lambda
);end whitespace
;;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)
(define factor
(lambda ()
(display "Entering <factor>")
(cond
[(equal? (car (current_token)) "id")
(newline)
(id)
(display "Leaving <factor>")];end first equality check in condition
[(equal? (car (current_token)) "int")
(newline)
(int)
(display "Leaving <factor>")];end second equality check in condition
[(equal? (car (current_token)) "(")
(newline)
(display "Found ")
(display (second (current_token)))
(next_token)
(newline)
(exp)
(cond
[(equal? (car (current_token)) ")")
(newline)
(display "Found ")
(display (second (current_token)))
(next_token)
(newline)
(display "Leaving <factor>")]
(else
(newline)
(display "Error: expected ')' but received ")
(display "'")
(display (second (current_token)))
(display "'")
(inc-count)
)
)
]
(else
(display "Error: No ID, interger literal, or left parentheses detected (<factor>)")
(inc-count)
(newline)
);end else statement
);end condition block
);end lambda
);end define factor
;;checks stmt 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 stmt
(lambda ()
(display "Entering <stmt>")
(cond
[(equal? (car (current_token)) "print")
(newline)
(display "Found ")
(display (second (current_token)))
(next_token)
(newline)
(exp)
(newline)
(display "Leaving <stmt>")];end first equality check in condition
(else
(assign)
(newline)
(display "Leaving <stmt>")
);end else statement
);end condition block
);end lambda
);end define stmt
;;checks pgm 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 pgm
(lambda ()
(newline)
(stmt)
(cond
((equal? (car (current_token)) "eof")
(newline)
(display "Reached end of file. Parse complete... ")
(newline)
(display "Parse completed with ")
(display (get-count))
(display " error(s).")
(newline)
(display "Now exiting program")
(exit)
);end of file check
(else
(pgm)
)
)
);end lambda
);end define stmt
;;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)
(newline)
(display "Leaving <exp>")
);end lambda
);end define exp
;;checks term 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 term
(lambda ()
(display "Entering <term>")
(newline)
(factor)
(ttail)
(newline)
(display "Leaving <term>")
);end lambda
);end define term
;;checks assign 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 assign
(lambda ()
(newline)
(display "Entering <assign>")
(cond
((equal? (car (current_token)) "id")
(newline)
(id))
(else
(display "Error: No id found in <assign>")
(inc-count)
(newline)
(display "Leaving <assign>")));end first condition block
(cond
((equal? (car (current_token)) "=")
(display "Found ")
(display (second (current_token)))
(next_token)
(newline)
(exp)
(newline)
(display "Leaving <assign>"))
(else
(newline)
(display "Error: No '=' found after the id in <assign>")
(inc-count)
(newline)
);end second else statement
);end second conditional
);end lambda
);end define assign
;;checks ttail 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 ttail
(lambda ()
(newline)
(display "Entering <ttail>")
(cond
[(equal? (car (current_token)) "*")
(newline)
(display "Found ")
(display (second (current_token)))];end first equality check in condition
[(equal? (car (current_token)) "/")
(newline)
(display "Found ")
(display (second (current_token)))];end second equality check in condition
(else
(newline)
(display "Next token is not * or /, choosing EPSILON production")
);end else statement
);end condition block
(while (or (equal? (car (current_token)) "*")
(equal? (car (current_token)) "/"))
(next_token)
(newline)
(factor))
(newline)
(display "Leaving <ttail>")
);end lambda
);end define factor
;;checks etail 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 etail
(lambda ()
(newline)
(display "Entering <etail>")
(cond
[(equal? (car (current_token)) "+")
(newline)
(display "Found ")
(display (second (current_token)))];end first equality check in condition
[(equal? (car (current_token)) "-")
(newline)
(display "Found ")
(display (second (current_token)))];end second equality check in condition
(else
(newline)
(display "Next token is not + or -, choosing EPSILON production")
);end else statement
);end condition block
(while (or (equal? (car (current_token)) "+")
(equal? (car (current_token)) "-"))
(next_token)
(newline)
(term))
(newline)
(display "Leaving <etail>")
);end lambda
);end define factor
;;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)
)
)
;;Counter function for Error checking
(define (make-counter)
(let ((count 0))
(values (lambda ()
(set! count (+ count 1))
count)
(lambda ()
count))))
;;Supplemental function to the Error counter function to define
;;incrmenting the counter versus retrieving the value
(define-values (inc-count get-count)
(make-counter))
;;Defines syntax for a iterative while loop
;;http://codeimmersion.i3ci.hampshire.edu/2009/10/15/a-scheme-while-loop/
(define-syntax (while stx)
(syntax-case stx ()
((_ condition expression ...)
#`(do ()
((not condition))
expression
...))))