# Grammar Namespace: LLama.Grammars A grammar is a set of [GrammarRule](./llama.grammars.grammarrule.md)s for deciding which characters are valid next. Can be used to constrain output to certain formats - e.g. force the model to output JSON ```csharp public sealed class Grammar ``` Inheritance [Object](https://docs.microsoft.com/en-us/dotnet/api/system.object) → [Grammar](./llama.grammars.grammar.md) ## Properties ### **StartRuleIndex** Index of the initial rule to start from ```csharp public ulong StartRuleIndex { get; } ``` #### Property Value [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)
### **Rules** The rules which make up this grammar ```csharp public IReadOnlyList Rules { get; } ``` #### Property Value [IReadOnlyList<GrammarRule>](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ireadonlylist-1)
## Constructors ### **Grammar(IReadOnlyList<GrammarRule>, UInt64)** Create a new grammar from a set of rules ```csharp public Grammar(IReadOnlyList rules, ulong startRuleIndex) ``` #### Parameters `rules` [IReadOnlyList<GrammarRule>](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ireadonlylist-1)
The rules which make up this grammar `startRuleIndex` [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)
Index of the initial rule to start from #### Exceptions [ArgumentOutOfRangeException](https://docs.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception)
## Methods ### **CreateInstance()** Create a `SafeLLamaGrammarHandle` instance to use for parsing ```csharp public SafeLLamaGrammarHandle CreateInstance() ``` #### Returns [SafeLLamaGrammarHandle](./llama.native.safellamagrammarhandle.md)
### **Parse(String, String)** Parse a string of GGML BNF into a Grammar ```csharp public static Grammar Parse(string gbnf, string startRule) ``` #### Parameters `gbnf` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)
The string to parse `startRule` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)
Name of the start rule of this grammar #### Returns [Grammar](./llama.grammars.grammar.md)
A Grammar which can be converted into a SafeLLamaGrammarHandle for sampling #### Exceptions [GrammarFormatException](./llama.exceptions.grammarformatexception.md)
Thrown if input is malformed ### **ToString()** ```csharp public string ToString() ``` #### Returns [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)