2024-04-06 14:31:33 +00:00
|
|
|
#ifdef NDEBUG
|
|
|
|
#undef NDEBUG
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define LLAMA_API_INTERNAL
|
|
|
|
|
|
|
|
#include "ggml.h"
|
|
|
|
#include "llama.h"
|
|
|
|
#include "grammar-parser.h"
|
|
|
|
#include "unicode.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <string>
|
2024-04-29 18:40:14 +00:00
|
|
|
#include <vector>
|
2024-04-06 14:31:33 +00:00
|
|
|
|
2024-04-29 18:40:14 +00:00
|
|
|
static llama_grammar* build_grammar(const std::string & grammar_str) {
|
|
|
|
auto parsed_grammar = grammar_parser::parse(grammar_str.c_str());
|
2024-04-06 14:31:33 +00:00
|
|
|
|
|
|
|
// Ensure we parsed correctly
|
|
|
|
assert(!parsed_grammar.rules.empty());
|
|
|
|
|
|
|
|
// Ensure we have a root node
|
|
|
|
assert(!(parsed_grammar.symbol_ids.find("root") == parsed_grammar.symbol_ids.end()));
|
|
|
|
|
|
|
|
std::vector<const llama_grammar_element*> grammar_rules(parsed_grammar.c_rules());
|
|
|
|
llama_grammar* grammar = llama_grammar_init(
|
|
|
|
grammar_rules.data(), grammar_rules.size(), parsed_grammar.symbol_ids.at("root"));
|
|
|
|
|
2024-04-29 18:40:14 +00:00
|
|
|
return grammar;
|
|
|
|
}
|
2024-04-06 14:31:33 +00:00
|
|
|
|
2024-04-29 18:40:14 +00:00
|
|
|
static bool match_string(const std::string & input, llama_grammar* grammar) {
|
2024-04-06 14:31:33 +00:00
|
|
|
auto decoded = decode_utf8(input, {});
|
|
|
|
|
|
|
|
const auto & code_points = decoded.first;
|
|
|
|
|
|
|
|
for (auto it = code_points.begin(), end = code_points.end() - 1; it != end; ++it) {
|
|
|
|
auto prev_stacks = grammar->stacks;
|
2024-04-11 18:47:34 +00:00
|
|
|
llama_grammar_accept(grammar->rules, prev_stacks, *it, grammar->stacks);
|
2024-04-29 18:40:14 +00:00
|
|
|
if (grammar->stacks.empty()) {
|
|
|
|
// no stacks means that the grammar failed to match at this point
|
|
|
|
return false;
|
|
|
|
}
|
2024-04-06 14:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto & stack : grammar->stacks) {
|
|
|
|
if (stack.empty()) {
|
2024-04-29 18:40:14 +00:00
|
|
|
// An empty stack means that the grammar has been completed
|
|
|
|
return true;
|
2024-04-06 14:31:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-29 18:40:14 +00:00
|
|
|
return false;
|
2024-04-06 14:31:33 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 18:40:14 +00:00
|
|
|
static void test_grammar(const std::string & test_desc, const std::string & grammar_str, const std::vector<std::string> & passing_strings, const std::vector<std::string> & failing_strings) {
|
|
|
|
fprintf(stderr, "⚫ Testing %s. Grammar: %s\n", test_desc.c_str(), grammar_str.c_str());
|
|
|
|
fflush(stderr);
|
2024-04-06 14:31:33 +00:00
|
|
|
|
2024-04-29 18:40:14 +00:00
|
|
|
auto grammar = build_grammar(grammar_str);
|
2024-04-06 14:31:33 +00:00
|
|
|
|
|
|
|
// Save the original grammar stacks so that we can reset after every new string we want to test
|
|
|
|
auto original_stacks = grammar->stacks;
|
|
|
|
|
2024-04-29 18:40:14 +00:00
|
|
|
fprintf(stderr, " 🔵 Valid strings:\n");
|
2024-04-06 14:31:33 +00:00
|
|
|
|
|
|
|
// Passing strings
|
2024-04-29 18:40:14 +00:00
|
|
|
for (const auto & test_string : passing_strings) {
|
|
|
|
fprintf(stderr, " \"%s\" ", test_string.c_str());
|
|
|
|
fflush(stderr);
|
2024-04-06 14:31:33 +00:00
|
|
|
|
2024-04-29 18:40:14 +00:00
|
|
|
bool matched = match_string(test_string, grammar);
|
2024-04-06 14:31:33 +00:00
|
|
|
|
2024-04-29 18:40:14 +00:00
|
|
|
if (!matched) {
|
|
|
|
fprintf(stderr, "❌ (failed to match)\n");
|
|
|
|
} else {
|
|
|
|
fprintf(stdout, "✅︎\n");
|
2024-04-06 14:31:33 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 18:40:14 +00:00
|
|
|
assert(matched);
|
2024-04-06 14:31:33 +00:00
|
|
|
|
|
|
|
// Reset the grammar stacks
|
|
|
|
grammar->stacks = original_stacks;
|
|
|
|
}
|
|
|
|
|
2024-04-29 18:40:14 +00:00
|
|
|
fprintf(stderr, " 🟠 Invalid strings:\n");
|
|
|
|
|
2024-04-06 14:31:33 +00:00
|
|
|
// Failing strings
|
2024-04-29 18:40:14 +00:00
|
|
|
for (const auto & test_string : failing_strings) {
|
|
|
|
fprintf(stderr, " \"%s\" ", test_string.c_str());
|
|
|
|
fflush(stderr);
|
2024-04-06 14:31:33 +00:00
|
|
|
|
2024-04-29 18:40:14 +00:00
|
|
|
bool matched = match_string(test_string, grammar);
|
2024-04-06 14:31:33 +00:00
|
|
|
|
2024-04-29 18:40:14 +00:00
|
|
|
if (matched) {
|
|
|
|
fprintf(stderr, "❌ (incorrectly matched)\n");
|
|
|
|
} else {
|
|
|
|
fprintf(stdout, "✅︎\n");
|
2024-04-06 14:31:33 +00:00
|
|
|
}
|
2024-04-29 18:40:14 +00:00
|
|
|
assert(!matched);
|
2024-04-06 14:31:33 +00:00
|
|
|
|
|
|
|
// Reset the grammar stacks
|
|
|
|
grammar->stacks = original_stacks;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up allocated memory
|
|
|
|
llama_grammar_free(grammar);
|
|
|
|
}
|
|
|
|
|
2024-04-29 18:40:14 +00:00
|
|
|
static void test_simple_grammar() {
|
|
|
|
// Test case for a simple grammar
|
|
|
|
test_grammar(
|
|
|
|
"simple grammar",
|
|
|
|
R"""(
|
|
|
|
root ::= expr
|
|
|
|
expr ::= term ("+" term)*
|
|
|
|
term ::= number
|
|
|
|
number ::= [0-9]+)""",
|
|
|
|
// Passing strings
|
|
|
|
{
|
|
|
|
"42",
|
|
|
|
"1+2+3+4+5",
|
|
|
|
"123+456",
|
|
|
|
},
|
|
|
|
// Failing strings
|
|
|
|
{
|
|
|
|
"+",
|
|
|
|
"/ 3",
|
|
|
|
"1+2+3+4+5+",
|
|
|
|
"12a45",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_complex_grammar() {
|
|
|
|
// Test case for a more complex grammar, with both failure strings and success strings
|
|
|
|
test_grammar(
|
|
|
|
"medium complexity grammar",
|
|
|
|
// Grammar
|
|
|
|
R"""(
|
|
|
|
root ::= expression
|
|
|
|
expression ::= term ws (("+"|"-") ws term)*
|
|
|
|
term ::= factor ws (("*"|"/") ws factor)*
|
|
|
|
factor ::= number | variable | "(" expression ")" | function-call
|
|
|
|
number ::= [0-9]+
|
|
|
|
variable ::= [a-zA-Z_][a-zA-Z0-9_]*
|
|
|
|
function-call ::= variable ws "(" (expression ("," ws expression)*)? ")"
|
|
|
|
ws ::= [ \t\n\r]?)""",
|
|
|
|
// Passing strings
|
|
|
|
{
|
|
|
|
"42",
|
|
|
|
"1*2*3*4*5",
|
|
|
|
"x",
|
|
|
|
"x+10",
|
|
|
|
"x1+y2",
|
|
|
|
"(a+b)*(c-d)",
|
|
|
|
"func()",
|
|
|
|
"func(x,y+2)",
|
|
|
|
"a*(b+c)-d/e",
|
|
|
|
"f(g(x),h(y,z))",
|
|
|
|
"x + 10",
|
|
|
|
"x1 + y2",
|
|
|
|
"(a + b) * (c - d)",
|
|
|
|
"func()",
|
|
|
|
"func(x, y + 2)",
|
|
|
|
"a * (b + c) - d / e",
|
|
|
|
"f(g(x), h(y, z))",
|
|
|
|
"123+456",
|
|
|
|
"123*456*789-123/456+789*123",
|
|
|
|
"123+456*789-123/456+789*123-456/789+123*456-789/123+456*789-123/456+789*123-456"
|
|
|
|
},
|
|
|
|
// Failing strings
|
|
|
|
{
|
|
|
|
"+",
|
|
|
|
"/ 3x",
|
|
|
|
"x + + y",
|
|
|
|
"a * / b",
|
|
|
|
"func(,)",
|
|
|
|
"func(x y)",
|
|
|
|
"(a + b",
|
|
|
|
"x + y)",
|
|
|
|
"a + b * (c - d",
|
|
|
|
"42 +",
|
|
|
|
"x +",
|
|
|
|
"x + 10 +",
|
|
|
|
"(a + b) * (c - d",
|
|
|
|
"func(",
|
|
|
|
"func(x, y + 2",
|
|
|
|
"a * (b + c) - d /",
|
|
|
|
"f(g(x), h(y, z)",
|
|
|
|
"123+456*789-123/456+789*123-456/789+123*456-789/123+456*789-123/456+789*123-456/",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_quantifiers() {
|
|
|
|
// A collection of tests to exercise * + and ? quantifiers
|
|
|
|
|
|
|
|
test_grammar(
|
|
|
|
"* quantifier",
|
|
|
|
// Grammar
|
|
|
|
R"""(root ::= "a"*)""",
|
|
|
|
// Passing strings
|
|
|
|
{
|
|
|
|
"",
|
|
|
|
"a",
|
|
|
|
"aaaaa",
|
|
|
|
"aaaaaaaaaaaaaaaaaa",
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
|
|
},
|
|
|
|
// Failing strings
|
|
|
|
{
|
|
|
|
"b",
|
|
|
|
"ab",
|
|
|
|
"aab",
|
|
|
|
"ba",
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"
|
|
|
|
}
|
|
|
|
);
|
|
|
|
test_grammar(
|
|
|
|
"+ quantifier",
|
|
|
|
// Grammar
|
|
|
|
R"""(root ::= "a"+)""",
|
|
|
|
// Passing strings
|
|
|
|
{
|
|
|
|
"a",
|
|
|
|
"aaaaa",
|
|
|
|
"aaaaaaaaaaaaaaaaaa",
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
|
|
},
|
|
|
|
// Failing strings
|
|
|
|
{
|
|
|
|
"",
|
|
|
|
"b",
|
|
|
|
"ab",
|
|
|
|
"aab",
|
|
|
|
"ba",
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"
|
|
|
|
}
|
|
|
|
);
|
|
|
|
test_grammar(
|
|
|
|
"? quantifier",
|
|
|
|
// Grammar
|
|
|
|
R"""(root ::= "a"?)""",
|
|
|
|
// Passing strings
|
|
|
|
{
|
|
|
|
"",
|
|
|
|
"a"
|
|
|
|
},
|
|
|
|
// Failing strings
|
|
|
|
{
|
|
|
|
"b",
|
|
|
|
"ab",
|
|
|
|
"aa",
|
|
|
|
"ba",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
test_grammar(
|
|
|
|
"mixed quantifiers",
|
|
|
|
// Grammar
|
|
|
|
R"""(
|
|
|
|
root ::= cons+ vowel* cons? (vowel cons)*
|
|
|
|
vowel ::= [aeiouy]
|
|
|
|
cons ::= [bcdfghjklmnpqrstvwxyz]
|
|
|
|
)""",
|
|
|
|
// Passing strings
|
|
|
|
{
|
|
|
|
"yes",
|
|
|
|
"no",
|
|
|
|
"noyes",
|
|
|
|
"crwth",
|
|
|
|
"four",
|
|
|
|
"bryyyy",
|
|
|
|
},
|
|
|
|
// Failing strings
|
|
|
|
{
|
|
|
|
"yess",
|
|
|
|
"yesno",
|
|
|
|
"forty",
|
|
|
|
"catyyy",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-04-06 14:31:33 +00:00
|
|
|
static void test_failure_missing_root() {
|
2024-04-29 18:40:14 +00:00
|
|
|
fprintf(stderr, "⚫ Testing missing root node:\n");
|
2024-04-06 14:31:33 +00:00
|
|
|
// Test case for a grammar that is missing a root rule
|
|
|
|
const std::string grammar_str = R"""(rot ::= expr
|
|
|
|
expr ::= term ("+" term)*
|
|
|
|
term ::= number
|
|
|
|
number ::= [0-9]+)""";
|
|
|
|
|
|
|
|
grammar_parser::parse_state parsed_grammar = grammar_parser::parse(grammar_str.c_str());
|
|
|
|
|
|
|
|
// Ensure we parsed correctly
|
|
|
|
assert(!parsed_grammar.rules.empty());
|
|
|
|
|
|
|
|
// Ensure we do NOT have a root node
|
|
|
|
assert(parsed_grammar.symbol_ids.find("root") == parsed_grammar.symbol_ids.end());
|
2024-04-29 18:40:14 +00:00
|
|
|
fprintf(stderr, " ✅︎ Passed\n");
|
2024-04-06 14:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_failure_missing_reference() {
|
2024-04-29 18:40:14 +00:00
|
|
|
fprintf(stderr, "⚫ Testing missing reference node:\n");
|
|
|
|
|
2024-04-06 14:31:33 +00:00
|
|
|
// Test case for a grammar that is missing a referenced rule
|
2024-04-29 18:40:14 +00:00
|
|
|
const std::string grammar_str =
|
|
|
|
R"""(root ::= expr
|
2024-04-06 14:31:33 +00:00
|
|
|
expr ::= term ("+" term)*
|
|
|
|
term ::= numero
|
|
|
|
number ::= [0-9]+)""";
|
|
|
|
|
2024-04-29 18:40:14 +00:00
|
|
|
fprintf(stderr, " Expected error: ");
|
2024-04-06 14:31:33 +00:00
|
|
|
|
|
|
|
grammar_parser::parse_state parsed_grammar = grammar_parser::parse(grammar_str.c_str());
|
|
|
|
|
|
|
|
// Ensure we did NOT parsed correctly
|
|
|
|
assert(parsed_grammar.rules.empty());
|
|
|
|
|
2024-04-29 18:40:14 +00:00
|
|
|
fprintf(stderr, " End of expected error.\n");
|
|
|
|
fprintf(stderr, " ✅︎ Passed\n");
|
2024-04-06 14:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2024-04-29 18:40:14 +00:00
|
|
|
fprintf(stdout, "Running grammar integration tests...\n");
|
2024-04-06 14:31:33 +00:00
|
|
|
test_simple_grammar();
|
|
|
|
test_complex_grammar();
|
2024-04-29 18:40:14 +00:00
|
|
|
test_quantifiers();
|
2024-04-06 14:31:33 +00:00
|
|
|
test_failure_missing_root();
|
|
|
|
test_failure_missing_reference();
|
2024-04-29 18:40:14 +00:00
|
|
|
fprintf(stdout, "All tests passed.\n");
|
2024-04-06 14:31:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|