|
|
@ -39,11 +39,9 @@
|
|
|
|
;; action array2d * term vector * non-term vector * kernel vector *
|
|
|
|
;; action array2d * term vector * non-term vector * kernel vector *
|
|
|
|
;; output-port ->
|
|
|
|
;; output-port ->
|
|
|
|
;; Prints out the parser given by table.
|
|
|
|
;; Prints out the parser given by table.
|
|
|
|
(define (display-parser table terms non-terms states prods port)
|
|
|
|
(define (display-parser a table terms non-terms prods port)
|
|
|
|
(let* ((num-terms (vector-length terms))
|
|
|
|
(let* ((num-terms (vector-length terms))
|
|
|
|
(num-non-terms (vector-length non-terms))
|
|
|
|
(num-non-terms (vector-length non-terms))
|
|
|
|
(num-gram-syms (+ num-terms num-non-terms))
|
|
|
|
|
|
|
|
(num-states (vector-length states))
|
|
|
|
|
|
|
|
(SR-conflicts 0)
|
|
|
|
(SR-conflicts 0)
|
|
|
|
(RR-conflicts 0))
|
|
|
|
(RR-conflicts 0))
|
|
|
|
(for-each
|
|
|
|
(for-each
|
|
|
@ -54,20 +52,19 @@
|
|
|
|
(gram-sym-symbol (prod-lhs prod))
|
|
|
|
(gram-sym-symbol (prod-lhs prod))
|
|
|
|
(map gram-sym-symbol (vector->list (prod-rhs prod)))))
|
|
|
|
(map gram-sym-symbol (vector->list (prod-rhs prod)))))
|
|
|
|
prods)
|
|
|
|
prods)
|
|
|
|
(let loop ((i 0))
|
|
|
|
(for-each-state
|
|
|
|
(if (< i num-states)
|
|
|
|
(lambda (state)
|
|
|
|
(begin
|
|
|
|
(fprintf port "State ~a~n" (kernel-index state))
|
|
|
|
(fprintf port "State ~a~n" i)
|
|
|
|
|
|
|
|
(for-each (lambda (item)
|
|
|
|
(for-each (lambda (item)
|
|
|
|
(fprintf port "\t~a~n" (item->string item)))
|
|
|
|
(fprintf port "\t~a~n" (item->string item)))
|
|
|
|
(kernel-items (vector-ref states i)))
|
|
|
|
(kernel-items state))
|
|
|
|
(newline port)
|
|
|
|
(newline port)
|
|
|
|
(let loop ((j 0))
|
|
|
|
(let loop ((j 0))
|
|
|
|
(if (< j num-terms)
|
|
|
|
(if (< j num-terms)
|
|
|
|
(begin
|
|
|
|
(begin
|
|
|
|
(let ((act (array2d-ref
|
|
|
|
(let ((act (array2d-ref
|
|
|
|
table
|
|
|
|
table
|
|
|
|
i
|
|
|
|
(kernel-index state)
|
|
|
|
(+ j num-non-terms))))
|
|
|
|
(+ j num-non-terms))))
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((list? act)
|
|
|
|
((list? act)
|
|
|
@ -94,7 +91,7 @@
|
|
|
|
(let loop ((j 0))
|
|
|
|
(let loop ((j 0))
|
|
|
|
(if (< j num-non-terms)
|
|
|
|
(if (< j num-non-terms)
|
|
|
|
(begin
|
|
|
|
(begin
|
|
|
|
(let ((s (array2d-ref table i j)))
|
|
|
|
(let ((s (array2d-ref table (kernel-index state) j)))
|
|
|
|
(if s
|
|
|
|
(if s
|
|
|
|
(print-entry
|
|
|
|
(print-entry
|
|
|
|
(gram-sym-symbol (vector-ref non-terms j))
|
|
|
|
(gram-sym-symbol (vector-ref non-terms j))
|
|
|
@ -102,14 +99,15 @@
|
|
|
|
port)))
|
|
|
|
port)))
|
|
|
|
(loop (add1 j)))))
|
|
|
|
(loop (add1 j)))))
|
|
|
|
|
|
|
|
|
|
|
|
(newline port)
|
|
|
|
(newline port))
|
|
|
|
(loop (add1 i)))))
|
|
|
|
a)
|
|
|
|
|
|
|
|
|
|
|
|
(if (> SR-conflicts 0)
|
|
|
|
(if (> SR-conflicts 0)
|
|
|
|
(fprintf port "~a shift/reduce conflicts~n" SR-conflicts))
|
|
|
|
(fprintf port "~a shift/reduce conflicts~n" SR-conflicts))
|
|
|
|
(if (> RR-conflicts 0)
|
|
|
|
(if (> RR-conflicts 0)
|
|
|
|
(fprintf port "~a reduce/reduce conflicts~n" RR-conflicts))))
|
|
|
|
(fprintf port "~a reduce/reduce conflicts~n" RR-conflicts))))
|
|
|
|
|
|
|
|
|
|
|
|
(define (resolve-conflicts table num-states num-terms num-non-terms)
|
|
|
|
(define (resolve-conflicts a table num-terms num-non-terms)
|
|
|
|
(letrec ((SR-conflicts 0)
|
|
|
|
(letrec ((SR-conflicts 0)
|
|
|
|
(RR-conflicts 0)
|
|
|
|
(RR-conflicts 0)
|
|
|
|
(get-action
|
|
|
|
(get-action
|
|
|
@ -130,19 +128,19 @@
|
|
|
|
(loop (car rest) (cdr rest)))
|
|
|
|
(loop (car rest) (cdr rest)))
|
|
|
|
(else (loop current-guess (cdr rest))))))
|
|
|
|
(else (loop current-guess (cdr rest))))))
|
|
|
|
(else entry)))))
|
|
|
|
(else entry)))))
|
|
|
|
(let loop ((state 0))
|
|
|
|
(for-each-state
|
|
|
|
(if (< state num-states)
|
|
|
|
(lambda (state)
|
|
|
|
(begin
|
|
|
|
|
|
|
|
(let loop ((term 0))
|
|
|
|
(let loop ((term 0))
|
|
|
|
(if (< term num-terms)
|
|
|
|
(if (< term num-terms)
|
|
|
|
(begin
|
|
|
|
(begin
|
|
|
|
(array2d-set! table state (+ num-non-terms term)
|
|
|
|
(array2d-set! table (kernel-index state) (+ num-non-terms term)
|
|
|
|
(get-action
|
|
|
|
(get-action
|
|
|
|
(array2d-ref table
|
|
|
|
(array2d-ref table
|
|
|
|
state
|
|
|
|
(kernel-index state)
|
|
|
|
(+ num-non-terms term))))
|
|
|
|
(+ num-non-terms term))))
|
|
|
|
(loop (add1 term)))))
|
|
|
|
(loop (add1 term))))))
|
|
|
|
(loop (add1 state)))))
|
|
|
|
a)
|
|
|
|
|
|
|
|
|
|
|
|
(if (> SR-conflicts 0)
|
|
|
|
(if (> SR-conflicts 0)
|
|
|
|
(fprintf (current-error-port)
|
|
|
|
(fprintf (current-error-port)
|
|
|
|
"~a shift/reduce conflicts~n"
|
|
|
|
"~a shift/reduce conflicts~n"
|
|
|
@ -154,16 +152,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (resolve-prec-conflicts table get-term get-prod
|
|
|
|
(define (resolve-prec-conflicts a table get-term get-prod
|
|
|
|
num-states num-terms num-non-terms)
|
|
|
|
num-terms num-non-terms)
|
|
|
|
(let loop ((state 0))
|
|
|
|
(for-each-state
|
|
|
|
(if (< state num-states)
|
|
|
|
(lambda (state)
|
|
|
|
(begin
|
|
|
|
|
|
|
|
(let loop ((term 0))
|
|
|
|
(let loop ((term 0))
|
|
|
|
(if (< term num-terms)
|
|
|
|
(if (< term num-terms)
|
|
|
|
(begin
|
|
|
|
(begin
|
|
|
|
(let ((action (array2d-ref table
|
|
|
|
(let ((action (array2d-ref table
|
|
|
|
state
|
|
|
|
(kernel-index state)
|
|
|
|
(+ num-non-terms term))))
|
|
|
|
(+ num-non-terms term))))
|
|
|
|
(if (and (list? action)
|
|
|
|
(if (and (list? action)
|
|
|
|
(= 2 (length action))
|
|
|
|
(= 2 (length action))
|
|
|
@ -185,7 +182,7 @@
|
|
|
|
(if (and s-prec r-prec)
|
|
|
|
(if (and s-prec r-prec)
|
|
|
|
(array2d-set!
|
|
|
|
(array2d-set!
|
|
|
|
table
|
|
|
|
table
|
|
|
|
state
|
|
|
|
(kernel-index state)
|
|
|
|
(+ num-non-terms term)
|
|
|
|
(+ num-non-terms term)
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((< (prec-num s-prec)
|
|
|
|
((< (prec-num s-prec)
|
|
|
@ -199,8 +196,8 @@
|
|
|
|
((eq? 'right (prec-assoc s-prec))
|
|
|
|
((eq? 'right (prec-assoc s-prec))
|
|
|
|
shift)
|
|
|
|
shift)
|
|
|
|
(else #f)))))))
|
|
|
|
(else #f)))))))
|
|
|
|
(loop (add1 term)))))
|
|
|
|
(loop (add1 term))))))
|
|
|
|
(loop (add1 state))))))
|
|
|
|
a))
|
|
|
|
|
|
|
|
|
|
|
|
;; In the result table the first index is the state and the second is the
|
|
|
|
;; In the result table the first index is the state and the second is the
|
|
|
|
;; term/non-term index (with the non-terms coming first)
|
|
|
|
;; term/non-term index (with the non-terms coming first)
|
|
|
@ -234,9 +231,8 @@
|
|
|
|
(array2d-set! v i1 i2 (list a old))))))))
|
|
|
|
(array2d-set! v i1 i2 (list a old))))))))
|
|
|
|
(get-lookahead (compute-LA a g)))
|
|
|
|
(get-lookahead (compute-LA a g)))
|
|
|
|
|
|
|
|
|
|
|
|
(let loop ((state 0))
|
|
|
|
(for-each-state
|
|
|
|
(if (< state num-states)
|
|
|
|
(lambda (state)
|
|
|
|
(begin
|
|
|
|
|
|
|
|
(let loop ((i 0))
|
|
|
|
(let loop ((i 0))
|
|
|
|
(if (< i num-gram-syms)
|
|
|
|
(if (< i num-gram-syms)
|
|
|
|
(begin
|
|
|
|
(begin
|
|
|
@ -244,12 +240,12 @@
|
|
|
|
(vector-ref get-non-term i)
|
|
|
|
(vector-ref get-non-term i)
|
|
|
|
(vector-ref get-term (- i num-non-terms))))
|
|
|
|
(vector-ref get-term (- i num-non-terms))))
|
|
|
|
(goto
|
|
|
|
(goto
|
|
|
|
(run-automaton (vector-ref get-state state)
|
|
|
|
(run-automaton (vector-ref get-state (kernel-index state))
|
|
|
|
s
|
|
|
|
s
|
|
|
|
a)))
|
|
|
|
a)))
|
|
|
|
(if goto
|
|
|
|
(if goto
|
|
|
|
(array2d-set! table
|
|
|
|
(array2d-set! table
|
|
|
|
state
|
|
|
|
(kernel-index state)
|
|
|
|
i
|
|
|
|
i
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((< i num-non-terms)
|
|
|
|
((< i num-non-terms)
|
|
|
@ -260,17 +256,12 @@
|
|
|
|
(make-shift
|
|
|
|
(make-shift
|
|
|
|
(kernel-index goto)))))))
|
|
|
|
(kernel-index goto)))))))
|
|
|
|
(loop (add1 i)))))
|
|
|
|
(loop (add1 i)))))
|
|
|
|
(let ((items
|
|
|
|
|
|
|
|
(filter (lambda (item)
|
|
|
|
|
|
|
|
(not (move-dot-right item)))
|
|
|
|
|
|
|
|
(kernel-items
|
|
|
|
|
|
|
|
(vector-ref get-state state)))))
|
|
|
|
|
|
|
|
(for-each
|
|
|
|
(for-each
|
|
|
|
(lambda (item)
|
|
|
|
(lambda (item)
|
|
|
|
(for-each
|
|
|
|
(for-each
|
|
|
|
(lambda (t)
|
|
|
|
(lambda (t)
|
|
|
|
(array2d-add! table
|
|
|
|
(array2d-add! table
|
|
|
|
state
|
|
|
|
(kernel-index state)
|
|
|
|
(+ num-non-terms (gram-sym-index t))
|
|
|
|
(+ num-non-terms (gram-sym-index t))
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((not (start-item? item))
|
|
|
|
((not (start-item? item))
|
|
|
@ -278,11 +269,14 @@
|
|
|
|
(item-prod-index item)
|
|
|
|
(item-prod-index item)
|
|
|
|
(gram-sym-index (prod-lhs (item-prod item)))
|
|
|
|
(gram-sym-index (prod-lhs (item-prod item)))
|
|
|
|
(vector-length (prod-rhs (item-prod item))))))))
|
|
|
|
(vector-length (prod-rhs (item-prod item))))))))
|
|
|
|
(get-lookahead (vector-ref get-state state)
|
|
|
|
(get-lookahead (vector-ref get-state (kernel-index state))
|
|
|
|
(item-prod item))))
|
|
|
|
(item-prod item))))
|
|
|
|
items))
|
|
|
|
(filter (lambda (item)
|
|
|
|
(loop (add1 state)))))
|
|
|
|
(not (move-dot-right item)))
|
|
|
|
(resolve-prec-conflicts table get-term get-prod num-states num-terms
|
|
|
|
(kernel-items
|
|
|
|
|
|
|
|
(vector-ref get-state (kernel-index state))))))
|
|
|
|
|
|
|
|
a)
|
|
|
|
|
|
|
|
(resolve-prec-conflicts a table get-term get-prod num-terms
|
|
|
|
num-non-terms)
|
|
|
|
num-non-terms)
|
|
|
|
(if (not (string=? file ""))
|
|
|
|
(if (not (string=? file ""))
|
|
|
|
(with-handlers [(exn:i/o:filesystem?
|
|
|
|
(with-handlers [(exn:i/o:filesystem?
|
|
|
@ -294,9 +288,9 @@
|
|
|
|
(exn:i/o:filesystem-detail e))))]
|
|
|
|
(exn:i/o:filesystem-detail e))))]
|
|
|
|
(call-with-output-file file
|
|
|
|
(call-with-output-file file
|
|
|
|
(lambda (port)
|
|
|
|
(lambda (port)
|
|
|
|
(display-parser table get-term get-non-term get-state (grammar-prods g)
|
|
|
|
(display-parser a table get-term get-non-term (grammar-prods g)
|
|
|
|
port)))))
|
|
|
|
port)))))
|
|
|
|
(resolve-conflicts table num-states num-terms num-non-terms)
|
|
|
|
(resolve-conflicts a table num-terms num-non-terms)
|
|
|
|
table))
|
|
|
|
table))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|