1
0

grammar-parser.h 874 B

1234567891011121314151617181920212223242526272829
  1. // Implements a parser for an extended Backus-Naur form (BNF), producing the
  2. // binary context-free grammar format specified by llama.h. Supports character
  3. // ranges, grouping, and repetition operators. As an example, a grammar for
  4. // arithmetic might look like:
  5. //
  6. // root ::= expr
  7. // expr ::= term ([-+*/] term)*
  8. // term ::= num | "(" space expr ")" space
  9. // num ::= [0-9]+ space
  10. // space ::= [ \t\n]*
  11. #pragma once
  12. #include "llama.h"
  13. #include <vector>
  14. #include <map>
  15. #include <cstdint>
  16. #include <string>
  17. namespace grammar_parser {
  18. struct parse_state {
  19. std::map<std::string, uint32_t> symbol_ids;
  20. std::vector<std::vector<llama_grammar_element>> rules;
  21. std::vector<const llama_grammar_element *> c_rules();
  22. };
  23. parse_state parse(const char * src);
  24. void print_grammar(FILE * file, const parse_state & state);
  25. }