All, I completed an LALR(1) parser generator a couple of weeks ago, and I thought it might be useful to some of you. (My CommonLisp version seems to be well-used). Anyway, here it is; it has been tested on a couple of small grammars and on a larger one (the purpose for which it was written) and seems to work fine. Still, there may be errors in it. This message contains three files; lalr.ss, lalr-test.ss, and a utility called assoc.ss (this is only needed for parse table construction). Each file is separated by a line like the next one. ----------------------------------------------------------------------- ;;; lalr.ss - An LALR(1) parser generator ;;; ;;; Author: Mark Johnson (mj@cs.brown.edu) ;;; Date: 24th May, 1993 ;;; Version: 0.9 ;;; ;;; ;;; The parser generator consists of two functions. The first constructs ;;; the parse tables, which the second function uses to actually parse. ;;; You can see how to use these in the file lalr-test.ss. ;;; ;;; (lalr-table grammar terminals print-table-flag) returns ;;; the lalr parsing table for the grammar. Its arguments are: ;;; ;;; grammar: A list of productions, each of which is a list of the ;;; form ( --> ... ), where is a symbol ;;; (a category label) and is a procedure of appropriate ;;; arity. The procedure will be called each time this production ;;; is reduced with the values associated with each child node. ;;; The categories can be any symbol _except_ $start$ and $end$. ;;; The grammar's start symbol is the parent category of the first ;;; production, i.e., (caar grammar). ;;; ;;; terminals: A list of all the categories that the lexical analyser ;;; can return. ;;; ;;; print-table-flag: If non-#f, causes the pretty-printing of the ;;; lalr parse tables as a side-effect. Parse conflicts are indicated ;;; in the table (search for ** ). ;;; ;;; (lalr-parser table lexical-analyser parse-error) returns the value ;;; associated with the root node if the parse is successful, or the ;;; value of parse-error otherwise. ;;; ;;; table: A parse table produced by lalr-table. ;;; ;;; lexical-analyser: A procedure of no arguments which advances the ;;; input stream by one element each time it is called, returning ;;; (cons ) where is the category label of the ;;; next token, and is the value associated with that token. ;;; It should return #f at the end of the input stream. ;;; ;;; parse-error: A procedure of no arguments, which is called if the ;;; the parser blocks (i.e., detects a syntactic error in the input ;;; stream). ;;; ;;; ;;; The parser resolves any parse conflicts in a standard way; ;;; shift/reduce conflicts are resolved by shifting, and reduce/reduce ;;; conflicts are resolved by choosing the longest applicable ;;; reduction. ;;; ;;; Note: It is most convenient to use the backquote mechanism to ;;; enter the grammar into scheme. The actions, which are procedures, ;;; can be created by unquoting a corresponding lambda expression ;;; (see the associated example file). You can use lalr-table to ;;; produce expressions that can appear in Scheme programs by changing ;;; the backquote infront of the grammar to a normal quote. (require 'sort) (require 'assoc) (define (lalr-table grammar terminals print-flag) (define new-start-symbol '$start$) (define end-marker '$end$) ;;;;;;; Utilities (define (list-prefix elts n) (if (zero? n) '() (cons (car elts) (list-prefix (cdr elts) (- n 1))))) (define (list-suffix elts n) (if (zero? n) elts (list-suffix (cdr elts) (- n 1)))) (define (sublist elts start . end) (if (null? end) (list-suffix elts start) (list-prefix (list-suffix elts start) (- (car end) start)))) (define (butlast elts) (cond ((null? elts) '()) ((null? (cdr elts)) '()) (else (cons (car elts) (butlast (cdr elts)))))) (define (last elts) (cond ((null? elts) #f) ((null? (cdr elts)) (car elts)) (else (last (cdr elts))))) (define (select p? es) (cond ((null? es) '()) ((p? (car es)) (cons (car es) (select p? (cdr es)))) (else (select p? (cdr es))))) (define (find-if p? es) (cond ((null? es) #f) ((p? (car es)) (car es)) (else (find-if p? (cdr es))))) (define (some p? es) (if (null? es) #f (or (p? (car es)) (some p? (cdr es))))) (define (every p? es) (if (null? es) #t (and (p? (car es)) (every p? (cdr es))))) (define (reduce f es init) (if (null? es) init (reduce f (cdr es) (f (car es) init)))) (define (union e1s e2s) (if (null? e1s) e2s (if (member (car e1s) e2s) (union (cdr e1s) e2s) (cons (car e1s) (union (cdr e1s) e2s))))) (define (intersection e1s e2s) (if (null? e1s) '() (if (member (car e1s) e2s) (cons (car e1s) (intersection (cdr e1s) e2s)) (intersection (cdr e1s) e2s)))) (define (subtract e1s e2s) (if (null? e1s) '() (if (member (car e1s) e2s) (subtract (cdr e1s) e2s) (cons (car e1s) (subtract (cdr e1s) e2s))))) (define (unions sets) (cond ((null? sets) '()) ((null? (cdr sets)) (car sets)) (else (union (car sets) (unions (cdr sets)))))) (define (close op es) (define (close1 todo sofar) (if (pair? todo) (close1 (cdr todo) (if (member (car todo) sofar) sofar (close1 (op (car todo)) (cons (car todo) sofar)))) sofar)) (close1 es '())) (define (collect f es) (define (collect1 todo sofar) (if (null? todo) sofar (let ((val (f (car todo)))) (collect1 (cdr todo) (if val (adjoin val sofar) sofar))))) (collect1 es '())) (define (adjoin e es) (if (member e es) es (cons e es))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; ;;; Globals ;;; (define cat@ atom@) (define memoize1 (lambda (assoc-maker fn) (let* ((store ((assoc-maker 'make))) (ref (assoc-maker 'ref)) (setter! (assoc-maker 'set!))) (lambda (arg) (or (ref store arg) (let ((val (fn arg))) (setter! store arg val) val)))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; ;;; Rules and Grammars ;;; (define (make-rule index-number mother daughters action) (vector index-number mother daughters action)) (define (rule-no rule) (vector-ref rule 0)) (define (rule-mother rule) (vector-ref rule 1)) (define (rule-daughters rule) (vector-ref rule 2)) (define (rule-action rule) (vector-ref rule 3)) (define (transform-rule grammar-rule rule-no) (let ((l (length grammar-rule))) (make-rule rule-no (car grammar-rule) (butlast (cddr grammar-rule)) (last grammar-rule)))) (let* ((grules (let ((i -1)) (map (lambda (r) (set! i (1+ i)) (transform-rule r i)) grammar))) (nrules (length grules)) (nonterminals (collect rule-mother grules)) (start-symbol (caar grammar)) (expand (memoize1 cat@ (lambda (cat) (select (lambda (rule) (eq? (rule-mother rule) cat)) grules)))) (gcats (union terminals (collect rule-mother grules))) (derives-epsilon? (memoize1 cat@ (lambda (c) (define (try dejaVu cat) (and (not (member cat dejaVu)) (some (lambda (r) (every (lambda (c1) (try (cons cat dejaVu) c1)) (rule-daughters r))) (expand cat)))) (try '() c)))) (left-corners (lambda (c) (reduce (lambda (rule sofar) (define (skip rhs sofar) (if (null? rhs) sofar (if (derives-epsilon? (car rhs)) (skip (cdr rhs) (adjoin (car rhs) sofar)) (adjoin (car rhs) sofar)))) (skip (rule-daughters rule) sofar)) (expand c) '()))) (left-most-terminals (memoize1 cat@ (lambda (c0) (select (lambda (term) (or (eq? end-marker term) (member term terminals))) (close left-corners (list c0))))))) (define (left-most catList) (if (pair? catList) (if (derives-epsilon? (car catList)) (union (left-most-terminals (car catList)) (left-most (cdr catList))) (left-most-terminals (car catList))) '())) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; ;;; LR(0) parsing table constructor ;;; (define (make-item rule pos) (vector pos rule '())) (define (item-rule item) (vector-ref item 1)) (define (item-pos item) (vector-ref item 0)) (define (item-las item) (vector-ref item 2)) (define (item-las-push! item la) (vector-set! item 2 (cons la (vector-ref item 2)))) (define (item-daughters item) (rule-daughters (item-rule item))) (define (item-right item) (list-suffix (item-daughters item) (item-pos item))) (define (item-next item) (let ((rhs (item-right item))) (if (pair? rhs) (car rhs) #f))) ;;;(define (item rn1 rn2) #f) ;;; (else (< (item-pos item1) (item-pos item2)))))) (define (item ip1 ip2) #t) ((< ip1 ip2) #f) (else (< (rule-no (item-rule item1)) (rule-no (item-rule item2))))))) ;;; deleted because states must *not* share items! ;;;(define cat->items ;;; (memoize1 cat@ ;;; (lambda (cat) ;;; (map (lambda (rule) (make-item rule 0)) ;;; (expand cat))))) (define (cat->items cat) (map (lambda (rule) (make-item rule 0)) (expand cat))) (define (close-items items) (close (lambda (item) (let ((rh-cat (item-next item))) (if rh-cat (cat->items rh-cat) '()))) items)) (define (shift-items items cat) (collect (lambda (item) (if (eq? cat (item-next item)) (make-item (item-rule item) (1+ (item-pos item))) #f)) items)) ;;; returns the set of categories appearing to the right of the dot (define (items-next items) (collect item-next items)) ;;; The actual table construction functions (define (make-state no items) (vector no items #f)) (define (state-no state) (vector-ref state 0)) (define (state-items state) (vector-ref state 1)) (define (state-shifts state) (vector-ref state 2)) (define (state-shifts-set! state shifts) (vector-set! state 2 shifts)) (define (sort-items! items) (sort! items item") (for-each space-display (sublist (rule-daughters (item-rule item)) 0 (item-pos item))) (space-display ".") (for-each space-display (item-right item)) (space-display ";") (for-each space-display (item-las item))) (state-items state)) (newline) (for-each (lambda (shift) (newline) (display " On ") (display (car shift)) (display " shift to state ") (display (cdr shift))) (state-shifts state)) (for-each (lambda (item) (newline) (display " On") (for-each space-display (item-las item)) (display " reduce: ") (display (rule-mother (item-rule item))) (display " -->") (for-each space-display (rule-daughters (item-rule item))) (let ((cs (intersection (item-las item) deja-vu))) (if (not (null? cs)) (set! conflicts (union cs conflicts))))) (select (lambda (item) (null? (item-right item))) (state-items state))) (if (not (null? conflicts)) (begin (newline) (display " ** Conflicting actions on") (for-each space-display conflicts))) (newline)))) (propagate-la 0 (item-rule initial-item) 0 end-marker) (if print-flag (print-table)) (let ((shift-vec (make-vector (vector-length state-vec))) (goto-vec (make-vector (vector-length state-vec))) (redn-vec (make-vector (vector-length state-vec))) (rule-parent-vec (make-vector nrules)) (rule-length-vec (make-vector nrules)) (rule-action-vec (make-vector nrules))) (do ((i 0 (+ i 1))) ((= i (vector-length state-vec))) (let* ((state (vector-ref state-vec i)) (so-far (map car (state-shifts state)))) (vector-set! shift-vec i (select (lambda (shift) (member (car shift) terminals)) (state-shifts state))) (vector-set! goto-vec i (select (lambda (shift) (member (car shift) nonterminals)) (state-shifts state))) (vector-set! redn-vec i (map (lambda (item) (let ((new-las (subtract (item-las item) so-far))) (set! so-far (append new-las so-far)) (cons (rule-no (item-rule item)) new-las))) (select (lambda (item) (null? (item-right item))) (state-items state)))))) (for-each (lambda (rule) (let ((no (rule-no rule))) (vector-set! rule-parent-vec no (rule-mother rule)) (vector-set! rule-length-vec no (length (rule-daughters rule))) (vector-set! rule-action-vec no (rule-action rule)))) grules) (vector shift-vec goto-vec redn-vec rule-parent-vec rule-length-vec rule-action-vec) )))) (define (lalr-parser lalr-tables lexical-analyser parse-error) (define end-marker '$end$) (define (find-redn la redns) (if (null? redns) #f (if (memq la (cdar redns)) (caar redns) (find-redn la (cdr redns))))) (define (list-prefix elts n) (if (zero? n) '() (cons (car elts) (list-prefix (cdr elts) (- n 1))))) (define (list-suffix elts n) (if (zero? n) elts (list-suffix (cdr elts) (- n 1)))) (let ((shift-vec (vector-ref lalr-tables 0)) (goto-vec (vector-ref lalr-tables 1)) (redn-vec (vector-ref lalr-tables 2)) (rule-parent (vector-ref lalr-tables 3)) (rule-length (vector-ref lalr-tables 4)) (rule-action (vector-ref lalr-tables 5)) (next-cat #f) (next-val #f)) (define (advance-input) (let ((p (lexical-analyser))) (if (pair? p) (begin (set! next-cat (car p)) (set! next-val (cdr p))) (begin (set! next-cat end-marker) (set! next-val #f))))) (define (move* state states vals) (let* ((shift-pair (assq next-cat (vector-ref shift-vec state)))) (if shift-pair (let ((old-val next-val)) (advance-input) (move* (cdr shift-pair) (cons state states) (cons old-val vals))) (let ((redn (find-redn next-cat (vector-ref redn-vec state)))) (if redn (if (and (= redn -1) (eq? next-cat end-marker)) (car vals) (let* ((l (vector-ref rule-length redn)) (new-states (if (zero? l) (cons state states) (list-suffix states (- l 1))))) (move* (cdr (assq (vector-ref rule-parent redn) (vector-ref goto-vec (car new-states)))) new-states (cons (apply (vector-ref rule-action redn) (reverse (list-prefix vals l))) (list-suffix vals l))))) (parse-error)))))) (advance-input) (move* 0 '() '()))) ----------------------------------------------------------------------- ;;; associators.ss ;;; ;;; (eval-when (compile) (optimize-level 2)) ;;; ;;; Mark Johnson, April 1993. ;;; ;;; An associator is a suite of functions for manipulating a finite function ;;; from keys to values. This file contains associators based on avl trees ;;; (which are balanced binary trees) and tries (which extend an associator ;;; from keys of type T to keys of type list-of-T), as well as associators ;;; based on association lists and vectors. ;;; ;;; At the bottom of this file there are predefined associators for atoms ;;; and for lists of atoms. ;;; ;;; Because all associators have the same interface, it should be possible ;;; to e.g., develop a program using simple, general associators (e.g., ;;; the associators based on association lists), and substitute more ;;; efficient, specialized associators if needed. ;;; ;;; An associator-maker is a function which maps keywords into appropriate ;;; associator manipulation functions. Here are the keywords an associator ;;; should understand: ;;; ;;; ((associator-maker 'make)) returns a new associator that associates ;;; #f with every value. ;;; ((associator-maker 'ref) associator key) returns value associated with key ;;; in associator. ;;; ((associator-maker 'set!) associator key value) destructively changes the ;;; value associated with key in associator to be value, and ;;; returns the old value. ;;; ((associator-maker 'update!) associator key update-fn) destructively ;;; changes the value associated with key to (update-fn key ;;; old-value), where old-value was the value that the associator ;;; previously associated with value. ;;; ((associator-maker 'push!) associator key elt) destructively changes the ;;; value associated with key to (cons elt elts), where elts is ;;; '() if the value associated with key was #f, and the value ;;; associated with key otherwise. ;;; ((associator-maker 'inc!) associator key inc) destructively changes the ;;; value associated with key to (+ inc value), where value is 0 ;;; if the value associated with key was #f, and the value ;;; associated with key otherwise. ;;; ((associator-maker 'map) associator fn) returns a new associator that ;;; assigns (fn key value) to each key where associator ;;; assigned a non-#f value value to key. ;;; ((associator-maker 'map!) associator fn) is the same as map, except ;;; that associator is destructively updated. ;;; ((associator-maker 'for-each) associator fn) calls (fn key value) on ;;; each key-value pair in associator such that value =/= #f. ;;; ((associator-maker 'reduce) associator fn start) calls ;;; (fn key value so-far) on each key-value pair in associator, ;;; where so-far is the value returned from the previous fn ;;; call (so-far in the first fn call is start). ;;; ;;; The idea is that given an associator-maker, the various associator ;;; manipulation functions will be assigned to local variables, rather than ;;; obtained by associator-maker each time the manipulation functions are used. ;;; ;;; Often a particular type of associator needs additional parameters, e.g., ;;; an ordering or equality predicate. The avl-maker, for example, takes an ;;; ordering predicate and returns an associator-maker. (define (assoc:inc!-maker update!) (lambda (assoc key inc) (if (not (zero? inc)) (update! assoc key (lambda (val) (if val (+ val inc) inc)))))) (define (assoc:push!-maker update!) (lambda (assoc key elt) (update! assoc key (lambda (elts) (if elts (cons elt elts) (list elt)))))) ;;; alist-associators are simple association lists. ;;; They are lists of the form (@ (key1 . val1) ...) ;;; They have the advantage of being easily readable. (define (alist-maker eqp?) (define (find-pair avs key) (if (pair? avs) (if (eqp? (caar avs) key) (car avs) (find-pair (cdr avs) key)) #f)) (define (push alist key value) (set-cdr! alist (cons (cons key value) (cdr alist))) #f) (define (lookup alist key) (let ((p (find-pair (cdr alist) key))) (if (pair? p) (cdr p) #f))) (define (update! alist key update-fn) (let ((p (find-pair (cdr alist) key))) (if (pair? p) (let ((v (cdr p))) (set-cdr! p (update-fn v)) v) (push alist key (update-fn #f))))) (define (reduce avs fn start) (if (pair? avs) (reduce (cdr avs) fn (if (cdar avs) (fn (caar avs) (cdar avs) start) start)) start)) (lambda (selector) (case selector ((make) (lambda () (list '@))) ((ref) lookup) ((set!) (lambda (alist key value) (update! alist key (lambda (oldvalue) value)))) ((update!) update!) ((inc!) (assoc:inc!-maker update!)) ((push!) (assoc:push!-maker update!)) ((map) (lambda (alist fn) (cons '@ (map (lambda (p) (cons (car p) (if (cdr p) (fn (car p) (cdr p)) #f))) (cdr alist))))) ((for-each) (lambda (alist fn) (for-each (lambda (p) (if (cdr p) (fn (car p) (cdr p)))) (cdr alist)))) ((map!) (lambda (alist fn) (for-each (lambda (p) (if (cdr p) (set-cdr! p (fn (car p) (cdr p))))) (cdr alist)) alist)) ((reduce) (lambda (alist fn start) (reduce (cdr alist) fn start))) (else (error "Unimplemented selector: " selector))))) ;;; avl-associators are balanced binary trees (define (avl-maker ordered?) (define (node-height node) (if node (vector-ref node 0) 0)) (define (node-left node) (vector-ref node 1)) (define (node-key node) (vector-ref node 2)) (define (node-value node) (vector-ref node 3)) (define (node-right node) (vector-ref node 4)) (define (set-node-height! node height) (vector-set! node 0 height)) (define (set-node-left! node left) (vector-set! node 1 left)) (define (set-node-key! node key) (vector-set! node 2 key)) (define (set-node-value! node value) (vector-set! node 3 value)) (define (set-node-right! node right) (vector-set! node 4 right)) (define (set-node-key-value! node key value) (set-node-height! node 1) (set-node-key! node key) (set-node-value! node value)) (define (empty? node) (not (vector-ref node 0))) (define (search key node) (if node (cond ((ordered? key (node-key node)) (search key (node-left node))) ((ordered? (node-key node) key) (search key (node-right node))) (else (node-value node))) #f)) (define (compute-height! node) (set-node-height! node (+ (max (node-height (node-left node)) (node-height (node-right node))) 1))) (define (rotate-left! a) (let* ((b (node-right a)) (b-key (node-key b)) (b-value (node-value b)) (b-right (node-right b))) (set-node-right! b (node-left b)) (set-node-left! b (node-left a)) (set-node-key! b (node-key a)) (set-node-value! b (node-value a)) (compute-height! b) (set-node-left! a b) (set-node-key! a b-key) (set-node-value! a b-value) (set-node-right! a b-right) ; (compute-height! a) a)) (define (rotate-right! a) (let* ((b (node-left a)) (b-key (node-key b)) (b-value (node-value b)) (b-left (node-left b))) (set-node-left! b (node-right b)) (set-node-right! b (node-right a)) (set-node-key! b (node-key a)) (set-node-value! b (node-value a)) (compute-height! b) (set-node-right! a b) (set-node-key! a b-key) (set-node-value! a b-value) (set-node-left! a b-left) ; (compute-height! a) a)) (define (rebalance-node! node) (case (- (node-height (node-left node)) (node-height (node-right node))) ((2) (rotate-right! node)) ((-2) (rotate-left! node))) (compute-height! node)) (define (insert! key value node) (let ((old-value (cond ((ordered? key (node-key node)) (if (node-left node) (insert! key value (node-left node)) (begin (set-node-left! node (vector 1 #f key value #f)) #f))) ((ordered? (node-key node) key) (if (node-right node) (insert! key value (node-right node)) (begin (set-node-right! node (vector 1 #f key value #f)) #f))) (else (let ((old-value (node-value node))) ; (set-node-key! node key) (set-node-value! node value) old-value))))) (rebalance-node! node) old-value)) (define (update! tree key update-fn) (define (updater key update-fn node) (cond ((ordered? key (node-key node)) (if (node-left node) (updater key update-fn (node-left node)) (set-node-left! node (vector 1 #f key (update-fn #f) #f)))) ((ordered? (node-key node) key) (if (node-right node) (updater key update-fn (node-right node)) (set-node-right! node (vector 1 #f key (update-fn #f) #f)))) (else (set-node-value! node (update-fn (node-value node))))) (rebalance-node! node)) (if (empty? tree) (set-node-key-value! tree key (update-fn #f)) (updater key update-fn tree))) (define (foreach-node fn node) (if node (begin (foreach-node fn (node-left node)) (fn (node-key node) (node-value node)) (foreach-node fn (node-right node))))) (define (map!-node fn node) (if node (begin (map!-node fn (node-left node)) (set-node-value! node (fn (node-key node) (node-value node))) (map!-node fn (node-right node))))) (define (reduce-node fn node so-far) (if node (reduce-node fn (node-left node) (fn (node-key node) (node-value node) (reduce-node fn (node-right node) so-far))) so-far)) (define (map-node fn node) (if (vector? node) (vector (vector-ref node 0) (map-node fn (vector-ref node 1)) (vector-ref node 2) (fn (vector-ref node 2) (vector-ref node 3)) (map-node fn (vector-ref node 4))) #f)) (lambda (selector) (case selector ((make) (lambda () (make-vector 5 #f))) ((make-with-value) (lambda (key value) (vector 1 #f key value #f))) ((empty?) empty?) ((ref) (lambda (tree key) (if (empty? tree) #f (search key tree)))) ((set!) (lambda (tree key value) (if (empty? tree) (begin (set-node-key-value! tree key value) #f) (insert! key value tree)))) ((for-each) (lambda (tree fn) (if (empty? tree) #f (foreach-node fn tree)))) ((map) (lambda (tree fn) (if (empty? tree) (make-vector 5 #f) (map-node fn tree)))) ((map!) (lambda (tree fn) (if (empty? tree) #f (map!-node fn tree)) tree)) ((reduce) (lambda (tree fn start) (if (empty? tree) start (reduce-node fn tree start)))) ((update!) update!) ((inc!) (assoc:inc!-maker update!)) ((push!) (assoc:push!-maker update!)) (else (error "Unimplemented selector: " selector))))) (define (trie-maker associator-maker) (let ((assoc-new (associator-maker 'make)) (assoc-update! (associator-maker 'update!)) (assoc-insert! (associator-maker 'set!)) (assoc-lookup (associator-maker 'ref)) (assoc-map (associator-maker 'map)) (assoc-map! (associator-maker 'map!)) (assoc-reduce (associator-maker 'reduce)) (assoc-foreach (associator-maker 'for-each))) (define (search trie keys) (if trie (if (null? keys) (car trie) (if (not (null? (cdr trie))) (search (assoc-lookup (cdr trie) (car keys)) (cdr keys)) #f)) #f)) (define (assoc-new-value key value) (let ((a (assoc-new))) (assoc-insert! a key value) a)) (define (new-path keys value) (if (null? keys) (list value) (cons #f (assoc-new-value (car keys) (new-path (cdr keys) value))))) (define (insert! trie keys value) (let ((old-value #f)) (define (inserter keys trie) (cond ((null? keys) (set! old-value (car trie)) (set-car! trie value)) ((null? (cdr trie)) (set-cdr! trie (assoc-new-value (car keys) (new-path (cdr keys) value)))) (else (assoc-update! (cdr trie) (car keys) (lambda (sub-trie) (if sub-trie (inserter (cdr keys) sub-trie) (new-path (cdr keys) value)))))) trie) (inserter keys trie) old-value)) (define (update! trie keys update-fn) (define (updater keys trie) (cond ((null? trie) (new-path keys (update-fn #f))) ((null? keys) (set-car! trie (update-fn (car trie)))) ((null? (cdr trie)) (set-cdr! trie (assoc-new-value (car keys) (new-path (cdr keys) (update-fn #f))))) (else (assoc-update! (cdr trie) (car keys) (lambda (sub-trie) (if sub-trie (updater (cdr keys) sub-trie) (new-path (cdr keys) (update-fn #f))))))) trie) (updater keys trie)) (define (reduce-trie fn keys trie so-far0) (if (pair? trie) (let ((so-far1 (if (car trie) (fn (reverse keys) (car trie) so-far0) so-far0))) (if (null? (cdr trie)) so-far1 (assoc-reduce (cdr trie) (lambda (key new-trie so-far2) (reduce-trie fn (cons key keys) new-trie so-far2)) so-far1))) so-far0)) (define (foreach-trie fn keys trie) (if (pair? trie) (begin (if (car trie) (fn (reverse keys) (car trie))) (if (not (null? (cdr trie))) (assoc-foreach (cdr trie) (lambda (key new-trie) (foreach-trie fn (cons key keys) new-trie))))))) (define (map!-trie fn keys trie) (if (pair? trie) (begin (if (car trie) (set-car! trie (fn (reverse keys) (car trie)))) (if (not (null? (cdr trie))) (assoc-foreach (cdr trie) (lambda (key new-trie) (map!-trie fn (cons key keys) new-trie))))))) (define (map-trie fn keys trie) (if trie (cons (if (car trie) (fn (reverse keys) (car trie)) #f) (if (null? (cdr trie)) '() (assoc-map (cdr trie) (lambda (key new-trie) (map-trie fn (cons key keys) new-trie))))) #f)) (lambda (selector) (case selector ((make) (lambda () (list #f))) ((ref) search) ((set!) insert!) ((map) (lambda (trie fn) (map-trie fn '() trie))) ((update!) update!) ((reduce) (lambda (trie fn start) (reduce-trie fn '() trie start))) ((for-each) (lambda (trie fn) (foreach-trie fn '() trie) trie)) ((map!) (lambda (trie fn) (map!-trie fn '() trie) trie)) ((inc!) (assoc:inc!-maker update!)) ((push!) (assoc:push!-maker update!)) (else (error "Unimplemented selector: " selector)))))) (define (vector-associator size fill) (lambda (selector) (case selector ((make) (lambda () (make-vector size fill))) ((ref) vector-ref) ((set!) (lambda (vec i v) (let ((old-value (vector-ref vec i))) (vector-set! vec i v) old-value))) ((map) (lambda (old-vec fn) (let ((new-vec (make-vector size))) (do ((i (- size 1) (- i 1))) ((negative? i) new-vec) (let ((v (vector-ref old-vec i))) (if v (vector-set! new-vec i (fn i v)))))))) ((update!) (lambda (vec i f) (vector-set! vec i (f (vector-ref vec i))))) ((reduce) (lambda (vec f thread) (define (reduce i thread) (if (negative? i) thread (reduce (- i 1) (let ((v (vector-ref vec i))) (if v (f i v thread) thread))))) (reduce (- size 1) thread))) ((for-each) (lambda (vec proc) (do ((i (- size 1) (- i 1))) ((negative? i)) (let ((v (vector-ref vec i))) (if v (proc i v)))))) ((map!) (lambda (vec proc) (do ((i (- size 1) (- i 1))) ((negative? i)) (let ((v (vector-ref vec i))) (if v (vector-set! vec i (proc i v))))) vec)) ((inc!) (lambda (vec i inc) (if (not (zero? inc)) (let ((v (vector-ref vec i))) (vector-set! vec i (if v (+ v i) i)))))) ((push!) (lambda (vec i e) (vector-set! vec i (let ((v (vector-ref vec i))) (if v (cons e v) (list v)))))) (else (error "Unimplemented selector: " selector))))) ;;; define an associator from atoms to values (define atom@ (avl-maker (lambda (a1 a2) (if (symbol? a1) (if (symbol? a2) (stringstring a1) (symbol->string a2)) #f) (if (symbol? a2) #t (< a1 a2)))))) (define atom@-make (atom@ 'make)) (define atom@-ref (atom@ 'ref)) (define atom@-set! (atom@ 'set!)) (define atom@-map (atom@ 'map)) (define atom@-map! (atom@ 'map!)) (define atom@-update! (atom@ 'update!)) (define atom@-reduce (atom@ 'reduce)) (define atom@-for-each (atom@ 'for-each)) (define atom@-push! (atom@ 'push!)) (define atom@-inc! (atom@ 'inc!)) ;;; define an associator from list-of-symbols->value (define atoms@ (trie-maker atom@)) (define atoms@-make (atoms@ 'make)) (define atoms@-ref (atoms@ 'ref)) (define atoms@-set! (atoms@ 'set!)) (define atoms@-map (atoms@ 'map)) (define atoms@-map! (atoms@ 'map!)) (define atoms@-update! (atoms@ 'update!)) (define atoms@-reduce (atoms@ 'reduce)) (define atoms@-for-each (atoms@ 'for-each)) (define atoms@-push! (atoms@ 'push!)) (define atoms@-inc! (atoms@ 'inc!)) ----------------------------------------------------------------------- ;;; lalr-test.ss - An example file showing how the lalr parser can be used. ;;; ;;; An example using the lalr parser. This file defines the function ;;; eval-string, which takes a string representing a simple arithmetic ;;; expression and returns its value. E.g. ;;; ;;; (eval-string " 45 + 76 * 2 ") ==> 197 ;;; ;;; To produce an expression defining the parse table which you could compile, ;;; change the backquote to a quote in the definition of expr-grammar, ;;; and evaluate ;;; ;;; `(define table ,(list 'quasiquote (lalr-table expr-grammar expr-terminals #f))) ;;; ;;; Be sure to include any procedures referenced in expr-grammar (in this ;;; example, binary-apply and identity) (define (binary-apply expr1 op expr2) (op expr1 expr2)) (define (identity expr) expr) (define expr-grammar `((expr --> expr expr-op term ,binary-apply) ;;; change ` to ' (expr --> term ,identity) ;;; if you want to generate (term --> term term-op term0 ,binary-apply) ;;; a Scheme program table (term --> term0 ,identity) (term0 --> lparen expr rparen ,(lambda (lp expr rp) expr)) (term0 --> number ,identity) (number --> number digit ,(lambda (n d) (+ (* 10 n) d))) (number --> digit ,identity))) (define expr-terminals '(expr-op term-op lparen rparen digit)) (define table (lalr-table expr-grammar expr-terminals #f)) (define (eval-string string) (let ((pos 0)) (define (lexical-analyser) (if (< pos (string-length string)) (let ((char (string-ref string pos))) (set! pos (+ pos 1)) (if (char=? char #\ ) (lexical-analyser) (case char ((#\+) `(expr-op . ,+)) ((#\-) `(expr-op . ,-)) ((#\*) `(term-op . ,*)) ((#\/) `(term-op . ,/)) ((#\() '(lparen . #f)) ((#\)) '(rparen . #f)) ((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9) `(digit . ,(- (char->integer char) (char->integer #\0)))) (else (parse-error))))))) (define (parse-error) (display "Error somewhere in ") (write (substring string 0 pos)) (newline)) (lalr-parser table lexical-analyser parse-error)))