branch: externals/parser-generator
commit 7ee5504003bcc1c0a7fbc9e090ba65812d608307
Author: Christian Johansson <[email protected]>
Commit: Christian Johansson <[email protected]>
More work on LLk parser
---
parser-generator-ll.el | 48 ++++++++++++++++++++++-------------
test/parser-generator-ll-test.el | 55 ++++++++++++++++++++++++++++++++++------
2 files changed, 78 insertions(+), 25 deletions(-)
diff --git a/parser-generator-ll.el b/parser-generator-ll.el
index 649ebd26a4..19ff1790fd 100644
--- a/parser-generator-ll.el
+++ b/parser-generator-ll.el
@@ -47,18 +47,18 @@
(state-look-ahead-production-number
(nth 3 state-look-ahead-list)))
(puthash
- state-look-ahead-string
+ (format "%S" state-look-ahead-string)
(list
state-look-ahead-action
state-look-ahead-reduction
state-look-ahead-production-number)
state-hash-table))
(puthash
- state-look-ahead-string
+ (format "%S" state-look-ahead-string)
state-look-ahead-action
state-hash-table))))
(puthash
- state-key
+ (format "%S" state-key)
state-hash-table
hash-parsing-table)))
(setq
@@ -84,30 +84,37 @@
(let* ((state (car stack))
(state-action-table
(gethash
- state
+ (format "%S" state)
parser-generator-ll--parsing-table))
(look-ahead-list
(parser-generator-lex-analyzer--peek-next-look-ahead))
(look-ahead))
+ (message "\nstate: %S" state)
+ (message "\nstate-action-table: %S" state-action-table)
(unless state-action-table
(signal
'error
- (format
- "State action table lacks actions for state: '%S'!"
- state)))
+ (list
+ (format
+ "State action table lacks actions for state: '%S'!"
+ state))))
- (unless look-ahead
- (signal
- 'error
- (format
- "Reached end of input without accepting!")))
+ (if look-ahead-list
+ (progn
+ (message "look-ahead-list: %S" look-ahead-list)
+ (setq
+ look-ahead
+ (list (car (car look-ahead-list)))))
+ (setq
+ look-ahead
+ (list parser-generator--eof-identifier)))
- (setq
- look-ahead
- (car (car look-ahead-list)))
+ (message "look-ahead: %S" look-ahead)
- (unless (gethash look-ahead state-action-table)
+ (unless (gethash
+ (format "%S" look-ahead)
+ state-action-table)
(let ((possible-look-aheads))
(maphash
(lambda (k _v) (push k possible-look-aheads))
@@ -120,17 +127,24 @@
state
possible-look-aheads))))
- (let* ((action (gethash look-ahead state-action-table))
+ (let* ((action
+ (gethash
+ (format "%S" look-ahead)
+ state-action-table))
(action-type action))
+ (message "action: %S" action)
(when (listp action)
(setq action-type (car action)))
+ (message "action-type: %S" action-type)
(cond
((equal action-type 'pop)
+ (message "pushed: %S" look-ahead-list)
(push
(car (parser-generator-lex-analyzer--pop-token))
stack))
((equal action-type 'reduce)
+ (message "reduced: %S" (nth 1 action))
(push
(nth 1 action)
stack)
diff --git a/test/parser-generator-ll-test.el b/test/parser-generator-ll-test.el
index 2f260c10d6..03d012f2e0 100644
--- a/test/parser-generator-ll-test.el
+++ b/test/parser-generator-ll-test.el
@@ -293,7 +293,7 @@
)
(parser-generator-process-grammar)
(parser-generator-ll-generate-parser-tables)
- (message "parser-generator-ll--parsing-table: %S"
parser-generator-ll--parsing-table)
+ ;; (message "parser-generator-ll--parsing-table: %S"
parser-generator-ll--parsing-table)
(setq
parser-generator-lex-analyzer--function
(lambda (index)
@@ -348,13 +348,52 @@
(should
(equal
'(
- (((S) ($)) (((a b) (reduce (a b ((A) ($))) 1)) (($ $) (reduce (e) 0))))
- (((A) ($)) (((b $) (reduce (b) 3)) ((a a) (reduce (((S) (a a)) a a) 2))
((a b) (reduce (((S) (a a)) a a) 2))))
- (((S) (a a)) (((a a) (reduce (e) 0)) ((a b) (reduce (a b ((A) (a a)))
1))))
- (((A) (a a)) (((b a) (reduce (b) 3)) ((a a) (reduce (((S) (a a)) a a)
2)) ((a b) (reduce (((S) (a a)) a a) 2))))
- (b (((b b) pop) ((b a) pop) ((b $) pop)))
- (a (((a b) pop) ((a a) pop) ((a $) pop)))
- ($ ((($ $) accept))))
+ ("((S) ($))"
+ (
+ ("(a b)" (reduce (a b ((A) ($))) 1))
+ ("($ $)" (reduce (e) 0))
+ )
+ )
+ ("((A) ($))"
+ (
+ ("(b $)" (reduce (b) 3))
+ ("(a a)" (reduce (((S) (a a)) a a) 2))
+ ("(a b)" (reduce (((S) (a a)) a a) 2))
+ )
+ )
+ ("((S) (a a))"
+ (
+ ("(a a)" (reduce (e) 0))
+ ("(a b)" (reduce (a b ((A) (a a))) 1))
+ )
+ )
+ ("((A) (a a))"
+ (
+ ("(b a)" (reduce (b) 3))
+ ("(a a)" (reduce (((S) (a a)) a a) 2))
+ ("(a b)" (reduce (((S) (a a)) a a) 2))
+ )
+ )
+ ("b"
+ (
+ ("(b b)" pop)
+ ("(b a)" pop)
+ ("(b $)" pop)
+ )
+ )
+ ("a"
+ (
+ ("(a b)" pop)
+ ("(a a)" pop)
+ ("(a $)" pop)
+ )
+ )
+ ("$"
+ (
+ ("($ $)" accept)
+ )
+ )
+ )
(parser-generator--hash-to-list
parser-generator-ll--parsing-table
t)))