handling nested data in LISP

Discussion of Common Lisp
Post Reply
ProkhorZhakharov

handling nested data in LISP

Post by ProkhorZhakharov » Thu Aug 20, 2009 1:40 pm

Hi everybody

I have a (UNIX-shell) program with an output like this:

(root
(class living_thing
(spec human
(prop has_legs))
(spec bird (prop has_wings))))

The program spits that kind of code out whenever it has computed its input.
Now I have three problems:

1. How do I feed my program time after time?
2. How do i lookup the data:
here the problems are:
1. I dont know the structures depht (which is variable) 2. I dont know how to retrieve the information once stored because of 1.
3. The set of tags is variable.
3. How to store it in a DB (solved conceptually by my own)

I am WILLED to RTFM but i couldn't find an algorythm in Perl so i'm trying in Lisp now.


Any ideas?

Best regards

PS: Linux on x86

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: handling nested data in LISP

Post by nuntius » Fri Aug 21, 2009 11:47 am

This format looks very much like native lisp; but the example doesn't make it clear whether common lisp, scheme, or another variant.

Here's a simple common-lisp program that reads your data.

Code: Select all

;; create a package to store the shell output
(defpackage :shell-output)
;; read the output from a file
(defun parse-shell-output (filename)
  (with-open-file (infile filename :direction :input)
    (let ((*package* (find-package :shell-output))
           (*readtable* (copy-readtable)))
      (setf (readtable-case *readtable*) :preserve)
      (read infile))))
If you don't need to preserve the distinction between uppercase and lowercase, simply remove the readtable stuff.

Use the standard lisp tools (first, second, ..., nth, car, cdr, mapX, etc.) to access the data structure returned by (parse-shell-output "filename").

Post Reply