From: Runser Guillaume
Subject: Translate string to lisp expression
Date: 
Message-ID: <01bd1aea$cf5a1a20$1211fcc1@pentium200>
Small problem for good programmers :

My goal is to take a string, containing a mathematic expression like
"(25*3+5*(3-5))"
and to translate it in a Lisp List of Atoms like
'( 25 * 3 + 5 * ( 3 - 5))

in order to apply my translation function in EMACS LISP !!

Did anyone have an idea ???

Thank you

·························@wanadoo.fr

From: Ingvar Mattsson
Subject: Re: Translate string to lisp expression
Date: 
Message-ID: <v8oh1o6wis.fsf@sunserv.idasys.se>
"Runser Guillaume" <··················@wanadoo.fr> writes:

> Small problem for good programmers :
> 
> My goal is to take a string, containing a mathematic expression like
> "(25*3+5*(3-5))"
> and to translate it in a Lisp List of Atoms like
> '( 25 * 3 + 5 * ( 3 - 5))
> 
> in order to apply my translation function in EMACS LISP !!

Suggested functions are:
 * one function that scans a string and inserts spaces before and after
   operators. A naive implementation is something like:
   (setq operators "+-*/")
   (defun expand (str)
      (let ((rv "")
            (c 0)
            (ch 7)
	    (max (length str)))
         (while (< c max)
            (if (string-match
                  (regexp-quote (format "%c" (aref str c))) operators)
                (setq rv (format "%s %c " rv (aref str c)))
              (setq rv (format "%s%c" rv (aref str c)))))
         rv))
 * read-from-string

If this is supposed to be homework, please note that the function
isn't tested and it can probably be improved (if it even works).

//Ingvar (OK, Erik, what neat emacs-lisp functions am I in ignorace of?)
-- 
Sysadmin, disgruntled, unpolite.
I don't speak for my employer nor do they speak for me.
Accept this and life will be easier.		······@idasys.se
From: Cedric Adjih
Subject: Re: Translate string to lisp expression
Date: 
Message-ID: <690f3d$3ri@news-rocq.inria.fr>
Runser Guillaume <··················@wanadoo.fr> wrote:

: Small problem for good programmers :

: My goal is to take a string, containing a mathematic expression like
: "(25*3+5*(3-5))"
: and to translate it in a Lisp List of Atoms like
: '( 25 * 3 + 5 * ( 3 - 5))

: in order to apply my translation function in EMACS LISP !!

: Did anyone have an idea ???

Well, yes.
I'm far from being a good Elisp programmer, and the following
is not the most elegant ; but it seems to work:

;; - begin elisp code -

(defun string-decorate-regex (string regex prefix postfix)
  "add prefix,postfix around each occurence of a regex"
  (let ((match-start (string-match regex string)))
    (if (null match-start) 
	string
      (let ((match-stop (match-end 0)))
	(concat (substring string 0 match-start)
		prefix
		(substring string match-stop match-start)
		postfix
		(string-space-regex (substring string match-stop) 
				    regex prefix postfix)))))))

(defun string-decorate-multiple-regex (string regex-pre-post-list)
  "same as string-decorate-regex except that makes changes for
   multiple regexes passed in a list of the following form:
   ((REGEX0 PREFIX0 POSTFIX0) (REGEX1 PREFIX1 POSTFIX1) ...)"
  (if (null regex-pre-post-list)
      string
    (string-decorate-multiple-regex 
     (apply #'string-decorate-regex string (car regex-pre-post-list))
     (cdr regex-pre-post-list))))

(defun formula-to-atoms (string)
  (car (read-from-string 
	(concat "( "
		(string-decorate-multiple-regex 
		 string
		 '(("[+]" " " " ") ("-" " " " ") ("[*]" " " " ")
		   ("/" " " " ") ("[(]" " \\" " ") ("[)]" " \\" " ")
		   ("[0-9]+" " " " ")))
		" )"))))
  
(formula-to-atoms "(25*3+5*(3-5))")
;=> (\( 25 * 3 + 5 * \( 3 - 5 \) \))

;; - end elisp code -


-- Cedric Adjih, not speaking for INRIA, etc...
From: Markku Laukkanen
Subject: Re: Translate string to lisp expression
Date: 
Message-ID: <34B365F9.23487708@research.nokia.com>
Runser Guillaume wrote:

> Small problem for good programmers :
>
> My goal is to take a string, containing a mathematic expression like
> "(25*3+5*(3-5))"
> and to translate it in a Lisp List of Atoms like
> '( 25 * 3 + 5 * ( 3 - 5))
>
> in order to apply my translation function in EMACS LISP !!
>
> Did anyone have an idea ???
>
> Thank you
>
> ·························@wanadoo.fr

Would be piece of cake if using CL, using Emacs lisp probably a little
bit more difficult

In CL, define new readtable, where * ** - + ( and ) has a special
meaning (reader macro characters)


In Emacs Lisp, write the CL reader, a little bit harder to do :-)

    PKY