|
|
@ -4,7 +4,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
(require "grammar.ss"
|
|
|
|
(require "grammar.ss"
|
|
|
|
"graph.ss"
|
|
|
|
"graph.ss"
|
|
|
|
"array2d.ss"
|
|
|
|
|
|
|
|
(lib "list.ss")
|
|
|
|
(lib "list.ss")
|
|
|
|
(lib "class.ss"))
|
|
|
|
(lib "class.ss"))
|
|
|
|
|
|
|
|
|
|
|
@ -43,41 +42,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; build-transition-table : int (listof (cons/c trans-key X) ->
|
|
|
|
|
|
|
|
;; (vectorof (symbol X hashtable))
|
|
|
|
|
|
|
|
(define (build-transition-table num-states assoc)
|
|
|
|
|
|
|
|
(let ((transitions (make-vector num-states #f)))
|
|
|
|
|
|
|
|
(let loop ((i (sub1 (vector-length transitions))))
|
|
|
|
|
|
|
|
(when (>= i 0)
|
|
|
|
|
|
|
|
(vector-set! transitions i (make-hash-table))
|
|
|
|
|
|
|
|
(loop (sub1 i))))
|
|
|
|
|
|
|
|
(for-each
|
|
|
|
|
|
|
|
(lambda (trans-key/kernel)
|
|
|
|
|
|
|
|
(let ((tk (car trans-key/kernel)))
|
|
|
|
|
|
|
|
(hash-table-put! (vector-ref transitions (kernel-index (trans-key-st tk)))
|
|
|
|
|
|
|
|
(gram-sym-symbol (trans-key-gs tk))
|
|
|
|
|
|
|
|
(cdr trans-key/kernel))))
|
|
|
|
|
|
|
|
assoc)
|
|
|
|
|
|
|
|
transitions))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; reverse-assoc : (listof (cons/c trans-key? kernel?)) ->
|
|
|
|
|
|
|
|
;; (listof (cons/c trans-key? (listof kernel?)))
|
|
|
|
|
|
|
|
(define (reverse-assoc assoc)
|
|
|
|
|
|
|
|
(let ((reverse-hash (make-hash-table 'equal))
|
|
|
|
|
|
|
|
(hash-table-add!
|
|
|
|
|
|
|
|
(lambda (ht k v)
|
|
|
|
|
|
|
|
(hash-table-put! ht k (cons v (hash-table-get ht k (lambda () null)))))))
|
|
|
|
|
|
|
|
(for-each
|
|
|
|
|
|
|
|
(lambda (trans-key/kernel)
|
|
|
|
|
|
|
|
(let ((tk (car trans-key/kernel)))
|
|
|
|
|
|
|
|
(hash-table-add! reverse-hash
|
|
|
|
|
|
|
|
(make-trans-key (cdr trans-key/kernel)
|
|
|
|
|
|
|
|
(trans-key-gs tk))
|
|
|
|
|
|
|
|
(trans-key-st tk))))
|
|
|
|
|
|
|
|
assoc)
|
|
|
|
|
|
|
|
(hash-table-map reverse-hash cons)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; kernel-list-remove-duplicates
|
|
|
|
;; kernel-list-remove-duplicates
|
|
|
|
;; LR0-automaton = object of class lr0%
|
|
|
|
;; LR0-automaton = object of class lr0%
|
|
|
|
(define lr0%
|
|
|
|
(define lr0%
|
|
|
|
(class object%
|
|
|
|
(class object%
|
|
|
|
(super-instantiate ())
|
|
|
|
(super-instantiate ())
|
|
|
|
;; Hash tables that map a trans-keys to a kernel
|
|
|
|
;; term-assoc : (listof (cons/c trans-key? kernel?))
|
|
|
|
(init term-hash non-term-hash)
|
|
|
|
;; non-term-assoc : (listof (cons/c trans-key? kernel?))
|
|
|
|
(init-field states epsilons num-terms num-non-terms)
|
|
|
|
;; states : (vectorof kernel?)
|
|
|
|
|
|
|
|
;; epsilons : ???
|
|
|
|
|
|
|
|
(init-field term-assoc non-term-assoc states epsilons)
|
|
|
|
|
|
|
|
|
|
|
|
(define term-transitions (make-lr0-table term-hash (vector-length states) num-terms #f))
|
|
|
|
(define transitions (build-transition-table (vector-length states)
|
|
|
|
(define non-term-transitions (make-lr0-table non-term-hash (vector-length states) num-non-terms #f))
|
|
|
|
(append term-assoc non-term-assoc)))
|
|
|
|
|
|
|
|
|
|
|
|
(define reverse-term-hash (reverse-hash term-hash))
|
|
|
|
(define reverse-term-assoc (reverse-assoc term-assoc))
|
|
|
|
(define reverse-non-term-hash (reverse-hash non-term-hash))
|
|
|
|
(define reverse-non-term-assoc (reverse-assoc non-term-assoc))
|
|
|
|
(define reverse-term-transitions (make-lr0-table reverse-term-hash (vector-length states) num-terms null))
|
|
|
|
(define reverse-transitions
|
|
|
|
(define reverse-non-term-transitions (make-lr0-table reverse-non-term-hash (vector-length states) num-non-terms null))
|
|
|
|
(build-transition-table (vector-length states)
|
|
|
|
|
|
|
|
(append reverse-term-assoc reverse-non-term-assoc)))
|
|
|
|
|
|
|
|
|
|
|
|
(define mapped-non-terms
|
|
|
|
(define mapped-non-terms (map car non-term-assoc))
|
|
|
|
(hash-table-map non-term-hash (lambda (k v) k)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define reverse-mapped-non-terms
|
|
|
|
|
|
|
|
(hash-table-map reverse-non-term-hash (lambda (k v) k)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/public (get-mapped-non-term-keys)
|
|
|
|
(define/public (get-mapped-non-term-keys)
|
|
|
|
mapped-non-terms)
|
|
|
|
mapped-non-terms)
|
|
|
|
|
|
|
|
|
|
|
|
(define/public (get-states)
|
|
|
|
|
|
|
|
states)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/public (get-num-states)
|
|
|
|
(define/public (get-num-states)
|
|
|
|
(vector-length states))
|
|
|
|
(vector-length states))
|
|
|
|
|
|
|
|
|
|
|
|
(define/public (get-epsilon-trans)
|
|
|
|
(define/public (get-epsilon-trans)
|
|
|
|
epsilons)
|
|
|
|
epsilons)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/public (get-transitions)
|
|
|
|
|
|
|
|
(append term-assoc non-term-assoc))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; for-each-state : (state ->) ->
|
|
|
|
;; Iteration over the states in an automaton
|
|
|
|
;; Iteration over the states in an automaton
|
|
|
|
(define/public (for-each-state f)
|
|
|
|
(define/public (for-each-state f)
|
|
|
|
(let ((num-states (vector-length states)))
|
|
|
|
(let ((num-states (vector-length states)))
|
|
|
@ -87,46 +121,24 @@
|
|
|
|
(f (vector-ref states i))
|
|
|
|
(f (vector-ref states i))
|
|
|
|
(loop (add1 i)))))))
|
|
|
|
(loop (add1 i)))))))
|
|
|
|
|
|
|
|
|
|
|
|
;; run-automaton: kernel * gram-sym -> kernel | #f
|
|
|
|
;; run-automaton: kernel? gram-sym? -> (union kernel #f)
|
|
|
|
;; returns the state that the transition trans-key provides or #f
|
|
|
|
;; returns the state reached from state k on input s, or #f when k
|
|
|
|
;; if there is no such state
|
|
|
|
;; has no transition on s
|
|
|
|
(define/public (run-automaton k s)
|
|
|
|
(define/public (run-automaton k s)
|
|
|
|
(if (term? s)
|
|
|
|
(hash-table-get (vector-ref transitions (kernel-index k))
|
|
|
|
(array2d-ref term-transitions (kernel-index k) (term-index s))
|
|
|
|
(gram-sym-symbol s)
|
|
|
|
(array2d-ref non-term-transitions (kernel-index k) (non-term-index s))))
|
|
|
|
(lambda () #f)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; run-automaton-back : (listof kernel?) gram-sym? -> (listof kernel)
|
|
|
|
|
|
|
|
;; returns the list of states that can reach k by transitioning on s.
|
|
|
|
(define/public (run-automaton-back k s)
|
|
|
|
(define/public (run-automaton-back k s)
|
|
|
|
(apply append
|
|
|
|
(apply append
|
|
|
|
(if (term? s)
|
|
|
|
(map
|
|
|
|
(map (lambda (k)
|
|
|
|
(lambda (k)
|
|
|
|
(array2d-ref reverse-term-transitions (kernel-index k) (term-index s)))
|
|
|
|
(hash-table-get (vector-ref reverse-transitions (kernel-index k))
|
|
|
|
k)
|
|
|
|
(gram-sym-symbol s)
|
|
|
|
(map (lambda (k)
|
|
|
|
(lambda () null)))
|
|
|
|
(array2d-ref reverse-non-term-transitions (kernel-index k) (non-term-index s)))
|
|
|
|
k)))))
|
|
|
|
k))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (make-lr0-table auto-hash states syms def)
|
|
|
|
|
|
|
|
(let ((t (make-array2d states syms def)))
|
|
|
|
|
|
|
|
(hash-table-map auto-hash
|
|
|
|
|
|
|
|
(lambda (k v)
|
|
|
|
|
|
|
|
(array2d-set! t
|
|
|
|
|
|
|
|
(kernel-index (trans-key-st k))
|
|
|
|
|
|
|
|
(gram-sym-index (trans-key-gs k))
|
|
|
|
|
|
|
|
v)))
|
|
|
|
|
|
|
|
t))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (reverse-hash hash)
|
|
|
|
|
|
|
|
(let ((reverse-hash (make-hash-table 'equal))
|
|
|
|
|
|
|
|
(hash-table-add!
|
|
|
|
|
|
|
|
(lambda (ht k v)
|
|
|
|
|
|
|
|
(hash-table-put! ht k (cons v (hash-table-get ht k (lambda () null)))))))
|
|
|
|
|
|
|
|
(hash-table-for-each hash
|
|
|
|
|
|
|
|
(lambda (k v)
|
|
|
|
|
|
|
|
(hash-table-add! reverse-hash
|
|
|
|
|
|
|
|
(make-trans-key v (trans-key-gs k))
|
|
|
|
|
|
|
|
(trans-key-st k))))
|
|
|
|
|
|
|
|
reverse-hash))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (union comp<?)
|
|
|
|
(define (union comp<?)
|
|
|
|
(letrec ((union
|
|
|
|
(letrec ((union
|
|
|
@ -153,27 +165,14 @@
|
|
|
|
(kernel-items k))
|
|
|
|
(kernel-items k))
|
|
|
|
"}")))
|
|
|
|
"}")))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (add-lr0-transition! ttable nttable key value)
|
|
|
|
|
|
|
|
(hash-table-put!
|
|
|
|
|
|
|
|
(if (term? (trans-key-gs key))
|
|
|
|
|
|
|
|
ttable
|
|
|
|
|
|
|
|
nttable)
|
|
|
|
|
|
|
|
key
|
|
|
|
|
|
|
|
value))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; build-LR0-automaton: grammar -> LR0-automaton
|
|
|
|
;; build-LR0-automaton: grammar -> LR0-automaton
|
|
|
|
;; Constructs the kernels of the sets of LR(0) items of g
|
|
|
|
;; Constructs the kernels of the sets of LR(0) items of g
|
|
|
|
(define (build-lr0-automaton grammar)
|
|
|
|
(define (build-lr0-automaton grammar)
|
|
|
|
; (printf "LR(0) automaton:~n")
|
|
|
|
; (printf "LR(0) automaton:~n")
|
|
|
|
(letrec (
|
|
|
|
(letrec (
|
|
|
|
(terms (list->vector (send grammar get-terms)))
|
|
|
|
|
|
|
|
(non-terms (list->vector (send grammar get-non-terms)))
|
|
|
|
|
|
|
|
(num-non-terms (send grammar get-num-non-terms))
|
|
|
|
|
|
|
|
(num-gram-syms (+ num-non-terms (send grammar get-num-terms)))
|
|
|
|
|
|
|
|
(epsilons (make-hash-table 'equal))
|
|
|
|
(epsilons (make-hash-table 'equal))
|
|
|
|
|
|
|
|
(grammar-symbols (append (send grammar get-non-terms)
|
|
|
|
|
|
|
|
(send grammar get-terms)))
|
|
|
|
;; first-non-term: non-term -> non-term list
|
|
|
|
;; first-non-term: non-term -> non-term list
|
|
|
|
;; given a non-terminal symbol C, return those non-terminal
|
|
|
|
;; given a non-terminal symbol C, return those non-terminal
|
|
|
|
;; symbols A s.t. C -> An for some string of terminals and
|
|
|
|
;; symbols A s.t. C -> An for some string of terminals and
|
|
|
@ -218,8 +217,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; maps trans-keys to kernels
|
|
|
|
;; maps trans-keys to kernels
|
|
|
|
(automaton-term (make-hash-table 'equal))
|
|
|
|
(automaton-term null)
|
|
|
|
(automaton-non-term (make-hash-table 'equal))
|
|
|
|
(automaton-non-term null)
|
|
|
|
|
|
|
|
|
|
|
|
;; keeps the kernels we have seen, so we can have a unique
|
|
|
|
;; keeps the kernels we have seen, so we can have a unique
|
|
|
|
;; list for each kernel
|
|
|
|
;; list for each kernel
|
|
|
@ -235,12 +234,11 @@
|
|
|
|
(goto
|
|
|
|
(goto
|
|
|
|
(lambda (kernel)
|
|
|
|
(lambda (kernel)
|
|
|
|
(let (
|
|
|
|
(let (
|
|
|
|
;; maps each gram-syms to a list of items
|
|
|
|
;; maps a gram-syms to a list of items
|
|
|
|
|
|
|
|
(table (make-hash-table))
|
|
|
|
(table (make-vector num-gram-syms null))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; add-item!:
|
|
|
|
;; add-item!:
|
|
|
|
;; (item list) vector * item ->
|
|
|
|
;; (symbol (listof item) hashtable) item? ->
|
|
|
|
;; adds i into the table grouped with the grammar
|
|
|
|
;; adds i into the table grouped with the grammar
|
|
|
|
;; symbol following its dot
|
|
|
|
;; symbol following its dot
|
|
|
|
(add-item!
|
|
|
|
(add-item!
|
|
|
@ -248,16 +246,13 @@
|
|
|
|
(let ((gs (sym-at-dot i)))
|
|
|
|
(let ((gs (sym-at-dot i)))
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
(gs
|
|
|
|
(gs
|
|
|
|
(let* ((add (if (term? gs)
|
|
|
|
(let ((already
|
|
|
|
num-non-terms
|
|
|
|
(hash-table-get table
|
|
|
|
0))
|
|
|
|
(gram-sym-symbol gs)
|
|
|
|
(already
|
|
|
|
(lambda () null))))
|
|
|
|
(vector-ref table
|
|
|
|
(unless (member i already)
|
|
|
|
(+ add
|
|
|
|
(hash-table-put! table
|
|
|
|
(gram-sym-index gs)))))
|
|
|
|
(gram-sym-symbol gs)
|
|
|
|
(if (not (member i already))
|
|
|
|
|
|
|
|
(vector-set! table
|
|
|
|
|
|
|
|
(+ add (gram-sym-index gs))
|
|
|
|
|
|
|
|
(cons i already)))))
|
|
|
|
(cons i already)))))
|
|
|
|
((= 0 (vector-length (prod-rhs (item-prod i))))
|
|
|
|
((= 0 (vector-length (prod-rhs (item-prod i))))
|
|
|
|
(let ((current (hash-table-get epsilons
|
|
|
|
(let ((current (hash-table-get epsilons
|
|
|
@ -301,34 +296,34 @@
|
|
|
|
new-kernel
|
|
|
|
new-kernel
|
|
|
|
k)
|
|
|
|
k)
|
|
|
|
k)))))
|
|
|
|
k)))))
|
|
|
|
(add-lr0-transition! automaton-term automaton-non-term
|
|
|
|
(cond
|
|
|
|
(make-trans-key kernel gs)
|
|
|
|
((term? gs)
|
|
|
|
|
|
|
|
(set! automaton-term (cons (cons (make-trans-key kernel gs)
|
|
|
|
|
|
|
|
unique-kernel)
|
|
|
|
|
|
|
|
automaton-term)))
|
|
|
|
|
|
|
|
(else
|
|
|
|
|
|
|
|
(set! automaton-non-term (cons (cons (make-trans-key kernel gs)
|
|
|
|
unique-kernel)
|
|
|
|
unique-kernel)
|
|
|
|
; (printf "~a -> ~a on ~a~n"
|
|
|
|
automaton-non-term))))
|
|
|
|
; (kernel->string kernel)
|
|
|
|
#;(printf "~a -> ~a on ~a~n"
|
|
|
|
; (kernel->string unique-kernel)
|
|
|
|
(kernel->string kernel)
|
|
|
|
; (gram-sym-symbol gs))
|
|
|
|
(kernel->string unique-kernel)
|
|
|
|
|
|
|
|
(gram-sym-symbol gs))
|
|
|
|
(if new
|
|
|
|
(if new
|
|
|
|
unique-kernel
|
|
|
|
unique-kernel
|
|
|
|
#f)))
|
|
|
|
#f)))
|
|
|
|
(let loop ((i 0))
|
|
|
|
(let loop ((gsyms grammar-symbols))
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((< i num-non-terms)
|
|
|
|
((null? gsyms) null)
|
|
|
|
(let ((items (vector-ref table i)))
|
|
|
|
|
|
|
|
(cond
|
|
|
|
|
|
|
|
((null? items) (loop (add1 i)))
|
|
|
|
|
|
|
|
(else
|
|
|
|
(else
|
|
|
|
(cons (list (vector-ref non-terms i) items)
|
|
|
|
(let ((items (hash-table-get table
|
|
|
|
(loop (add1 i)))))))
|
|
|
|
(gram-sym-symbol (car gsyms))
|
|
|
|
((< i num-gram-syms)
|
|
|
|
(lambda () null))))
|
|
|
|
(let ((items (vector-ref table i)))
|
|
|
|
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((null? items) (loop (add1 i)))
|
|
|
|
((null? items) (loop (cdr gsyms)))
|
|
|
|
(else
|
|
|
|
(else
|
|
|
|
(cons (list (vector-ref terms (- i num-non-terms))
|
|
|
|
(cons (list (car gsyms) items)
|
|
|
|
items)
|
|
|
|
(loop (cdr gsyms))))))))))))))
|
|
|
|
(loop (add1 i)))))))
|
|
|
|
|
|
|
|
(else null))))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(starts
|
|
|
|
(starts
|
|
|
|
(map (lambda (init-prod) (list (make-item init-prod 0)))
|
|
|
|
(map (lambda (init-prod) (list (make-item init-prod 0)))
|
|
|
@ -350,9 +345,7 @@
|
|
|
|
automaton-term
|
|
|
|
automaton-term
|
|
|
|
automaton-non-term
|
|
|
|
automaton-non-term
|
|
|
|
(list->vector (reverse! seen-kernels))
|
|
|
|
(list->vector (reverse! seen-kernels))
|
|
|
|
epsilons
|
|
|
|
epsilons))
|
|
|
|
(vector-length terms)
|
|
|
|
|
|
|
|
num-non-terms))
|
|
|
|
|
|
|
|
((null? old-kernels)
|
|
|
|
((null? old-kernels)
|
|
|
|
(loop (deq! new-kernels) seen-kernels))
|
|
|
|
(loop (deq! new-kernels) seen-kernels))
|
|
|
|
(else
|
|
|
|
(else
|
|
|
|