branch: externals/parser-generator commit e463baea4e60a8b257a73652d17672b4335e99f1 Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Passing tests for sorting lists --- parser.el | 21 ++++++--------------- test/parser-test.el | 10 +++++----- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/parser.el b/parser.el index 038378f..2d2dfa0 100644 --- a/parser.el +++ b/parser.el @@ -117,13 +117,13 @@ (defun parser--sort-list (a b) "Return non-nil if a element in A is greater than a element in B in lexicographic order." - (let ((max-index (1- (min (length a) (length b)))) + (let ((length (min (length a) (length b))) (index 0) (continue t) (response nil)) (while (and continue - (< index max-index)) + (< index length)) (let ((a-element (nth index a)) (b-element (nth index b))) (if (string-greaterp a-element b-element) @@ -428,7 +428,7 @@ (when (> (length sub-rhs-leading-terminals) k) (setq sub-rhs-leading-terminals (butlast sub-rhs-leading-terminals (- (length sub-rhs-leading-terminals) k)))) (push `(,sub-rhs-leading-terminals ,alternative-all-leading-terminals-p ,(1+ input-tape-index)) stack)))) - (setq sub-terminal-index (1+ sub-terminal-index))))) + (setq sub-terminal-index (1+ sub-terminal-index))))) (parser--debug (message "Sub-terminal-set: %s" sub-terminal-set)) (when (or @@ -562,22 +562,19 @@ (setq input-tape-index (1+ input-tape-index))) (when (> first-length 0) (push first first-list)))))) - (message "first-list-before-sort: %s" first-list) (setq first-list (sort first-list 'parser--sort-list)) - (message "first-list-after-sort: %s" first-list) first-list)))) ;; Definition p. 343, FOLLOW(β) = w, w is the set {w|β=>*aβy and w is in FIRST(y)} (defun parser--follow (β) - "Calculate follow-set of B." + "Calculate follow-set of Β." ;; Make sure argument is a list (unless (listp β) (setq β (list β))) (let ((follow-set nil) (match-length (length β))) ;; Iterate all productions in grammar - (let ((productions (parser--get-grammar-productions)) - (k parser--look-ahead-number)) + (let ((productions (parser--get-grammar-productions))) (dolist (p productions) ;; Iterate all RHS of every production (let ((production-rhs (cdr p)) @@ -600,19 +597,13 @@ (progn (setq match-index (1+ match-index)) (when (= match-index match-length) - (parser--debug - (message "found full follow hit: %s" β)) (if (= rhs-index (1- rhs-count)) ;; If rest of RHS is empty add e in follow-set (push '(e) follow-set) ;; Otherwise add FOLLOW(rest) to follow-set (let ((rest (nthcdr (1+ rhs-index) rhs))) - (parser--debug - (message "rest: %s" rest)) (let ((first-set (parser--first rest))) - (parser--debug - (message "rest-first-set: %s" first-set)) - (push first-set follow-set)))) + (setq follow-set (append first-set follow-set))))) (setq match-index 0))) (when (> match-index 0) (setq match-index 0)))) diff --git a/test/parser-test.el b/test/parser-test.el index e787e4c..1817fcd 100644 --- a/test/parser-test.el +++ b/test/parser-test.el @@ -152,7 +152,7 @@ (parser--set-look-ahead-number 1) (should (equal - '(("d") ("c")) + '(("c") ("d")) (parser--first 'S))) (message "Passed first 1 with semi-complex grammar") @@ -176,7 +176,7 @@ (parser--set-look-ahead-number 1) (should (equal - '((a) (a) (c) (e)) + '((a) (b) (c) (e)) (parser--first 'S))) (message "Passed first 1 with complex grammar") @@ -185,7 +185,7 @@ (parser--set-look-ahead-number 2) (should (equal - '((a) (a c) (a b) (c a) (b a) (e) (c) (b) (c b)) + '((a b) (a c) (a) (b a) (b) (c a) (c) (c b) (e)) (parser--first 'S))) (message "Passed first 2 with complex grammar") @@ -193,7 +193,7 @@ (parser--set-look-ahead-number 3) (should (equal - '((a c b) (a) (a c) (a b) (c a) (c a c) (c a b) (b a) (b a c) (b a b) (c b) (e) (c) (b) (c b a)) + '((a) (a b) (a c) (a c b) (b a) (b a b) (b a c) (b) (c a) (c a b) (c a c) (c b) (c) (c b a) (e)) (parser--first 'S))) (message "Passed first 3 with complex grammar") @@ -209,7 +209,7 @@ (parser--set-look-ahead-number 2) (should (equal - '((c b) (c a)) + '((c a) (c b)) (parser--e-free-first 'S))) (message "Passed empty-free-first 2 with complex grammar")