[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/parser-generator 16bb2d8 262/434: Added translation exa
From: |
ELPA Syncer |
Subject: |
[elpa] externals/parser-generator 16bb2d8 262/434: Added translation example LRk parser |
Date: |
Mon, 29 Nov 2021 15:59:54 -0500 (EST) |
branch: externals/parser-generator
commit 16bb2d8863a0f637bb16965654cb5fed1e006817
Author: Christian Johansson <christian@cvj.se>
Commit: Christian Johansson <christian@cvj.se>
Added translation example LRk parser
---
docs/Syntax-Analysis/LRk.md | 111 ++++++++++++++++++++++++++++++++++++++------
1 file changed, 97 insertions(+), 14 deletions(-)
diff --git a/docs/Syntax-Analysis/LRk.md b/docs/Syntax-Analysis/LRk.md
index 6c6eec7..04c8d59 100644
--- a/docs/Syntax-Analysis/LRk.md
+++ b/docs/Syntax-Analysis/LRk.md
@@ -15,12 +15,12 @@ A valid LR-item for a viable prefix has this structure:
Example with grammar with production: S -> SaSb and S is non-terminal and a, b
are terminals. Look-ahead number: 1
``` emacs-lisp
-(S nil (S a S b) (a))
+((S) nil (S a S b) (a))
```
* A is the production LHS
-* B, C is parts of the production RHS, if the dot is the left B is nil and C
is the entire RHS. If the dot is at the right then B is the production RHS and
C is nil, otherwise B and C contains parts of the RHS
-* L is the terminal look-ahead
+* B, C is parts of the production RHS, if the dot is at the left B is nil and
C is the entire RHS. If the dot is at the right then B is the production RHS
and C is nil, otherwise B and C contains parts of the RHS
+* L is the item look-ahead
## LR items for prefix (S)
@@ -35,12 +35,12 @@ Calculate the set of LR items valid for any viable prefix S.
(should
(equal
- '((S nil (S a S b) (a))
- (S nil (S a S b) (e))
- (S nil nil (a))
- (S nil nil (e))
- (Sp nil (S) (e)))
- (parser--lr-items-for-prefix 'e)))
+ '(((S) nil (S a S b) ($))
+ ((S) nil (S a S b) (a))
+ ((S) nil nil ($))
+ ((S) nil nil (a))
+ ((Sp) nil (S) ($)))
+ (parser-generator-lr--items-for-prefix 'e)))
```
``` emacs-lisp
@@ -52,10 +52,10 @@ Calculate the set of LR items valid for any viable prefix S.
(should
(equal
- '((S (S) (a S b) (a))
- (S (S) (a S b) (e))
- (Sp (S) nil (e)))
- (parser--lr-items-for-prefix 'S)))
+ '(((S) (S) (a S b) ($))
+ ((S) (S) (a S b) (a))
+ ((Sp) (S) nil ($)))
+ (parser-generator-lr--items-for-prefix 'S)))
```
## Parse
@@ -93,6 +93,89 @@ Perform a right-parse of input-stream.
## Translate
-Coming soon!
+Each production RHS can optionally contain a lambda-expression that will be
called if specified when a reduction is made, example:
+
+```emacs-lisp
+(let ((buffer (generate-new-buffer "*a*")))
+ (switch-to-buffer buffer)
+ (insert "if (a) { b; }")
+
+ (parser-generator-set-grammar '((Sp S) (";" OPEN_ROUND_BRACKET
CLOSE_ROUND_BRACKET ECHO IF OPEN_CURLY_BRACKET CLOSE_CURLY_BRACKET VARIABLE)
((Sp S) (S (IF OPEN_ROUND_BRACKET VARIABLE CLOSE_ROUND_BRACKET
OPEN_CURLY_BRACKET VARIABLE ";" CLOSE_CURLY_BRACKET (lambda(args) (format
"(when %s %s)" (nth 2 args) (nth 5 args)))))) Sp))
+ (parser-generator-set-look-ahead-number 1)
+ (parser-generator-process-grammar)
+ (parser-generator-lr-generate-parser-tables)
+
+ (setq
+ parser-generator-lex-analyzer--function
+ (lambda (index)
+ (with-current-buffer buffer
+ (unless (>= index (point-max))
+ (goto-char index)
+ (unless (looking-at "[^ \n\t]")
+ (search-forward-regexp "[^ \n\t]" nil t nil)
+ (forward-char -1))
+ (let ((token))
+ (cond
+ ((looking-at "if")
+ (setq token `(IF ,(match-beginning 0) . ,(match-end 0))))
+ ((looking-at "echo")
+ (setq token `(ECHO ,(match-beginning 0) . ,(match-end 0))))
+ ((looking-at "(")
+ (setq token `(OPEN_ROUND_BRACKET ,(match-beginning 0) .
,(match-end 0))))
+ ((looking-at ")")
+ (setq token `(CLOSE_ROUND_BRACKET ,(match-beginning 0) .
,(match-end 0))))
+ ((looking-at "{")
+ (setq token `(OPEN_CURLY_BRACKET ,(match-beginning 0) .
,(match-end 0))))
+ ((looking-at "}")
+ (setq token `(CLOSE_CURLY_BRACKET ,(match-beginning 0) .
,(match-end 0))))
+ ((looking-at ";")
+ (setq token `(";" ,(match-beginning 0) . ,(match-end 0))))
+ ((looking-at "[a-zA-Z]+")
+ (setq token `(VARIABLE ,(match-beginning 0) . ,(match-end 0))))
+ (t (error "Invalid syntax! Could not lex-analyze at %s!"
(point))))
+ token)))))
+
+ (setq
+ parser-generator-lex-analyzer--get-function
+ (lambda (token)
+ (with-current-buffer buffer
+ (let ((start (car (cdr token)))
+ (end (cdr (cdr token))))
+ (when (<= end (point-max))
+ (buffer-substring-no-properties start end))))))
+
+ (should
+ (equal
+ "(when a b)"
+ (parser-generator-lr-translate)))
+ (message "Passed test with non-nested translation")
+
+ (switch-to-buffer buffer)
+ (kill-region (point-min) (point-max))
+
+ (parser-generator-set-grammar '((Sp S T) (";" OPEN_ROUND_BRACKET
CLOSE_ROUND_BRACKET ECHO IF OPEN_CURLY_BRACKET CLOSE_CURLY_BRACKET VARIABLE)
((Sp S) (S (IF OPEN_ROUND_BRACKET VARIABLE CLOSE_ROUND_BRACKET
OPEN_CURLY_BRACKET T CLOSE_CURLY_BRACKET (lambda(args) (format "(when %s %s)"
(nth 2 args) (nth 5 args))))) (T (ECHO VARIABLE ";" (lambda(args) (format
"(message %s)" (nth 1 args)))) (VARIABLE ";" (lambda(args) (format "%s" (nth 0
args)))))) Sp))
+ (parser-generator-set-look-ahead-number 1)
+ (parser-generator-process-grammar)
+ (parser-generator-lr-generate-parser-tables)
+
+ (insert "if (a) { echo b; }")
+
+ (should
+ (equal
+ "(when a (message b))"
+ (parser-generator-lr-translate)))
+
+ (message "Passed test with nested-translation with depth 2")
+
+ (switch-to-buffer buffer)
+ (kill-region (point-min) (point-max))
+ (goto-char 1)
+ (insert "if (a) { echo b }")
+
+ (should-error
+ (parser-generator-lr-parse))
+
+ (kill-buffer buffer))
+```
[Back to syntax analysis](../../Syntax-Analysis.md)
- [elpa] externals/parser-generator d49f74f 244/434: Added failing test for action-tables LRk parser k=2, (continued)
- [elpa] externals/parser-generator d49f74f 244/434: Added failing test for action-tables LRk parser k=2, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator fe05328 250/434: Passed unit tests for LRk parser k=2, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator d1f4682 248/434: Added a function that converts a FIRST-item to a look-ahead item, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 6845262 243/434: Passed GOTO-tables k=2, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 3a0d4cd 255/434: Added translation test for LRk=2, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 03ca2e9 247/434: Look-ahead generation now supports EOF symbol, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 3563720 266/434: Updated docs about lexical analysis, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 412fd2a 273/434: Passing generation of GOTO-table for LR(0) grammar, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator bc23604 274/434: Fixed look-ahead, first and e-free-first with k = 0, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 1879cb0 271/434: Merge branch 'master' of git.cvj.se:/home/git/emacs-parser-generator, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 16bb2d8 262/434: Added translation example LRk parser,
ELPA Syncer <=
- [elpa] externals/parser-generator d173c1e 281/434: More work on exporting parser as code function, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator bd887ff 277/434: LR(0) Parser passing translation test, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 852f969 283/434: Merge branch 'master' of git.cvj.se:/home/git/emacs-parser-generator, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 0d6d46d 294/434: Debugging incremental issue with exported translator, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator c4c68b2 302/434: Added progress-indicator to goto-table generation, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 586789d 308/434: Added cache to first calculation, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 236a124 322/434: More work on resolving conflicts, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator 04c360b 318/434: LR-items set validation now supports symbols with attributes, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator ea02245 325/434: Improved debug output, ELPA Syncer, 2021/11/29
- [elpa] externals/parser-generator e56bcff 324/434: Improved output of productions and action-tables, ELPA Syncer, 2021/11/29