0

我正在尝试在方案中生成符号表,但我被困在 set-symbol 函数上。该数字对应于代码的块级别或“范围”。

First symbol it reads in
((c class 0))
Next symbols 
(((c class 0) (a int 0) (b float 0)))
We read a bracket and read the next variables to a new scope.
(((a char 1) (d int 1)) ((c class 0) (a int 0) (b float 0)))
We leave that scope and "pop the stack".
(((c class 0) (a int 0) (b float 0)))

如何始终添加到范围内第一个列表的最深列表?

4

1 回答 1

1

我怀疑你最好使用不同的表示。众多的其中之一是:

(define (make-symbol-table parent alist)
  `(SYMBOL-TABLE ,parent ,@alist))
(define symbol-table-parent cadr)
(define symbol-table-alist  cddr)

(define (symbol-table-depth table)
  (let ((parent (symbol-table-parent table)))
    (if (not parent)
        1
        (+ 1 (symbol-table-depth parent))))

(define (symbol-table-lookup table name)
  (cond ((assoc name (symbol-table-alist table)) => cdr)
        (else (let ((parent (symbol-table-parent table)))
                (and parent (symbol-table-lookup parent name))))))
于 2013-03-23T18:42:11.880 回答