branch: externals/parser-generator commit b73c4ed6c88740f8e422ade0358dc4cd2cd78c6e Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Made e-symbol customizable --- README.md | 2 +- parser.el | 36 ++++++++++++++++++++---------------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 8c514f2..aa3667c 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ Grammar consists of `N`, `T`, `P` and `S`, where `N` is non-terminals, `T` is te ### e -The symbol `'e` is hard-coded to be the empty symbol. The symbol is allowed in some grammars and not in others. +The symbol defined in variable `parser--e-identifier`, with default-value: 'e`, symbolizes the e symbol. The symbol is allowed in some grammars and not in others. ### Non-terminals diff --git a/parser.el b/parser.el index a2f2b32..efa4dfe 100644 --- a/parser.el +++ b/parser.el @@ -14,29 +14,33 @@ nil "Whether to print debug messages or not.") -(defvar parser--table-non-terminal-p +(defvar parser--e-identifier + 'e + "The identifier used for e-symbol. Default value 'e.") + +(defvar parser--grammar nil - "Hash-table of terminals for quick checking.") + "Current grammar used in parser.") -(defvar parser--table-productions +(defvar parser--f-sets nil - "Hash-table of productions for quick retrieving.") + "Generated F-sets for grammar.") -(defvar parser--table-terminal-p +(defvar parser--look-ahead-number nil - "Hash-table of non-terminals for quick checking.") + "Current look-ahead number used.") -(defvar parser--grammar +(defvar parser--table-non-terminal-p nil - "Current grammar used in parser.") + "Hash-table of terminals for quick checking.") -(defvar parser--look-ahead-number +(defvar parser--table-productions nil - "Current look-ahead number used.") + "Hash-table of productions for quick retrieving.") -(defvar parser--f-sets +(defvar parser--table-terminal-p nil - "Generated F-sets for grammar.") + "Hash-table of non-terminals for quick checking.") ;; Macros @@ -167,7 +171,7 @@ (defun parser--valid-e-p (symbol) "Return whether SYMBOL is the e identifier or not." - (eq symbol 'e)) + (eq symbol parser--e-identifier)) (defun parser--valid-grammar-p (G) "Return if grammar G is valid or not. Grammar should contain list with 4 elements: non-terminals (N), terminals (T), productions (P), start (S) where N, T and P are lists containing symbols and/or strings and S is a symbol or string." @@ -649,7 +653,7 @@ (let ((S) (marked-sets (make-hash-table :test 'equal)) (symbols (append (parser--get-grammar-non-terminals) (parser--get-grammar-terminals)))) - (let ((e-set (parser--lr-items-for-prefix 'e))) + (let ((e-set (parser--lr-items-for-prefix parser--e-identifier))) ;; TODO (1) Place V(e) in S. The set V(e) is initially unmarked. ) (let ((found-unmarked t)) @@ -731,7 +735,7 @@ ;; Set follow to nil if it's the e-identifier (when (and (= (length sub-rhs) 1) - (eq (car sub-rhs) 'e)) + (parser--valid-e-p (car sub-rhs))) (setq sub-rhs nil)) (parser--debug @@ -756,7 +760,7 @@ ;; 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)) + (parser--valid-e-p (car γ))) (let ((prefix-acc) (prefix-previous (gethash '(e) lr-items))) (dolist (prefix γ)