You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
282 lines
10 KiB
Racket
282 lines
10 KiB
Racket
23 years ago
|
(module lalr mzscheme
|
||
|
|
||
|
;; Compute LALR lookaheads from DeRemer and Pennello 1982
|
||
|
|
||
|
(require "lr0.ss"
|
||
|
"grammar.ss"
|
||
17 years ago
|
mzlib/list
|
||
|
mzlib/class)
|
||
23 years ago
|
|
||
|
(provide compute-LA)
|
||
23 years ago
|
|
||
|
;; compute-DR: LR0-automaton * grammar -> (trans-key -> term set)
|
||
23 years ago
|
;; computes for each state, non-term transition pair, the terminals
|
||
|
;; which can transition out of the resulting state
|
||
23 years ago
|
;; output term set is represented in bit-vector form
|
||
23 years ago
|
(define (compute-DR a g)
|
||
|
(lambda (tk)
|
||
23 years ago
|
(let ((r (send a run-automaton (trans-key-st tk) (trans-key-gs tk))))
|
||
23 years ago
|
(term-list->bit-vector
|
||
|
(filter
|
||
|
(lambda (term)
|
||
23 years ago
|
(send a run-automaton r term))
|
||
23 years ago
|
(send g get-terms))))))
|
||
23 years ago
|
|
||
|
;; compute-reads:
|
||
|
;; LR0-automaton * grammar -> (trans-key -> trans-key list)
|
||
|
(define (compute-reads a g)
|
||
23 years ago
|
(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))))))
|
||
20 years ago
|
|
||
23 years ago
|
;; compute-read: LR0-automaton * grammar -> (trans-key -> term set)
|
||
|
;; output term set is represented in bit-vector form
|
||
23 years ago
|
(define (compute-read a g)
|
||
|
(let* ((dr (compute-DR a g))
|
||
|
(reads (compute-reads a g)))
|
||
23 years ago
|
(digraph-tk->terml (send a get-mapped-non-term-keys)
|
||
20 years ago
|
reads
|
||
|
dr
|
||
|
(send a get-num-states))))
|
||
23 years ago
|
;; returns the list of all k such that state k transitions to state start on the
|
||
|
;; transitions in rhs (in order)
|
||
23 years ago
|
(define (run-lr0-backward a rhs dot-pos start num-states)
|
||
23 years ago
|
(let loop ((states (list start))
|
||
23 years ago
|
(i (sub1 dot-pos)))
|
||
23 years ago
|
(cond
|
||
23 years ago
|
((< i 0) states)
|
||
|
(else (loop (send a run-automaton-back states (vector-ref rhs i))
|
||
|
(sub1 i))))))
|
||
23 years ago
|
|
||
23 years ago
|
;; 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)
|
||
|
;; and gamma =>* epsilon
|
||
|
(define (prod->items-for-include g prod nt)
|
||
|
(let* ((rhs (prod-rhs prod))
|
||
|
(rhs-l (vector-length rhs)))
|
||
|
(append (if (and (> rhs-l 0) (eq? nt (vector-ref rhs (sub1 rhs-l))))
|
||
|
(list (make-item prod (sub1 rhs-l)))
|
||
|
null)
|
||
|
(let loop ((i (sub1 rhs-l)))
|
||
|
(cond
|
||
|
((and (> i 0)
|
||
|
(non-term? (vector-ref rhs i))
|
||
|
(send g nullable-non-term? (vector-ref rhs i)))
|
||
|
(if (eq? nt (vector-ref rhs (sub1 i)))
|
||
|
(cons (make-item prod (sub1 i))
|
||
|
(loop (sub1 i)))
|
||
|
(loop (sub1 i))))
|
||
|
(else null))))))
|
||
23 years ago
|
|
||
23 years ago
|
;; prod-list->items-for-include: grammar * prod list * non-term -> lr0-item list
|
||
|
;; return the list of all (B -> beta . nt gamma) such that (B -> beta nt gamma) in prod-list
|
||
|
;; 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)))
|
||
23 years ago
|
|
||
23 years ago
|
;; comput-includes: lr0-automaton * grammar -> (trans-key -> trans-key list)
|
||
|
(define (compute-includes a g)
|
||
23 years ago
|
(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))
|
||
23 years ago
|
(lambda (tk)
|
||
23 years ago
|
(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-lookback: lr0-automaton * grammar -> (kernel * proc -> trans-key list)
|
||
23 years ago
|
(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)))
|
||
23 years ago
|
(run-lr0-backward a (prod-rhs prod) (vector-length (prod-rhs prod)) state num-states)))))
|
||
23 years ago
|
|
||
|
;; compute-follow: LR0-automaton * grammar -> (trans-key -> term set)
|
||
|
;; output term set is represented in bit-vector form
|
||
23 years ago
|
(define (compute-follow a g includes)
|
||
|
(let ((read (compute-read a g)))
|
||
23 years ago
|
(digraph-tk->terml (send a get-mapped-non-term-keys)
|
||
23 years ago
|
includes
|
||
|
read
|
||
20 years ago
|
(send a get-num-states))))
|
||
23 years ago
|
|
||
20 years ago
|
;; compute-LA: LR0-automaton * grammar -> kernel * prod -> term set
|
||
23 years ago
|
;; output term set is represented in bit-vector form
|
||
23 years ago
|
(define (compute-LA a g)
|
||
23 years ago
|
(let* ((includes (compute-includes a g))
|
||
|
(lookback (compute-lookback a g))
|
||
23 years ago
|
(follow (compute-follow a g includes)))
|
||
23 years ago
|
(lambda (k p)
|
||
|
(let* ((l (lookback k p))
|
||
|
(f (map follow l)))
|
||
|
(apply bitwise-ior (cons 0 f))))))
|
||
23 years ago
|
|
||
|
|
||
|
(define (print-DR dr a g)
|
||
|
(print-input-st-sym dr "DR" a g print-output-terms))
|
||
|
(define (print-Read Read a g)
|
||
|
(print-input-st-sym Read "Read" a g print-output-terms))
|
||
|
(define (print-includes i a g)
|
||
|
(print-input-st-sym i "includes" a g print-output-st-nt))
|
||
|
(define (print-lookback l a g)
|
||
|
(print-input-st-prod l "lookback" a g print-output-st-nt))
|
||
|
(define (print-follow f a g)
|
||
|
(print-input-st-sym f "follow" a g print-output-terms))
|
||
|
(define (print-LA l a g)
|
||
|
(print-input-st-prod l "LA" a g print-output-terms))
|
||
|
|
||
|
(define (print-input-st-sym f name a g print-output)
|
||
|
(printf "~a:~n" name)
|
||
23 years ago
|
(send a for-each-state
|
||
23 years ago
|
(lambda (state)
|
||
|
(for-each
|
||
|
(lambda (non-term)
|
||
|
(let ((res (f (make-trans-key state non-term))))
|
||
|
(if (not (null? res))
|
||
|
(printf "~a(~a, ~a) = ~a~n"
|
||
|
name
|
||
|
state
|
||
|
(gram-sym-symbol non-term)
|
||
|
(print-output res)))))
|
||
23 years ago
|
(send g get-non-terms))))
|
||
23 years ago
|
(newline))
|
||
|
|
||
|
(define (print-input-st-prod f name a g print-output)
|
||
|
(printf "~a:~n" name)
|
||
23 years ago
|
(send a for-each-state
|
||
23 years ago
|
(lambda (state)
|
||
|
(for-each
|
||
|
(lambda (non-term)
|
||
|
(for-each
|
||
|
(lambda (prod)
|
||
|
(let ((res (f state prod)))
|
||
|
(if (not (null? res))
|
||
|
(printf "~a(~a, ~a) = ~a~n"
|
||
|
name
|
||
|
(kernel-index state)
|
||
|
(prod-index prod)
|
||
|
(print-output res)))))
|
||
23 years ago
|
(send g get-prods-for-non-term non-term)))
|
||
|
(send g get-non-terms)))))
|
||
23 years ago
|
|
||
|
(define (print-output-terms r)
|
||
|
(map
|
||
|
(lambda (p)
|
||
|
(gram-sym-symbol p))
|
||
|
r))
|
||
23 years ago
|
|
||
23 years ago
|
(define (print-output-st-nt r)
|
||
|
(map
|
||
|
(lambda (p)
|
||
|
(list
|
||
|
(kernel-index (trans-key-st p))
|
||
|
(gram-sym-symbol (trans-key-gs p))))
|
||
|
r))
|
||
|
|
||
20 years ago
|
;; init-tk-map : int -> (vectorof hashtable?)
|
||
|
(define (init-tk-map n)
|
||
|
(let ((v (make-vector n #f)))
|
||
|
(let loop ((i (sub1 (vector-length v))))
|
||
|
(when (>= i 0)
|
||
|
(vector-set! v i (make-hash-table))
|
||
|
(loop (sub1 i))))
|
||
|
v))
|
||
|
|
||
|
;; lookup-tk-map : (vectorof (symbol? int hashtable)) -> trans-key? -> int
|
||
|
(define (lookup-tk-map map)
|
||
|
(lambda (tk)
|
||
|
(let ((st (trans-key-st tk))
|
||
|
(gs (trans-key-gs tk)))
|
||
|
(hash-table-get (vector-ref map (kernel-index st))
|
||
|
(gram-sym-symbol gs)
|
||
|
(lambda () 0)))))
|
||
|
|
||
|
;; add-tk-map : (vectorof (symbol? int hashtable)) -> trans-key int ->
|
||
|
(define (add-tk-map map)
|
||
|
(lambda (tk v)
|
||
|
(let ((st (trans-key-st tk))
|
||
|
(gs (trans-key-gs tk)))
|
||
|
(hash-table-put! (vector-ref map (kernel-index st))
|
||
|
(gram-sym-symbol gs)
|
||
|
v))))
|
||
|
|
||
23 years ago
|
;; digraph-tk->terml:
|
||
|
;; (trans-key list) * (trans-key -> trans-key list) * (trans-key -> term list) * int * int * int
|
||
|
;; -> (trans-key -> term list)
|
||
|
;; DeRemer and Pennello 1982
|
||
|
;; Computes (f x) = (f- x) union Union{(f y) | y in (edges x)}
|
||
|
;; A specialization of digraph in the file graph.ss
|
||
20 years ago
|
(define (digraph-tk->terml nodes edges f- num-states)
|
||
23 years ago
|
(letrec (
|
||
|
;; Will map elements of trans-key to term sets represented as bit vectors
|
||
20 years ago
|
(results (init-tk-map num-states))
|
||
|
|
||
23 years ago
|
;; Maps elements of trans-keys to integers.
|
||
20 years ago
|
(N (init-tk-map num-states))
|
||
|
|
||
|
(get-N (lookup-tk-map N))
|
||
|
(set-N (add-tk-map N))
|
||
|
(get-f (lookup-tk-map results))
|
||
|
(set-f (add-tk-map results))
|
||
23 years ago
|
|
||
|
(stack null)
|
||
|
(push (lambda (x)
|
||
|
(set! stack (cons x stack))))
|
||
|
(pop (lambda ()
|
||
|
(begin0
|
||
|
(car stack)
|
||
|
(set! stack (cdr stack)))))
|
||
|
(depth (lambda () (length stack)))
|
||
|
|
||
|
;; traverse: 'a ->
|
||
|
(traverse
|
||
|
(lambda (x)
|
||
|
(push x)
|
||
|
(let ((d (depth)))
|
||
|
(set-N x d)
|
||
|
(set-f x (f- x))
|
||
|
(for-each (lambda (y)
|
||
20 years ago
|
(when (= 0 (get-N y))
|
||
|
(traverse y))
|
||
23 years ago
|
(set-f x (bitwise-ior (get-f x) (get-f y)))
|
||
|
(set-N x (min (get-N x) (get-N y))))
|
||
|
(edges x))
|
||
20 years ago
|
(when (= d (get-N x))
|
||
|
(let loop ((p (pop)))
|
||
|
(set-N p +inf.0)
|
||
|
(set-f p (get-f x))
|
||
|
(unless (equal? x p)
|
||
|
(loop (pop)))))))))
|
||
23 years ago
|
(for-each (lambda (x)
|
||
20 years ago
|
(when (= 0 (get-N x))
|
||
|
(traverse x)))
|
||
23 years ago
|
nodes)
|
||
|
get-f))
|
||
23 years ago
|
)
|
||
|
|
||
|
|
||
|
|