-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar-simplifier.rkt
41 lines (31 loc) · 1 KB
/
grammar-simplifier.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
#lang racket
(define (simplify-grammar g)
(cons 'parser-syntax (map simplify-rule (rest g))))
(define (simplify-rule r)
(list (first r) (second r) (simplify-alteration (fourth r))))
(define (simplify-alteration alt)
(filter (lambda (x)
(not (equal? x "|")))
alt))
(provide simplify-grammar)
(module+ test
(require rackunit)
(define-syntax-rule (check-simplify? got should)
(check-equal? (simplify-grammar (cons 'parse-syntax (list got)))
(cons 'parser-syntax (list should))))
(check-simplify?
'(rule "A"
":"
(alteration (expansion
(terminal "a")))
";")
'(rule "A" (alteration (expansion (terminal "a")))))
(check-simplify?
'(rule "A"
":"
(alteration (expansion (terminal "a"))
"|"
(expansion (terminal "b")))
";")
'(rule "A" (alteration (expansion (terminal "a"))
(expansion (terminal "b"))))))