This sucks.
This commit is contained in:
@@ -12,8 +12,7 @@
|
|||||||
[(and (even? a) (even? b)) (* 2 (gcd2 (floor (/ a 2)) (floor (/ b 2))))]
|
[(and (even? a) (even? b)) (* 2 (gcd2 (floor (/ a 2)) (floor (/ b 2))))]
|
||||||
[(even? b) (gcd2 a (floor (/ b 2)))]
|
[(even? b) (gcd2 a (floor (/ b 2)))]
|
||||||
[(even? a) (gcd2 (floor (/ a 2)) b)]
|
[(even? a) (gcd2 (floor (/ a 2)) b)]
|
||||||
[else
|
[else (if (<= a b)
|
||||||
(if (<= a b)
|
|
||||||
(gcd2 a (- b a))
|
(gcd2 a (- b a))
|
||||||
(gcd2 b (- a b)))]))
|
(gcd2 b (- a b)))]))
|
||||||
|
|
||||||
|
|||||||
+59
-14
@@ -1,5 +1,32 @@
|
|||||||
#lang pl
|
#lang pl
|
||||||
|
|
||||||
|
#|
|
||||||
|
|
||||||
|
"{{{2 + 5}} * {5 * {{3 + 6}}}}"
|
||||||
|
The parenthesis ^here & ^here are necessary because otherwise it would be an AE, not a Prod as R3 requires. It requires this to maintain order of operations.
|
||||||
|
|
||||||
|
"{{{2 + 5}} * {5 * {{3 * 6}}}}"
|
||||||
|
The parenthesis are no longer needed, as {3 * 6} is itself a prod satisfying R3.
|
||||||
|
|
||||||
|
If you have a grammar language with easy precedence, it's nice. Otherwise, it's cursed.
|
||||||
|
|
||||||
|
In Yacc:
|
||||||
|
%left ADD SUB
|
||||||
|
%left MUL DIV
|
||||||
|
%precedence NEG
|
||||||
|
|
||||||
|
exp:
|
||||||
|
...
|
||||||
|
| NUM {...}
|
||||||
|
| GROUPSTART exp GROUPEND { ... }
|
||||||
|
| SUB exp {...}
|
||||||
|
| exp SUM exp {...}
|
||||||
|
| exp SUB exp {...}
|
||||||
|
| exp MUL exp {...}
|
||||||
|
| exp DIV exp {...}
|
||||||
|
|
||||||
|
|#
|
||||||
|
|
||||||
(define-type AE
|
(define-type AE
|
||||||
[R1 Prod AE]
|
[R1 Prod AE]
|
||||||
[R2 Prod])
|
[R2 Prod])
|
||||||
@@ -12,30 +39,46 @@
|
|||||||
[R5 Number]
|
[R5 Number]
|
||||||
[R6 AE])
|
[R6 AE])
|
||||||
|
|
||||||
|
#|
|
||||||
|
<AE> ::= <PROD> + <AE> (1)
|
||||||
|
| <PROD> (2)
|
||||||
|
<PROD> ::= <ATOM> * <PROD> (3)
|
||||||
|
| <ATOM> (4)
|
||||||
|
<ATOM> ::= <num> (5)
|
||||||
|
| { <AE> } (6)
|
||||||
|
|#
|
||||||
|
|
||||||
; Symbols:
|
; Symbols:
|
||||||
; 'sum
|
; 'sum
|
||||||
; 'mul
|
; 'mul
|
||||||
; 'groups
|
; 'groups
|
||||||
; 'groupe
|
; 'groupe
|
||||||
|
|
||||||
; parse-ae: s: Sexpr → AE
|
(: parseae : (Sexpr -> AE))
|
||||||
(: parse-ae : (Sexpr -> AE))
|
(define (parseae s)
|
||||||
; 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
|
(match s
|
||||||
[(list prod ae) (parseae/group (rest s) '())]
|
[(list prod '+ ae) (R1 (parseprod prod) (parseae ae))]
|
||||||
[(cons (number: n) r) (R2 (R4 (R5 n)))]))
|
[prod (R2 (parseprod prod))]))
|
||||||
|
|
||||||
(parseae s '()))
|
(: parseprod : (Sexpr -> Prod))
|
||||||
|
(define (parseprod s)
|
||||||
|
(match s
|
||||||
|
[(list atom '* prod) (R3 (parseatom atom) (parseprod prod))]
|
||||||
|
[ atom (R4 (parseatom atom))]))
|
||||||
|
|
||||||
|
(: parseatom : (Sexpr -> Atom))
|
||||||
|
(define (parseatom s)
|
||||||
|
(match s
|
||||||
|
[ (number: n) (R5 n)]
|
||||||
|
[(list ae) (R6 (parseae ae))]))
|
||||||
|
|
||||||
(test (parse-ae '(3)) => (R2 (R4 (R5 3))))
|
(: parse : (String -> AE))
|
||||||
(test (parse-ae '(groups 3 groupe)) => (R2 (R4 (R6 (R2 (R4 (R5 3)))))))
|
(define (parse s) (parseae (string->sexpr s)))
|
||||||
(test (parse-ae '(1 plus 1)) => (R1 (R4 (R5 1)) (R2 (R4 (R5 1)))))
|
|
||||||
|
(test (parse "5") => (R2 (R4 (R5 5))))
|
||||||
|
(test (parse "{2 + 5}") => (R1 (R4 (R5 2)) (R2 (R4 (R5 5)))))
|
||||||
|
(test (parse "{{{2 + 5}} * {5 * {{3 + 6}}}}") => (R2 (R3 (R6 (R1 (R4 (R5 2)) (R2 (R4 (R5 5))))) (R3 (R5 5) (R4 (R6 (R1 (R4 (R5 3)) (R2 (R4 (R5 6)))))))))
|
||||||
|
)
|
||||||
|
|
||||||
; parse-ae/h: s: Sexpr, ae: AE → AE
|
; parse-ae/h: s: Sexpr, ae: AE → AE
|
||||||
|
|
||||||
@@ -49,6 +92,8 @@
|
|||||||
<ATOM> ::= <num> (5)
|
<ATOM> ::= <num> (5)
|
||||||
| { <AE> } (6)
|
| { <AE> } (6)
|
||||||
|
|
||||||
|
These are not wrapped in the few rules at the top level to convert them to AEs
|
||||||
|
|
||||||
"5"
|
"5"
|
||||||
<atom> (5)
|
<atom> (5)
|
||||||
<num> 5
|
<num> 5
|
||||||
|
|||||||
Reference in New Issue
Block a user