-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.rkt
84 lines (71 loc) · 1.87 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
#lang racket
(require typed-peg/core
typed-peg/tree)
(provide (rename-out [parse peg-parse]))
;; definition of the top level parser
(define (parse g s)
(match g
[(peg-grammar rs p)
(let* ([inp (string->list s)]
[r (run-expr rs p inp)])
(if (null? r)
(displayln "Could not parse the input string!")
(car r)))]))
(define (run-eps s)
(cons (tunit) s))
(define (run-chr c s)
(match s
['() '()]
[(cons c1 s1)
(if (eq? c c1)
(cons (tchr c) s1)
'())]))
(define (run-any s)
(match s
['() '()]
[(cons c s1) (cons (tchr c) s1)]))
(define (run-var g v s)
(match (assoc v g)
[#f (begin
(printf "Undefined variable: ~a\n~a\n" v g)
'())]
[(cons _ e1) (run-expr g e1 s)]))
(define (run-cat g e1 e2 s)
(match (run-expr g e1 s)
['() '()]
[(cons t1 s1)
(match (run-expr g e2 s1)
['() '()]
[(cons t2 s2)
(cons (tpair t1 t2) s2)])]))
(define (run-choice g e1 e2 s)
(match (run-expr g e1 s)
['() (match (run-expr g e2 s)
['() '()]
[(cons t2 s2)
(cons (tright t2) s2)])]
[(cons t1 s1)
(cons (tleft t1) s1)]))
(define (run-neg g e1 s)
(match (run-expr g e1 s)
['() (cons (tunit) s)]
[(cons t s1) '()]))
(define (run-star g e s)
(match (run-expr g e s)
['() (cons (tlist '()) s)]
[(cons t s1)
(match (run-expr g (pstar e) s1)
['() (cons (tlist (list t)) s1)]
[(cons (tlist t2) s2)
(cons (tlist (cons t t2)) s2)]
[(cons t s2) (raise 'invalid-tree)])]))
(define (run-expr g e s)
(match e
[(peps) (run-eps s)]
[(pchr c) (run-chr c s)]
[(pany) (run-any s)]
[(pvar v) (run-var g v s)]
[(pcat e1 e2) (run-cat g e1 e2 s)]
[(pchoice e1 e2) (run-choice g e1 e2 s)]
[(pneg e1) (run-neg g e1 s)]
[(pstar e) (run-star g e s)]))