Compare commits
3 Commits
000edb7976
...
dfd65eed08
Author | SHA1 | Date |
---|---|---|
Oliver Payne | dfd65eed08 | |
Oliver Payne | 2caab6255f | |
Oliver Payne | a3b0a23812 |
|
@ -1,22 +1,28 @@
|
|||
(define nouns '(noun student professor cat class))
|
||||
|
||||
(define verbs '(verb studies lectures eats sleeps))
|
||||
|
||||
(define articles '(article the a))
|
||||
(define prepositions '(prep for to in by with))
|
||||
|
||||
(define (log message level)
|
||||
(define (output message level)
|
||||
(if (= level 0) message
|
||||
(cons '_ (output message (- level 1)))))
|
||||
(display (output message level))
|
||||
(newline))
|
||||
;; output of parse
|
||||
'(sentence (noun-phrase (article the) (noun cat))
|
||||
(verb eats))
|
||||
|
||||
(define (parse-word word-list level)
|
||||
(log (list 'word-list word-list '*unparsed* *unparsed*) level)
|
||||
(define (parse-sentence)
|
||||
(list 'sentence
|
||||
(parse-noun-phrase)
|
||||
(parse-word verbs)))
|
||||
|
||||
(define (parse-noun-phrase)
|
||||
(list 'noun-phrase
|
||||
(parse-word articles)
|
||||
(parse-word nouns)))
|
||||
|
||||
(define (parse-word word-list)
|
||||
(require (not (null? *unparsed*)))
|
||||
(require (memq (car *unparsed*) (cdr word-list)))
|
||||
(let ((found-word (car *unparsed*)))
|
||||
(set! *unparsed* (cdr *unparsed*))
|
||||
(log (list (car word-list) found-word) level)
|
||||
(list (car word-list) found-word)))
|
||||
|
||||
(define *unparsed* '())
|
||||
|
@ -27,61 +33,43 @@
|
|||
(require (null? *unparsed*))
|
||||
sent))
|
||||
|
||||
|
||||
;: (parse '(the cat eats))
|
||||
;; output of parse
|
||||
'(sentence (noun-phrase (article the) (noun cat)) (verb eats))
|
||||
|
||||
(define prepositions '(prep for to in by with))
|
||||
|
||||
(define (parse-prepositional-phrase)
|
||||
(list 'prep-phrase
|
||||
(parse-word prepositions)
|
||||
(parse-noun-phrase)))
|
||||
|
||||
(define (parse-sentence)
|
||||
(list 'sentence
|
||||
;;(parse-noun-phrase)
|
||||
(parse-verb-phrase 1)))
|
||||
(parse-noun-phrase)
|
||||
(parse-verb-phrase)))
|
||||
|
||||
(define (parse-noun-phrase level)
|
||||
(define (maybe-extend noun-phrase)
|
||||
(log '(maybe-extend-noun) level)
|
||||
(amb noun-phrase
|
||||
(maybe-extend (list 'noun-phrase
|
||||
noun-phrase
|
||||
(parse-prepositional-phrase level)))))
|
||||
(log '(parse-noun-phrase) level)
|
||||
(maybe-extend (parse-simple-noun-phrase level)))
|
||||
|
||||
;; Note maybe-extend is tail recursive
|
||||
(define (parse-verb-phrase level)
|
||||
(define (parse-verb-phrase)
|
||||
(define (maybe-extend verb-phrase)
|
||||
(log '(maybe-extend-verb-phrase) level)
|
||||
(amb verb-phrase
|
||||
(maybe-extend (list 'verb-phrase
|
||||
verb-phrase
|
||||
(parse-prepositional-phrase level)))))
|
||||
(log '(parse-verb-phrase) level)
|
||||
(maybe-extend (parse-word verbs level)))
|
||||
(parse-prepositional-phrase)))))
|
||||
(maybe-extend (parse-word verbs)))
|
||||
|
||||
(define (parse-simple-noun-phrase level)
|
||||
(log '(parse-simple-noun) level)
|
||||
(define (parse-simple-noun-phrase)
|
||||
(list 'simple-noun-phrase
|
||||
(parse-word articles level)
|
||||
(parse-word nouns level)))
|
||||
|
||||
(define (parse-prepositional-phrase level)
|
||||
(log '(parse-prep) level)
|
||||
(list 'prep-phrase
|
||||
(parse-word prepositions level)
|
||||
(parse-noun-phrase level)))
|
||||
|
||||
|
||||
|
||||
;; Note: not tail recursive
|
||||
(define (parse-verb-phrase level)
|
||||
(log '(parse-verb-phrase) level)
|
||||
(amb (parse-word verbs level)
|
||||
(list 'verb-phrase
|
||||
(parse-verb-phrase (+ level 1))
|
||||
(parse-prepositional-phrase level))))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
(parse-word articles)
|
||||
(parse-word nouns)))
|
||||
|
||||
(define (parse-noun-phrase)
|
||||
(define (maybe-extend noun-phrase)
|
||||
(amb noun-phrase
|
||||
(maybe-extend (list 'noun-phrase
|
||||
noun-phrase
|
||||
(parse-prepositional-phrase)))))
|
||||
(maybe-extend (parse-simple-noun-phrase)))
|
||||
|
||||
|
||||
;; Exercise 4.45: Output of (parse '(the professor lectures to the student in the class with the cat))
|
||||
|
@ -193,7 +181,10 @@
|
|||
;; orinal procedure, we evaluate (parse-word verbs) once and bind it
|
||||
;; to verb-phrase. We then keep extending the phrase until it either
|
||||
;; succeeds or fails. If it fails, the whole amb call fails. The
|
||||
;; version here never fails because it keeps calling parse-verb-phrase.
|
||||
;; version here never fails because it keeps calling
|
||||
;; parse-verb-phrase, leading to an infinite recursion. The only case
|
||||
;; where this procedure works is when we pass it a valid verb phrase
|
||||
;; and don't try again (ie trigger a fail).
|
||||
|
||||
;; If we swap the order of the clauses in the amb, then we get an
|
||||
;; infinite loop, as the first clause immediately calls
|
||||
|
|
Loading…
Reference in New Issue