[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/parser-generator e88abf0 117/434: More work on parser,
From: |
ELPA Syncer |
Subject: |
[elpa] externals/parser-generator e88abf0 117/434: More work on parser, added error-handling |
Date: |
Mon, 29 Nov 2021 15:59:21 -0500 (EST) |
branch: externals/parser-generator
commit e88abf06bf5293f2c7eac61c5d61748185246fde
Author: Christian Johansson <christian@cvj.se>
Commit: Christian Johansson <christian@cvj.se>
More work on parser, added error-handling
---
parser-lr.el | 127 ++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 82 insertions(+), 45 deletions(-)
diff --git a/parser-lr.el b/parser-lr.el
index aaf899d..0c24c43 100644
--- a/parser-lr.el
+++ b/parser-lr.el
@@ -474,6 +474,7 @@
lr-new-item))
;; Algorithm 5.7, p. 375
+;; TODO Add support for lex-analyzer
;; TODO Add support for SDT
;; TODO Add support for semantic-actions
(defun parser-lr--parse (input-tape &optional input-tape-index pushdown-list)
@@ -483,34 +484,34 @@
(unless pushdown-list
(push 0 pushdown-list))
- (let ((input-tape-length (length input-tape))
- (right-parse)
- (goto-tables (parser-lr--generate-goto-tables))
- (no-error t))
+ (let ((accept nil)
+ (input-tape-length (length input-tape))
+ (output)
+ (goto-tables (parser-lr--generate-goto-tables)))
(let ((action-tables (parser-lr--generate-action-tables)))
(while (and
- no-error
+ (not accept)
(< input-tape-index input-tape-length))
;; (1) The lookahead string u, consisting of the next k input symbols,
is determined.
- (let ((look-ahead-string)
- (look-ahead-string-length 0))
+ (let ((look-ahead)
+ (look-ahead-length 0))
(while (and
(< input-tape-index input-tape-length)
- (< look-ahead-string-length parser--look-ahead-number))
- (push (pop input-tape) look-ahead-string)
- (setq look-ahead-string-length (1+ look-ahead-string-length))
+ (< look-ahead-length parser--look-ahead-number))
+ (push (pop input-tape) look-ahead)
+ (setq look-ahead-length (1+ look-ahead-length))
(setq input-tape-index (1+ input-tape-index)))
;; If we reached end of input-tape and look-ahead is too small,
append e-identifiers
- (while (< look-ahead-string-length parser--look-ahead-number)
- (push parser--e-identifier look-ahead-string)
- (setq look-ahead-string-length (1+ look-ahead-string-length)))
+ (while (< look-ahead-length parser--look-ahead-number)
+ (push parser--e-identifier look-ahead)
+ (setq look-ahead-length (1+ look-ahead-length)))
- (setq look-ahead-string (nreverse look-ahead-string))
- (message "Look-ahead-string: %s" look-ahead-string)
+ (setq look-ahead (nreverse look-ahead))
+ (message "look-ahead: %s" look-ahead)
(let ((table-index (car pushdown-list)))
(message "table-index: %s" table-index)
@@ -520,36 +521,72 @@
(message "action-table: %s" action-table)
(message "goto-table: %s" goto-table)
- ;; TODO (2) The parsing action f of the table on top of the
pushdown list is applied to the lookahead string u.
-
- ;; TODO (a) If f(u) = shift, then the next input symbol, say a
- ;; is removed from the input and shifted onto the pushdown
list.
- ;; The goto function g of the table on top of the pushdown
list
- ;; is applied to a to determine the new table to be placed on
- ;; top of the pushdown list. We then return to step(1). If
- ;; there is no next input symbol or g(a) is undefined, halt
- ;; and declare error.
-
- ;; TODO (b) If f(u) = reduce i and production i is A -> a,
- ;; then 2|a| symbols are removed from the top of the pushdown
- ;; list, and production number i is placed in the output
- ;; buffer. A new table T' is then exposed as the top table
- ;; of the pushdown list, and the goto function of T' is
applied
- ;; to A to determine the next table to be placed on top of the
- ;; pushdown list. We place A and this new table on top of the
- ;; the pushdown list and return to step (1)
-
- ;; TODO (c) If f(u) = error, we halt parsing (and, in practice
- ;; transfer to an error recovery routine).
-
- ;; TODO (d) If f(u) = accept, we halt and declare the string
- ;; in the output buffer to be the right parse of the original
- ;; input string.
-
- ))
-
- )))
- right-parse))
+ (let ((action-match nil)
+ (action-table-length (length action-table))
+ (action-index 0)
+ (possible-look-aheads))
+
+ ;; (2) The parsing action f of the table on top of the
pushdown list is applied to the lookahead string u.
+ (while (and
+ (not action-match)
+ (< action-index action-table-length))
+ (let ((action (nth action-index action-table)))
+ (let ((action-look-ahead (car action)))
+ (message "action-look-ahead: %s" action-look-ahead)
+ (push action-look-ahead possible-look-aheads)
+ (when (equal action-look-ahead look-ahead)
+ (setq action-match (cdr action))
+ (message "action-match: %s" action-match))))
+ (setq action-index (1+ action-index)))
+
+ (unless action-match
+ ;; (c) If f(u) = error, we halt parsing (and, in practice
+ ;; transfer to an error recovery routine).
+
+ (error (format
+ "Invalid syntax! Expected one of %s found %s at
input-tape-index %s"
+ possible-look-aheads
+ look-ahead
+ input-tape-index)))
+
+ (cond
+
+ ((eq action-match '(shift))
+ (let ((a (car look-ahead)))
+ ;; TODO (a) If f(u) = shift, then the next input symbol,
say a
+ ;; is removed from the input and shifted onto the
pushdown list.
+ ;; The goto function g of the table on top of the
pushdown list
+ ;; is applied to a to determine the new table to be
placed on
+ ;; top of the pushdown list. We then return to step(1). If
+ ;; there is no next input symbol or g(a) is undefined,
halt
+ ;; and declare error.
+
+ (push a pushdown-list)
+
+
+ ))
+
+ ((eq (car action-match) 'reduce)
+ ;; TODO (b) If f(u) = reduce i and production i is A -> a,
+ ;; then 2|a| symbols are removed from the top of the
pushdown
+ ;; list, and production number i is placed in the output
+ ;; buffer. A new table T' is then exposed as the top table
+ ;; of the pushdown list, and the goto function of T' is
applied
+ ;; to A to determine the next table to be placed on top
of the
+ ;; pushdown list. We place A and this new table on top of
the
+ ;; the pushdown list and return to step (1)
+
+ )
+
+ ((eq action-match '(accept))
+ ;; (d) If f(u) = accept, we halt and declare the string
+ ;; in the output buffer to be the right parse of the
original
+ ;; input string.
+
+ (setq accept t))
+
+ )))))))
+ output))
(provide 'parser-lr)
- [elpa] externals/parser-generator 7cfdea2 165/434: Passing tests for incremental lexer, (continued)
- [elpa] externals/parser-generator 7cfdea2 165/434: Passing tests for incremental lexer, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator b072fdd 175/434: Passed test for trailing e-identifier in EFF function, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator d435e50 122/434: Passing unit test for LR-parse, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator a31da28 173/434: Updated Parser WIP items, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator aaec6fa 189/434: Work on e-free first tests, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 922033f 198/434: Various stuff, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator fe10d4a 196/434: Passed tests for first 3 and first 4 of complex grammar, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 4cba5aa 203/434: Made new TODO items, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator ef60d96 204/434: Added failing test for new function the generates grammar prefixes, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 76e30f1 210/434: Sorted lines in test file, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator e88abf0 117/434: More work on parser, added error-handling,
ELPA Syncer <=
- [elpa] externals/parser-generator 8328ab3 130/434: Added unit test for failing LRk Grammar Parse, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator e89a740 138/434: Fixed bug with goto-table generation were tokens were strings, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator c667e18 121/434: Work on shift action in parsing algorithm, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator fab7e46 128/434: Fixed link to LRk grammar, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator bd06863 132/434: LR-parser now uses lex-analyzer for parsing, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator d14d427 140/434: Moved more about lex-analysis to separate document, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 1613e89 146/434: Added lex-analyzer get function, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 5c8a7a5 147/434: Preparations for SDT support, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 0e54a88 148/434: Optimized away one global variable, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 60e9c8a 153/434: Preparations for translation-support in LR-parser, ELPA Syncer, 2021/11/29