Compare commits

...

2 Commits

1 changed files with 26 additions and 3 deletions

View File

@ -4,6 +4,10 @@
(define articles '(article the a))
(define adjectives '(adjective happy lazy))
(define connectives '(and but or))
;; output of parse
'(sentence (noun-phrase (article the) (noun cat))
(verb eats))
@ -40,16 +44,26 @@
(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
(define (parse-simple-sentence)
(list 'simple-sentence
(parse-noun-phrase)
(parse-verb-phrase)))
(define (parse-sentence)
(define (maybe-extend sentence)
(amb sentence
(maybe-extend (list 'compound-sentence
sentence
(parse-word connectives)
(parse-simple-sentence)))))
(maybe-extend (parse-simple-sentence)))
(define (parse-verb-phrase)
(define (maybe-extend verb-phrase)
(amb verb-phrase
@ -58,10 +72,11 @@
(parse-prepositional-phrase)))))
(maybe-extend (parse-word verbs)))
;; Extend this to allow an adjective between the article and noun.
(define (parse-simple-noun-phrase)
(list 'simple-noun-phrase
(parse-word articles)
(parse-word nouns)))
(parse-adjective-phrase)))
(define (parse-noun-phrase)
(define (maybe-extend noun-phrase)
@ -71,6 +86,14 @@
(parse-prepositional-phrase)))))
(maybe-extend (parse-simple-noun-phrase)))
;; An adjective phrase is any number of adjectives followed by a
;; simple noun.
(define (parse-adjective-phrase)
(amb (list (parse-word nouns))
(cons
(parse-word adjectives)
(parse-adjective-phrase))))
;; Exercise 4.45: Output of (parse '(the professor lectures to the student in the class with the cat))