Grammar Class

Provides run time support for obtaining and managing grammar information.

Namespace: System.Speech.Recognition
Assembly: System.Speech (in system.speech.dll)

public class Grammar
public class Grammar
public class Grammar
Not applicable.

The System.Speech.Recognition.Grammar class provides run time objects that allow an application to specify a specific combination of words, choices of words, and other speech elements that the uses to identify meaningful phrases.

The Grammar object fully supports the W3C Speech Recognition Grammar Specification (SRGS) and Context Free Grammar (CFG) specifications. For more information, see SpeechRecognitionGrammarSpecification.

Grammars may be precise word phrases, such as "Turn the computer off," or provide choices, semantic lookup tables or wildcards, such as "Change color to" and a look up table of acceptable values.

An application's recognition engine, as managed by instances of SpeechRecognizer or SpeechRecognitionEngine, may create and load one or more instances Grammar, independently enabling or disabling particular grammars instance , and set Grammar properties such as priorities n(Priority and weight (Weight)

The grammar a Grammar object manages can be loaded from

SRGS and CFG formats allow the inclusion of multiple grammar rules, so on construction applications can specify which rule to load as the grammar's entry point or root. Support for the inclusion of handler methods (for recognition, initialization, parsing and errors), scripts and executables in a loaded grammar is provided by allowing the specification of parameters during Grammar construction.


For details on how to programmatically construct a grammar containing a script, see discussion of the Script, OnInit, OnParse, OnError, and OnRecognition properties on the System.Speech.Recognition.SrgsGrammar.SrgsRuleclass, the Language and Script properties on the System.Speech.Recognition.SrgsGrammar.SrgsDocument class, and the Script property on the System.Speech.Recognition.SrgsGrammar.SrgsTag class.

Because grammar formats allow for references to other grammar objects as both relative and fully qualified URIs, a base URI can be specified during construction of a Grammar object.


Windows and provide security for applications loading a Grammar from a DLL or working with a Grammar supporting scripting.

Scripts in Grammar objects are always run as if downloaded from a web page in the InternetZone, and the Common Runtime Layer (CLR) isolates any DLL loaded to obtain a Grammar. In either case, the system prevent permanent modification to the state of a Windows system.

However, it remains a best practice to always verify the safety of any URI or DLL used to load a Grammar.

The example below generates a file dialog to open a Grammar as a stream, selecting a specific grammar rule, and then loading that grammar into an instance of a recognition engine.

private void _grammarAddButton_Click(object sender, EventArgs eventArgs) {
  // Create the Open Grammar dialog box.
  OpenFileDialog dialog = new OpenFileDialog();
  dialog.Filter = "SRGS files {*.xml;*.srgs}|*.xml;*.srgs|CFG files|*.cfg|All files|*.*";
  dialog.Title = "Open Grammar";
  if (dialog.ShowDialog() == DialogResult.OK) {
    try {
      Uri baseURI = null;
      baseURI = new Uri("");
      Stream stream = dialog.OpenFile();
      Uri uriTarget = new Uri(dialog.FileName);
      Grammar grammar = new Grammar(stream, "SetColorRule", baseURI, null);
      grammar.Name = String.Format("{0} ({1})",
                   grammar.RuleName, dialog.FileName);
      grammar.Enabled = true;
    } catch (Exception exp) {
      // Catch all exceptions while loading the grammar file.
      MessageBox.Show(String.Format("{0} failed to load.\nError Message:\n{1}",
                    dialog.FileName, exp.Message));

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions