Parsing.
This commit is contained in:
+41
-4
@@ -1,8 +1,45 @@
|
||||
;; The first three lines of this file were inserted by DrRacket. They record metadata
|
||||
;; about the language level of this file in a form that our tools can easily process.
|
||||
#reader(lib "htdp-intermediate-reader.ss" "lang")((modname main) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
|
||||
#|
|
||||
#lang pl
|
||||
|
||||
(define-type AE
|
||||
[R1 Prod AE]
|
||||
[R2 Prod])
|
||||
|
||||
(define-type Prod
|
||||
[R3 Atom Prod]
|
||||
[R4 Atom])
|
||||
|
||||
(define-type Atom
|
||||
[R5 Number]
|
||||
[R6 AE])
|
||||
|
||||
; Symbols:
|
||||
; 'sum
|
||||
; 'mul
|
||||
; 'groups
|
||||
; 'groupe
|
||||
|
||||
; parse-ae: s: Sexpr → AE
|
||||
(: parse-ae : (Sexpr -> AE))
|
||||
; Parse a Sexpr.
|
||||
(define (parse-ae s)
|
||||
; parseae: s: Sexpr, a: Sexpr → AE
|
||||
(: parseae : (Sexpr Sexpr -> AE))
|
||||
; Parse a Sexpr and while accumulating in a.
|
||||
(define (parseae s a)
|
||||
(match s
|
||||
[(list prod ae) (parseae/group (rest s) '())]
|
||||
[(cons (number: n) r) (R2 (R4 (R5 n)))]))
|
||||
|
||||
(parseae s '()))
|
||||
|
||||
|
||||
(test (parse-ae '(3)) => (R2 (R4 (R5 3))))
|
||||
(test (parse-ae '(groups 3 groupe)) => (R2 (R4 (R6 (R2 (R4 (R5 3)))))))
|
||||
(test (parse-ae '(1 plus 1)) => (R1 (R4 (R5 1)) (R2 (R4 (R5 1)))))
|
||||
|
||||
; parse-ae/h: s: Sexpr, ae: AE → AE
|
||||
|
||||
#|
|
||||
<AE> ::= <PROD> + <AE> (1)
|
||||
| <PROD> (2)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user