2021-02-07 03:48:35 +01:00
|
|
|
(load "util.scm")
|
|
|
|
(load "misc/amb.scm")
|
|
|
|
|
2021-02-18 01:37:53 +01:00
|
|
|
(define nouns '(noun student professor cat class))
|
|
|
|
(define verbs '(verb studies lectures eats sleeps))
|
2021-02-07 03:48:35 +01:00
|
|
|
(define articles '(article the a))
|
|
|
|
|
|
|
|
(define *unparsed* '())
|
|
|
|
|
2021-02-18 01:37:53 +01:00
|
|
|
; By default mit-scheme evaluates the list elements from last to first element.
|
|
|
|
; That means with the parse-implementation in the book the later part of a
|
|
|
|
; phrase is evaluated first which does not yield any results. To force the
|
|
|
|
; correct order we use a let expression and then built the return list from the
|
|
|
|
; let variables.
|
|
|
|
|
|
|
|
(define (parse-sentence)
|
|
|
|
(let* ((noun-phrase (parse-noun-phrase))
|
|
|
|
(verb-phrase (parse-word verbs)))
|
|
|
|
(list 'sentence noun-phrase verb-phrase)))
|
|
|
|
|
2021-02-07 03:48:35 +01:00
|
|
|
(define (parse-noun-phrase)
|
2021-02-18 01:37:53 +01:00
|
|
|
(let* ((article-phrase (parse-word articles))
|
|
|
|
(noun-phrase (parse-word nouns)))
|
|
|
|
(list 'noun-phrase article-phrase noun-phrase)))
|
2021-02-07 03:48:35 +01:00
|
|
|
|
|
|
|
(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*))
|
|
|
|
(list (car word-list) found-word)))
|
|
|
|
|
|
|
|
(define (parse input)
|
|
|
|
(set! *unparsed* input)
|
|
|
|
(let ((sent (parse-sentence)))
|
|
|
|
(require (null? *unparsed*))
|
|
|
|
sent))
|
|
|
|
|
|
|
|
(display "\nex-4.45 - parse-sentence\n")
|
|
|
|
|
2021-02-18 01:37:53 +01:00
|
|
|
(my-assert (parse '(the cat eats))
|
|
|
|
'(sentence (noun-phrase (article the) (noun cat)) (verb eats)))
|
2021-02-07 03:48:35 +01:00
|
|
|
|
|
|
|
;'(The professor lectures to the student in the class with the cat)
|
|
|
|
|
|
|
|
(display "\nex-4.46\n")
|
|
|
|
|
|
|
|
|