|
|
@ -78,14 +78,14 @@
|
|
|
|
(for ([sym (in-list symbols)]
|
|
|
|
(for ([sym (in-list symbols)]
|
|
|
|
#:unless (identifier? sym))
|
|
|
|
#:unless (identifier? sym))
|
|
|
|
(raise-syntax-error #f "End token must be a symbol" stx sym))
|
|
|
|
(raise-syntax-error #f "End token must be a symbol" stx sym))
|
|
|
|
(let ([d (duplicate-list? (map syntax-e symbols))])
|
|
|
|
(define d (duplicate-list? (map syntax-e symbols)))
|
|
|
|
(when d
|
|
|
|
(when d
|
|
|
|
(raise-syntax-error #f (format "Duplicate end token definition for ~a" d) stx arg))
|
|
|
|
(raise-syntax-error #f (format "Duplicate end token definition for ~a" d) stx arg))
|
|
|
|
(when (null? symbols)
|
|
|
|
(when (null? symbols)
|
|
|
|
(raise-syntax-error #f "end declaration must contain at least 1 token" stx arg))
|
|
|
|
(raise-syntax-error #f "end declaration must contain at least 1 token" stx arg))
|
|
|
|
(when end
|
|
|
|
(when end
|
|
|
|
(raise-syntax-error #f "Multiple end declarations" stx))
|
|
|
|
(raise-syntax-error #f "Multiple end declarations" stx))
|
|
|
|
(set! end symbols)))]
|
|
|
|
(set! end symbols))]
|
|
|
|
[(precs DECLS ...)
|
|
|
|
[(precs DECLS ...)
|
|
|
|
(if precs
|
|
|
|
(if precs
|
|
|
|
(raise-syntax-error #f "Multiple precs declarations" stx)
|
|
|
|
(raise-syntax-error #f "Multiple precs declarations" stx)
|
|
|
@ -113,38 +113,38 @@
|
|
|
|
(raise-syntax-error #f "missing end declaration" stx))
|
|
|
|
(raise-syntax-error #f "missing end declaration" stx))
|
|
|
|
(unless start
|
|
|
|
(unless start
|
|
|
|
(raise-syntax-error #f "missing start declaration" stx))
|
|
|
|
(raise-syntax-error #f "missing start declaration" stx))
|
|
|
|
(let-values ([(table all-term-syms actions check-syntax-fix)
|
|
|
|
(define-values (table all-term-syms actions check-syntax-fix)
|
|
|
|
(build-parser (if debug debug "")
|
|
|
|
(build-parser (if debug debug "")
|
|
|
|
src-pos
|
|
|
|
src-pos
|
|
|
|
suppress
|
|
|
|
suppress
|
|
|
|
tokens
|
|
|
|
tokens
|
|
|
|
start
|
|
|
|
start
|
|
|
|
end
|
|
|
|
end
|
|
|
|
precs
|
|
|
|
precs
|
|
|
|
grammar)])
|
|
|
|
grammar))
|
|
|
|
(when (and yacc-output (not (string=? yacc-output "")))
|
|
|
|
(when (and yacc-output (not (string=? yacc-output "")))
|
|
|
|
(with-handlers [(exn:fail:filesystem?
|
|
|
|
(with-handlers [(exn:fail:filesystem?
|
|
|
|
(λ (e) (eprintf "Cannot write yacc-output to file \"~a\"\n" yacc-output)))]
|
|
|
|
(λ (e) (eprintf "Cannot write yacc-output to file \"~a\"\n" yacc-output)))]
|
|
|
|
(call-with-output-file yacc-output
|
|
|
|
(call-with-output-file yacc-output
|
|
|
|
(λ (port)
|
|
|
|
(λ (port)
|
|
|
|
(display-yacc (syntax->datum grammar)
|
|
|
|
(display-yacc (syntax->datum grammar)
|
|
|
|
tokens
|
|
|
|
tokens
|
|
|
|
(map syntax->datum start)
|
|
|
|
(map syntax->datum start)
|
|
|
|
(and precs (syntax->datum precs))
|
|
|
|
(and precs (syntax->datum precs))
|
|
|
|
port))
|
|
|
|
port))
|
|
|
|
#:exists 'truncate)))
|
|
|
|
#:exists 'truncate)))
|
|
|
|
(with-syntax ([check-syntax-fix check-syntax-fix]
|
|
|
|
(with-syntax ([check-syntax-fix check-syntax-fix]
|
|
|
|
[err error]
|
|
|
|
[err error]
|
|
|
|
[ends end]
|
|
|
|
[ends end]
|
|
|
|
[starts start]
|
|
|
|
[starts start]
|
|
|
|
[debug debug]
|
|
|
|
[debug debug]
|
|
|
|
[table (convert-parse-table table)]
|
|
|
|
[table (convert-parse-table table)]
|
|
|
|
[all-term-syms all-term-syms]
|
|
|
|
[all-term-syms all-term-syms]
|
|
|
|
[actions actions]
|
|
|
|
[actions actions]
|
|
|
|
[src-pos src-pos])
|
|
|
|
[src-pos src-pos])
|
|
|
|
#'(begin
|
|
|
|
#'(begin
|
|
|
|
check-syntax-fix
|
|
|
|
check-syntax-fix
|
|
|
|
(parser-body debug err (quote starts) (quote ends) table all-term-syms actions src-pos)))))]
|
|
|
|
(parser-body debug err (quote starts) (quote ends) table all-term-syms actions src-pos))))]
|
|
|
|
[_ (raise-syntax-error #f "parser must have the form (parser args ...)" stx)]))
|
|
|
|
[_ (raise-syntax-error #f "parser must have the form (parser args ...)" stx)]))
|
|
|
|
|
|
|
|
|
|
|
|
(define (reduce-stack stack num ret-vals src-pos)
|
|
|
|
(define (reduce-stack stack num ret-vals src-pos)
|
|
|
@ -237,31 +237,29 @@
|
|
|
|
stack)]
|
|
|
|
stack)]
|
|
|
|
[else
|
|
|
|
[else
|
|
|
|
;; (printf "discard input:~a\n" tok)
|
|
|
|
;; (printf "discard input:~a\n" tok)
|
|
|
|
(let-values ([(tok val start-pos end-pos)
|
|
|
|
(call-with-values (λ () (extract (get-token))) remove-input)])))))
|
|
|
|
(extract (get-token))])
|
|
|
|
|
|
|
|
(remove-input tok val start-pos end-pos))])))))
|
|
|
|
|
|
|
|
(let remove-states ()
|
|
|
|
(let remove-states ()
|
|
|
|
(let ([a (find-action stack 'error #f start-pos end-pos)])
|
|
|
|
(define a (find-action stack 'error #f start-pos end-pos))
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
[(runtime-shift? a)
|
|
|
|
[(runtime-shift? a)
|
|
|
|
;; (printf "shift:~a\n" (runtime-shift-state a))
|
|
|
|
;; (printf "shift:~a\n" (runtime-shift-state a))
|
|
|
|
(set! stack
|
|
|
|
(set! stack
|
|
|
|
(cons
|
|
|
|
(cons
|
|
|
|
(stack-frame (runtime-shift-state a)
|
|
|
|
(stack-frame (runtime-shift-state a)
|
|
|
|
#f
|
|
|
|
#f
|
|
|
|
start-pos
|
|
|
|
start-pos
|
|
|
|
end-pos)
|
|
|
|
end-pos)
|
|
|
|
stack))
|
|
|
|
stack))
|
|
|
|
(remove-input tok val start-pos end-pos)]
|
|
|
|
(remove-input tok val start-pos end-pos)]
|
|
|
|
[else
|
|
|
|
[else
|
|
|
|
;; (printf "discard state:~a\n" (car stack))
|
|
|
|
;; (printf "discard state:~a\n" (car stack))
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
[(< (length stack) 2)
|
|
|
|
[(< (length stack) 2)
|
|
|
|
(raise-read-error "parser: Cannot continue after error"
|
|
|
|
(raise-read-error "parser: Cannot continue after error"
|
|
|
|
#f #f #f #f #f)]
|
|
|
|
#f #f #f #f #f)]
|
|
|
|
[else
|
|
|
|
[else
|
|
|
|
(set! stack (cdr stack))
|
|
|
|
(set! stack (cdr stack))
|
|
|
|
(remove-states)])])))))
|
|
|
|
(remove-states)])]))))
|
|
|
|
|
|
|
|
|
|
|
|
(define (find-action stack tok val start-pos end-pos)
|
|
|
|
(define (find-action stack tok val start-pos end-pos)
|
|
|
|
(unless (hash-ref all-term-syms tok #f)
|
|
|
|
(unless (hash-ref all-term-syms tok #f)
|
|
|
@ -278,55 +276,55 @@
|
|
|
|
(error 'get-token "expected a nullary procedure, got ~e" get-token))
|
|
|
|
(error 'get-token "expected a nullary procedure, got ~e" get-token))
|
|
|
|
(let parsing-loop ([stack (make-empty-stack start-number)]
|
|
|
|
(let parsing-loop ([stack (make-empty-stack start-number)]
|
|
|
|
[ip (get-token)])
|
|
|
|
[ip (get-token)])
|
|
|
|
(let-values ([(tok val start-pos end-pos) (extract ip)])
|
|
|
|
(define-values (tok val start-pos end-pos) (extract ip))
|
|
|
|
(let ([action (find-action stack tok val start-pos end-pos)])
|
|
|
|
(define action (find-action stack tok val start-pos end-pos))
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
[(runtime-shift? action)
|
|
|
|
[(runtime-shift? action)
|
|
|
|
;; (printf "shift:~a\n" (runtime-shift-state action))
|
|
|
|
;; (printf "shift:~a\n" (runtime-shift-state action))
|
|
|
|
(parsing-loop (cons (stack-frame (runtime-shift-state action)
|
|
|
|
(parsing-loop (cons (stack-frame (runtime-shift-state action)
|
|
|
|
val
|
|
|
|
val
|
|
|
|
start-pos
|
|
|
|
start-pos
|
|
|
|
end-pos)
|
|
|
|
end-pos)
|
|
|
|
stack)
|
|
|
|
stack)
|
|
|
|
(get-token))]
|
|
|
|
(get-token))]
|
|
|
|
[(runtime-reduce? action)
|
|
|
|
[(runtime-reduce? action)
|
|
|
|
;; (printf "reduce:~a\n" (runtime-reduce-prod-num action))
|
|
|
|
;; (printf "reduce:~a\n" (runtime-reduce-prod-num action))
|
|
|
|
(let-values ([(new-stack args)
|
|
|
|
(let-values ([(new-stack args)
|
|
|
|
(reduce-stack stack
|
|
|
|
(reduce-stack stack
|
|
|
|
(runtime-reduce-rhs-length action)
|
|
|
|
(runtime-reduce-rhs-length action)
|
|
|
|
null
|
|
|
|
null
|
|
|
|
src-pos)])
|
|
|
|
src-pos)])
|
|
|
|
(let ([goto
|
|
|
|
(define goto
|
|
|
|
(runtime-goto-state
|
|
|
|
(runtime-goto-state
|
|
|
|
(hash-ref
|
|
|
|
(hash-ref
|
|
|
|
(vector-ref table (stack-frame-state (car new-stack)))
|
|
|
|
(vector-ref table (stack-frame-state (car new-stack)))
|
|
|
|
(runtime-reduce-lhs action)))])
|
|
|
|
(runtime-reduce-lhs action))))
|
|
|
|
(parsing-loop
|
|
|
|
(parsing-loop
|
|
|
|
(cons
|
|
|
|
(cons
|
|
|
|
(if src-pos
|
|
|
|
|
|
|
|
(stack-frame
|
|
|
|
|
|
|
|
goto
|
|
|
|
|
|
|
|
(apply (vector-ref actions (runtime-reduce-prod-num action)) args)
|
|
|
|
|
|
|
|
(if (null? args) start-pos (cadr args))
|
|
|
|
|
|
|
|
(if (null? args)
|
|
|
|
|
|
|
|
end-pos
|
|
|
|
|
|
|
|
(list-ref args (- (* (runtime-reduce-rhs-length action) 3) 1))))
|
|
|
|
|
|
|
|
(stack-frame
|
|
|
|
|
|
|
|
goto
|
|
|
|
|
|
|
|
(apply (vector-ref actions (runtime-reduce-prod-num action)) args)
|
|
|
|
|
|
|
|
#f
|
|
|
|
|
|
|
|
#f))
|
|
|
|
|
|
|
|
new-stack)
|
|
|
|
|
|
|
|
ip)))]
|
|
|
|
|
|
|
|
[(runtime-accept? action)
|
|
|
|
|
|
|
|
;; (printf "accept\n")
|
|
|
|
|
|
|
|
(stack-frame-value (car stack))]
|
|
|
|
|
|
|
|
[else
|
|
|
|
|
|
|
|
(if src-pos
|
|
|
|
(if src-pos
|
|
|
|
(err #t tok val start-pos end-pos)
|
|
|
|
(stack-frame
|
|
|
|
(err #t tok val))
|
|
|
|
goto
|
|
|
|
(parsing-loop (fix-error stack tok val start-pos end-pos get-token)
|
|
|
|
(apply (vector-ref actions (runtime-reduce-prod-num action)) args)
|
|
|
|
(get-token))]))))))
|
|
|
|
(if (null? args) start-pos (cadr args))
|
|
|
|
|
|
|
|
(if (null? args)
|
|
|
|
|
|
|
|
end-pos
|
|
|
|
|
|
|
|
(list-ref args (- (* (runtime-reduce-rhs-length action) 3) 1))))
|
|
|
|
|
|
|
|
(stack-frame
|
|
|
|
|
|
|
|
goto
|
|
|
|
|
|
|
|
(apply (vector-ref actions (runtime-reduce-prod-num action)) args)
|
|
|
|
|
|
|
|
#f
|
|
|
|
|
|
|
|
#f))
|
|
|
|
|
|
|
|
new-stack)
|
|
|
|
|
|
|
|
ip))]
|
|
|
|
|
|
|
|
[(runtime-accept? action)
|
|
|
|
|
|
|
|
;; (printf "accept\n")
|
|
|
|
|
|
|
|
(stack-frame-value (car stack))]
|
|
|
|
|
|
|
|
[else
|
|
|
|
|
|
|
|
(if src-pos
|
|
|
|
|
|
|
|
(err #t tok val start-pos end-pos)
|
|
|
|
|
|
|
|
(err #t tok val))
|
|
|
|
|
|
|
|
(parsing-loop (fix-error stack tok val start-pos end-pos get-token)
|
|
|
|
|
|
|
|
(get-token))]))))
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
[(null? (cdr starts)) (make-parser 0)]
|
|
|
|
[(null? (cdr starts)) (make-parser 0)]
|
|
|
|
[else
|
|
|
|
[else
|
|
|
|