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.
111 lines
4.8 KiB
Racket
111 lines
4.8 KiB
Racket
24 years ago
|
(module parser-builder mzscheme
|
||
|
|
||
14 years ago
|
(require "input-file-parser.rkt"
|
||
|
"grammar.rkt"
|
||
|
"table.rkt"
|
||
17 years ago
|
mzlib/class
|
||
|
mzlib/contract)
|
||
21 years ago
|
(require-for-template mzscheme)
|
||
24 years ago
|
|
||
22 years ago
|
(provide/contract
|
||
20 years ago
|
(build-parser ((string? any/c any/c (listof identifier?) (listof identifier?)
|
||
12 years ago
|
(listof identifier?) (union syntax? false/c) syntax?)
|
||
|
. ->* .
|
||
|
(any/c any/c any/c any/c))))
|
||
24 years ago
|
|
||
21 years ago
|
;; fix-check-syntax : (listof identifier?) (listof identifier?) (listof identifier?)
|
||
20 years ago
|
;; (union syntax? false/c) syntax?) -> syntax?
|
||
21 years ago
|
(define (fix-check-syntax input-terms start ends assocs prods)
|
||
21 years ago
|
(let* ((term-binders (get-term-list input-terms))
|
||
|
(get-term-binder
|
||
|
(let ((t (make-hash-table)))
|
||
|
(for-each
|
||
|
(lambda (term)
|
||
|
(hash-table-put! t (syntax-e term) term))
|
||
|
term-binders)
|
||
|
(lambda (x)
|
||
21 years ago
|
(let ((r (hash-table-get t (syntax-e x) (lambda () #f))))
|
||
|
(if r
|
||
|
(syntax-local-introduce (datum->syntax-object r (syntax-e x) x x))
|
||
|
x)))))
|
||
21 years ago
|
(rhs-list
|
||
|
(syntax-case prods ()
|
||
|
(((_ rhs ...) ...)
|
||
|
(syntax->list (syntax (rhs ... ...)))))))
|
||
21 years ago
|
(with-syntax (((tmp ...) (map syntax-local-introduce term-binders))
|
||
|
((term-group ...)
|
||
21 years ago
|
(map (lambda (tg)
|
||
|
(syntax-property
|
||
|
(datum->syntax-object tg #f)
|
||
|
'disappeared-use
|
||
|
tg))
|
||
|
input-terms))
|
||
|
((end ...)
|
||
21 years ago
|
(map get-term-binder ends))
|
||
21 years ago
|
((start ...)
|
||
|
(map get-term-binder start))
|
||
21 years ago
|
((bind ...)
|
||
|
(syntax-case prods ()
|
||
21 years ago
|
(((bind _ ...) ...)
|
||
|
(syntax->list (syntax (bind ...))))))
|
||
21 years ago
|
(((bound ...) ...)
|
||
|
(map
|
||
|
(lambda (rhs)
|
||
|
(syntax-case rhs ()
|
||
|
(((bound ...) (_ pbound) __)
|
||
|
(map get-term-binder
|
||
|
(cons (syntax pbound)
|
||
|
(syntax->list (syntax (bound ...))))))
|
||
|
(((bound ...) _)
|
||
|
(map get-term-binder
|
||
|
(syntax->list (syntax (bound ...)))))))
|
||
|
rhs-list))
|
||
|
((prec ...)
|
||
|
(if assocs
|
||
|
(map get-term-binder
|
||
|
(syntax-case assocs ()
|
||
21 years ago
|
(((__ term ...) ...)
|
||
21 years ago
|
(syntax->list (syntax (term ... ...))))))
|
||
|
null)))
|
||
21 years ago
|
#`(when #f
|
||
21 years ago
|
(let ((bind void) ... (tmp void) ...)
|
||
21 years ago
|
(void bound ... ... term-group ... start ... end ... prec ...))))))
|
||
14 years ago
|
(require mzlib/list "parser-actions.rkt")
|
||
21 years ago
|
(define (build-parser filename src-pos suppress input-terms start end assocs prods)
|
||
21 years ago
|
(let* ((grammar (parse-input input-terms start end assocs prods src-pos))
|
||
23 years ago
|
(table (build-table grammar filename suppress))
|
||
20 years ago
|
(all-tokens (make-hash-table))
|
||
24 years ago
|
(actions-code
|
||
23 years ago
|
`(vector ,@(map prod-action (send grammar get-prods)))))
|
||
20 years ago
|
(for-each (lambda (term)
|
||
|
(hash-table-put! all-tokens (gram-sym-symbol term) #t))
|
||
|
(send grammar get-terms))
|
||
20 years ago
|
#;(let ((num-states (vector-length table))
|
||
|
(num-gram-syms (+ (send grammar get-num-terms)
|
||
|
(send grammar get-num-non-terms)))
|
||
|
(num-ht-entries (apply + (map length (vector->list table))))
|
||
|
(num-reduces
|
||
|
(let ((ht (make-hash-table)))
|
||
|
(for-each
|
||
|
(lambda (x)
|
||
|
(when (reduce? x)
|
||
|
(hash-table-put! ht x #t)))
|
||
|
(map cdr (apply append (vector->list table))))
|
||
|
(length (hash-table-map ht void)))))
|
||
15 years ago
|
(printf "~a states, ~a grammar symbols, ~a hash-table entries, ~a reduces\n"
|
||
20 years ago
|
num-states num-gram-syms num-ht-entries num-reduces)
|
||
15 years ago
|
(printf "~a -- ~aKB, previously ~aKB\n"
|
||
20 years ago
|
(/ (+ 2 num-states
|
||
|
(* 4 num-states) (* 2 1.5 num-ht-entries)
|
||
|
(* 5 num-reduces)) 256.0)
|
||
|
(/ (+ 2 num-states
|
||
|
(* 4 num-states) (* 2 2.3 num-ht-entries)
|
||
|
(* 5 num-reduces)) 256.0)
|
||
|
(/ (+ 2 (* num-states num-gram-syms) (* 5 num-reduces)) 256.0)))
|
||
20 years ago
|
(values table
|
||
|
all-tokens
|
||
|
actions-code
|
||
|
(fix-check-syntax input-terms start end assocs prods))))
|
||
23 years ago
|
|
||
|
)
|