From 39505951eaaddd0e49b99fc9d6d2d70028f07e5a Mon Sep 17 00:00:00 2001 From: Jack Firth Date: Tue, 29 Mar 2022 23:43:43 -0700 Subject: [PATCH] Reorganize imports and exports of `grammar.rkt` --- parser-tools/private-yacc/grammar.rkt | 63 +++++++++++++-------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/parser-tools/private-yacc/grammar.rkt b/parser-tools/private-yacc/grammar.rkt index 2204867..140c8ab 100644 --- a/parser-tools/private-yacc/grammar.rkt +++ b/parser-tools/private-yacc/grammar.rkt @@ -5,32 +5,7 @@ (require racket/class yaragg/parser-tools/private-yacc/yacc-helper racket/contract) - -;; Each production has a unique index 0 <= index <= number of productions -(struct prod (lhs rhs index prec action) #:mutable) -;; The dot-pos field is the index of the element in the rhs -;; of prod that the dot immediately precedes. -;; Thus 0 <= dot-pos <= (vector-length rhs). -(struct item (prod dot-pos) #:transparent) - -;; gram-sym = (union term? non-term?) -;; Each term has a unique index 0 <= index < number of terms -;; Each non-term has a unique index 0 <= index < number of non-terms -(struct term (sym index prec) #:mutable) -(struct non-term (sym index) #:mutable) - -;; a precedence declaration. -(struct prec (num assoc) #:transparent) - -(provide/contract - [item (prod? (or/c #f natural-number/c) . -> . item?)] - [term (symbol? (or/c #f natural-number/c) (or/c prec? #f) . -> . term?)] - [non-term (symbol? (or/c #f natural-number/c) . -> . non-term?)] - [prec (natural-number/c (or/c 'left 'right 'nonassoc) . -> . prec?)] - [prod (non-term? (vectorof (or/c non-term? term?)) - (or/c #f natural-number/c) (or/c #f prec?) syntax? . -> . prod?)]) - (provide ;; Things that work on items start-item? item-prod item->string @@ -47,9 +22,33 @@ grammar% ;; Things that work on productions - prod-index prod-prec prod-rhs prod-lhs prod-action) + prod-index prod-prec prod-rhs prod-lhs prod-action + + (contract-out + [item (prod? (or/c #f natural-number/c) . -> . item?)] + [term (symbol? (or/c #f natural-number/c) (or/c prec? #f) . -> . term?)] + [non-term (symbol? (or/c #f natural-number/c) . -> . non-term?)] + [prec (natural-number/c (or/c 'left 'right 'nonassoc) . -> . prec?)] + [prod (non-term? (vectorof (or/c non-term? term?)) + (or/c #f natural-number/c) (or/c #f prec?) syntax? . -> . prod?)])) + +;; Each production has a unique index 0 <= index <= number of productions +(struct prod (lhs rhs index prec action) #:mutable) +;; The dot-pos field is the index of the element in the rhs +;; of prod that the dot immediately precedes. +;; Thus 0 <= dot-pos <= (vector-length rhs). +(struct item (prod dot-pos) #:transparent) + +;; gram-sym = (union term? non-term?) +;; Each term has a unique index 0 <= index < number of terms +;; Each non-term has a unique index 0 <= index < number of non-terms +(struct term (sym index prec) #:mutable) +(struct non-term (sym index) #:mutable) +;; a precedence declaration. +(struct prec (num assoc) #:transparent) + ;;---------------------- LR items -------------------------- ;; item bool @@ -74,7 +73,7 @@ (cond [(= (item-dot-pos i) (vector-length (prod-rhs (item-prod i)))) #f] [else (item (item-prod i) - (add1 (item-dot-pos i)))])) + (add1 (item-dot-pos i)))])) ;; sym-at-dot: LR-item -> gram-sym | #f ;; returns the symbol after the dot in the item or #f if there is none @@ -156,19 +155,19 @@ (define num-non-terms (length non-terms)) (for ([(nt count) (in-indexed non-terms)]) - (set-non-term-index! nt count)) + (set-non-term-index! nt count)) (for ([(t count) (in-indexed terms)]) - (set-term-index! t count)) + (set-term-index! t count)) (for ([(prod count) (in-indexed all-prods)]) - (set-prod-index! prod count)) + (set-prod-index! prod count)) ;; indexed by the index of the non-term - contains the list of productions for that non-term (define nt->prods (let ((v (make-vector (length prods) #f))) (for ([prods (in-list prods)]) - (vector-set! v (non-term-index (prod-lhs (car prods))) prods)) + (vector-set! v (non-term-index (prod-lhs (car prods))) prods)) v)) (define nullable-non-terms @@ -219,7 +218,7 @@ (define (possible-nullable prods) (for/list ([prod (in-list prods)] #:when (vector-andmap non-term? (prod-rhs prod))) - prod)) + prod)) ;; set-nullables: production list -> production list ;; makes one pass through the productions, adding the ones