|
|
@ -11,20 +11,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
(provide compute-LA)
|
|
|
|
(provide compute-LA)
|
|
|
|
|
|
|
|
|
|
|
|
(define (array2d-add! a i1 i2 v)
|
|
|
|
|
|
|
|
(let ((old (array2d-ref a i1 i2)))
|
|
|
|
|
|
|
|
(array2d-set! a i1 i2 (cons v old))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; compute-DR: LR0-automaton * grammar -> (trans-key -> term list)
|
|
|
|
;; compute-DR: LR0-automaton * grammar -> (trans-key -> term list)
|
|
|
|
;; computes for each state, non-term transition pair, the terminals
|
|
|
|
;; computes for each state, non-term transition pair, the terminals
|
|
|
|
;; which can transition out of the resulting state
|
|
|
|
;; which can transition out of the resulting state
|
|
|
|
(define (compute-DR a g)
|
|
|
|
(define (compute-DR a g)
|
|
|
|
(lambda (tk)
|
|
|
|
(lambda (tk)
|
|
|
|
(let ((r (run-automaton (trans-key-st tk) (trans-key-gs tk) a)))
|
|
|
|
(let ((r (run-automaton (trans-key-st tk) (trans-key-gs tk) a)))
|
|
|
|
|
|
|
|
(term-list->bit-vector
|
|
|
|
(filter
|
|
|
|
(filter
|
|
|
|
(lambda (term)
|
|
|
|
(lambda (term)
|
|
|
|
(run-automaton r term a))
|
|
|
|
(run-automaton r term a))
|
|
|
|
(grammar-terms g)))))
|
|
|
|
(grammar-terms g))))))
|
|
|
|
|
|
|
|
|
|
|
|
;; compute-reads:
|
|
|
|
;; compute-reads:
|
|
|
|
;; LR0-automaton * grammar -> (trans-key -> trans-key list)
|
|
|
|
;; LR0-automaton * grammar -> (trans-key -> trans-key list)
|
|
|
@ -41,12 +38,11 @@
|
|
|
|
(define (compute-read a g)
|
|
|
|
(define (compute-read a g)
|
|
|
|
(let* ((dr (compute-DR a g))
|
|
|
|
(let* ((dr (compute-DR a g))
|
|
|
|
(reads (compute-reads a g)))
|
|
|
|
(reads (compute-reads a g)))
|
|
|
|
(digraph (filter (lambda (x) (non-term? (trans-key-gs x)))
|
|
|
|
(digraph (get-mapped-lr0-non-term-keys a)
|
|
|
|
(hash-table-map (lr0-transitions a) (lambda (k v) k)))
|
|
|
|
|
|
|
|
reads
|
|
|
|
reads
|
|
|
|
dr
|
|
|
|
dr
|
|
|
|
(union term<?)
|
|
|
|
bitwise-ior
|
|
|
|
null)))
|
|
|
|
(lambda () 0))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; comput-includes-and-lookback:
|
|
|
|
;; comput-includes-and-lookback:
|
|
|
@ -60,7 +56,6 @@
|
|
|
|
(lookback (make-array2d num-states
|
|
|
|
(lookback (make-array2d num-states
|
|
|
|
(grammar-num-prods g)
|
|
|
|
(grammar-num-prods g)
|
|
|
|
null)))
|
|
|
|
null)))
|
|
|
|
|
|
|
|
|
|
|
|
(for-each-state
|
|
|
|
(for-each-state
|
|
|
|
(lambda (state)
|
|
|
|
(lambda (state)
|
|
|
|
(for-each
|
|
|
|
(for-each
|
|
|
@ -70,26 +65,21 @@
|
|
|
|
(let loop ((i (make-item prod 0))
|
|
|
|
(let loop ((i (make-item prod 0))
|
|
|
|
(p state))
|
|
|
|
(p state))
|
|
|
|
(if (and p i)
|
|
|
|
(if (and p i)
|
|
|
|
(begin
|
|
|
|
(let ((new-i (move-dot-right i))
|
|
|
|
(if (and (non-term? (sym-at-dot i))
|
|
|
|
(next-sym (sym-at-dot i)))
|
|
|
|
(nullable-after-dot? (move-dot-right i)
|
|
|
|
(if (and (non-term? next-sym)
|
|
|
|
g))
|
|
|
|
(nullable-after-dot? new-i g))
|
|
|
|
(array2d-add! includes
|
|
|
|
(array2d-add! includes
|
|
|
|
(kernel-index p)
|
|
|
|
(kernel-index p)
|
|
|
|
(gram-sym-index
|
|
|
|
(gram-sym-index next-sym)
|
|
|
|
(sym-at-dot i))
|
|
|
|
(make-trans-key state non-term)))
|
|
|
|
(make-trans-key
|
|
|
|
(if (not new-i)
|
|
|
|
state
|
|
|
|
|
|
|
|
non-term)))
|
|
|
|
|
|
|
|
(if (not (move-dot-right i))
|
|
|
|
|
|
|
|
(array2d-add! lookback
|
|
|
|
(array2d-add! lookback
|
|
|
|
(kernel-index p)
|
|
|
|
(kernel-index p)
|
|
|
|
(prod-index prod)
|
|
|
|
(prod-index prod)
|
|
|
|
(make-trans-key
|
|
|
|
(make-trans-key state non-term)))
|
|
|
|
state
|
|
|
|
(loop new-i
|
|
|
|
non-term)))
|
|
|
|
(run-automaton p next-sym a))))))
|
|
|
|
(loop (move-dot-right i)
|
|
|
|
|
|
|
|
(run-automaton p (sym-at-dot i) a))))))
|
|
|
|
|
|
|
|
(get-nt-prods g non-term)))
|
|
|
|
(get-nt-prods g non-term)))
|
|
|
|
non-terms))
|
|
|
|
non-terms))
|
|
|
|
a)
|
|
|
|
a)
|
|
|
@ -106,21 +96,20 @@
|
|
|
|
;; compute-follow: LR0-automaton * grammar -> (trans-key -> term list)
|
|
|
|
;; compute-follow: LR0-automaton * grammar -> (trans-key -> term list)
|
|
|
|
(define (compute-follow a g includes)
|
|
|
|
(define (compute-follow a g includes)
|
|
|
|
(let ((read (compute-read a g)))
|
|
|
|
(let ((read (compute-read a g)))
|
|
|
|
(digraph (filter (lambda (x) (non-term? (trans-key-gs x)))
|
|
|
|
(digraph (get-mapped-lr0-non-term-keys a)
|
|
|
|
(hash-table-map (lr0-transitions a) (lambda (k v) k)))
|
|
|
|
|
|
|
|
includes
|
|
|
|
includes
|
|
|
|
read
|
|
|
|
read
|
|
|
|
(union term<?)
|
|
|
|
bitwise-ior
|
|
|
|
null)))
|
|
|
|
(lambda () 0))))
|
|
|
|
|
|
|
|
|
|
|
|
;; compute-LA: LR0-automaton * grammar -> (kernel * prod -> term list)
|
|
|
|
;; compute-LA: LR0-automaton * grammar -> (kernel * prod -> term list)
|
|
|
|
(define (compute-LA a g)
|
|
|
|
(define (compute-LA a g)
|
|
|
|
(let-values (((includes lookback) (compute-includes-and-lookback a g)))
|
|
|
|
(let-values (((includes lookback) (time (compute-includes-and-lookback a g))))
|
|
|
|
(let ((follow (compute-follow a g includes)))
|
|
|
|
(let ((follow (time (compute-follow a g includes))))
|
|
|
|
(lambda (k p)
|
|
|
|
(lambda (k p)
|
|
|
|
(let* ((l (lookback k p))
|
|
|
|
(let* ((l (lookback k p))
|
|
|
|
(f (map follow l)))
|
|
|
|
(f (map follow l)))
|
|
|
|
(apply append f))))))
|
|
|
|
(apply bitwise-ior (cons 0 f)))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (print-DR dr a g)
|
|
|
|
(define (print-DR dr a g)
|
|
|
|