I wrote the following code.
(defmacro make-binary-parser (name op next)
`(defun ,name (line)
(let* ((result (,next line))
(token (peek-token line)))
(cond
((eq (op token) ,op)
(next-token line)
(list (op token) result (,name line)))
(t result)))))
(make-binary-parser div-op '/ umn-op)
(make-binary-parser mul-op '* div-op)
(make-binary-parser min-op '- mul-op)
(make-binary-parser pls-op '+ min-op)
it works. But I wished to collapse this into two functions
/* and +-
When I do this I get something like "2/3*2" --> (/ 2 (* 3 2))
How do I avoid this?
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
In article <················@news.chello.no>, John Thingstad
<··············@chello.no> wrote:
> I wrote the following code.
>
> (defmacro make-binary-parser (name op next)
> `(defun ,name (line)
> (let* ((result (,next line))
> (token (peek-token line)))
> (cond
> ((eq (op token) ,op)
> (next-token line)
> (list (op token) result (,name line)))
> (t result)))))
>
> (make-binary-parser div-op '/ umn-op)
> (make-binary-parser mul-op '* div-op)
> (make-binary-parser min-op '- mul-op)
> (make-binary-parser pls-op '+ min-op)
>
> it works. But I wished to collapse this into two functions
> /* and +-
> When I do this I get something like "2/3*2" --> (/ 2 (* 3 2))
> How do I avoid this?
I presume the answer you were looking for is (* (/ 2 3) 2)
Take a look at
http://www-cgi.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/lisp/code/syntax/parcil/parcil.cl,
and in particular the code for the function parse-expression, and in
particular the part of the code that deals with operator priority.
E.
John Thingstad wrote:
> I wrote the following code.
>
> (defmacro make-binary-parser (name op next)
> `(defun ,name (line)
> (let* ((result (,next line))
> (token (peek-token line)))
> (cond
> ((eq (op token) ,op)
> (next-token line)
> (list (op token) result (,name line)))
> (t result)))))
>
> (make-binary-parser div-op '/ umn-op)
> (make-binary-parser mul-op '* div-op)
> (make-binary-parser min-op '- mul-op)
> (make-binary-parser pls-op '+ min-op)
>
> it works. But I wished to collapse this into two functions
> /* and +-
> When I do this I get something like "2/3*2" --> (/ 2 (* 3 2))
> How do I avoid this?
This is not a Lisp question. Your problem lies in not expressing
anywhere the rules of arithmetic precedence or associativity. ie, You
have some work to do:
find the leftmost operator with the lowest precedence. form a function:
(<left-lowest>
<recursively parse stuff-before>
<recursively parse stuff-after>)
use something like "32/4*4-8/2/2" as a test case.
kt
--
Home? http://tilton-technology.com
Cells? http://www.common-lisp.net/project/cells/
Cello? http://www.common-lisp.net/project/cello/
Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film
Your Project Here! http://alu.cliki.net/Industry%20Application