Skip to content

Conversation

Seven-Streams
Copy link
Collaborator

At present, we have several methods to build grammars: from an EBNF grammar, from a json schema, or from a regex e.t.c. This PR offers more functions to build grammar. Typically, here are:

  • empty(), return an empty grammar.
  • string(str), return a grammar which match the specific string.
  • character_class(bool, List[char]), return a grammar which matches(or rejects) a chracterclass.
  • star(grammar), return a grammar matches grammar*.
  • plus(grammar), return a grammar matches grammar+.
  • optional(grammar), return a grammar matches grammar?.

For example, if we want to create a grammar to match a signed integer, we can use:

import xgrammar as xgr
grammar1 = xgr.Grammar.union(xgr.Grammar.string("-"), xgr.Grammar.string("+"))
grammar2 = xgr.Grammar.character_class("[1-9]")
grammar3 = xgr.Grammar.star(xgr.Grammar.character_class("[0-9]"))
grammar1_optional = xgr.Grammar.optional(grammar1)
grammar = xgr.Grammar.concat(grammar1_optional, grammar2, grammar3)

Then, we can use grammar to match integers.

Signed-off-by: Yuchuan [email protected]

@Seven-Streams Seven-Streams requested a review from Ubospica August 13, 2025 10:21
@Seven-Streams Seven-Streams force-pushed the main-dev/2025-07-31/grammar_building_apis branch from cd01019 to cfbd875 Compare September 9, 2025 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant