branch: externals/parser-generator commit 4fe014a47fbd1daaf48b696e48b5236364c20ddb Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Updating documentation regarding operator precedence --- README.md | 4 +--- docs/Syntax-Analysis.md | 20 ++++++++++++++++++++ docs/Syntax-Analysis/LR0.md | 36 ++++++++++++++++++++++++++++++++++++ docs/Syntax-Analysis/LRk.md | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e2b7477..67fc98c 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,7 @@ [](https://www.gnu.org/licenses/gpl-3.0.txt) [](https://travis-ci.org/cjohansson/emacs-parser-generator) -The idea of this plugin is to provide functions for various kinds of context-free grammar parser generations with support for syntax-directed-translations (SDT) and semantic actions (SA) and the possibility of exporting parsers and translators (as elisp code) to enable plugin-agnostic usage. This project is also about implementing algorithms described in the book `The Theory of Parsing, Translation and Compiling (Volume 1)` by `Alfred V. Aho and Jeffrey D. Ullman` (1972). Also this proje [...] - -This is just started, so most stuff are *WIP*. +The idea of this plugin is to provide functions for various kinds of context-free grammar parser generations with support for syntax-directed-translations (SDT) and semantic actions (SA) and the possibility of exporting parsers and translators (as generated elisp code) to enable Emacs plugin-agnostic usage. This project is also about implementing algorithms described in the book `The Theory of Parsing, Translation and Compiling (Volume 1)` by `Alfred V. Aho and Jeffrey D. Ullman` (1972). [...] ## Lexical Analysis diff --git a/docs/Syntax-Analysis.md b/docs/Syntax-Analysis.md index b384185..9659f1e 100644 --- a/docs/Syntax-Analysis.md +++ b/docs/Syntax-Analysis.md @@ -39,10 +39,30 @@ Grammar consists of `N`, `T`, `P` and `S`, where `N` is non-terminals, `T` is te * P = `'((S (A B)) (A (B a) e) (B (C b) C) (C c e))` * S = `'S` +Example: + ``` emacs-lisp (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)) ``` +Productions can include context-sensitive attributes like this: + +``` emacs-lisp +((S (A B %prec first)) (A (B a %weight) e) (B (C b) C) (C c e)) +``` + +### Global attributes + +A list of valid attributes can be set in the variable `parser-generator--global-attributes`. + +### Context-sensitive attributes + +A list of valid attributes can be set in the variable `parser-generator--context-sensitive-attributes`. + +### Global declaration + +Can be set in variable `parser-generator--global-declaration`. This may be used differently in different parsing algorithms. + ### 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. diff --git a/docs/Syntax-Analysis/LR0.md b/docs/Syntax-Analysis/LR0.md index 39c8a4d..fed7694 100644 --- a/docs/Syntax-Analysis/LR0.md +++ b/docs/Syntax-Analysis/LR0.md @@ -21,6 +21,42 @@ Example with grammar with production: S -> SaSb and S is non-terminal and a, b a * A is the production LHS * 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 +## Declaring operator precedence + +You can set global symbol operator precedence and also context-sensitive precedence, like in GNU Bison. Example + +``` emacs-lisp +(setq + parser-generator--global-attributes + '(%left %precedence %right)) + (setq + parser-generator-lr--global-precedence-attributes + '(%left %precedence %right)) + (setq + parser-generator--context-sensitive-attributes + '(%prec)) + (setq + parser-generator-lr--context-sensitive-precedence-attribute + '%prec) + (setq + parser-generator--global-declaration + '((%left a) + (%right b) + (%left c) + (%precedence FIRST))) +(parser-generator-set-grammar + '( + (Sp S A B) + (a b c) + ( + (Sp S) + (S (A c) B) + (A (a b %prec a)) + (B (a b c %prec FIRST)) + ) + Sp)) +``` + ## Parse Perform a right-parse of input-stream. Example from [Wikipedia](https://en.wikipedia.org/wiki/LR_parser#Constructing_LR(0)_parsing_tables). diff --git a/docs/Syntax-Analysis/LRk.md b/docs/Syntax-Analysis/LRk.md index 25fa359..db12254 100644 --- a/docs/Syntax-Analysis/LRk.md +++ b/docs/Syntax-Analysis/LRk.md @@ -22,6 +22,42 @@ Example with grammar with production: S -> SaSb and S is non-terminal and a, b a * 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 +## Declaring operator precedence + +You can set global symbol operator precedence and also context-sensitive precedence, like in GNU Bison. Example + +``` emacs-lisp +(setq + parser-generator--global-attributes + '(%left %precedence %right)) + (setq + parser-generator-lr--global-precedence-attributes + '(%left %precedence %right)) + (setq + parser-generator--context-sensitive-attributes + '(%prec)) + (setq + parser-generator-lr--context-sensitive-precedence-attribute + '%prec) + (setq + parser-generator--global-declaration + '((%left a) + (%right b) + (%left c) + (%precedence FIRST))) +(parser-generator-set-grammar + '( + (Sp S A B) + (a b c) + ( + (Sp S) + (S (A c) B) + (A (a b %prec a)) + (B (a b c %prec FIRST)) + ) + Sp)) +``` + ## LR items for prefix (S) Calculate the set of LR items valid for any viable prefix S.