|
|
@ -4,14 +4,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
(require "grammar.ss"
|
|
|
|
(require "grammar.ss"
|
|
|
|
"lr0.ss"
|
|
|
|
"lr0.ss"
|
|
|
|
"array2d.ss"
|
|
|
|
|
|
|
|
"lalr.ss"
|
|
|
|
"lalr.ss"
|
|
|
|
"parser-actions.ss"
|
|
|
|
"parser-actions.ss"
|
|
|
|
(lib "list.ss")
|
|
|
|
(lib "contract.ss")
|
|
|
|
|
|
|
|
(lib "list.ss")
|
|
|
|
(lib "class.ss"))
|
|
|
|
(lib "class.ss"))
|
|
|
|
|
|
|
|
|
|
|
|
(provide build-table)
|
|
|
|
(provide/contract
|
|
|
|
|
|
|
|
(build-table ((is-a?/c grammar%) string? any/c . -> .
|
|
|
|
|
|
|
|
(vectorof (listof (cons/c (union term? non-term?) action?))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; A parse-table is (vectorof (listof (cons/c gram-sym? action)))
|
|
|
|
|
|
|
|
;; A grouped-parse-table is (vectorof (listof (cons/c gram-sym? (listof action))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; make-parse-table : int -> parse-table
|
|
|
|
|
|
|
|
(define (make-parse-table num-states)
|
|
|
|
|
|
|
|
(make-vector num-states null))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; table-add!: parse-table nat symbol action ->
|
|
|
|
|
|
|
|
(define (table-add! table state-index symbol val)
|
|
|
|
|
|
|
|
(vector-set! table state-index (cons (cons symbol val)
|
|
|
|
|
|
|
|
(vector-ref table state-index))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; group-table : parse-table -> grouped-parse-table
|
|
|
|
|
|
|
|
(define (group-table table)
|
|
|
|
|
|
|
|
(list->vector
|
|
|
|
|
|
|
|
(map
|
|
|
|
|
|
|
|
(lambda (state-entry)
|
|
|
|
|
|
|
|
(let ((ht (make-hash-table 'equal)))
|
|
|
|
|
|
|
|
(for-each
|
|
|
|
|
|
|
|
(lambda (gs/actions)
|
|
|
|
|
|
|
|
(let ((group (hash-table-get ht (car gs/actions) (lambda () null))))
|
|
|
|
|
|
|
|
(unless (member (cdr gs/actions) group)
|
|
|
|
|
|
|
|
(hash-table-put! ht (car gs/actions) (cons (cdr gs/actions) group)))))
|
|
|
|
|
|
|
|
state-entry)
|
|
|
|
|
|
|
|
(hash-table-map ht cons)))
|
|
|
|
|
|
|
|
(vector->list table))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; table-map : (vectorof (listof (cons/c gram-sym? X))) (gram-sym? X -> Y) ->
|
|
|
|
|
|
|
|
;; (vectorof (listof (cons/c gram-sym? Y)))
|
|
|
|
|
|
|
|
(define (table-map f table)
|
|
|
|
|
|
|
|
(list->vector
|
|
|
|
|
|
|
|
(map
|
|
|
|
|
|
|
|
(lambda (state-entry)
|
|
|
|
|
|
|
|
(map
|
|
|
|
|
|
|
|
(lambda (gs/X)
|
|
|
|
|
|
|
|
(cons (car gs/X) (f (car gs/X) (cdr gs/X))))
|
|
|
|
|
|
|
|
state-entry))
|
|
|
|
|
|
|
|
(vector->list table))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (bit-vector-for-each f bv)
|
|
|
|
(define (bit-vector-for-each f bv)
|
|
|
|
(letrec ((for-each
|
|
|
|
(letrec ((for-each
|
|
|
@ -25,19 +66,19 @@
|
|
|
|
(for-each bv 0)))
|
|
|
|
(for-each bv 0)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; print-entry: symbol * action * output-port ->
|
|
|
|
;; print-entry: symbol action output-port ->
|
|
|
|
;; prints the action a for lookahead sym to port
|
|
|
|
;; prints the action a for lookahead sym to the given port
|
|
|
|
(define (print-entry sym a port)
|
|
|
|
(define (print-entry sym a port)
|
|
|
|
(let ((s "\t~a\t\t\t\t\t~a\t~a\n"))
|
|
|
|
(let ((s "\t~a\t\t\t\t\t~a\t~a\n"))
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((shift? a)
|
|
|
|
((shift? a)
|
|
|
|
(fprintf port s sym "shift" (shift-state a)))
|
|
|
|
(fprintf port s sym "shift" (shift-state a)))
|
|
|
|
((reduce? a)
|
|
|
|
((reduce? a)
|
|
|
|
(fprintf port s sym "reduce" (reduce-prod-num a)))
|
|
|
|
(fprintf port s sym "reduce" (prod-index (reduce-prod a))))
|
|
|
|
((accept? a)
|
|
|
|
((accept? a)
|
|
|
|
(fprintf port s sym "accept" ""))
|
|
|
|
(fprintf port s sym "accept" ""))
|
|
|
|
(a
|
|
|
|
((goto? a)
|
|
|
|
(fprintf port s sym "goto" a)))))
|
|
|
|
(fprintf port s sym "goto" (goto-state a))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; count: ('a -> bool) * 'a list -> num
|
|
|
|
;; count: ('a -> bool) * 'a list -> num
|
|
|
@ -48,14 +89,10 @@
|
|
|
|
((pred (car list)) (+ 1 (count pred (cdr list))))
|
|
|
|
((pred (car list)) (+ 1 (count pred (cdr list))))
|
|
|
|
(else (count pred (cdr list)))))
|
|
|
|
(else (count pred (cdr list)))))
|
|
|
|
|
|
|
|
|
|
|
|
;; display-parser:
|
|
|
|
;; display-parser: LR0-automaton grouped-parse-table (listof prod?) output-port ->
|
|
|
|
;; action array2d * term vector * non-term vector * kernel vector *
|
|
|
|
|
|
|
|
;; output-port ->
|
|
|
|
|
|
|
|
;; Prints out the parser given by table.
|
|
|
|
;; Prints out the parser given by table.
|
|
|
|
(define (display-parser a table terms non-terms prods port)
|
|
|
|
(define (display-parser a grouped-table prods port)
|
|
|
|
(let* ((num-terms (vector-length terms))
|
|
|
|
(let* ((SR-conflicts 0)
|
|
|
|
(num-non-terms (vector-length non-terms))
|
|
|
|
|
|
|
|
(SR-conflicts 0)
|
|
|
|
|
|
|
|
(RR-conflicts 0))
|
|
|
|
(RR-conflicts 0))
|
|
|
|
(for-each
|
|
|
|
(for-each
|
|
|
|
(lambda (prod)
|
|
|
|
(lambda (prod)
|
|
|
@ -66,231 +103,169 @@
|
|
|
|
(map gram-sym-symbol (vector->list (prod-rhs prod)))))
|
|
|
|
(map gram-sym-symbol (vector->list (prod-rhs prod)))))
|
|
|
|
prods)
|
|
|
|
prods)
|
|
|
|
(send a for-each-state
|
|
|
|
(send a for-each-state
|
|
|
|
(lambda (state)
|
|
|
|
(lambda (state)
|
|
|
|
(fprintf port "State ~a~n" (kernel-index state))
|
|
|
|
(fprintf port "State ~a~n" (kernel-index state))
|
|
|
|
(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 state))
|
|
|
|
(kernel-items state))
|
|
|
|
(newline port)
|
|
|
|
(newline port)
|
|
|
|
(let loop ((j 0))
|
|
|
|
(for-each
|
|
|
|
(if (< j num-terms)
|
|
|
|
(lambda (gs/action)
|
|
|
|
(begin
|
|
|
|
(let ((sym (gram-sym-symbol (car gs/action)))
|
|
|
|
(let ((act (array2d-ref
|
|
|
|
(act (cdr gs/action)))
|
|
|
|
table
|
|
|
|
(cond
|
|
|
|
(kernel-index state)
|
|
|
|
((null? (cdr act))
|
|
|
|
(+ j num-non-terms))))
|
|
|
|
(print-entry sym (car act) port))
|
|
|
|
(cond
|
|
|
|
(else
|
|
|
|
((list? act)
|
|
|
|
(fprintf port "begin conflict:~n")
|
|
|
|
(fprintf port "begin conflict:~n")
|
|
|
|
(if (> (count reduce? act) 1)
|
|
|
|
(if (> (count reduce? act) 1)
|
|
|
|
(set! RR-conflicts (add1 RR-conflicts)))
|
|
|
|
(set! RR-conflicts (add1 RR-conflicts)))
|
|
|
|
(if (> (count shift? act) 0)
|
|
|
|
(if (> (count shift? act) 0)
|
|
|
|
(set! SR-conflicts (add1 SR-conflicts)))
|
|
|
|
(set! SR-conflicts (add1 SR-conflicts)))
|
|
|
|
(map (lambda (x) (print-entry sym x port)) act)
|
|
|
|
(map (lambda (x)
|
|
|
|
(fprintf port "end conflict~n")))))
|
|
|
|
(print-entry
|
|
|
|
(vector-ref grouped-table (kernel-index state)))
|
|
|
|
(gram-sym-symbol (vector-ref terms j))
|
|
|
|
(newline port)))
|
|
|
|
x
|
|
|
|
|
|
|
|
port))
|
|
|
|
(when (> SR-conflicts 0)
|
|
|
|
act)
|
|
|
|
(fprintf port "~a shift/reduce conflicts~n" SR-conflicts))
|
|
|
|
(fprintf port "end conflict~n"))
|
|
|
|
(when (> RR-conflicts 0)
|
|
|
|
(act (print-entry
|
|
|
|
(fprintf port "~a reduce/reduce conflicts~n" RR-conflicts))))
|
|
|
|
(gram-sym-symbol (vector-ref terms j))
|
|
|
|
|
|
|
|
act
|
|
|
|
;; resolve-conflict : (listof action?) -> action? bool bool
|
|
|
|
port))))
|
|
|
|
(define (resolve-conflict actions)
|
|
|
|
(loop (add1 j)))))
|
|
|
|
(cond
|
|
|
|
|
|
|
|
((null? (cdr actions))
|
|
|
|
(newline port)
|
|
|
|
(values (car actions) #f #f))
|
|
|
|
|
|
|
|
(else
|
|
|
|
(let loop ((j 0))
|
|
|
|
(let ((SR-conflict? (> (count shift? actions) 0))
|
|
|
|
(if (< j num-non-terms)
|
|
|
|
(RR-conflict? (> (count reduce? actions) 1)))
|
|
|
|
(begin
|
|
|
|
(let loop ((current-guess #f)
|
|
|
|
(let ((s (array2d-ref table (kernel-index state) j)))
|
|
|
|
(rest actions))
|
|
|
|
(if s
|
|
|
|
(cond
|
|
|
|
(print-entry
|
|
|
|
((null? rest) (values current-guess SR-conflict? RR-conflict?))
|
|
|
|
(gram-sym-symbol (vector-ref non-terms j))
|
|
|
|
((shift? (car rest)) (values (car rest) SR-conflict? RR-conflict?))
|
|
|
|
s
|
|
|
|
((not current-guess)
|
|
|
|
port)))
|
|
|
|
(loop (car rest) (cdr rest)))
|
|
|
|
(loop (add1 j)))))
|
|
|
|
((and (reduce? (car rest))
|
|
|
|
|
|
|
|
(< (prod-index (reduce-prod (car rest)))
|
|
|
|
(newline port)))
|
|
|
|
(prod-index (reduce-prod current-guess))))
|
|
|
|
|
|
|
|
(loop (car rest) (cdr rest)))
|
|
|
|
(if (> SR-conflicts 0)
|
|
|
|
((accept? (car rest))
|
|
|
|
(fprintf port "~a shift/reduce conflicts~n" SR-conflicts))
|
|
|
|
(fprintf (current-error-port)
|
|
|
|
(if (> RR-conflicts 0)
|
|
|
|
"accept/reduce or accept/shift conflicts. Check the grammar for useless cycles of productions~n")
|
|
|
|
(fprintf port "~a reduce/reduce conflicts~n" RR-conflicts))))
|
|
|
|
(loop current-guess (cdr rest)))
|
|
|
|
|
|
|
|
(else (loop current-guess (cdr rest)))))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; resolve-conflicts : grouped-parse-table bool -> parse-table
|
|
|
|
|
|
|
|
(define (resolve-conflicts grouped-table suppress)
|
|
|
|
|
|
|
|
(let* ((SR-conflicts 0)
|
|
|
|
|
|
|
|
(RR-conflicts 0)
|
|
|
|
|
|
|
|
(table (table-map
|
|
|
|
|
|
|
|
(lambda (gs actions)
|
|
|
|
|
|
|
|
(let-values (((action SR? RR?)
|
|
|
|
|
|
|
|
(resolve-conflict actions)))
|
|
|
|
|
|
|
|
(when SR?
|
|
|
|
|
|
|
|
(set! SR-conflicts (add1 SR-conflicts)))
|
|
|
|
|
|
|
|
(when RR?
|
|
|
|
|
|
|
|
(set! RR-conflicts (add1 RR-conflicts)))
|
|
|
|
|
|
|
|
action))
|
|
|
|
|
|
|
|
grouped-table)))
|
|
|
|
|
|
|
|
(unless suppress
|
|
|
|
|
|
|
|
(when (> SR-conflicts 0)
|
|
|
|
|
|
|
|
(fprintf (current-error-port)
|
|
|
|
|
|
|
|
"~a shift/reduce conflicts~n"
|
|
|
|
|
|
|
|
SR-conflicts))
|
|
|
|
|
|
|
|
(when (> RR-conflicts 0)
|
|
|
|
|
|
|
|
(fprintf (current-error-port)
|
|
|
|
|
|
|
|
"~a reduce/reduce conflicts~n"
|
|
|
|
|
|
|
|
RR-conflicts)))
|
|
|
|
|
|
|
|
table))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (resolve-conflicts a table num-terms num-non-terms suppress)
|
|
|
|
;; resolve-sr-conflict : (listof action) (union int #f) -> (listof action)
|
|
|
|
(letrec ((SR-conflicts 0)
|
|
|
|
;; Resolves a single shift-reduce conflict, if precedences are in place.
|
|
|
|
(RR-conflicts 0)
|
|
|
|
(define (resolve-sr-conflict/prec actions shift-prec)
|
|
|
|
(get-action
|
|
|
|
(let* ((shift (if (shift? (car actions))
|
|
|
|
(lambda (entry)
|
|
|
|
(car actions)
|
|
|
|
(cond
|
|
|
|
(cadr actions)))
|
|
|
|
((list? entry)
|
|
|
|
(reduce (if (shift? (car actions))
|
|
|
|
(if (> (count shift? entry) 0)
|
|
|
|
(cadr actions)
|
|
|
|
(set! SR-conflicts (add1 SR-conflicts)))
|
|
|
|
(car actions)))
|
|
|
|
(if (> (count reduce? entry) 1)
|
|
|
|
(reduce-prec (prod-prec (reduce-prod reduce))))
|
|
|
|
(set! RR-conflicts (add1 RR-conflicts)))
|
|
|
|
(cond
|
|
|
|
(let loop ((current-guess (make-reduce +inf.0 -1 -1))
|
|
|
|
((and shift-prec reduce-prec)
|
|
|
|
(rest entry))
|
|
|
|
(list
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((null? rest) current-guess)
|
|
|
|
((< (prec-num shift-prec) (prec-num reduce-prec))
|
|
|
|
((shift? (car rest)) (car rest))
|
|
|
|
reduce)
|
|
|
|
((and (reduce? (car rest))
|
|
|
|
((> (prec-num shift-prec) (prec-num reduce-prec))
|
|
|
|
(< (reduce-prod-num (car rest))
|
|
|
|
shift)
|
|
|
|
(reduce-prod-num current-guess)))
|
|
|
|
((eq? 'left (prec-assoc shift-prec))
|
|
|
|
(loop (car rest) (cdr rest)))
|
|
|
|
reduce)
|
|
|
|
((accept? (car rest))
|
|
|
|
((eq? 'right (prec-assoc shift-prec))
|
|
|
|
(fprintf (current-error-port)
|
|
|
|
shift))))
|
|
|
|
"accept/reduce or accept/shift conflicts. Check the grammar for useless cycles of productions~n")
|
|
|
|
(else actions))))
|
|
|
|
(loop current-guess (cdr rest)))
|
|
|
|
|
|
|
|
(else (loop current-guess (cdr rest))))))
|
|
|
|
|
|
|
|
(else entry)))))
|
|
|
|
|
|
|
|
(send a for-each-state
|
|
|
|
|
|
|
|
(lambda (state)
|
|
|
|
|
|
|
|
(let loop ((term 0))
|
|
|
|
|
|
|
|
(if (< term num-terms)
|
|
|
|
|
|
|
|
(begin
|
|
|
|
|
|
|
|
(array2d-set! table (kernel-index state) (+ num-non-terms term)
|
|
|
|
|
|
|
|
(get-action
|
|
|
|
|
|
|
|
(array2d-ref table
|
|
|
|
|
|
|
|
(kernel-index state)
|
|
|
|
|
|
|
|
(+ num-non-terms term))))
|
|
|
|
|
|
|
|
(loop (add1 term)))))))
|
|
|
|
|
|
|
|
(if (not suppress)
|
|
|
|
|
|
|
|
(begin
|
|
|
|
|
|
|
|
(if (> SR-conflicts 0)
|
|
|
|
|
|
|
|
(fprintf (current-error-port)
|
|
|
|
|
|
|
|
"~a shift/reduce conflicts~n"
|
|
|
|
|
|
|
|
SR-conflicts))
|
|
|
|
|
|
|
|
(if (> RR-conflicts 0)
|
|
|
|
|
|
|
|
(fprintf (current-error-port)
|
|
|
|
|
|
|
|
"~a reduce/reduce conflicts~n"
|
|
|
|
|
|
|
|
RR-conflicts))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; resolve-prec-conflicts : parse-table -> grouped-parse-table
|
|
|
|
(define (resolve-prec-conflicts a table get-term get-prod
|
|
|
|
(define (resolve-prec-conflicts table)
|
|
|
|
num-terms num-non-terms)
|
|
|
|
(table-map
|
|
|
|
(send a for-each-state
|
|
|
|
(lambda (gs actions)
|
|
|
|
(lambda (state)
|
|
|
|
(cond
|
|
|
|
(let loop ((term 0))
|
|
|
|
((and (term? gs)
|
|
|
|
(if (< term num-terms)
|
|
|
|
(= 2 (length actions))
|
|
|
|
(begin
|
|
|
|
(or (shift? (car actions))
|
|
|
|
(let ((action (array2d-ref table
|
|
|
|
(shift? (cadr actions))))
|
|
|
|
(kernel-index state)
|
|
|
|
(resolve-sr-conflict/prec actions (term-prec gs)))
|
|
|
|
(+ num-non-terms term))))
|
|
|
|
(else actions)))
|
|
|
|
(if (and (list? action)
|
|
|
|
(group-table table)))
|
|
|
|
(= 2 (length action))
|
|
|
|
|
|
|
|
(or (shift? (car action))
|
|
|
|
;; build-table: grammar string bool -> parse-table
|
|
|
|
(shift? (cadr action))))
|
|
|
|
|
|
|
|
(let* ((shift (if (shift? (car action))
|
|
|
|
|
|
|
|
(car action)
|
|
|
|
|
|
|
|
(cadr action)))
|
|
|
|
|
|
|
|
(reduce (if (shift? (car action))
|
|
|
|
|
|
|
|
(cadr action)
|
|
|
|
|
|
|
|
(car action)))
|
|
|
|
|
|
|
|
(s-prec (term-prec
|
|
|
|
|
|
|
|
(vector-ref get-term
|
|
|
|
|
|
|
|
term)))
|
|
|
|
|
|
|
|
(r-prec (prod-prec
|
|
|
|
|
|
|
|
(vector-ref
|
|
|
|
|
|
|
|
get-prod
|
|
|
|
|
|
|
|
(reduce-prod-num reduce)))))
|
|
|
|
|
|
|
|
(if (and s-prec r-prec)
|
|
|
|
|
|
|
|
(array2d-set!
|
|
|
|
|
|
|
|
table
|
|
|
|
|
|
|
|
(kernel-index state)
|
|
|
|
|
|
|
|
(+ num-non-terms term)
|
|
|
|
|
|
|
|
(cond
|
|
|
|
|
|
|
|
((< (prec-num s-prec)
|
|
|
|
|
|
|
|
(prec-num r-prec))
|
|
|
|
|
|
|
|
reduce)
|
|
|
|
|
|
|
|
((> (prec-num s-prec)
|
|
|
|
|
|
|
|
(prec-num r-prec))
|
|
|
|
|
|
|
|
shift)
|
|
|
|
|
|
|
|
((eq? 'left (prec-assoc s-prec))
|
|
|
|
|
|
|
|
reduce)
|
|
|
|
|
|
|
|
((eq? 'right (prec-assoc s-prec))
|
|
|
|
|
|
|
|
shift)
|
|
|
|
|
|
|
|
(else #f)))))))
|
|
|
|
|
|
|
|
(loop (add1 term))))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; 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)
|
|
|
|
|
|
|
|
;; buile-table: grammar * string -> action array2d
|
|
|
|
|
|
|
|
(define (build-table g file suppress)
|
|
|
|
(define (build-table g file suppress)
|
|
|
|
(let* ((a (build-lr0-automaton g))
|
|
|
|
(let* ((a (build-lr0-automaton g))
|
|
|
|
(num-terms (send g get-num-terms))
|
|
|
|
(term-list (send g get-terms))
|
|
|
|
(num-non-terms (send g get-num-non-terms))
|
|
|
|
(term-vector (list->vector term-list))
|
|
|
|
(get-term (list->vector (send g get-terms)))
|
|
|
|
(non-term-list (send g get-non-terms))
|
|
|
|
(get-non-term (list->vector (send g get-non-terms)))
|
|
|
|
(end-terms (send g get-end-terms))
|
|
|
|
(get-prod (list->vector (send g get-prods)))
|
|
|
|
(table (make-parse-table (send a get-num-states)))
|
|
|
|
(end-term-indexes
|
|
|
|
|
|
|
|
(map
|
|
|
|
|
|
|
|
(lambda (term)
|
|
|
|
|
|
|
|
(+ num-non-terms (gram-sym-index term)))
|
|
|
|
|
|
|
|
(send g get-end-terms)))
|
|
|
|
|
|
|
|
(num-gram-syms (+ num-terms num-non-terms))
|
|
|
|
|
|
|
|
(table (make-array2d (send a get-num-states) num-gram-syms #f))
|
|
|
|
|
|
|
|
(array2d-add!
|
|
|
|
|
|
|
|
(lambda (v i1 i2 a)
|
|
|
|
|
|
|
|
(let ((old (array2d-ref v i1 i2)))
|
|
|
|
|
|
|
|
(cond
|
|
|
|
|
|
|
|
((not old) (array2d-set! v i1 i2 a))
|
|
|
|
|
|
|
|
((list? old) (if (not (member a old))
|
|
|
|
|
|
|
|
(array2d-set! v i1 i2 (cons a old))))
|
|
|
|
|
|
|
|
(else (if (not (equal? a old))
|
|
|
|
|
|
|
|
(array2d-set! v i1 i2 (list a old))))))))
|
|
|
|
|
|
|
|
(get-lookahead (compute-LA a g)))
|
|
|
|
(get-lookahead (compute-LA a g)))
|
|
|
|
|
|
|
|
|
|
|
|
(send a for-each-state
|
|
|
|
(send a for-each-state
|
|
|
|
(lambda (state)
|
|
|
|
(lambda (state)
|
|
|
|
(let loop ((i 0))
|
|
|
|
(for-each
|
|
|
|
(if (< i num-gram-syms)
|
|
|
|
(lambda (gs)
|
|
|
|
(begin
|
|
|
|
(let ((goto (send a run-automaton state gs)))
|
|
|
|
(let* ((s (if (< i num-non-terms)
|
|
|
|
(when goto
|
|
|
|
(vector-ref get-non-term i)
|
|
|
|
(table-add! table (kernel-index state) gs
|
|
|
|
(vector-ref get-term (- i num-non-terms))))
|
|
|
|
(cond
|
|
|
|
(goto
|
|
|
|
((non-term? gs)
|
|
|
|
(send a run-automaton state s)))
|
|
|
|
(make-goto (kernel-index goto)))
|
|
|
|
(if goto
|
|
|
|
((member gs end-terms)
|
|
|
|
(array2d-set! table
|
|
|
|
(make-accept))
|
|
|
|
(kernel-index state)
|
|
|
|
(else
|
|
|
|
i
|
|
|
|
(make-shift
|
|
|
|
(cond
|
|
|
|
(kernel-index goto))))))))
|
|
|
|
((< i num-non-terms)
|
|
|
|
(append non-term-list term-list))
|
|
|
|
(kernel-index goto))
|
|
|
|
|
|
|
|
((member i end-term-indexes)
|
|
|
|
(for-each
|
|
|
|
(make-accept))
|
|
|
|
(lambda (item)
|
|
|
|
(else
|
|
|
|
(let ((item-prod (item-prod item)))
|
|
|
|
(make-shift
|
|
|
|
(bit-vector-for-each
|
|
|
|
(kernel-index goto)))))))
|
|
|
|
(lambda (term-index)
|
|
|
|
(loop (add1 i)))))
|
|
|
|
(unless (start-item? item)
|
|
|
|
(for-each
|
|
|
|
(table-add! table
|
|
|
|
(lambda (item)
|
|
|
|
(kernel-index state)
|
|
|
|
(let ((item-prod (item-prod item)))
|
|
|
|
(vector-ref term-vector term-index)
|
|
|
|
(bit-vector-for-each
|
|
|
|
(make-reduce item-prod))))
|
|
|
|
(lambda (term-index)
|
|
|
|
(get-lookahead state item-prod))))
|
|
|
|
(array2d-add! table
|
|
|
|
|
|
|
|
(kernel-index state)
|
|
|
|
(append (hash-table-get (send a get-epsilon-trans) state (lambda () null))
|
|
|
|
(+ num-non-terms term-index)
|
|
|
|
(filter (lambda (item)
|
|
|
|
(cond
|
|
|
|
(not (move-dot-right item)))
|
|
|
|
((not (start-item? item))
|
|
|
|
(kernel-items state))))))
|
|
|
|
(make-reduce
|
|
|
|
|
|
|
|
(prod-index item-prod)
|
|
|
|
|
|
|
|
(gram-sym-index (prod-lhs item-prod))
|
|
|
|
|
|
|
|
(vector-length (prod-rhs item-prod)))))))
|
|
|
|
|
|
|
|
(get-lookahead state item-prod))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(append (hash-table-get (send a get-epsilon-trans) state (lambda () null))
|
|
|
|
|
|
|
|
(filter (lambda (item)
|
|
|
|
|
|
|
|
(not (move-dot-right item)))
|
|
|
|
|
|
|
|
(kernel-items state))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(resolve-prec-conflicts a table get-term get-prod num-terms
|
|
|
|
(let ((grouped-table (resolve-prec-conflicts table)))
|
|
|
|
num-non-terms)
|
|
|
|
(unless (string=? file "")
|
|
|
|
(if (not (string=? file ""))
|
|
|
|
|
|
|
|
(with-handlers [(exn:fail:filesystem?
|
|
|
|
(with-handlers [(exn:fail:filesystem?
|
|
|
|
(lambda (e)
|
|
|
|
(lambda (e)
|
|
|
|
(fprintf
|
|
|
|
(fprintf
|
|
|
@ -299,12 +274,9 @@
|
|
|
|
file)))]
|
|
|
|
file)))]
|
|
|
|
(call-with-output-file file
|
|
|
|
(call-with-output-file file
|
|
|
|
(lambda (port)
|
|
|
|
(lambda (port)
|
|
|
|
(display-parser a table get-term get-non-term (send g get-prods)
|
|
|
|
(display-parser a grouped-table (send g get-prods) port)))))
|
|
|
|
port)))))
|
|
|
|
|
|
|
|
|
|
|
|
(resolve-conflicts grouped-table suppress))))
|
|
|
|
(resolve-conflicts a table num-terms num-non-terms suppress)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
table))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|