#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 #| ::= + (1) | (2) ::= * (3) | (4) ::= (5) | { } (6) "5" (5) 5 "{5}" (6) (2) (4) (5) 5 "{2 + 5}" (6) (1) (4) (5) 2 (2) (4) (5) 5 "{{{2 + 5}} * {5 * {{3 + 6}}}}" (6) (2) (3) (6) (6) (1) (4) (5) 2 (2) (4) (5) 5 (4) (6) (3) (5) 5 (4) (6) (6) (1) (4) (5) 3 (2) (4) (5) 6 |#