;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; ;;; Code for using a DFA with file I/O. ;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun accept-first-from-file (dfa infile) "Call accept-first-from-stream, reading data from infile." (with-open-file (instream infile) (accept-first-from-stream dfa instream))) (defun accept-first-from-stream (dfa stream) "Reads inputs from stream and returns the first one that the dfa accepts. Returns NIL if end-of-file is reached without accepting any inputs. " (let ((input (read stream nil :EOF))) (format t "~&Input: ~S~%" input) (cond ((eq input :EOF) nil) ((accepts input dfa) input) (t (accept-first-from-stream dfa stream))))) (defun filter-list-using-dfa (inputs dfafile outfile) "Runs list of inputs through the DFA in dfafile, writing out the ones the DFA accepts to outfile. " (with-open-file (instream dfafile) (let ((dfa (read instream))) (with-open-file (outstream outfile :direction :output) (mapcar #'(lambda (x) (if (accepts x dfa) (format outstream "~S~%" x))) inputs))))) (defun it-filter-list-using-dfa (inputs dfafile outfile) "Same as filter-list-using-dfa but iterative, using dolist." (with-open-file (instream dfafile) (let ((dfa (read instream))) (with-open-file (outstream outfile :direction :output) (dolist (x inputs) (if (accepts x dfa) (format outstream "~S~%" x))))))) (defun filter-file-using-dfa (dfafile infile outfile) "Filters the inputs in infile, writing to outfile only those accepted by the DFA in dfafile. Uses 'do' for iteration. " )