grammar-inference-engine/TODO_GBNF_OUTPUT.md

29 lines
924 B
Markdown
Raw Normal View History

# TODO: GBNF Output Format
When grammars are finalized, output them in GBNF (GGML BNF) format for compatibility with llama.cpp and other inference engines.
GBNF is a BNF-like grammar format used by llama.cpp for constrained decoding. It supports:
- Sequences: `rule ::= token1 token2`
- Alternatives: `rule ::= alt1 | alt2`
- Optional: `rule ::= (token)?`
- Repetition: `rule ::= (token)*`
- Character classes: `[a-z]`, `[^abc]`
Example GBNF:
```
root ::= ws? item ws?
item ::= identifier ws? "(" ws? args? ws? ")"
args ::= identifier (ws? "," ws? identifier)*
identifier ::= [a-zA-Z_][a-zA-Z0-9_]*
ws ::= [ \t\n]*
```
Map SORE operators to GBNF:
- `r·s` (concatenation) → `rule ::= r s`
- `r|s` (disjunction) → `rule ::= r | s`
- `r*` (star) → `rule ::= (r)*`
- `r?` (optional) → `rule ::= (r)?`
- `r+` (plus) → `rule ::= r (r)*`
Implementation: Add `to_gbnf(sore)` function to `bex/` when ready.