Skip to content

Grammars bracket repetition symbol not working #1547

Closed
@Viagounet

Description

@Viagounet

Hello, I tried checking for similar issues about this problem but couldn't find one.
I've had an issue with not being able to use the repetition brackets symbol when working with grammars.

I'm using Ubuntu 20.04, Python 3.12 and llama_cpp_python==0.2.79.

The following works fine:

from llama_cpp import LlamaGrammar

grammar_string = r"""root ::= "repeating" [a-z]+"""
my_grammar = LlamaGrammar.from_string(grammar_string, verbose=True)

But this doesn't:

from llama_cpp import LlamaGrammar

grammar_string = r"""root ::= "repeating" [a-z]{1,}"""
my_grammar = LlamaGrammar.from_string(grammar_string, verbose=True)

It returns this error:

parse: error parsing grammar: expecting newline or end at {1,}
Traceback (most recent call last):
    my_grammar = LlamaGrammar.from_string(grammar_string, verbose=True)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/anaconda3/envs/DocLLM/lib/python3.12/site-packages/llama_cpp/llama_grammar.py", line 71, in from_string
    raise ValueError(
ValueError: from_string: error parsing grammar file: parsed_grammar.rules is empty

The llama.cpp GBNF Guide seems to say it should be possible to use this pattern?

Repetition and Optional Symbols

* after a symbol or sequence means that it can be repeated zero or more times (equivalent to {0,}).
+ denotes that the symbol or sequence should appear one or more times (equivalent to {1,}).
? makes the preceding symbol or sequence optional (equivalent to {0,1}).
{m} repeats the precedent symbol or sequence exactly m times
{m,} repeats the precedent symbol or sequence at least m times
{m,n} repeats the precedent symbol or sequence at between m and n times (included)
{0,n} repeats the precedent symbol or sequence at most n times (included)

Not sure if my understanding of GBNF is lacking or if it's a real bug.
Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions