-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.rkt
645 lines (569 loc) · 19.2 KB
/
parser.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
#lang racket
(require (lib "eopl.ss" "eopl"))
(require parser-tools/lex
(prefix-in : parser-tools/lex-sre)
parser-tools/yacc)
(define simple-math-lexer
(lexer
(";" (token-s-semicolon))
("pass" (token-kw-pass))
("break" (token-kw-break))
("continue" (token-kw-continue))
("=" (token-s-assign))
("return" (token-kw-return))
("print" (token-kw-print))
("global" (token-kw-global))
("def" (token-kw-def))
("(" (token-s-open-par))
(")" (token-s-close-par))
(":" (token-s-colon))
("," (token-s-comma))
("if" (token-kw-if))
("else" (token-kw-else))
("for" (token-kw-for))
("in" (token-kw-in))
("or" (token-kw-or))
("and" (token-kw-and))
("not" (token-kw-not))
("==" (token-s-eq))
("<" (token-s-lt))
(">" (token-s-gt))
("<=" (token-s-lte))
(">=" (token-s-gte))
("+" (token-s-plus))
("-" (token-s-minus))
("*" (token-s-mult))
("/" (token-s-div))
("**" (token-s-pow))
("+" (token-s-plus-assign))
("-" (token-s-minus-assign))
("*" (token-s-mult-assign))
("/" (token-s-div-assign))
("**" (token-s-pow-assign))
("[" (token-s-open-brac))
("]" (token-s-close-brac))
((:or (:+ (char-range #\0 #\9)) (:: (:+ (char-range #\0 #\9)) #\. (:+ (char-range #\0 #\9)))) (token-a-number (string->number lexeme)))
("True" (token-a-true))
("False" (token-a-false))
("None" (token-a-none))
((:: (:or (char-range #\a #\z) (char-range #\A #\Z) #\_) (:* (:or (char-range #\a #\z) (char-range #\A #\Z) #\_ (char-range #\0 #\9)))) (token-identifier (string->symbol lexeme)))
(whitespace (simple-math-lexer input-port))
((eof) (token-EOF))))
(define-tokens a (identifier a-number))
(define-empty-tokens b (
EOF s-semicolon kw-print kw-pass kw-break kw-continue s-assign kw-return kw-global kw-def s-open-par s-close-par
s-colon s-comma kw-if kw-else kw-for kw-in kw-or kw-and kw-not s-eq s-lt s-gt s-lte s-gte s-plus s-minus s-mult s-div
s-pow s-plus-assign s-minus-assign s-mult-assign s-div-assign s-pow-assign s-open-brac s-close-brac a-true a-false a-none
))
(define-datatype program program?
(a-program
(prgrm-stmts statements?)))
(define-datatype statements statements?
(a-stmt
(stmt statement?))
(a-stmts
(stmts statements?)
(stmt statement?)))
(define-datatype statement statement?
(stmt-compound-stmt
(stmt compound-stmt?))
(stmt-simple-stmt
(stmt simple-stmt?)))
(define-datatype simple-stmt simple-stmt?
(simple-stmt-assignment
(assignment assignment-stmt?))
(simple-stmt-return-stmt
(stmt return-stmt?))
(simple-stmt-global-stmt
(stmt global-stmt?))
(simple-stmt-print-stmt
(stmt print-stmt?))
(simple-stmt-pass)
(simple-stmt-break)
(simple-stmt-continue))
(define-datatype compound-stmt compound-stmt?
(compound-stmt-function-def
(stmt function-def?))
(compound-stmt-if-stmt
(stmt if-stmt?))
(compound-stmt-for-stmt
(stmt for-stmt?)))
(define-datatype assignment-stmt assignment-stmt?
(an-assignment-stmt
(id symbol?)
(exp expression?)))
(define-datatype return-stmt return-stmt?
(void-return-stmt)
(value-return-stmt
(exp expression?)))
(define-datatype global-stmt global-stmt?
(a-global-stmt
(id symbol?)))
(define-datatype print-stmt print-stmt?
(a-print-stmt
(expression expression?)))
(define-datatype function-def function-def?
(function-def-with-params
(id symbol?)
(params params?)
(stmts statements?))
(funcion-def-without-params
(id symbol?)
(stmts statements?)))
(define-datatype params params?
(a-param
(params params-with-default?))
(a-params
(params-exp params?)
(param-exp params-with-default?)))
(define-datatype params-with-default params-with-default?
(a-param-with-default
(id symbol?)
(exp expression?)))
(define-datatype if-stmt if-stmt?
(an-if-stmt
(exp1 expression?)
(stmts statements?)
(else-block else-block?)))
(define-datatype else-block else-block?
(an-else-block
(stmts statements?)))
(define-datatype for-stmt for-stmt?
(a-for-stmt
(id symbol?)
(exp expression?)
(stmts statements?)))
(define-datatype expression expression?
(an-expression
(exp disjunction-exp?)))
(define-datatype disjunction-exp disjunction-exp?
(a-cnj-disj
(exp conjunction-exp?))
(or-exp
(dis-exp disjunction-exp?)
(con-exp conjunction-exp?)))
(define-datatype conjunction-exp conjunction-exp?
(an-inv-conj
(exp inversion-exp?))
(and-exp
(con-exp conjunction-exp?)
(inv-exp inversion-exp?)))
(define-datatype inversion-exp inversion-exp?
(not-exp
(inv-exp inversion-exp?))
(a-comparison-inv
(cmp-exp comparison-exp?)))
(define-datatype comparison-exp comparison-exp?
(a-sum-comparison
(exp sum-exp?))
(a-cmp-op-sum-pairs-comparison
(exp1 sum-exp?)
(exp2 cmp-op-sum-pairs-exp?)))
(define-datatype cmp-op-sum-pairs-exp cmp-op-sum-pairs-exp?
(a-pair-cmp-op-sum-pairs
(exp cmp-op-sum-pair-exp?))
(a-cmp-op-sum-pairs
(exp1 cmp-op-sum-pairs-exp?)
(exp2 cmp-op-sum-pair-exp?)))
(define-datatype cmp-op-sum-pair-exp cmp-op-sum-pair-exp?
(eq-sum-cmp
(exp eq-sum-exp?))
(lt-sum-cmp
(exp lt-sum-exp?))
(gt-sum-cmp
(exp gt-sum-exp?)))
(define-datatype eq-sum-exp eq-sum-exp?
(a-eq
(exp sum-exp?)))
(define-datatype lt-sum-exp lt-sum-exp?
(a-lt
(exp sum-exp?)))
(define-datatype gt-sum-exp gt-sum-exp?
(a-gt
(exp sum-exp?)))
(define-datatype sum-exp sum-exp?
(a-sum
(exp1 sum-exp?)
(exp2 term-exp?))
(a-minus
(exp1 sum-exp?)
(exp2 term-exp?))
(a-term-sum
(exp term-exp?)))
(define-datatype term-exp term-exp?
(a-mul-term
(exp1 term-exp?)
(exp2 factor-exp?))
(a-div-term
(exp1 term-exp?)
(exp2 factor-exp?))
(a-term
(exp factor-exp?)))
(define-datatype factor-exp factor-exp?
(a-plus-factor
(exp factor-exp?))
(a-minus-factor
(exp factor-exp?))
(a-power-factor
(exp power-exp?)))
(define-datatype power-exp power-exp?
(a-power
(atom atom-exp?)
(factor factor-exp?))
(a-primary-power
(exp primary-exp?)))
(define-datatype primary-exp primary-exp?
(an-atom-primary
(exp atom-exp?))
(an-array-primary
(prim primary-exp?)
(exp expression?))
(a-callable-primary
(prim primary-exp?))
(a-callable-primary-with-args
(prim primary-exp?)
(args arguments-exp?)))
(define-datatype arguments-exp arguments-exp?
(argument
(exp expression?))
(arguments
(args arguments-exp?)
(exp expression?)))
(define-datatype atom-exp atom-exp?
(id-atom
(id symbol?))
(bool-atom
(val boolean?))
(none-atom)
(num-atom
(val number?))
(list-atom
(val list-exp?)))
(define-datatype list-exp list-exp?
(a-list
(exps expressions?))
(empty-list))
(define-datatype expressions expressions?
(a-exps
(exps expressions?)
(exp expression?))
(a-exp
(exp expression?)))
(define simple-math-parser
(parser
(start program)
(end EOF)
(error void)
(tokens a b)
(grammar
(program
((statements) (a-program $1)))
(statements
((statement s-semicolon) (a-stmt $1))
((statements statement s-semicolon) (a-stmts $1 $2)))
(statement
((compound-stmt) (stmt-compound-stmt $1))
((simple-stmt) (stmt-simple-stmt $1)))
(simple-stmt
((assignment) (simple-stmt-assignment $1))
((return-stmt) (simple-stmt-return-stmt $1))
((global-stmt) (simple-stmt-global-stmt $1))
((print-stmt) (simple-stmt-print-stmt $1))
((kw-pass) (simple-stmt-pass))
((kw-break) (simple-stmt-break))
((kw-continue) (simple-stmt-continue)))
(compound-stmt
((function-def) (compound-stmt-function-def $1))
((if-stmt) (compound-stmt-if-stmt $1))
((for-stmt) (compound-stmt-for-stmt $1)))
(assignment
((identifier s-assign expression) (an-assignment-stmt $1 $3)))
(return-stmt
((kw-return) (void-return-stmt))
((kw-return expression) (value-return-stmt $2)))
(global-stmt
((kw-global identifier) (a-global-stmt $2)))
(print-stmt
((kw-print expression) (a-print-stmt $2)))
(function-def
((kw-def identifier s-open-par params s-close-par s-colon statements) (function-def-with-params $2 $4 $7))
((kw-def identifier s-open-par s-close-par s-colon statements) (funcion-def-without-params $2 $6)))
(params
((param-with-default) (a-param $1))
((params s-comma param-with-default) (a-params $1 $3)))
(param-with-default
((identifier s-assign expression) (a-param-with-default $1 $3)))
(if-stmt
((kw-if expression s-colon statements else-block) (an-if-stmt $2 $4 $5)))
(else-block
((kw-else s-colon statements) (an-else-block $3)))
(for-stmt
((kw-for identifier kw-in expression s-colon statements) (a-for-stmt $2 $4 $6)))
(expression
((disjunction) (an-expression $1)))
(disjunction
((conjunction) (a-cnj-disj $1))
((disjunction kw-or conjunction) (or-exp $1 $3)))
(conjunction
((inversion) (an-inv-conj $1))
((conjunction kw-and inversion) (and-exp $1 $3)))
(inversion
((kw-not inversion) (not-exp $2))
((comparison) (a-comparison-inv $1)))
(comparison
((sum compare-op-sum-pairs) (a-cmp-op-sum-pairs-comparison $1 $2))
((sum) (a-sum-comparison $1)))
(compare-op-sum-pairs
((compare-op-sum-pair) (a-pair-cmp-op-sum-pairs $1))
((compare-op-sum-pairs compare-op-sum-pair) (a-cmp-op-sum-pairs $1 $2)))
(compare-op-sum-pair
((eq-sum) (eq-sum-cmp $1))
((lt-sum) (lt-sum-cmp $1))
((gt-sum) (gt-sum-cmp $1)))
(eq-sum
((s-eq sum) (a-eq $2)))
(lt-sum
((s-lt sum) (a-lt $2)))
(gt-sum
((s-gt sum) (a-gt $2)))
(sum
((sum s-plus term) (a-sum $1 $3))
((sum s-minus term) (a-minus $1 $3))
((term) (a-term-sum $1)))
(term
((term s-mult factor) (a-mul-term $1 $3))
((term s-div factor) (a-div-term $1 $3))
((factor) (a-term $1)))
(factor
((s-plus factor) (a-plus-factor $2))
((s-minus factor) (a-minus-factor $2))
((power) (a-power-factor $1)))
(power
((atom s-pow factor) (a-power $1 $3))
((primary) (a-primary-power $1)))
(primary
((atom) (an-atom-primary $1))
((primary s-open-brac expression s-close-brac) (an-array-primary $1 $3))
((primary s-open-par s-close-par) (a-callable-primary $1))
((primary s-open-par arguments s-close-par) (a-callable-primary-with-args $1 $3)))
(arguments
((expression) (argument $1))
((arguments s-comma expression) (arguments $1 $3)))
(atom
((identifier) (id-atom $1))
((a-true) (bool-atom true))
((a-false) (bool-atom false))
((a-none) (none-atom))
((a-number) (num-atom $1))
((List) (list-atom $1)))
(List
((s-open-brac expressions s-close-brac) (a-list $2))
((s-open-brac s-close-brac) (empty-list)))
(expressions
((expressions s-comma expression) (a-exps $1 $3))
((expression) (a-exp $1))))))
(define empty-env
(lambda () '()))
(define extend-env
(lambda (var val env)
(cons (cons var val) env)))
(define apply-env
(lambda (initial-env search-var)
(letrec ((loop (lambda (env)
(cond ((null? env)
(report-no-binding-found search-var initial-env))
((and (pair? env) (pair? (car env)))
(let ((saved-var (caar env))
(saved-val (cdar env))
(saved-env (cdr env)))
(if (eqv? search-var saved-var)
saved-val
(loop saved-env))))
(else
(report-invalid-env initial-env))))))
(loop initial-env))))
(define report-no-binding-found
(lambda (search-var env)
(eopl:error 'apply-env "No binding for ~s in ~s" search-var env)))
(define report-invalid-env
(lambda (env)
(eopl:error 'apply-env "Bad environment ~s" env)))
(define init-env
(lambda () (empty-env)))
(define run
(lambda (program)
(value-of-program program)))
(define value-of-program
(lambda (pgm)
(cases program pgm
(a-program (stmts)
(value-of-stmts stmts (init-env))))))
(define value-of-stmts
(lambda (stmts env)
(cases statements stmts
(a-stmt (stmt)
(value-of-stmt stmt env))
(a-stmts (stmts stmt)
(let ((new-env (value-of-stmts stmts env)))
(value-of-stmt stmt new-env))))))
(define value-of-stmt
(lambda (stmt env)
(cases statement stmt
(stmt-compound-stmt (stmtt)
(value-of-stmt-compound-stmt stmtt env))
(stmt-simple-stmt (stmtt)
(value-of-simple-stmt stmtt env)))))
(define value-of-stmt-compound-stmt
(lambda (compund-statement env)
(cases compound-stmt compund-statement
(compound-stmt-function-def (stmt)
(compound-stmt-function-def stmt env)
)
(compound-stmt-if-stmt (stmt)
(value-of-if-stmt stmt env)
)
(compound-stmt-for-stmt (stmt)
(value-of-for-stmt stmt env)
)
)
)
)
(define value-of-for-stmt
'f
)
(define value-of-simple-stmt
(lambda (stmt env)
(cases simple-stmt stmt
(simple-stmt-assignment (stmtt)
(value-of-assignment stmtt env))
(simple-stmt-return-stmt (stmtt) 'f)
(simple-stmt-global-stmt (stmtt) 'f)
(simple-stmt-print-stmt (stmt) (value-of-print-stmt stmt env))
(simple-stmt-pass () env)
(simple-stmt-break () 'f)
(simple-stmt-continue () 'f))))
(define value-of-print-stmt
(lambda (stmt env)
(cases print-stmt stmt
(a-print-stmt (exp)
(begin
(display (value-of-expression exp env))
(display "\n")
env)))))
(define value-of-assignment
(lambda (ass env)
(cases assignment-stmt ass
(an-assignment-stmt (id exp)
(let ((exp-val (value-of-expression exp env)))
(extend-env id exp-val env))))))
(define value-of-expression
(lambda (expr env)
(cases expression expr
(an-expression (exp)
(value-of-disjunction exp env)))))
(define value-of-disjunction
(lambda (expr env)
(cases disjunction-exp expr
(a-cnj-disj (exp)
(value-of-conjunction exp env))
(or-exp (dis-exp con-exp)
(or (value-of-disjunction dis-exp env) (value-of-conjunction con-exp env))))))
(define value-of-conjunction
(lambda (expr env)
(cases conjunction-exp expr
(an-inv-conj (exp)
(value-of-inversion exp env))
(and-exp (con-exp inv-exp)
(and (value-of-conjunction con-exp env) (value-of-inversion exp env))))))
(define value-of-inversion
(lambda (expr env)
(cases inversion-exp expr
(not-exp (exp)
(not (value-of-inversion exp env)))
(a-comparison-inv (exp)
(value-of-comparison exp env)))))
(define value-of-comparison
(lambda (expr env)
(cases comparison-exp expr
(a-sum-comparison (exp)
(value-of-sum exp env))
(a-cmp-op-sum-pairs-comparison (exp1 exp2) ('f)))))
(define value-of-sum
(lambda (expr env)
(cases sum-exp expr
(a-sum (exp1 exp2)
(+ (value-of-sum exp1 env) (value-of-term exp2 env)))
(a-minus (exp1 exp2)
(- (value-of-sum exp1 env) (value-of-term exp2 env)))
(a-term-sum (exp)
(value-of-term exp env)))))
(define (convert-to-bool param)
(cond
[(list? param) (not (empty? param))]
[(boolean? param) param]
[(number? param) (not (equal? param 0))]
)
)
(define value-of-if-stmt
(lambda (expr env)
(cases if-stmt expr
(an-if-stmt (exp1 stmts else-block)
(if (value-of-expression exp1 env) (value-of-stmts stmts env) (value-of-else-block else-block env))
))))
(define value-of-else-block
(lambda (expr env)
(cases else-block expr
(an-else-block (stmts)
(value-of-stmts stmts env)
))))
(define value-of-term
(lambda (expr env)
(cases term-exp expr
(a-mul-term (exp1 exp2)
(* (value-of-term exp1 env) (value-of-factor exp2 env)))
(a-div-term (exp1 exp2)
(/ (value-of-term exp1 env) (value-of-factor exp2 env)))
(a-term (exp)
(value-of-factor exp env)))))
(define value-of-factor
(lambda (expr env)
(cases factor-exp expr
(a-plus-factor (exp)
(value-of-factor exp env))
(a-minus-factor (exp)
(- 0 (value-of-factor exp env)))
(a-power-factor (exp)
(value-of-power exp env)))))
(define value-of-power
(lambda (expr env)
(cases power-exp expr
(a-power (atom factor)
(expt (value-of-atom atom env) (value-of-factor factor env)))
(a-primary-power (exp)
(value-of-primary exp env)))))
(define value-of-primary
(lambda (expr env)
(cases primary-exp expr
(an-atom-primary (exp)
(value-of-atom exp env))
(an-array-primary (prim exp) 'f)
(a-callable-primary (prim) 'f)
(a-callable-primary-with-args (prim args) 'f))))
(define value-of-atom
(lambda (expr env)
(cases atom-exp expr
(id-atom (id)
(apply-env env id))
(bool-atom (val) val)
(none-atom () null)
(num-atom (val) val)
(list-atom (val) 'f))))
;test
(define lex-this (lambda (lexer input) (lambda () (lexer input))))
;(define my-lexer (lex-this simple-math-lexer (open-input-string "a = 2; b = a; c = a + b;")))
;(simple-math-parser my-lexer)
;(define parser-res (simple-math-parser my-lexer))
(define evaluate
(lambda (path)
(run (simple-math-parser (lex-this simple-math-lexer (open-input-string (file->string (string->path path))))))))
(evaluate "test.txt")