[comp.lang.lisp] Reading a string into a list

dkb@cs.brown.edu (Dilip Barman) (10/12/90)

Thanks to you all for helping solve my lexical analyzer problem.
I find LISP much more difficult than, say, PASCAL, for file
I/O and character operations.  Anyway, I read the common LISP
book about varying length strings and readtables and settled on
the approach several of you suggested re: readtables.  For
reference, here is what I did.  Thanks again!!!

(Note:  starred variables are globals - e.g., 
 (defconstant *EOS* 'End-of-sentence))


; Thanks to Eliot Handelman (eliot@phoenix.Princeton.edu) and 
; Tim Moore (moore%cdr.utah.edu@cs.utah.edu) for helping us to
; understand character parsing and the readtable.  Here is how we input
; our strings character-by-character.

; Define a readtable that allows us to handle periods, slashes, and dashes
(defvar lexanalyzers-read-table (copy-readtable))
(set-macro-character #\. #'(lambda (stream char) *EOS*)
                            nil lexanalyzers-read-table)    ; replace period by *EOS*
(set-macro-character #\- #'(lambda (stream char) *DASH*)
                            nil lexanalyzers-read-table)
(set-macro-character #\/ #'(lambda (stream char) *SLASH*)
                            nil lexanalyzers-read-table)


; Define a function which translates a string into a list  dkb 10/3
(defun stringToList (string)
  (let ((words '())
	(index 0)
	(*readtable* lexanalyzers-read-table))
    (loop
     (multiple-value-bind (word next-index)
         (read-from-string string nil nil :start index)
       (setq index next-index)
       (if word
           (push word words)
           (return (nreverse words)))))))



Dilip Barman     dkb@cs.brown.edu
U.S. mail: Brown University                       Home: 40 Everett Avenue
           Dept. of Computer Science, Box 1910          Providence, RI 02906
           Providence, RI 02912 (401)863-7666           (401)521-9731