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.
290 lines
11 KiB
Racket
290 lines
11 KiB
Racket
16 years ago
|
#lang scheme/base
|
||
24 years ago
|
|
||
|
;; Routine to build the LALR table
|
||
14 years ago
|
|
||
|
(require "grammar.rkt"
|
||
|
"lr0.rkt"
|
||
|
"lalr.rkt"
|
||
|
"parser-actions.rkt"
|
||
|
mzlib/contract
|
||
17 years ago
|
mzlib/list
|
||
14 years ago
|
mzlib/class)
|
||
24 years ago
|
|
||
20 years ago
|
(provide/contract
|
||
|
(build-table ((is-a?/c grammar%) string? any/c . -> .
|
||
|
(vectorof (listof (cons/c (union term? non-term?) action?))))))
|
||
24 years ago
|
|
||
20 years ago
|
;; A parse-table is (vectorof (listof (cons/c gram-sym? action)))
|
||
|
;; A grouped-parse-table is (vectorof (listof (cons/c gram-sym? (listof action))))
|
||
|
|
||
|
;; make-parse-table : int -> parse-table
|
||
|
(define (make-parse-table num-states)
|
||
|
(make-vector num-states null))
|
||
|
|
||
|
;; table-add!: parse-table nat symbol action ->
|
||
|
(define (table-add! table state-index symbol val)
|
||
|
(vector-set! table state-index (cons (cons symbol val)
|
||
|
(vector-ref table state-index))))
|
||
|
|
||
|
;; group-table : parse-table -> grouped-parse-table
|
||
|
(define (group-table table)
|
||
|
(list->vector
|
||
|
(map
|
||
|
(lambda (state-entry)
|
||
16 years ago
|
(let ((ht (make-hash)))
|
||
20 years ago
|
(for-each
|
||
|
(lambda (gs/actions)
|
||
16 years ago
|
(let ((group (hash-ref ht (car gs/actions) (lambda () null))))
|
||
20 years ago
|
(unless (member (cdr gs/actions) group)
|
||
16 years ago
|
(hash-set! ht (car gs/actions) (cons (cdr gs/actions) group)))))
|
||
20 years ago
|
state-entry)
|
||
16 years ago
|
(hash-map ht cons)))
|
||
20 years ago
|
(vector->list table))))
|
||
|
|
||
|
;; table-map : (vectorof (listof (cons/c gram-sym? X))) (gram-sym? X -> Y) ->
|
||
|
;; (vectorof (listof (cons/c gram-sym? Y)))
|
||
|
(define (table-map f table)
|
||
|
(list->vector
|
||
|
(map
|
||
|
(lambda (state-entry)
|
||
|
(map
|
||
|
(lambda (gs/X)
|
||
|
(cons (car gs/X) (f (car gs/X) (cdr gs/X))))
|
||
|
state-entry))
|
||
|
(vector->list table))))
|
||
|
|
||
23 years ago
|
|
||
|
(define (bit-vector-for-each f bv)
|
||
|
(letrec ((for-each
|
||
|
(lambda (bv number)
|
||
|
(cond
|
||
|
((= 0 bv) (void))
|
||
|
((= 1 (bitwise-and 1 bv))
|
||
|
(f number)
|
||
|
(for-each (arithmetic-shift bv -1) (add1 number)))
|
||
|
(else (for-each (arithmetic-shift bv -1) (add1 number)))))))
|
||
|
(for-each bv 0)))
|
||
|
|
||
|
|
||
20 years ago
|
;; print-entry: symbol action output-port ->
|
||
|
;; prints the action a for lookahead sym to the given port
|
||
24 years ago
|
(define (print-entry sym a port)
|
||
|
(let ((s "\t~a\t\t\t\t\t~a\t~a\n"))
|
||
|
(cond
|
||
|
((shift? a)
|
||
|
(fprintf port s sym "shift" (shift-state a)))
|
||
|
((reduce? a)
|
||
20 years ago
|
(fprintf port s sym "reduce" (prod-index (reduce-prod a))))
|
||
24 years ago
|
((accept? a)
|
||
|
(fprintf port s sym "accept" ""))
|
||
20 years ago
|
((goto? a)
|
||
|
(fprintf port s sym "goto" (goto-state a))))))
|
||
24 years ago
|
|
||
|
|
||
|
;; count: ('a -> bool) * 'a list -> num
|
||
|
;; counts the number of elements in list that satisfy pred
|
||
|
(define (count pred list)
|
||
|
(cond
|
||
|
((null? list) 0)
|
||
|
((pred (car list)) (+ 1 (count pred (cdr list))))
|
||
|
(else (count pred (cdr list)))))
|
||
|
|
||
20 years ago
|
;; display-parser: LR0-automaton grouped-parse-table (listof prod?) output-port ->
|
||
24 years ago
|
;; Prints out the parser given by table.
|
||
20 years ago
|
(define (display-parser a grouped-table prods port)
|
||
|
(let* ((SR-conflicts 0)
|
||
24 years ago
|
(RR-conflicts 0))
|
||
23 years ago
|
(for-each
|
||
|
(lambda (prod)
|
||
|
(fprintf port
|
||
15 years ago
|
"~a\t~a\t=\t~a\n"
|
||
23 years ago
|
(prod-index prod)
|
||
|
(gram-sym-symbol (prod-lhs prod))
|
||
|
(map gram-sym-symbol (vector->list (prod-rhs prod)))))
|
||
|
prods)
|
||
23 years ago
|
(send a for-each-state
|
||
20 years ago
|
(lambda (state)
|
||
15 years ago
|
(fprintf port "State ~a\n" (kernel-index state))
|
||
20 years ago
|
(for-each (lambda (item)
|
||
15 years ago
|
(fprintf port "\t~a\n" (item->string item)))
|
||
20 years ago
|
(kernel-items state))
|
||
|
(newline port)
|
||
|
(for-each
|
||
|
(lambda (gs/action)
|
||
|
(let ((sym (gram-sym-symbol (car gs/action)))
|
||
|
(act (cdr gs/action)))
|
||
|
(cond
|
||
20 years ago
|
((null? act) (void))
|
||
20 years ago
|
((null? (cdr act))
|
||
|
(print-entry sym (car act) port))
|
||
|
(else
|
||
15 years ago
|
(fprintf port "begin conflict:\n")
|
||
16 years ago
|
(when (> (count reduce? act) 1)
|
||
|
(set! RR-conflicts (add1 RR-conflicts)))
|
||
|
(when (> (count shift? act) 0)
|
||
|
(set! SR-conflicts (add1 SR-conflicts)))
|
||
20 years ago
|
(map (lambda (x) (print-entry sym x port)) act)
|
||
15 years ago
|
(fprintf port "end conflict\n")))))
|
||
20 years ago
|
(vector-ref grouped-table (kernel-index state)))
|
||
|
(newline port)))
|
||
|
|
||
|
(when (> SR-conflicts 0)
|
||
15 years ago
|
(fprintf port "~a shift/reduce conflict~a\n"
|
||
16 years ago
|
SR-conflicts
|
||
|
(if (= SR-conflicts 1) "" "s")))
|
||
20 years ago
|
(when (> RR-conflicts 0)
|
||
15 years ago
|
(fprintf port "~a reduce/reduce conflict~a\n"
|
||
16 years ago
|
RR-conflicts
|
||
|
(if (= RR-conflicts 1) "" "s")))))
|
||
20 years ago
|
|
||
|
;; resolve-conflict : (listof action?) -> action? bool bool
|
||
|
(define (resolve-conflict actions)
|
||
|
(cond
|
||
20 years ago
|
((null? actions) (values (make-no-action) #f #f))
|
||
20 years ago
|
((null? (cdr actions))
|
||
|
(values (car actions) #f #f))
|
||
|
(else
|
||
|
(let ((SR-conflict? (> (count shift? actions) 0))
|
||
|
(RR-conflict? (> (count reduce? actions) 1)))
|
||
|
(let loop ((current-guess #f)
|
||
|
(rest actions))
|
||
|
(cond
|
||
|
((null? rest) (values current-guess SR-conflict? RR-conflict?))
|
||
|
((shift? (car rest)) (values (car rest) SR-conflict? RR-conflict?))
|
||
|
((not current-guess)
|
||
|
(loop (car rest) (cdr rest)))
|
||
|
((and (reduce? (car rest))
|
||
|
(< (prod-index (reduce-prod (car rest)))
|
||
|
(prod-index (reduce-prod current-guess))))
|
||
|
(loop (car rest) (cdr rest)))
|
||
|
((accept? (car rest))
|
||
13 years ago
|
(eprintf "accept/reduce or accept/shift conflicts. Check the grammar for useless cycles of productions\n")
|
||
20 years ago
|
(loop current-guess (cdr rest)))
|
||
|
(else (loop current-guess (cdr rest)))))))))
|
||
|
|
||
|
;; resolve-conflicts : grouped-parse-table bool -> parse-table
|
||
|
(define (resolve-conflicts grouped-table suppress)
|
||
|
(let* ((SR-conflicts 0)
|
||
|
(RR-conflicts 0)
|
||
|
(table (table-map
|
||
|
(lambda (gs actions)
|
||
|
(let-values (((action SR? RR?)
|
||
|
(resolve-conflict actions)))
|
||
|
(when SR?
|
||
|
(set! SR-conflicts (add1 SR-conflicts)))
|
||
|
(when RR?
|
||
|
(set! RR-conflicts (add1 RR-conflicts)))
|
||
|
action))
|
||
|
grouped-table)))
|
||
|
(unless suppress
|
||
|
(when (> SR-conflicts 0)
|
||
13 years ago
|
(eprintf "~a shift/reduce conflict~a\n"
|
||
16 years ago
|
SR-conflicts
|
||
|
(if (= SR-conflicts 1) "" "s")))
|
||
20 years ago
|
(when (> RR-conflicts 0)
|
||
13 years ago
|
(eprintf "~a reduce/reduce conflict~a\n"
|
||
16 years ago
|
RR-conflicts
|
||
|
(if (= RR-conflicts 1) "" "s"))))
|
||
20 years ago
|
table))
|
||
|
|
||
24 years ago
|
|
||
20 years ago
|
;; resolve-sr-conflict : (listof action) (union int #f) -> (listof action)
|
||
|
;; Resolves a single shift-reduce conflict, if precedences are in place.
|
||
|
(define (resolve-sr-conflict/prec actions shift-prec)
|
||
|
(let* ((shift (if (shift? (car actions))
|
||
|
(car actions)
|
||
|
(cadr actions)))
|
||
|
(reduce (if (shift? (car actions))
|
||
|
(cadr actions)
|
||
|
(car actions)))
|
||
|
(reduce-prec (prod-prec (reduce-prod reduce))))
|
||
|
(cond
|
||
|
((and shift-prec reduce-prec)
|
||
20 years ago
|
(cond
|
||
|
((< (prec-num shift-prec) (prec-num reduce-prec))
|
||
|
(list reduce))
|
||
|
((> (prec-num shift-prec) (prec-num reduce-prec))
|
||
|
(list shift))
|
||
|
((eq? 'left (prec-assoc shift-prec))
|
||
|
(list reduce))
|
||
|
((eq? 'right (prec-assoc shift-prec))
|
||
|
(list shift))
|
||
|
(else null)))
|
||
20 years ago
|
(else actions))))
|
||
24 years ago
|
|
||
20 years ago
|
|
||
|
;; resolve-prec-conflicts : parse-table -> grouped-parse-table
|
||
|
(define (resolve-prec-conflicts table)
|
||
|
(table-map
|
||
|
(lambda (gs actions)
|
||
|
(cond
|
||
|
((and (term? gs)
|
||
|
(= 2 (length actions))
|
||
|
(or (shift? (car actions))
|
||
|
(shift? (cadr actions))))
|
||
|
(resolve-sr-conflict/prec actions (term-prec gs)))
|
||
|
(else actions)))
|
||
|
(group-table table)))
|
||
|
|
||
|
;; build-table: grammar string bool -> parse-table
|
||
23 years ago
|
(define (build-table g file suppress)
|
||
23 years ago
|
(let* ((a (build-lr0-automaton g))
|
||
20 years ago
|
(term-vector (list->vector (send g get-terms)))
|
||
20 years ago
|
(end-terms (send g get-end-terms))
|
||
|
(table (make-parse-table (send a get-num-states)))
|
||
20 years ago
|
(get-lookahead (compute-LA a g))
|
||
16 years ago
|
(reduce-cache (make-hash)))
|
||
20 years ago
|
|
||
|
(for-each
|
||
|
(lambda (trans-key/state)
|
||
|
(let ((from-state-index (kernel-index (trans-key-st (car trans-key/state))))
|
||
|
(gs (trans-key-gs (car trans-key/state)))
|
||
|
(to-state (cdr trans-key/state)))
|
||
|
(table-add! table from-state-index gs
|
||
|
(cond
|
||
|
((non-term? gs)
|
||
|
(make-goto (kernel-index to-state)))
|
||
|
((member gs end-terms)
|
||
|
(make-accept))
|
||
|
(else
|
||
|
(make-shift
|
||
|
(kernel-index to-state)))))))
|
||
|
(send a get-transitions))
|
||
|
|
||
23 years ago
|
(send a for-each-state
|
||
20 years ago
|
(lambda (state)
|
||
|
(for-each
|
||
|
(lambda (item)
|
||
|
(let ((item-prod (item-prod item)))
|
||
|
(bit-vector-for-each
|
||
|
(lambda (term-index)
|
||
|
(unless (start-item? item)
|
||
16 years ago
|
(let ((r (hash-ref reduce-cache item-prod
|
||
20 years ago
|
(lambda ()
|
||
|
(let ((r (make-reduce item-prod)))
|
||
16 years ago
|
(hash-set! reduce-cache item-prod r)
|
||
20 years ago
|
r)))))
|
||
|
(table-add! table
|
||
|
(kernel-index state)
|
||
|
(vector-ref term-vector term-index)
|
||
|
r))))
|
||
20 years ago
|
(get-lookahead state item-prod))))
|
||
16 years ago
|
(append (hash-ref (send a get-epsilon-trans) state (lambda () null))
|
||
20 years ago
|
(filter (lambda (item)
|
||
|
(not (move-dot-right item)))
|
||
|
(kernel-items state))))))
|
||
23 years ago
|
|
||
20 years ago
|
(let ((grouped-table (resolve-prec-conflicts table)))
|
||
|
(unless (string=? file "")
|
||
21 years ago
|
(with-handlers [(exn:fail:filesystem?
|
||
23 years ago
|
(lambda (e)
|
||
13 years ago
|
(eprintf
|
||
16 years ago
|
"Cannot write debug output to file \"~a\": ~a\n"
|
||
|
file
|
||
|
(exn-message e))))]
|
||
23 years ago
|
(call-with-output-file file
|
||
|
(lambda (port)
|
||
16 years ago
|
(display-parser a grouped-table (send g get-prods) port))
|
||
|
#:exists 'truncate)))
|
||
20 years ago
|
(resolve-conflicts grouped-table suppress))))
|