From: Mark Tarver
Subject: Re: convert to lists
Date: 
Message-ID: <8d12305e-d248-4a73-8628-9b0470040002@s13g2000prd.googlegroups.com>
On 20 Jan, 01:36, Mark Tarver <··········@ukonline.co.uk> wrote:
> On 19 Jan, 17:23, AngelP <··········@gmail.com> wrote:
>
>
>
>
>
> > Hi,
> > I have started with task to convert list  '(1 2 3 4 [ 1 2 3 [ 1 2 ]
> > [ 1 2 ] 3 ]) to a lisp list -
> > '(1 2 3 4 ( 1 2 3 ( 1 2 ) ( 1 2 ) 3 ))
> > I got to this, but it does not work
> > (defun to-lisp (all)
> >   (let ((stack (list (list))))
> >     (dolist (x all)
> >       (print x)
> >       (print stack)
> >       (cond
> >        ((equal (first all) '[) (push (list) stack))
> >        ((equal (first all) ']) (let (( l (pop stack)))
> >                                  (setf (first stack) (append (first stack) l))))
> >        (t (setf (first stack) (list* x (first stack))))))
> >     stack))
>
> > Can you give me a hint?
>
> Actually this is the sort of problem that lends itself to language
> oriented programming.  Here is a solution in Qi-YACC; I used < .. >
> rather than [ ... ] because [ ...] has a special meaning in Qi.
>
> (defcc <paren>
>   < <paren> > <paren1> := [<paren> | <paren1>];
>   <token> <paren> := [<token> | <paren>];
>   <e> := [];)
>
> (defcc <paren1>
>    <paren>;)
>
> (defcc <token>
>   -*- := (if (element? -*- [< >]) #\Escape -*-);)
>
> Some examples;
>
> (30-) (compile <paren> [< 4 < 5 > >])
> [[4 [5]]]
>
> (31-) (compile <paren> [< 4 < 5  >])
> #\Escape    (unbalanced parens - Mark)
>
> (32-) (compile <paren> [< 4 < 5  > >])
> [[4 [5]]]
>
> (33-) (compile <paren> [> < 4 < 5  > >])
> #\Escape (unbalanced parens - Mark)
>
> Mark- Hide quoted text -
>
> - Show quoted text -

It occurred to me that you might want to run the generated Lisp. This
should run directly in Lisp.  The resulting machine-generated Lisp
program (very lightly edited) is:

(DEFUN <paren> (Stream)
 (cases
  (IF (AND (CONSP (FIRST Stream)) (EQ (FIRST (FIRST Stream)) '<))
   (LET
    ((<paren> (<paren> (LIST (REST (FIRST Stream)) (SECOND Stream)))))
    (IF (NOT (failure? <paren>))
     (IF (AND (CONSP (FIRST <paren>)) (EQ (FIRST (FIRST <paren>)) '>))
      (LET
       ((<paren1> (<paren1> (LIST (REST (FIRST <paren>)) (SECOND
<paren>)))))
       (IF (NOT (failure? <paren1>))
        (LIST (FIRST <paren1>) (cons (SECOND <paren>) (SECOND
<paren1>)))
        (LIST NIL #\Escape)))
      (LIST NIL #\Escape))
     (LIST NIL #\Escape)))
   (LIST NIL #\Escape))
  (LET ((<token> (<token> Stream)))
   (IF (NOT (failure? <token>))
    (LET ((<paren> (<paren> <token>)))
     (IF (NOT (failure? <paren>))
      (LIST (FIRST <paren>) (cons (SECOND <token>) (SECOND <paren>)))
      (LIST NIL #\Escape)))
    (LIST NIL #\Escape)))
  (LET ((<e> (<e> Stream)))
   (IF (NOT (failure? <e>)) (LIST (FIRST <e>) NIL) (LIST NIL #
\Escape)))))

(DEFUN <paren1> (Stream)
 (cases
  (LET ((<paren> (<paren> Stream)))
   (IF (NOT (failure? <paren>))
    (LIST (FIRST <paren>) (APPEND (SECOND <paren>) NIL))
    (LIST NIL #\Escape)))))

(DEFUN <token> (Stream)
 (cases
  (IF (CONSP (FIRST Stream))
   (LIST (FIRST (LIST (REST (FIRST Stream)) (SECOND Stream)))
    (IF (MEMBER (CAAR Stream) (cons '< (cons '> NIL))) #\Escape
     (CAAR Stream)))
   (LIST NIL #\Escape))))

" to run under bare Lisp outside Qi - add"

(DEFUN compile (F X)
 (LET ((O (FUNCALL F (LIST X NIL))))
  (IF (OR (failure? O) (NOT (NULL (CAR O)))) #\Escape (CADR O))))

(DEFMACRO cases (&REST X)
  (IF (NULL X)
      '*Failure*
      `(LET ((Y ,(CAR X)))
            (IF (failure? Y)
                ,(CONS 'cases (CDR X))
                Y))))

(SETQ *Failure* '(NIL #\Escape))

(DEFUN <e> (Stream) Stream)

(DEFUN failure? (X) (EQ (CADR X) #\Escape))

Mark