|
|
|
@ -10,12 +10,6 @@
|
|
|
|
|
(lib "class.ss"))
|
|
|
|
|
|
|
|
|
|
(provide compute-LA)
|
|
|
|
|
|
|
|
|
|
(define (list-head l n)
|
|
|
|
|
(cond
|
|
|
|
|
((= 0 n) null)
|
|
|
|
|
(else (cons (car l) (list-head (cdr l) (sub1 n))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; compute-DR: LR0-automaton * grammar -> (trans-key -> term set)
|
|
|
|
|
;; computes for each state, non-term transition pair, the terminals
|
|
|
|
@ -33,38 +27,49 @@
|
|
|
|
|
;; compute-reads:
|
|
|
|
|
;; LR0-automaton * grammar -> (trans-key -> trans-key list)
|
|
|
|
|
(define (compute-reads a g)
|
|
|
|
|
(lambda (tk)
|
|
|
|
|
(let ((r (send a run-automaton (trans-key-st tk) (trans-key-gs tk))))
|
|
|
|
|
(map (lambda (x) (make-trans-key r x))
|
|
|
|
|
(filter (lambda (non-term)
|
|
|
|
|
(and (send g nullable-non-term? non-term)
|
|
|
|
|
(send a run-automaton r non-term)))
|
|
|
|
|
(send g get-non-terms ))))))
|
|
|
|
|
(let ((nullable-non-terms
|
|
|
|
|
(filter (lambda (nt) (send g nullable-non-term? nt))
|
|
|
|
|
(send g get-non-terms))))
|
|
|
|
|
(lambda (tk)
|
|
|
|
|
(let ((r (send a run-automaton (trans-key-st tk) (trans-key-gs tk))))
|
|
|
|
|
(map (lambda (x) (make-trans-key r x))
|
|
|
|
|
(filter (lambda (non-term) (send a run-automaton r non-term))
|
|
|
|
|
nullable-non-terms))))))
|
|
|
|
|
|
|
|
|
|
;; compute-read: LR0-automaton * grammar -> (trans-key -> term set)
|
|
|
|
|
;; output term set is represented in bit-vector form
|
|
|
|
|
(define (compute-read a g)
|
|
|
|
|
(let* ((dr (compute-DR a g))
|
|
|
|
|
(reads (compute-reads a g)))
|
|
|
|
|
(digraph-tk->terml (send a get-mapped-non-term-keys)
|
|
|
|
|
reads
|
|
|
|
|
dr
|
|
|
|
|
(send a get-num-states)
|
|
|
|
|
(send g get-num-terms)
|
|
|
|
|
(send g get-num-non-terms))))
|
|
|
|
|
(time
|
|
|
|
|
(digraph-tk->terml (send a get-mapped-non-term-keys)
|
|
|
|
|
reads
|
|
|
|
|
dr
|
|
|
|
|
(send a get-num-states)
|
|
|
|
|
(send g get-num-terms)
|
|
|
|
|
(send g get-num-non-terms))))
|
|
|
|
|
)
|
|
|
|
|
; ;; run-lr0-backward: lr0-automaton * gram-sym list * kernel * int -> kernel list
|
|
|
|
|
; ;; returns the list of all k such that state k transitions to state start on the
|
|
|
|
|
; ;; transitions in rhs (in order)
|
|
|
|
|
; (define (run-lr0-backward a rhs start num-states)
|
|
|
|
|
; (let loop ((states (list start))
|
|
|
|
|
; (rhs (reverse rhs)))
|
|
|
|
|
; (cond
|
|
|
|
|
; ((null? rhs) states)
|
|
|
|
|
; (else (loop (send a run-automaton-back states (car rhs))
|
|
|
|
|
; (cdr rhs))))))
|
|
|
|
|
|
|
|
|
|
;; run-lr0-backward: lr0-automaton * gram-sym list * kernel * int -> kernel list
|
|
|
|
|
;; gram-sym list * kernel * int -> kernel list
|
|
|
|
|
;; returns the list of all k such that state k transitions to state start on the
|
|
|
|
|
;; transitions in rhs (in order)
|
|
|
|
|
(define (run-lr0-backward a rhs start num-states)
|
|
|
|
|
(define (run-lr0-backward a rhs dot-pos start num-states)
|
|
|
|
|
(let loop ((states (list start))
|
|
|
|
|
(rhs (reverse rhs)))
|
|
|
|
|
(i (sub1 dot-pos)))
|
|
|
|
|
(cond
|
|
|
|
|
((null? rhs) states)
|
|
|
|
|
(else (loop (kernel-list-remove-duplicates
|
|
|
|
|
(send a run-automaton-back states (car rhs))
|
|
|
|
|
num-states)
|
|
|
|
|
(cdr rhs))))))
|
|
|
|
|
((< i 0) states)
|
|
|
|
|
(else (loop (send a run-automaton-back states (vector-ref rhs i))
|
|
|
|
|
(sub1 i))))))
|
|
|
|
|
|
|
|
|
|
;; prod->items-for-include: grammar * prod * non-term -> lr0-item list
|
|
|
|
|
;; returns the list of all (B -> beta . nt gamma) such that prod = (B -> beta nt gamma)
|
|
|
|
@ -91,74 +96,75 @@
|
|
|
|
|
;; and gamma =>* epsilon
|
|
|
|
|
(define (prod-list->items-for-include g prod-list nt)
|
|
|
|
|
(apply append (map (lambda (prod) (prod->items-for-include g prod nt)) prod-list)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; comput-includes: lr0-automaton * grammar -> (trans-key -> trans-key list)
|
|
|
|
|
(define (compute-includes a g)
|
|
|
|
|
(let ((non-terms (send g get-non-terms))
|
|
|
|
|
(num-states (send a get-num-states)))
|
|
|
|
|
(let ((num-states (send a get-num-states))
|
|
|
|
|
(items-for-input-nt (make-vector (send g get-num-non-terms) null)))
|
|
|
|
|
(for-each
|
|
|
|
|
(lambda (input-nt)
|
|
|
|
|
(vector-set! items-for-input-nt (non-term-index input-nt)
|
|
|
|
|
(prod-list->items-for-include g (send g get-prods) input-nt)))
|
|
|
|
|
(send g get-non-terms))
|
|
|
|
|
(lambda (tk)
|
|
|
|
|
(let ((goal-state (trans-key-st tk))
|
|
|
|
|
(non-term (trans-key-gs tk)))
|
|
|
|
|
(apply append
|
|
|
|
|
(map (lambda (B)
|
|
|
|
|
(map (lambda (state)
|
|
|
|
|
(make-trans-key state B))
|
|
|
|
|
(kernel-list-remove-duplicates
|
|
|
|
|
(let ((items (prod-list->items-for-include g (send g get-prods-for-non-term B) non-term)))
|
|
|
|
|
(apply append
|
|
|
|
|
(map (lambda (item)
|
|
|
|
|
(let ((rhs (prod-rhs (item-prod item))))
|
|
|
|
|
(run-lr0-backward a
|
|
|
|
|
(list-head (vector->list rhs)
|
|
|
|
|
(- (vector-length rhs)
|
|
|
|
|
(item-dot-pos item)))
|
|
|
|
|
goal-state
|
|
|
|
|
num-states)))
|
|
|
|
|
items)))
|
|
|
|
|
num-states)))
|
|
|
|
|
non-terms))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; comput-includes: lr0-automaton * grammar -> (trans-key -> trans-key list)
|
|
|
|
|
; (define (compute-includes a g)
|
|
|
|
|
; (let* ((non-terms (send g get-non-terms))
|
|
|
|
|
; (num-states (vector-length (send a get-states)))
|
|
|
|
|
; (num-non-terms (length non-terms))
|
|
|
|
|
; (includes (make-array2d num-states num-non-terms null)))
|
|
|
|
|
; (send a for-each-state
|
|
|
|
|
; (lambda (state)
|
|
|
|
|
; (for-each
|
|
|
|
|
; (lambda (non-term)
|
|
|
|
|
; (for-each
|
|
|
|
|
; (lambda (prod)
|
|
|
|
|
; (let loop ((i (make-item prod 0))
|
|
|
|
|
; (p state))
|
|
|
|
|
; (if (and p i)
|
|
|
|
|
; (let* ((next-sym (sym-at-dot i))
|
|
|
|
|
; (new-i (move-dot-right i)))
|
|
|
|
|
; (if (and (non-term? next-sym)
|
|
|
|
|
; (send g nullable-after-dot? new-i))
|
|
|
|
|
; (array2d-add! includes
|
|
|
|
|
; (kernel-index p)
|
|
|
|
|
; (gram-sym-index next-sym)
|
|
|
|
|
; (make-trans-key state non-term)))
|
|
|
|
|
; (if next-sym
|
|
|
|
|
; (loop new-i
|
|
|
|
|
; (send a run-automaton p next-sym)))))))
|
|
|
|
|
; (send g get-prods-for-non-term non-term)))
|
|
|
|
|
; non-terms)))
|
|
|
|
|
(let* ((goal-state (trans-key-st tk))
|
|
|
|
|
(non-term (trans-key-gs tk))
|
|
|
|
|
(items (vector-ref items-for-input-nt (non-term-index non-term))))
|
|
|
|
|
(trans-key-list-remove-dups
|
|
|
|
|
(apply append
|
|
|
|
|
(map (lambda (item)
|
|
|
|
|
(let* ((prod (item-prod item))
|
|
|
|
|
(rhs (prod-rhs prod))
|
|
|
|
|
(lhs (prod-lhs prod)))
|
|
|
|
|
(map (lambda (state)
|
|
|
|
|
(make-trans-key state lhs))
|
|
|
|
|
(run-lr0-backward a
|
|
|
|
|
rhs
|
|
|
|
|
(item-dot-pos item)
|
|
|
|
|
goal-state
|
|
|
|
|
num-states))))
|
|
|
|
|
items)))))))
|
|
|
|
|
|
|
|
|
|
; ;; compute-includes: lr0-automaton * grammar -> (trans-key -> trans-key list)
|
|
|
|
|
; (define (compute-includes a g)
|
|
|
|
|
; (let* ((non-terms (send g get-non-terms))
|
|
|
|
|
; (num-states (vector-length (send a get-states)))
|
|
|
|
|
; (num-non-terms (length non-terms))
|
|
|
|
|
; (includes (make-array2d num-states num-non-terms null)))
|
|
|
|
|
; (send a for-each-state
|
|
|
|
|
; (lambda (state)
|
|
|
|
|
; (for-each
|
|
|
|
|
; (lambda (non-term)
|
|
|
|
|
; (for-each
|
|
|
|
|
; (lambda (prod)
|
|
|
|
|
; (let loop ((i (make-item prod 0))
|
|
|
|
|
; (p state))
|
|
|
|
|
; (if (and p i)
|
|
|
|
|
; (let* ((next-sym (sym-at-dot i))
|
|
|
|
|
; (new-i (move-dot-right i)))
|
|
|
|
|
; (if (and (non-term? next-sym)
|
|
|
|
|
; (send g nullable-after-dot? new-i))
|
|
|
|
|
; (array2d-add! includes
|
|
|
|
|
; (kernel-index p)
|
|
|
|
|
; (gram-sym-index next-sym)
|
|
|
|
|
; (make-trans-key state non-term)))
|
|
|
|
|
; (if next-sym
|
|
|
|
|
; (loop new-i
|
|
|
|
|
; (send a run-automaton p next-sym)))))))
|
|
|
|
|
; (send g get-prods-for-non-term non-term)))
|
|
|
|
|
; non-terms)))
|
|
|
|
|
;
|
|
|
|
|
; (lambda (tk)
|
|
|
|
|
; (array2d-ref includes
|
|
|
|
|
; (kernel-index (trans-key-st tk))
|
|
|
|
|
; (gram-sym-index (trans-key-gs tk))))))
|
|
|
|
|
;
|
|
|
|
|
; compute-lookback: lr0-automaton * grammar -> (kernel * proc -> trans-key list)
|
|
|
|
|
; (lambda (tk)
|
|
|
|
|
; (array2d-ref includes
|
|
|
|
|
; (kernel-index (trans-key-st tk))
|
|
|
|
|
; (gram-sym-index (trans-key-gs tk))))))
|
|
|
|
|
|
|
|
|
|
;; compute-lookback: lr0-automaton * grammar -> (kernel * proc -> trans-key list)
|
|
|
|
|
(define (compute-lookback a g)
|
|
|
|
|
(let ((num-states (send a get-num-states)))
|
|
|
|
|
(lambda (state prod)
|
|
|
|
|
(map (lambda (k) (make-trans-key k (prod-lhs prod)))
|
|
|
|
|
(run-lr0-backward a (vector->list (prod-rhs prod)) state num-states)))))
|
|
|
|
|
(run-lr0-backward a (prod-rhs prod) (vector-length (prod-rhs prod)) state num-states)))))
|
|
|
|
|
|
|
|
|
|
;; compute-follow: LR0-automaton * grammar -> (trans-key -> term set)
|
|
|
|
|
;; output term set is represented in bit-vector form
|
|
|
|
@ -176,7 +182,7 @@
|
|
|
|
|
(define (compute-LA a g)
|
|
|
|
|
(let* ((includes (compute-includes a g))
|
|
|
|
|
(lookback (compute-lookback a g))
|
|
|
|
|
(follow (compute-follow a g includes)))
|
|
|
|
|
(follow (compute-follow a g includes)))
|
|
|
|
|
(lambda (k p)
|
|
|
|
|
(let* ((l (lookback k p))
|
|
|
|
|
(f (map follow l)))
|
|
|
|
|