refactor into racket/base
parent
fc1e00bc2a
commit
fd446e6013
@ -1,24 +1,23 @@
|
||||
(module lex-plt-v200 mzscheme
|
||||
(require br-parser-tools/lex
|
||||
(prefix : br-parser-tools/lex-sre))
|
||||
#lang racket/base
|
||||
(require (for-syntax racket/base)
|
||||
br-parser-tools/lex
|
||||
(prefix-in : br-parser-tools/lex-sre))
|
||||
|
||||
(provide epsilon
|
||||
~
|
||||
(rename :* *)
|
||||
(rename :+ +)
|
||||
(rename :? ?)
|
||||
(rename :or :)
|
||||
(rename :& &)
|
||||
(rename :: @)
|
||||
(rename :~ ^)
|
||||
(rename :/ -))
|
||||
(provide epsilon ~
|
||||
(rename-out [:* *]
|
||||
[:+ +]
|
||||
[:? ?]
|
||||
[:or :]
|
||||
[:& &]
|
||||
[:: @]
|
||||
[:~ ^]
|
||||
[:/ -]))
|
||||
|
||||
(define-lex-trans epsilon
|
||||
(syntax-rules ()
|
||||
((_) "")))
|
||||
|
||||
(define-lex-trans ~
|
||||
(syntax-rules ()
|
||||
((_ re) (complement re)))))
|
||||
(define-lex-trans (epsilon stx)
|
||||
(syntax-case stx ()
|
||||
[(_) #'""]))
|
||||
|
||||
(define-lex-trans (~ stx)
|
||||
(syntax-case stx ()
|
||||
[(_ RE) #'(complement RE)]))
|
||||
|
||||
|
@ -1,119 +1,103 @@
|
||||
(module lex-sre mzscheme
|
||||
(require br-parser-tools/lex)
|
||||
|
||||
(provide (rename sre-* *)
|
||||
(rename sre-+ +)
|
||||
?
|
||||
(rename sre-= =)
|
||||
(rename sre->= >=)
|
||||
**
|
||||
(rename sre-or or)
|
||||
:
|
||||
seq
|
||||
&
|
||||
~
|
||||
(rename sre-- -)
|
||||
(rename sre-/ /)
|
||||
/-only-chars)
|
||||
|
||||
(define-lex-trans sre-*
|
||||
(syntax-rules ()
|
||||
((_ re ...)
|
||||
(repetition 0 +inf.0 (union re ...)))))
|
||||
|
||||
(define-lex-trans sre-+
|
||||
(syntax-rules ()
|
||||
((_ re ...)
|
||||
(repetition 1 +inf.0 (union re ...)))))
|
||||
|
||||
(define-lex-trans ?
|
||||
(syntax-rules ()
|
||||
((_ re ...)
|
||||
(repetition 0 1 (union re ...)))))
|
||||
|
||||
(define-lex-trans sre-=
|
||||
(syntax-rules ()
|
||||
((_ n re ...)
|
||||
(repetition n n (union re ...)))))
|
||||
|
||||
(define-lex-trans sre->=
|
||||
(syntax-rules ()
|
||||
((_ n re ...)
|
||||
(repetition n +inf.0 (union re ...)))))
|
||||
|
||||
(define-lex-trans **
|
||||
(syntax-rules ()
|
||||
((_ low #f re ...)
|
||||
(** low +inf.0 re ...))
|
||||
((_ low high re ...)
|
||||
(repetition low high (union re ...)))))
|
||||
|
||||
(define-lex-trans sre-or
|
||||
(syntax-rules ()
|
||||
((_ re ...)
|
||||
(union re ...))))
|
||||
|
||||
(define-lex-trans :
|
||||
(syntax-rules ()
|
||||
((_ re ...)
|
||||
(concatenation re ...))))
|
||||
|
||||
(define-lex-trans seq
|
||||
(syntax-rules ()
|
||||
((_ re ...)
|
||||
(concatenation re ...))))
|
||||
|
||||
(define-lex-trans &
|
||||
(syntax-rules ()
|
||||
((_ re ...)
|
||||
(intersection re ...))))
|
||||
|
||||
(define-lex-trans ~
|
||||
(syntax-rules ()
|
||||
((_ re ...)
|
||||
(char-complement (union re ...)))))
|
||||
#lang racket/base
|
||||
(require (for-syntax racket/base)
|
||||
br-parser-tools/lex)
|
||||
|
||||
(provide (rename-out [sre-* *]
|
||||
[sre-+ +]
|
||||
[sre-= =]
|
||||
[sre->= >=]
|
||||
[sre-or or]
|
||||
[sre-- -]
|
||||
[sre-/ /])
|
||||
? ** : seq & ~ /-only-chars)
|
||||
|
||||
(define-lex-trans (sre-* stx)
|
||||
(syntax-case stx ()
|
||||
[(_ RE ...)
|
||||
#'(repetition 0 +inf.0 (union RE ...))]))
|
||||
|
||||
(define-lex-trans (sre-+ stx)
|
||||
(syntax-case stx ()
|
||||
[(_ RE ...)
|
||||
#'(repetition 1 +inf.0 (union RE ...))]))
|
||||
|
||||
(define-lex-trans (? stx)
|
||||
(syntax-case stx ()
|
||||
[(_ RE ...)
|
||||
#'(repetition 0 1 (union RE ...))]))
|
||||
|
||||
(define-lex-trans (sre-= stx)
|
||||
(syntax-case stx ()
|
||||
[(_ N RE ...)
|
||||
#'(repetition N N (union RE ...))]))
|
||||
|
||||
(define-lex-trans (sre->= stx)
|
||||
(syntax-case stx ()
|
||||
[(_ N RE ...)
|
||||
#'(repetition N +inf.0 (union RE ...))]))
|
||||
|
||||
(define-lex-trans (** stx)
|
||||
(syntax-case stx ()
|
||||
[(_ LOW #f RE ...)
|
||||
#'(** LOW +inf.0 RE ...)]
|
||||
[(_ LOW HIGH RE ...)
|
||||
#'(repetition LOW HIGH (union RE ...))]))
|
||||
|
||||
(define-lex-trans (sre-or stx)
|
||||
(syntax-case stx ()
|
||||
[(_ RE ...)
|
||||
#'(union RE ...)]))
|
||||
|
||||
(define-lex-trans (: stx)
|
||||
(syntax-case stx ()
|
||||
[(_ RE ...)
|
||||
#'(concatenation RE ...)]))
|
||||
|
||||
(define-lex-trans (seq stx)
|
||||
(syntax-case stx ()
|
||||
[(_ RE ...)
|
||||
#'(concatenation RE ...)]))
|
||||
|
||||
(define-lex-trans (& stx)
|
||||
(syntax-case stx ()
|
||||
[(_ RE ...)
|
||||
#'(intersection RE ...)]))
|
||||
|
||||
(define-lex-trans (~ stx)
|
||||
(syntax-case stx ()
|
||||
[(_ RE ...)
|
||||
#'(char-complement (union RE ...))]))
|
||||
|
||||
;; set difference
|
||||
(define-lex-trans (sre-- stx)
|
||||
(syntax-case stx ()
|
||||
((_)
|
||||
[(_)
|
||||
(raise-syntax-error #f
|
||||
"must have at least one argument"
|
||||
stx))
|
||||
((_ big-re re ...)
|
||||
(syntax (& big-re (complement (union re ...)))))))
|
||||
stx)]
|
||||
[(_ BIG-RE RE ...)
|
||||
#'(& BIG-RE (complement (union RE ...)))]))
|
||||
|
||||
(define-lex-trans (sre-/ stx)
|
||||
(syntax-case stx ()
|
||||
((_ range ...)
|
||||
(let ((chars
|
||||
(apply append (map (lambda (r)
|
||||
(let ((x (syntax-e r)))
|
||||
[(_ RANGE ...)
|
||||
(let ([chars
|
||||
(apply append (for/list ([r (in-list (syntax->list #'(RANGE ...)))])
|
||||
(let ([x (syntax-e r)])
|
||||
(cond
|
||||
((char? x) (list x))
|
||||
((string? x) (string->list x))
|
||||
(else
|
||||
(raise-syntax-error
|
||||
#f
|
||||
"not a char or string"
|
||||
stx
|
||||
r)))))
|
||||
(syntax->list (syntax (range ...)))))))
|
||||
[(char? x) (list x)]
|
||||
[(string? x) (string->list x)]
|
||||
[else
|
||||
(raise-syntax-error #f "not a char or string" stx r)]))))])
|
||||
(unless (even? (length chars))
|
||||
(raise-syntax-error
|
||||
#f
|
||||
"not given an even number of characters"
|
||||
stx))
|
||||
#`(/-only-chars #,@chars)))))
|
||||
|
||||
(define-lex-trans /-only-chars
|
||||
(syntax-rules ()
|
||||
((_ c1 c2)
|
||||
(char-range c1 c2))
|
||||
((_ c1 c2 c ...)
|
||||
(union (char-range c1 c2)
|
||||
(/-only-chars c ...)))))
|
||||
|
||||
)
|
||||
(raise-syntax-error #f "not given an even number of characters" stx))
|
||||
#`(/-only-chars #,@chars))]))
|
||||
|
||||
(define-lex-trans (/-only-chars stx)
|
||||
(syntax-case stx ()
|
||||
[(_ C1 C2)
|
||||
#'(char-range C1 C2)]
|
||||
[(_ C1 C2 C ...)
|
||||
#'(union (char-range C1 C2) (/-only-chars C ...))]))
|
||||
|
||||
|
||||
|
@ -1,374 +1,297 @@
|
||||
(module input-file-parser mzscheme
|
||||
|
||||
;; routines for parsing the input to the parser generator and producing a
|
||||
;; grammar (See grammar.rkt)
|
||||
|
||||
#lang racket/base
|
||||
(require "yacc-helper.rkt"
|
||||
"../private-lex/token-syntax.rkt"
|
||||
"grammar.rkt"
|
||||
mzlib/class
|
||||
racket/contract)
|
||||
(require-for-template mzscheme)
|
||||
racket/class
|
||||
racket/contract
|
||||
(for-template racket/base))
|
||||
|
||||
;; routines for parsing the input to the parser generator and producing a
|
||||
;; grammar (See grammar.rkt)
|
||||
|
||||
(define (is-a-grammar%? x) (is-a? x grammar%))
|
||||
(provide/contract
|
||||
(parse-input ((listof identifier?) (listof identifier?) (listof identifier?)
|
||||
(or/c #f syntax?) syntax? any/c . -> . is-a-grammar%?))
|
||||
(get-term-list ((listof identifier?) . -> . (listof identifier?))))
|
||||
[parse-input ((listof identifier?) (listof identifier?) (listof identifier?)
|
||||
(or/c #f syntax?) syntax? any/c . -> . is-a-grammar%?)]
|
||||
[get-term-list ((listof identifier?) . -> . (listof identifier?))])
|
||||
|
||||
(define stx-for-original-property (read-syntax #f (open-input-string "original")))
|
||||
|
||||
;; get-args: ??? -> (values (listof syntax) (or/c #f (cons integer? stx)))
|
||||
(define (get-args i rhs src-pos term-defs)
|
||||
(let ((empty-table (make-hash-table))
|
||||
(biggest-pos #f))
|
||||
(hash-table-put! empty-table 'error #t)
|
||||
(for-each (lambda (td)
|
||||
(let ((v (syntax-local-value td)))
|
||||
(if (e-terminals-def? v)
|
||||
(for-each (lambda (s)
|
||||
(hash-table-put! empty-table (syntax-object->datum s) #t))
|
||||
(syntax->list (e-terminals-def-t v))))))
|
||||
term-defs)
|
||||
(let ([args
|
||||
(let get-args ((i i)
|
||||
(rhs rhs))
|
||||
(define empty-table (make-hasheq))
|
||||
(define biggest-pos #f)
|
||||
(hash-set! empty-table 'error #t)
|
||||
(for* ([td (in-list term-defs)]
|
||||
[v (in-value (syntax-local-value td))]
|
||||
#:when (e-terminals-def? v)
|
||||
[s (in-list (syntax->list (e-terminals-def-t v)))])
|
||||
(hash-set! empty-table (syntax->datum s) #t))
|
||||
(define args
|
||||
(let get-args ([i i][rhs rhs])
|
||||
(cond
|
||||
((null? rhs) null)
|
||||
(else
|
||||
(let ((b (car rhs))
|
||||
(name (if (hash-table-get empty-table (syntax-object->datum (car rhs)) (lambda () #f))
|
||||
[(null? rhs) null]
|
||||
[else
|
||||
(define b (car rhs))
|
||||
(define name (if (hash-ref empty-table (syntax->datum (car rhs)) #f)
|
||||
(gensym)
|
||||
(string->symbol (format "$~a" i)))))
|
||||
(string->symbol (format "$~a" i))))
|
||||
(cond
|
||||
(src-pos
|
||||
(let ([start-pos-id
|
||||
(datum->syntax-object b (string->symbol (format "$~a-start-pos" i)) b stx-for-original-property)]
|
||||
[end-pos-id
|
||||
(datum->syntax-object b (string->symbol (format "$~a-end-pos" i)) b stx-for-original-property)])
|
||||
[src-pos
|
||||
(define start-pos-id
|
||||
(datum->syntax b (string->symbol (format "$~a-start-pos" i)) b stx-for-original-property))
|
||||
(define end-pos-id
|
||||
(datum->syntax b (string->symbol (format "$~a-end-pos" i)) b stx-for-original-property))
|
||||
(set! biggest-pos (cons start-pos-id end-pos-id))
|
||||
`(,(datum->syntax-object b name b stx-for-original-property)
|
||||
,start-pos-id
|
||||
,end-pos-id
|
||||
,@(get-args (add1 i) (cdr rhs)))))
|
||||
(else
|
||||
`(,(datum->syntax-object b name b stx-for-original-property)
|
||||
,@(get-args (add1 i) (cdr rhs)))))))))])
|
||||
(values args biggest-pos))))
|
||||
(list* (datum->syntax b name b stx-for-original-property)
|
||||
start-pos-id
|
||||
end-pos-id
|
||||
(get-args (add1 i) (cdr rhs)))]
|
||||
[else
|
||||
(list* (datum->syntax b name b stx-for-original-property)
|
||||
(get-args (add1 i) (cdr rhs)))])])))
|
||||
(values args biggest-pos))
|
||||
|
||||
;; Given the list of terminal symbols and the precedence/associativity definitions,
|
||||
;; builds terminal structures (See grammar.rkt)
|
||||
;; build-terms: symbol list * symbol list list -> term list
|
||||
(define (build-terms term-list precs)
|
||||
(let ((counter 0)
|
||||
|
||||
(define counter 0)
|
||||
;;(term-list (cons (gensym) term-list))
|
||||
|
||||
;; Will map a terminal symbol to its precedence/associativity
|
||||
(prec-table (make-hash-table)))
|
||||
(define prec-table (make-hasheq))
|
||||
|
||||
;; Fill the prec table
|
||||
(for-each
|
||||
(lambda (p-decl)
|
||||
(begin0
|
||||
(let ((assoc (car p-decl)))
|
||||
(for-each
|
||||
(lambda (term-sym)
|
||||
(hash-table-put! prec-table term-sym (make-prec counter assoc)))
|
||||
(cdr p-decl)))
|
||||
(set! counter (add1 counter))))
|
||||
precs)
|
||||
(for ([p-decl (in-list precs)])
|
||||
(define assoc (car p-decl))
|
||||
(for ([term-sym (in-list (cdr p-decl))])
|
||||
(hash-set! prec-table term-sym (make-prec counter assoc)))
|
||||
(set! counter (add1 counter)))
|
||||
|
||||
;; Build the terminal structures
|
||||
(map
|
||||
(lambda (term-sym)
|
||||
(for/list ([term-sym (in-list term-list)])
|
||||
(make-term term-sym
|
||||
#f
|
||||
(hash-table-get prec-table term-sym (lambda () #f))))
|
||||
term-list)))
|
||||
(hash-ref prec-table term-sym (λ () #f)))))
|
||||
|
||||
;; Retrieves the terminal symbols from a terminals-def (See terminal-syntax.rkt)
|
||||
;; get-terms-from-def: identifier? -> (listof identifier?)
|
||||
(define (get-terms-from-def term-syn)
|
||||
(let ((t (syntax-local-value term-syn (lambda () #f))))
|
||||
(define t (syntax-local-value term-syn #f))
|
||||
(cond
|
||||
((terminals-def? t) (syntax->list (terminals-def-t t)))
|
||||
((e-terminals-def? t) (syntax->list (e-terminals-def-t t)))
|
||||
(else
|
||||
[(terminals-def? t) (syntax->list (terminals-def-t t))]
|
||||
[(e-terminals-def? t) (syntax->list (e-terminals-def-t t))]
|
||||
[else
|
||||
(raise-syntax-error
|
||||
'parser-tokens
|
||||
"undefined token group"
|
||||
term-syn)))))
|
||||
term-syn)]))
|
||||
|
||||
(define (get-term-list term-group-names)
|
||||
(remove-duplicates
|
||||
(cons (datum->syntax-object #f 'error)
|
||||
(apply append
|
||||
(map get-terms-from-def term-group-names)))))
|
||||
(cons (datum->syntax #f 'error)
|
||||
(apply append (map get-terms-from-def term-group-names)))))
|
||||
|
||||
(define (parse-input term-defs start ends prec-decls prods src-pos)
|
||||
(let* ((start-syms (map syntax-e start))
|
||||
|
||||
(list-of-terms (map syntax-e (get-term-list term-defs)))
|
||||
|
||||
(end-terms
|
||||
(map
|
||||
(lambda (end)
|
||||
(define start-syms (map syntax-e start))
|
||||
(define list-of-terms (map syntax-e (get-term-list term-defs)))
|
||||
(define end-terms
|
||||
(for/list ([end (in-list ends)])
|
||||
(unless (memq (syntax-e end) list-of-terms)
|
||||
(raise-syntax-error
|
||||
'parser-end-tokens
|
||||
(format "End token ~a not defined as a token"
|
||||
(syntax-e end))
|
||||
end))
|
||||
(syntax-e end))
|
||||
ends))
|
||||
|
||||
(syntax-e end)))
|
||||
;; Get the list of terminals out of input-terms
|
||||
|
||||
(list-of-non-terms
|
||||
(define list-of-non-terms
|
||||
(syntax-case prods ()
|
||||
(((non-term production ...) ...)
|
||||
[((NON-TERM PRODUCTION ...) ...)
|
||||
(begin
|
||||
(for-each
|
||||
(lambda (nts)
|
||||
(if (memq (syntax-object->datum nts) list-of-terms)
|
||||
(for ([nts (in-list (syntax->list #'(NON-TERM ...)))]
|
||||
#:when (memq (syntax->datum nts) list-of-terms))
|
||||
(raise-syntax-error
|
||||
'parser-non-terminals
|
||||
(format "~a used as both token and non-terminal"
|
||||
(syntax-object->datum nts))
|
||||
nts)))
|
||||
(syntax->list (syntax (non-term ...))))
|
||||
|
||||
(let ((dup (duplicate-list? (syntax-object->datum
|
||||
(syntax (non-term ...))))))
|
||||
(if dup
|
||||
(format "~a used as both token and non-terminal" (syntax->datum nts))
|
||||
nts))
|
||||
(let ([dup (duplicate-list? (syntax->datum #'(NON-TERM ...)))])
|
||||
(when dup
|
||||
(raise-syntax-error
|
||||
'parser-non-terminals
|
||||
(format "non-terminal ~a defined multiple times"
|
||||
dup)
|
||||
(format "non-terminal ~a defined multiple times" dup)
|
||||
prods)))
|
||||
|
||||
(syntax-object->datum (syntax (non-term ...)))))
|
||||
(_
|
||||
(raise-syntax-error
|
||||
(syntax->datum #'(NON-TERM ...)))]
|
||||
[_ (raise-syntax-error
|
||||
'parser-grammar
|
||||
"Grammar must be of the form (grammar (non-terminal productions ...) ...)"
|
||||
prods))))
|
||||
|
||||
prods)]))
|
||||
;; Check the precedence declarations for errors and turn them into data
|
||||
(precs
|
||||
(define precs
|
||||
(syntax-case prec-decls ()
|
||||
(((type term ...) ...)
|
||||
(let ((p-terms
|
||||
(syntax-object->datum (syntax (term ... ...)))))
|
||||
[((TYPE TERM ...) ...)
|
||||
(let ([p-terms (syntax->datum #'(TERM ... ...))])
|
||||
(cond
|
||||
((duplicate-list? p-terms) =>
|
||||
(lambda (d)
|
||||
[(duplicate-list? p-terms) =>
|
||||
(λ (d)
|
||||
(raise-syntax-error
|
||||
'parser-precedences
|
||||
(format "duplicate precedence declaration for token ~a"
|
||||
d)
|
||||
prec-decls)))
|
||||
(else
|
||||
(for-each
|
||||
(lambda (a)
|
||||
(for-each
|
||||
(lambda (t)
|
||||
(if (not (memq (syntax-object->datum t)
|
||||
list-of-terms))
|
||||
(format "duplicate precedence declaration for token ~a" d)
|
||||
prec-decls))]
|
||||
[else (for ([t (in-list (syntax->list #'(TERM ... ...)))]
|
||||
#:when (not (memq (syntax->datum t) list-of-terms)))
|
||||
(raise-syntax-error
|
||||
'parser-precedences
|
||||
(format
|
||||
"Precedence declared for non-token ~a"
|
||||
(syntax-object->datum t))
|
||||
t)))
|
||||
(syntax->list a)))
|
||||
(syntax->list (syntax ((term ...) ...))))
|
||||
(for-each
|
||||
(lambda (type)
|
||||
(if (not (memq (syntax-object->datum type)
|
||||
`(left right nonassoc)))
|
||||
(format "Precedence declared for non-token ~a" (syntax->datum t))
|
||||
t))
|
||||
(for ([type (in-list (syntax->list #'(TYPE ...)))]
|
||||
#:unless (memq (syntax->datum type) `(left right nonassoc)))
|
||||
(raise-syntax-error
|
||||
'parser-precedences
|
||||
"Associativity must be left, right or nonassoc"
|
||||
type)))
|
||||
(syntax->list (syntax (type ...))))
|
||||
(syntax-object->datum prec-decls)))))
|
||||
(#f null)
|
||||
(_
|
||||
(raise-syntax-error
|
||||
type))
|
||||
(syntax->datum prec-decls)]))]
|
||||
[#f null]
|
||||
[_ (raise-syntax-error
|
||||
'parser-precedences
|
||||
"Precedence declaration must be of the form (precs (assoc term ...) ...) where assoc is left, right or nonassoc"
|
||||
prec-decls))))
|
||||
|
||||
(terms (build-terms list-of-terms precs))
|
||||
prec-decls)]))
|
||||
|
||||
(non-terms (map (lambda (non-term) (make-non-term non-term #f))
|
||||
(define terms (build-terms list-of-terms precs))
|
||||
(define non-terms (map (λ (non-term) (make-non-term non-term #f))
|
||||
list-of-non-terms))
|
||||
(term-table (make-hash-table))
|
||||
(non-term-table (make-hash-table)))
|
||||
(define term-table (make-hasheq))
|
||||
(define non-term-table (make-hasheq))
|
||||
|
||||
(for-each (lambda (t)
|
||||
(hash-table-put! term-table (gram-sym-symbol t) t))
|
||||
terms)
|
||||
(for ([t (in-list terms)])
|
||||
(hash-set! term-table (gram-sym-symbol t) t))
|
||||
|
||||
(for-each (lambda (nt)
|
||||
(hash-table-put! non-term-table (gram-sym-symbol nt) nt))
|
||||
non-terms)
|
||||
(for ([nt (in-list non-terms)])
|
||||
(hash-set! non-term-table (gram-sym-symbol nt) nt))
|
||||
|
||||
(let* (
|
||||
;; parse-prod: syntax-object -> gram-sym vector
|
||||
(parse-prod
|
||||
(lambda (prod-so)
|
||||
(define (parse-prod prod-so)
|
||||
(syntax-case prod-so ()
|
||||
((prod-rhs-sym ...)
|
||||
[(PROD-RHS-SYM ...)
|
||||
(andmap identifier? (syntax->list prod-so))
|
||||
(begin
|
||||
(for-each (lambda (t)
|
||||
(if (memq (syntax-object->datum t) end-terms)
|
||||
(for ([t (in-list (syntax->list prod-so))]
|
||||
#:when (memq (syntax->datum t) end-terms))
|
||||
(raise-syntax-error
|
||||
'parser-production-rhs
|
||||
(format "~a is an end token and cannot be used in a production"
|
||||
(syntax-object->datum t))
|
||||
t)))
|
||||
(syntax->list prod-so))
|
||||
(list->vector
|
||||
(map (lambda (s)
|
||||
(hash-table-get
|
||||
term-table
|
||||
(syntax-object->datum s)
|
||||
(lambda ()
|
||||
(hash-table-get
|
||||
non-term-table
|
||||
(syntax-object->datum s)
|
||||
(lambda ()
|
||||
(raise-syntax-error
|
||||
(format "~a is an end token and cannot be used in a production" (syntax->datum t))
|
||||
t))
|
||||
(for/vector ([s (in-list (syntax->list prod-so))])
|
||||
(cond
|
||||
[(hash-ref term-table (syntax->datum s) #f)]
|
||||
[(hash-ref non-term-table (syntax->datum s) #f)]
|
||||
[else (raise-syntax-error
|
||||
'parser-production-rhs
|
||||
(format
|
||||
"~a is not declared as a terminal or non-terminal"
|
||||
(syntax-object->datum s))
|
||||
s))))))
|
||||
(syntax->list prod-so)))))
|
||||
(_
|
||||
(raise-syntax-error
|
||||
(format "~a is not declared as a terminal or non-terminal" (syntax->datum s))
|
||||
s)])))]
|
||||
[_ (raise-syntax-error
|
||||
'parser-production-rhs
|
||||
"production right-hand-side must have form (symbol ...)"
|
||||
prod-so)))))
|
||||
prod-so)]))
|
||||
|
||||
;; parse-action: syntax-object * syntax-object -> syntax-object
|
||||
(parse-action
|
||||
(lambda (rhs act)
|
||||
(let-values ([(args biggest) (get-args 1 (syntax->list rhs) src-pos term-defs)])
|
||||
(let ([act
|
||||
(define (parse-action rhs act-in)
|
||||
(define-values (args biggest) (get-args 1 (syntax->list rhs) src-pos term-defs))
|
||||
(define act
|
||||
(if biggest
|
||||
(with-syntax ([$n-start-pos (datum->syntax-object (car biggest) '$n-start-pos)]
|
||||
[$n-end-pos (datum->syntax-object (cdr biggest) '$n-end-pos)])
|
||||
#`(let ([$n-start-pos #,(car biggest)]
|
||||
[$n-end-pos #,(cdr biggest)])
|
||||
#,act))
|
||||
act)])
|
||||
(quasisyntax/loc act
|
||||
(lambda #,args
|
||||
#,act))))))
|
||||
(with-syntax ([(CAR-BIGGEST . CDR-BIGGEST) biggest]
|
||||
[$N-START-POS (datum->syntax (car biggest) '$n-start-pos)]
|
||||
[$N-END-POS (datum->syntax (cdr biggest) '$n-end-pos)]
|
||||
[ACT-IN act-in])
|
||||
#'(let ([$N-START-POS CAR-BIGGEST]
|
||||
[$N-END-POS CDR-BIGGEST])
|
||||
ACT-IN))
|
||||
act-in))
|
||||
(with-syntax ([ARGS args][ACT act])
|
||||
(syntax/loc #'ACT (λ ARGS ACT))))
|
||||
|
||||
;; parse-prod+action: non-term * syntax-object -> production
|
||||
(parse-prod+action
|
||||
(lambda (nt prod-so)
|
||||
(define (parse-prod+action nt prod-so)
|
||||
(syntax-case prod-so ()
|
||||
((prod-rhs action)
|
||||
(let ((p (parse-prod (syntax prod-rhs))))
|
||||
[(PROD-RHS ACTION)
|
||||
(let ([p (parse-prod #'PROD-RHS)])
|
||||
(make-prod
|
||||
nt
|
||||
p
|
||||
#f
|
||||
(let loop ((i (sub1 (vector-length p))))
|
||||
(let loop ([i (sub1 (vector-length p))])
|
||||
(if (>= i 0)
|
||||
(let ((gs (vector-ref p i)))
|
||||
(let ([gs (vector-ref p i)])
|
||||
(if (term? gs)
|
||||
(term-prec gs)
|
||||
(loop (sub1 i))))
|
||||
#f))
|
||||
(parse-action (syntax prod-rhs) (syntax action)))))
|
||||
((prod-rhs (prec term) action)
|
||||
(identifier? (syntax term))
|
||||
(let ((p (parse-prod (syntax prod-rhs))))
|
||||
(parse-action #'PROD-RHS #'ACTION)))]
|
||||
[(PROD-RHS (PREC TERM) ACTION)
|
||||
(identifier? #'TERM)
|
||||
(let ([p (parse-prod #'PROD-RHS)])
|
||||
(make-prod
|
||||
nt
|
||||
p
|
||||
#f
|
||||
(term-prec
|
||||
(hash-table-get
|
||||
term-table
|
||||
(syntax-object->datum (syntax term))
|
||||
(lambda ()
|
||||
(raise-syntax-error
|
||||
(cond
|
||||
[(hash-ref term-table (syntax->datum #'TERM) #f)]
|
||||
[else (raise-syntax-error
|
||||
'parser-production-rhs
|
||||
(format
|
||||
"unrecognized terminal ~a in precedence declaration"
|
||||
(syntax-object->datum (syntax term)))
|
||||
(syntax term)))))
|
||||
(parse-action (syntax prod-rhs) (syntax action)))))
|
||||
(_
|
||||
(raise-syntax-error
|
||||
(syntax->datum #'TERM))
|
||||
#'TERM)]))
|
||||
(parse-action #'PROD-RHS #'ACTION)))]
|
||||
[_ (raise-syntax-error
|
||||
'parser-production-rhs
|
||||
"production must have form [(symbol ...) expression] or [(symbol ...) (prec symbol) expression]"
|
||||
prod-so)))))
|
||||
prod-so)]))
|
||||
|
||||
;; parse-prod-for-nt: syntax-object -> production list
|
||||
(parse-prods-for-nt
|
||||
(lambda (prods-so)
|
||||
(define (parse-prods-for-nt prods-so)
|
||||
(syntax-case prods-so ()
|
||||
((nt productions ...)
|
||||
(> (length (syntax->list (syntax (productions ...)))) 0)
|
||||
(let ((nt (hash-table-get non-term-table
|
||||
(syntax-object->datum (syntax nt)))))
|
||||
(map (lambda (p) (parse-prod+action nt p))
|
||||
(syntax->list (syntax (productions ...))))))
|
||||
(_
|
||||
(raise-syntax-error
|
||||
[(NT PRODUCTIONS ...)
|
||||
(positive? (length (syntax->list #'(PRODUCTIONS ...))))
|
||||
(let ([nt (hash-ref non-term-table (syntax->datum #'NT))])
|
||||
(map (λ (p) (parse-prod+action nt p)) (syntax->list #'(PRODUCTIONS ...))))]
|
||||
[_ (raise-syntax-error
|
||||
'parser-productions
|
||||
"A production for a non-terminal must be (non-term right-hand-side ...) with at least 1 right hand side"
|
||||
prods-so))))))
|
||||
prods-so)]))
|
||||
|
||||
(for-each
|
||||
(lambda (sstx ssym)
|
||||
(unless (memq ssym list-of-non-terms)
|
||||
(for ([sstx (in-list start)]
|
||||
[ssym (in-list start-syms)]
|
||||
#:unless (memq ssym list-of-non-terms))
|
||||
(raise-syntax-error
|
||||
'parser-start
|
||||
(format "Start symbol ~a not defined as a non-terminal" ssym)
|
||||
sstx)))
|
||||
start start-syms)
|
||||
sstx))
|
||||
|
||||
(let* ((starts (map (lambda (x) (make-non-term (gensym) #f)) start-syms))
|
||||
(end-non-terms (map (lambda (x) (make-non-term (gensym) #f)) start-syms))
|
||||
(parsed-prods (map parse-prods-for-nt (syntax->list prods)))
|
||||
(start-prods
|
||||
(map (lambda (start end-non-term)
|
||||
(list (make-prod start (vector end-non-term) #f #f
|
||||
(syntax (lambda (x) x)))))
|
||||
starts end-non-terms))
|
||||
(prods
|
||||
`(,@start-prods
|
||||
,@(map
|
||||
(lambda (end-nt start-sym)
|
||||
(map
|
||||
(lambda (end)
|
||||
(define starts (map (λ (x) (make-non-term (gensym) #f)) start-syms))
|
||||
(define end-non-terms (map (λ (x) (make-non-term (gensym) #f)) start-syms))
|
||||
(define parsed-prods (map parse-prods-for-nt (syntax->list prods)))
|
||||
(define start-prods (for/list ([start (in-list starts)]
|
||||
[end-non-term (in-list end-non-terms)])
|
||||
(list (make-prod start (vector end-non-term) #f #f #'values))))
|
||||
(define new-prods
|
||||
(append start-prods
|
||||
(for/list ([end-nt (in-list end-non-terms)]
|
||||
[start-sym (in-list start-syms)])
|
||||
(for/list ([end (in-list end-terms)])
|
||||
(make-prod end-nt
|
||||
(vector
|
||||
(hash-table-get non-term-table start-sym)
|
||||
(hash-table-get term-table end))
|
||||
(hash-ref non-term-table start-sym)
|
||||
(hash-ref term-table end))
|
||||
#f
|
||||
#f
|
||||
(syntax (lambda (x) x))))
|
||||
end-terms))
|
||||
end-non-terms start-syms)
|
||||
,@parsed-prods)))
|
||||
#'values)))
|
||||
parsed-prods))
|
||||
|
||||
(make-object grammar%
|
||||
prods
|
||||
new-prods
|
||||
(map car start-prods)
|
||||
terms
|
||||
(append starts (append end-non-terms non-terms))
|
||||
(map (lambda (term-name)
|
||||
(hash-table-get term-table term-name))
|
||||
end-terms)))))))
|
||||
(map (λ (term-name) (hash-ref term-table term-name)) end-terms)))
|
||||
|
@ -1,118 +1,71 @@
|
||||
(module yacc-helper mzscheme
|
||||
|
||||
(require mzlib/list
|
||||
#lang racket/base
|
||||
(require (prefix-in rl: racket/list)
|
||||
"../private-lex/token-syntax.rkt")
|
||||
|
||||
;; General helper routines
|
||||
|
||||
(provide duplicate-list? remove-duplicates overlap? vector-andmap display-yacc)
|
||||
|
||||
(define (vector-andmap f v)
|
||||
(let loop ((i 0))
|
||||
(cond
|
||||
((= i (vector-length v)) #t)
|
||||
(else (if (f (vector-ref v i))
|
||||
(loop (add1 i))
|
||||
#f)))))
|
||||
(define (vector-andmap pred vec)
|
||||
(for/and ([item (in-vector vec)])
|
||||
(pred vec)))
|
||||
|
||||
;; duplicate-list?: symbol list -> #f | symbol
|
||||
;; returns a symbol that exists twice in l, or false if no such symbol
|
||||
;; exists
|
||||
(define (duplicate-list? l)
|
||||
(letrec ((t (make-hash-table))
|
||||
(dl? (lambda (l)
|
||||
(cond
|
||||
((null? l) #f)
|
||||
((hash-table-get t (car l) (lambda () #f)) =>
|
||||
(lambda (x) x))
|
||||
(else
|
||||
(hash-table-put! t (car l) (car l))
|
||||
(dl? (cdr l)))))))
|
||||
(dl? l)))
|
||||
(define (duplicate-list? syms)
|
||||
(rl:check-duplicates syms eq?))
|
||||
|
||||
;; remove-duplicates: syntax-object list -> syntax-object list
|
||||
;; removes the duplicates from the lists
|
||||
(define (remove-duplicates sl)
|
||||
(let ((t (make-hash-table)))
|
||||
(letrec ((x
|
||||
(lambda (sl)
|
||||
(cond
|
||||
((null? sl) sl)
|
||||
((hash-table-get t (syntax-object->datum (car sl)) (lambda () #f))
|
||||
(x (cdr sl)))
|
||||
(else
|
||||
(hash-table-put! t (syntax-object->datum (car sl)) #t)
|
||||
(cons (car sl) (x (cdr sl))))))))
|
||||
(x sl))))
|
||||
(define (remove-duplicates syms)
|
||||
(rl:remove-duplicates syms equal? #:key syntax->datum))
|
||||
|
||||
;; overlap?: symbol list * symbol list -> #f | symbol
|
||||
;; Returns an symbol in l1 intersect l2, or #f is no such symbol exists
|
||||
(define (overlap? l1 l2)
|
||||
(let/ec ret
|
||||
(let ((t (make-hash-table)))
|
||||
(for-each (lambda (s1)
|
||||
(hash-table-put! t s1 s1))
|
||||
l1)
|
||||
(for-each (lambda (s2)
|
||||
(cond
|
||||
((hash-table-get t s2 (lambda () #f)) =>
|
||||
(lambda (o) (ret o)))))
|
||||
l2)
|
||||
#f)))
|
||||
(define (overlap? syms1 syms2)
|
||||
(for/first ([sym1 (in-list syms1)]
|
||||
#:when (memq sym1 syms2))
|
||||
sym1))
|
||||
|
||||
|
||||
(define (display-yacc grammar tokens start precs port)
|
||||
(let-syntax ((p (syntax-rules ()
|
||||
((_ args ...) (fprintf port args ...)))))
|
||||
(let* ((tokens (map syntax-local-value tokens))
|
||||
(eterms (filter e-terminals-def? tokens))
|
||||
(terms (filter terminals-def? tokens))
|
||||
(term-table (make-hash-table))
|
||||
(display-rhs
|
||||
(lambda (rhs)
|
||||
(for-each (lambda (sym) (p "~a " (hash-table-get term-table sym (lambda () sym))))
|
||||
(car rhs))
|
||||
(if (= 3 (length rhs))
|
||||
(let-syntax ([p (syntax-rules ()
|
||||
((_ args ...) (fprintf port args ...)))])
|
||||
(let* ([tokens (map syntax-local-value tokens)]
|
||||
[eterms (filter e-terminals-def? tokens)]
|
||||
[terms (filter terminals-def? tokens)]
|
||||
[term-table (make-hasheq)]
|
||||
[display-rhs
|
||||
(λ (rhs)
|
||||
(for ([sym (in-list (car rhs))])
|
||||
(p "~a " (hash-ref term-table sym (λ () sym))))
|
||||
(when (= 3 (length rhs))
|
||||
(p "%prec ~a" (cadadr rhs)))
|
||||
(p "\n"))))
|
||||
(for-each
|
||||
(lambda (t)
|
||||
(for-each
|
||||
(lambda (t)
|
||||
(hash-table-put! term-table t (format "'~a'" t)))
|
||||
(syntax-object->datum (e-terminals-def-t t))))
|
||||
eterms)
|
||||
(for-each
|
||||
(lambda (t)
|
||||
(for-each
|
||||
(lambda (t)
|
||||
(p "\n"))])
|
||||
(for* ([t (in-list eterms)]
|
||||
[t (in-list (syntax->datum (e-terminals-def-t t)))])
|
||||
(hash-set! term-table t (format "'~a'" t)))
|
||||
(for* ([t (in-list terms)]
|
||||
[t (in-list (syntax->datum (terminals-def-t t)))])
|
||||
(p "%token ~a\n" t)
|
||||
(hash-table-put! term-table t (format "~a" t)))
|
||||
(syntax-object->datum (terminals-def-t t))))
|
||||
terms)
|
||||
(if precs
|
||||
(for-each (lambda (prec)
|
||||
(hash-set! term-table t (format "~a" t)))
|
||||
(when precs
|
||||
(for ([prec (in-list precs)])
|
||||
(p "%~a " (car prec))
|
||||
(for-each (lambda (tok)
|
||||
(p " ~a" (hash-table-get term-table tok)))
|
||||
(cdr prec))
|
||||
(p "\n"))
|
||||
precs))
|
||||
(for ([tok (in-list (cdr prec))])
|
||||
(p " ~a" (hash-ref term-table tok)))
|
||||
(p "\n")))
|
||||
(p "%start ~a\n" start)
|
||||
(p "%%\n")
|
||||
|
||||
(for-each (lambda (prod)
|
||||
(let ((nt (car prod)))
|
||||
(for ([prod (in-list grammar)])
|
||||
(define nt (car prod))
|
||||
(p "~a: " nt)
|
||||
(display-rhs (cadr prod))
|
||||
(for-each (lambda (rhs)
|
||||
(for ([rhs (in-list (cddr prod))])
|
||||
(p "| ")
|
||||
(display-rhs rhs))
|
||||
(cddr prod))
|
||||
(p ";\n")))
|
||||
grammar)
|
||||
(p ";\n"))
|
||||
(p "%%\n"))))
|
||||
|
||||
|
||||
)
|
||||
|
||||
|
Loading…
Reference in New Issue