From: John Thingstad
Subject: Why do I get incorrect binding
Date: 
Message-ID: <opr5ufiothxfnb1n@news.chello.no>
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/

From: Erann Gat
Subject: Re: Why do I get incorrect binding
Date: 
Message-ID: <gNOSPAMat-0204041056230001@k-137-79-50-101.jpl.nasa.gov>
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.
From: Kenny Tilton
Subject: Re: Why do I get incorrect binding
Date: 
Message-ID: <tsibc.3278$WA4.1339@twister.nyc.rr.com>
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