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.
55 lines
1.9 KiB
Racket
55 lines
1.9 KiB
Racket
24 years ago
|
(module parser-actions mzscheme
|
||
20 years ago
|
(require "grammar.ss")
|
||
20 years ago
|
(provide (all-defined-except make-reduce make-reduce*)
|
||
|
(rename make-reduce* make-reduce))
|
||
24 years ago
|
|
||
20 years ago
|
;; An action is
|
||
|
;; - (make-shift int)
|
||
20 years ago
|
;; - (make-reduce prod runtime-action)
|
||
20 years ago
|
;; - (make-accept)
|
||
|
;; - (make-goto int)
|
||
20 years ago
|
;; - (no-action)
|
||
20 years ago
|
;; A reduce contains a runtime-reduce so that sharing of the reduces can
|
||
|
;; be easily transferred to sharing of runtime-reduces.
|
||
|
|
||
20 years ago
|
(define-struct action () (make-inspector))
|
||
|
(define-struct (shift action) (state) (make-inspector))
|
||
20 years ago
|
(define-struct (reduce action) (prod runtime-reduce) (make-inspector))
|
||
20 years ago
|
(define-struct (accept action) () (make-inspector))
|
||
|
(define-struct (goto action) (state) (make-inspector))
|
||
20 years ago
|
(define-struct (no-action action) () (make-inspector))
|
||
20 years ago
|
|
||
|
(define (make-reduce* p)
|
||
|
(make-reduce p
|
||
|
(vector (prod-index p)
|
||
|
(gram-sym-symbol (prod-lhs p))
|
||
|
(vector-length (prod-rhs p)))))
|
||
20 years ago
|
|
||
|
;; A runtime-action is
|
||
20 years ago
|
;; non-negative-int (shift)
|
||
20 years ago
|
;; (vector int symbol int) (reduce)
|
||
|
;; 'accept (accept)
|
||
20 years ago
|
;; negative-int (goto)
|
||
20 years ago
|
;; #f (no-action)
|
||
|
|
||
20 years ago
|
(define (action->runtime-action a)
|
||
|
(cond
|
||
|
((shift? a) (shift-state a))
|
||
20 years ago
|
((reduce? a) (reduce-runtime-reduce a))
|
||
20 years ago
|
((accept? a) 'accept)
|
||
20 years ago
|
((goto? a) (- (+ (goto-state a) 1)))
|
||
|
((no-action? a) #f)))
|
||
24 years ago
|
|
||
20 years ago
|
(define (runtime-shift? x) (and (integer? x) (>= x 0)))
|
||
|
(define runtime-reduce? vector?)
|
||
|
(define (runtime-accept? x) (eq? x 'accept))
|
||
|
(define (runtime-goto? x) (and (integer? x) (< x 0)))
|
||
24 years ago
|
|
||
20 years ago
|
(define runtime-shift-state values)
|
||
|
(define (runtime-reduce-prod-num x) (vector-ref x 0))
|
||
|
(define (runtime-reduce-lhs x) (vector-ref x 1))
|
||
|
(define (runtime-reduce-rhs-length x) (vector-ref x 2))
|
||
|
(define (runtime-goto-state x) (- (+ x 1)))
|
||
24 years ago
|
|
||
|
)
|