From 96f2cabe1eb93703f22beb6564b76ad4664dc3d1 Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Fri, 10 Oct 2025 14:00:16 -0400 Subject: [PATCH] Parsing. --- 05-parsing/main.rkt | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/05-parsing/main.rkt b/05-parsing/main.rkt index e5da014..95ee9e2 100644 --- a/05-parsing/main.rkt +++ b/05-parsing/main.rkt @@ -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 + +#| ::= + (1) | (2)