Lisp HUG Maillist Archive

graph-pane vs. parse tree

I want to display a parse tree as a graph, but the
results I get using capi:graph-pane confuse me.

The tree is of the form '((= (+ (- a b) c))).

I get a tree, where the root node displays as the full
expression "(= (+ (- a b) c))" using the code below. 
I want each node to display only the "token" (the car
of the list, e.g. "=", "+", etc) but the docs don't
give me a hint on how to control the format of the
node text (or, I misunderstand how to use graph-pane).

Thanks
pt (please reply to tarvydas@attcanada.ca)

(defun node-children (node)
  (if (listp node)
      (rest node)
    (if (eq 'lsym (type-of node))
        (list (lsym-name node))
      (if (eq 'ltype (type-of node))
          (list (ltype-kind node) (ltype-name node))
        nil))))

(defun draw-graph (tree)
  (capi:contain (make-instance 'capi:graph-pane
                               :roots tree
                               :children-function
'node-children
                               :layout-function
:top-down)
                :best-height 400
                :best-width 400))

to test: (draw-graph '((= (+ (- a b) c))) )


______________________________________________________________________ 
Find, Connect, Date! http://personals.yahoo.ca


Re: graph-pane vs. parse tree

> From: "Paul Tarvydas" <paultarvydas@yahoo.ca>
> To: <lisp-hug@xanalys.com>
> Sent: Wednesday, February 27, 2002 2:44 PM
> Subject: graph-pane vs. parse tree
>
> I want to display a parse tree as a graph, but the
> results I get using capi:graph-pane confuse me.
>
> The tree is of the form '((= (+ (- a b) c))).
>
> I get a tree, where the root node displays as the full
> expression "(= (+ (- a b) c))" using the code below.
> I want each node to display only the "token" (the car
> of the list, e.g. "=", "+", etc) but the docs don't
> give me a hint on how to control the format of the
> node text (or, I misunderstand how to use graph-pane).

CAPI:GRAPH-PANE inherits from CAPI:COLLECTION, and you can specify a
print-function for a CAPI:COLLECTION:

So this should do what you want:


(defun node-children (node)
  (if (listp node)
      (rest node)
    (if (eq 'lsym (type-of node))
        (list (lsym-name node))
      (if (eq 'ltype (type-of node))
          (list (ltype-kind node) (ltype-name node))
        nil))))

(defun node-print (node)
  (format nil "~S"
          (if (listp node)
              (car node)
            (if (eq 'lsym (type-of node))
                'replace-this-with-something-appropriate
              (if (eq 'ltype (type-of node))
                  'replace-this-with-something-appropriate
                node)))))

(defun draw-graph (tree)
  (capi:contain (make-instance 'capi:graph-pane
                               :roots tree
                               :print-function 'node-print
                               :children-function 'node-children
                               :layout-function :top-down)
                :best-height 400
                :best-width 400))

#|
(draw-graph '((= (+ (- a b) c))))
|#




Updated at: 2020-12-10 09:02 UTC