branch: externals/parser-generator commit 37d9fcb7d95cace5f4d0361c920270a4144e82d7 Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Improved documentation --- README.md | 2 +- docs/Lexical-Analysis.md | 4 ++- docs/Syntax-Analysis.md | 29 ++++++++++++++-------- .../LRk.md} | 10 ++++++-- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index f5c30e1..5e7c233 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ This is just started, so most stuff are *WIP*. We use a regular-language based lexical analyzer that can be implemented by a finite-state-machine (FSM). Read more [here](docs/Lexical-Analysis.md). -## Syntax Analysis +## Syntax Analysis / Parser We use deterministic push down transducer (PDT) based algorithms. Read more [here](docs/Syntax-Analysis.md). diff --git a/docs/Lexical-Analysis.md b/docs/Lexical-Analysis.md index 711703c..45f4b8c 100644 --- a/docs/Lexical-Analysis.md +++ b/docs/Lexical-Analysis.md @@ -22,6 +22,7 @@ Returns the look-ahead number of next terminals in stream. (setq index (1+ index))) (nreverse tokens)))) (parser-generator-lex-analyzer--reset) + (setq parser-generator--look-ahead-number 1) (should (equal @@ -38,7 +39,7 @@ Returns the look-ahead number of next terminals in stream. ### Pop token -Returns the next token in stream and moves index one forward. +Returns the next token in stream and moves the index one point forward. ``` emacs-lisp (require 'ert) @@ -56,6 +57,7 @@ Returns the next token in stream and moves index one forward. (setq index (1+ index))) (nreverse tokens)))) (parser-generator-lex-analyzer--reset) + (should (equal '(a) diff --git a/docs/Syntax-Analysis.md b/docs/Syntax-Analysis.md index bae8d00..7bca4da 100644 --- a/docs/Syntax-Analysis.md +++ b/docs/Syntax-Analysis.md @@ -13,7 +13,7 @@ We use push down transducer (PDT) based algorithms. * LL(k) *WIP* * Deterministic Shift-Reduce Parsing *WIP* -* [LR(k)](Deterministic-Right-Parser-for-LRk-Grammars.md) +* [LR(k)](Syntax-Analysis/LRk.md) * Formal Shift-Reduce Parsing Algorithms *WIP* * Simple Precedence Grammars *WIP* * Extended Precedence Grammars *WIP* @@ -42,10 +42,14 @@ Grammar consists of `N`, `T`, `P` and `S`, where `N` is non-terminals, `T` is te (parser-generator-set-grammar '((S A B C) (a b c) ((S (A B)) (A (B a) e) (B (C b) C) (C c e)) S)) ``` -### e +### e-identifier The symbol defined in variable `parser-generator--e-identifier`, with default-value: 'e`, symbolizes the e symbol. The symbol is allowed in some grammars and not in others. +### End-of-file identifier + +The symbol defined in variable `parser-generator--eof-identifier`, with default-value: '$`, symbolizes the end-of-file symbol. + ### Non-terminals A non-terminal is either a symbol or a string so `"A"` and `A` are equally valid. @@ -86,8 +90,9 @@ Is a simple integer above zero. You set it like this: `(parser-generator-set-loo A optional translation is defined as a lambda function as the last element of a production right-hand-side, example: -``` -(parser-generator-set-grammar '((Sp S) ("a" "b") ((Sp S) (S (S "a" S "b" (lambda(args) (nreverse args)))) (S e)) Sp)) +```emacs-lisp +(parser-generator-set-grammar + '((Sp S) ("a" "b") ((Sp S) (S (S "a" S "b" (lambda(args) (nreverse args)))) (S e)) Sp)) ``` You cannot have a SDT + SA on the same production right-hand side, just one or the other. @@ -96,8 +101,9 @@ You cannot have a SDT + SA on the same production right-hand side, just one or t A optional semantic action is defined as a lambda function as the last element of a production right-hand-side, example: -``` -(parser-generator-set-grammar '((Sp S) ("a" "b") ((Sp S) (S (S "a" S "b" (lambda(args) (nreverse args)))) (S e)) Sp)) +```emacs-lisp +(parser-generator-set-grammar + '((Sp S) ("a" "b") ((Sp S) (S (S "a" S "b" (lambda(args) (nreverse args)))) (S e)) Sp)) ``` You cannot have a SDT + SA on the same production right-hand side, just one or the other. @@ -111,7 +117,8 @@ Calculate the first look-ahead number of terminals of the sentential-form `S`, e ``` emacs-lisp (require 'ert) -(parser-generator-set-grammar '((S A B C) (a b c) ((S (A B)) (A (B a) e) (B (C b) C) (C c e)) S)) +(parser-generator-set-grammar + '((S A B C) (a b c) ((S (A B)) (A (B a) e) (B (C b) C) (C c e)) S)) (parser-generator-set-look-ahead-number 2) (parser-generator-process-grammar) @@ -123,12 +130,13 @@ Calculate the first look-ahead number of terminals of the sentential-form `S`, e ### E-FREE-FIRST(S) -Calculate the e-free-first look-ahead number of terminals of sentential-form `S`, example: +Calculate the e-free-first look-ahead number of terminals of sentential-form `S`, if you have multiple symbols the e-free-first will only affect the first symbol, the rest will be treated via first-function (above). Example: ``` emacs-lisp (require 'ert) -(parser-generator-set-grammar '((S A B C) (a b c) ((S (A B)) (A (B a) e) (B (C b) C) (C c e)) S)) +(parser-generator-set-grammar + '((S A B C) (a b c) ((S (A B)) (A (B a) e) (B (C b) C) (C c e)) S)) (parser-generator-set-look-ahead-number 2) (parser-generator-process-grammar) @@ -145,7 +153,8 @@ Calculate the look-ahead number of terminals possibly following S. ``` emacs-lisp (require 'ert) -(parser-generator-set-grammar '((S A B) (a c d f) ((S (A a)) (A B) (B (c f) d)) S)) +(parser-generator-set-grammar + '((S A B) (a c d f) ((S (A a)) (A B) (B (c f) d)) S)) (parser-generator-set-look-ahead-number 2) (parser-generator-process-grammar) diff --git a/docs/Deterministic-Right-Parser-for-LRk-Grammars.md b/docs/Syntax-Analysis/LRk.md similarity index 91% rename from docs/Deterministic-Right-Parser-for-LRk-Grammars.md rename to docs/Syntax-Analysis/LRk.md index 65f7fb5..127986f 100644 --- a/docs/Deterministic-Right-Parser-for-LRk-Grammars.md +++ b/docs/Syntax-Analysis/LRk.md @@ -1,4 +1,6 @@ -# Deterministic Right Parser for LR(k) Grammars +# LRk Parser + +LR(k) parser is the left-to-right parse calculating a right-derivation in reverse invented by Donald Knuth. This library contains functions to parse and translate grammar as well as validate grammars. @@ -89,4 +91,8 @@ Perform a right-parse of input-stream. (parser-generator-lr-parse))) ``` -[Back to start](../../../) +## Translate + +Coming soon! + +[Back to syntax analysis](../../Syntax-Analysis.md)