branch: externals/parser-generator commit 44eb5a3eb66550edf275e9a3d0e9dea2de9c7e3c Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Passing unit test for V(e) and V(S) --- parser.el | 18 ++++++++++++++---- test/parser-test.el | 12 +++++------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/parser.el b/parser.el index f33c556..fb09c89 100644 --- a/parser.el +++ b/parser.el @@ -11,7 +11,7 @@ (defvar parser--debug - t + nil "Whether to print debug messages or not.") (defvar parser--table-non-terminal-p @@ -677,6 +677,8 @@ ;; Check if RHS starts with a non-terminal (let ((rhs-first (car rhs))) + (parser--debug + (message "rhs-first: %s" rhs-first)) (when (parser--valid-non-terminal-p rhs-first) (let ((rhs-rest (append (cdr rhs) suffix))) (let ((rhs-rest-first (parser--first rhs-rest))) @@ -685,9 +687,18 @@ (unless rhs-rest-first (setq rhs-rest-first '((e)))) (let ((sub-production (parser--get-grammar-rhs rhs-first))) + (parser--debug + (message "sub-production: %s" sub-production)) ;; For each production with B as LHS (dolist (sub-rhs sub-production) + + ;; Set follow to nil if it's the e-identifier + (when (and + (= (length sub-rhs) 1) + (eq (car sub-rhs) 'e)) + (setq sub-rhs nil)) + (parser--debug (message "sub-rhs: %s" sub-rhs)) @@ -707,11 +718,10 @@ (message "V(e) = %s" lr-items-e)) (puthash '(e) lr-items-e lr-items)) - ;; Do step 2 only if prefix is not the e identifier + ;; 2 Suppose that we have constructed V(X1,X2,...,Xi-1) we construct V(X1,X2,...,Xi) as follows: (unless (and (= (length γ) 1) (eq (car γ) 'e)) - ;; 2 Suppose that we have constructed V(X1,X2,...,Xi-1) we construct V(X1,X2,...,Xi) as follows: (let ((prefix-acc) (prefix-previous (gethash '(e) lr-items))) (dolist (prefix γ) @@ -776,7 +786,7 @@ (puthash prefix-acc lr-new-item lr-items))))) (parser--debug - (message "γ: %s" γ)) + (message "γ: %s" γ)) (gethash γ lr-items)))) diff --git a/test/parser-test.el b/test/parser-test.el index fa0517f..87189e1 100644 --- a/test/parser-test.el +++ b/test/parser-test.el @@ -231,15 +231,13 @@ (parser--set-grammar '((Sp S) (a b) ((Sp S) (S (S a S b)) (S e)) Sp)) (parser--set-look-ahead-number 1) - '((S (e) nil (e)) (S (e) nil (a))) - (should (equal - '((Sp nil (S) (e)) - (S nil (S a S b) (e)) - (S nil (S a S b) (a)) - (S nil nil (e)) - (S nil nil (a))) + '((S nil nil (a)) + (S nil (S a S b) (a)) + (S nil nil (e)) + (S nil (S a S b) (e)) + (Sp nil (S) (e))) (parser--lr-items 'e))) (message "Passed V(e)")