rework GBNF example to be a commented grammar

This commit is contained in:
Evan Jones 2023-08-22 20:45:54 -04:00 committed by GitHub
parent fbea8db29c
commit 0a4034f717
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,13 +10,29 @@ GBNF (GGML BNF) is a format for defining [formal grammars](https://en.wikipedia.
In GBNF, we define *production rules* that specify how a *non-terminal* (rule name) can be replaced with sequences of *terminals* (characters, specifically Unicode [code points](https://en.wikipedia.org/wiki/Code_point)) and other non-terminals. The basic format of a production rule is `nonterminal ::= sequence...`. In GBNF, we define *production rules* that specify how a *non-terminal* (rule name) can be replaced with sequences of *terminals* (characters, specifically Unicode [code points](https://en.wikipedia.org/wiki/Code_point)) and other non-terminals. The basic format of a production rule is `nonterminal ::= sequence...`.
## Examples ## Example
Here are some examples and what they mean: Before going deeper, let's look at some of the features demonstrated in `grammars/chess.gbnf`, a small chess notation grammar:
```
# `root` specifies the pattern for the overall output
root ::= (
# it must start with the characters "1. " followed by a sequence
# of characters that match the `move` rule, followed by a space, followed
# by another move, and then a newline
"1. " move " " move "\n"
- `root ::= "1. " move " " move "\n"...`: In this rule from a chess game grammar, `root` requires the characters `"1. "`, followed by a sequence of characters that match the `move` rule, followed by a space, and so on # it's followed by one or more subsequent moves, numbered with one or two digits
- `move ::= (pawn | nonpawn | castle) [+#]?...`: Here, `move` is an abstract representation, which can be a pawn, nonpawn, or castle. The `[+#]?` denotes the possibility of checking or mate signs after moves. ([1-9] [0-9]? ". " move " " move "\n")+
- `ws ::= [ \t\n]*`: In this context, `ws` is an abstract representation of whitespace, which could be any combination of space, tab or newline characters, or nothing (`*` denotes one or more). )
# `move` is an abstract representation, which can be a pawn, nonpawn, or castle.
# The `[+#]?` denotes the possibility of checking or mate signs after moves
move ::= (pawn | nonpawn | castle) [+#]?
pawn ::= ...
nonpawn ::= ...
castle ::= ...
```
## Non-Terminals and Terminals ## Non-Terminals and Terminals