[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/parser-generator 315e40eff8 10/82: More work on LL tabl
From: |
Christian Johansson |
Subject: |
[elpa] externals/parser-generator 315e40eff8 10/82: More work on LL table generation |
Date: |
Thu, 12 May 2022 13:28:13 -0400 (EDT) |
branch: externals/parser-generator
commit 315e40eff8aef2ee433e2b3055a70ae8aa101c47
Author: Christian Johansson <christian@cvj.se>
Commit: Christian Johansson <christian@cvj.se>
More work on LL table generation
---
parser-generator-ll.el | 123 ++++++++++++++++++++++++++++++-------------------
1 file changed, 76 insertions(+), 47 deletions(-)
diff --git a/parser-generator-ll.el b/parser-generator-ll.el
index 9cba14f4fa..5e5fde8122 100644
--- a/parser-generator-ll.el
+++ b/parser-generator-ll.el
@@ -46,7 +46,8 @@
(defun parser-generator-ll--generate-tables ()
"Construction of LL(k)-tables. Output the set of LL(k) tables needed to
construct a parsing table for the grammar G."
- (let ((tables)
+ (let ((tables (make-hash-table :test 'equal))
+ (table-count 0)
(distinct-table-p (make-hash-table :test 'equal))
(stack)
(stack-item)
@@ -61,7 +62,11 @@
(parser-generator--get-grammar-production-number
production)))
(push
- (list (list start) start-rhs production-number nil)
+ (list
+ (list start)
+ start-rhs
+ production-number
+ nil)
stack))))
(setq stack (nreverse stack))
(parser-generator--debug
@@ -110,66 +115,83 @@
production
dot-look-ahead)))
- ;; For each non terminal in the right-hand side
- ;; push to stack with a local look ahead
+ ;; For each non-terminal in the production right-hand side
+ ;; push a new item to stack with a local-follow
+ ;; and a new left-hand-side
(let ((sub-symbol-index 0))
(dolist (sub-symbol production-rhs)
(when (parser-generator--valid-non-terminal-p
sub-symbol)
- (let ((local-look-ahead
- (nthcdr (1+ sub-symbol-index) production-rhs))
- (sub-symbol-rhss
- (parser-generator--get-grammar-rhs
- sub-symbol)))
- (dolist (sub-symbol-rhs sub-symbol-rhss)
- (let* ((sub-symbol-production
- (list (list sub-symbol) sub-symbol-rhs))
- (sub-symbol-production-number
- (parser-generator--get-grammar-production-number
- sub-symbol-production)))
- (push
- (list
- (list sub-symbol)
- sub-symbol-rhs
- sub-symbol-production-number
- local-look-ahead)
- stack))))))
+ (let* ((follow-set
+ (nthcdr (1+ sub-symbol-index) production-rhs))
+ (merged-follow
+ (append follow-set dot-look-ahead))
+ (local-follow-set
+ (parser-generator--first merged-follow nil t t))
+ (sub-symbol-rhss
+ (parser-generator--get-grammar-rhs
+ sub-symbol)))
+ (parser-generator--debug
+ (message "\nfollow-set: %S" follow-set)
+ (message "merged-follow: %S" follow-set)
+ (message "local-follow-set: %S" local-follow-set)
+ (message "sub-symbol-rhss: %S" sub-symbol-rhss))
+ (dolist (local-follow local-follow-set)
+ (dolist (sub-symbol-rhs sub-symbol-rhss)
+ (let* ((sub-symbol-production
+ (list (list sub-symbol) sub-symbol-rhs))
+ (sub-symbol-production-number
+ (parser-generator--get-grammar-production-number
+ sub-symbol-production))
+ (new-stack-item
+ (list
+ (list sub-symbol)
+ sub-symbol-rhs
+ sub-symbol-production-number
+ local-follow)))
+ (parser-generator--debug
+ (message "new-stack-item: %S" new-stack-item))
+ (push
+ new-stack-item
+ stack)))))))
(setq
sub-symbol-index
(1+ sub-symbol-index)))
- ;; Add all distinct combinations of LHS, local-look-ahead and
look-ahead
- ;; to tables list here
+ ;; Add all distinct combinations of left-hand-side,
+ ;; look-ahead and dot-look-ahead to tables list here
(when look-aheads
(dolist (look-ahead look-aheads)
(let ((table
(list
production-lhs
- production-rhs
- production-number
+ dot-look-ahead
look-ahead
- dot-look-ahead))
+ production-rhs
+ production-number))
(table-hash-key
(format
- "%S"
- (list dot-look-ahead production-lhs look-ahead))))
- (if
- (gethash
- table-hash-key
- distinct-table-p)
- (message
- "\nConflicting LL-items: %S vs %S"
- table
- (gethash
- table-hash-key
- distinct-table-p))
- (push
- table
- tables)
+ "%S-%S"
+ production-lhs
+ dot-look-ahead)))
+ (if (gethash table-hash-key distinct-table-p)
+ (let ((existing-table-key
+ (gethash table-hash-key distinct-table-p)))
+ (puthash
+ existing-table-key
+ (push
+ table
+ (gethash existing-table-key tables))
+ tables))
(puthash
table-hash-key
- table
- distinct-table-p)))))
+ table-count
+ distinct-table-p)
+ (puthash
+ table-count
+ (list table)
+ tables)
+ (setq table-count (1+ table-count))))))
(parser-generator--debug
(message "\nproduction-lhs: %S" production-lhs)
@@ -180,9 +202,16 @@
(message "first-dot-look-ahead: %S" first-dot-look-ahead)
(message "look-aheads: %S" look-aheads))))
- (sort
- tables
- 'parser-generator--sort-list)))
+ (let ((sorted-tables))
+ (maphash
+ (lambda (k v)
+ (push
+ (list k (sort v 'parser-generator--sort-list))
+ sorted-tables))
+ tables)
+ (sort
+ (reverse sorted-tables)
+ (lambda (a b) (< (car a) (car b)))))))
;; TODO
- [elpa] externals/parser-generator 2e2496d51f 54/82: Added notes, (continued)
- [elpa] externals/parser-generator 2e2496d51f 54/82: Added notes, Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator 2598402cc7 56/82: Added TODO item, Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator 7f3c384b6d 55/82: Passing more LLk tests, Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator 0856bb7784 58/82: Started on refactor were k=1 will be treated with different algorithm, Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator 2181545d26 64/82: Implemented test for validation of LL(1) grammar, Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator 4051737aeb 65/82: Added TODO item for LL(k) translation, Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator 08af836006 69/82: More work on SDT for LL grammar, Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator 7d87a2d154 79/82: Implemented exported LL(k) and LL(1) parser, Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator 75323b10e5 81/82: Merge branch 'feature/llk-parser', Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator db91a5f203 82/82: Removed unused function, Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator 315e40eff8 10/82: More work on LL table generation,
Christian Johansson <=
- [elpa] externals/parser-generator 34ab0f1718 21/82: More various tweaks, Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator 064bd259ff 26/82: Passing LLk validation tests, Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator f0de6698b9 29/82: Added todo item, Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator 97919972a7 35/82: Improved debug message, added TODO item, Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator e55a3f8a37 38/82: Updated TODO items, Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator 7ee5504003 45/82: More work on LLk parser, Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator fd2f90dd81 47/82: Added TODO-item, Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator b41b2dbffe 68/82: Removed debug output, Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator 566228f16c 71/82: More work on LLk translation, Christian Johansson, 2022/05/12
- [elpa] externals/parser-generator 234a6ca2db 70/82: More work on LLk SDT, Christian Johansson, 2022/05/12