Blob


1 (defun make-sum (a1 a2)
2 (list a1 '+ a2))
3 (defun make-product (m1 m2)
4 (list m1 '* m2))
5 (defun sum? (x)
6 (and (consp x) (eql (cadr x) '+)))
7 (defun addend (x)
8 (car x))
9 (defun augend (s)
10 (caddr s))
11 (defun product? (x)
12 (and (consp x) (eql (cadr x) '*)))
13 (defun multiplier (s)
14 (car s))
15 (defun multiplicand (s)
16 (caddr s))
18 (defvar *stream* '() "Token stream")
19 (defun init-stream (stream)
20 "Initialize the stream"
21 (setq *stream* stream))
22 (defun next-token ()
23 "Returns the next token of the stream"
24 (car *stream*))
25 (defun scan ()
26 (pop *stream*))
27 (defvar *stream-stack* '() "Stack of streams")
28 (defun push-stream (stream)
29 "Push the current *stream* on stack, and set this tream as *stream*"
30 (push *stream* *stream-stack*)
31 (init-stream stream))
32 (defun pop-stream ()
33 (init-stream (pop *stream-stack)))
34 (defun parse-factor ()
35 (let ((tok (next-token)))
36 (cond
37 ((or (numberp tok) (sybolp tok))
38 (scan)
39 tok)
40 ((listp tok)
41 (push-strea tok)
42 (let ((sum (parse-sum)))
43 (pop-stream)
44 (scan)
45 sum))
46 (t (error "Bad token in parse-atom -- ~A" tok)))))
47 (defun parse-term ()
48 (let ((lfact (parse-factor)))
49 (if (eq (next-token) '*)
50 (progn
51 (scan)
52 (let ((rterm (parse-term)))
53 (list '* lfact rterm)))
54 lfact)))
55 (defun (parse-