":"
(require (lib "list.ss" "srfi" "1"))
(require (lib "string.ss" "srfi" "13"))
(define lhs (make-hash-table 'equal))
(define rhs (make-hash-table 'equal))
(define rhsl (make-hash-table 'equal))
(define fold-lines-in-file
(lambda (filename proc init . mode)
(with-input-from-file filename
(lambda ()
(apply fold-lines proc init (current-input-port) mode)))))
(define fold-lines
(lambda (proc init . port+mode)
(let while ((accum init))
(let ([line (apply read-line port+mode)])
(if (eof-object? line) accum
(while (proc line)))))))
(define parse-rule!
(lambda (line)
(if (< 0 (string-length line))
(unless (eq? (string-ref line 0) #\#)
(let ([tokens (string-tokenize line)])
(if (string= (cadr tokens) "->")
(let* ([lhst (string->symbol (car tokens))]
[rhst (map-in-order (lambda (x)
(string->symbol x)) (cddr tokens))]
[rhsinit (car rhst)]
[rhslval (hash-table-get rhsl rhsinit '())]
[lhsval (hash-table-get lhs lhst '())]
[rhsval (hash-table-get rhs rhst '())])
(unless (member rhst lhsval)
(hash-table-put! lhs lhst (cons rhst lhsval)))
(unless (member lhst rhsval)
(hash-table-put! rhs rhst (cons lhst rhsval)))
(unless (member rhst rhslval)
(hash-table-put! rhsl rhsinit (cons rhst rhslval))))))))))
(define load-grammar
(lambda (filename)
(fold-lines-in-file filename
(lambda (line)
(parse-rule! (string-trim-both line)))
1)))