How do I generate syntax-independent and -specific grammars?
Last reviewed: 11/29/2008
HOW Article ID: H110802
The information in this article applies to:
- GrammarKit 3
Summary
The grammar management class, ChantGM, enables you to build a grammar independent of grammar syntax. Your application uses the ChantGrammar and adjunct classes to construct and modify grammar objects as needed and generate compiler-specific syntax on demand.
More Information
To construct syntax-independent grammars, use the ChantGrammar and adjunct classes to build grammar objects from which you can generate compiler-specific syntax. These classes support generic and syntax-specific definitions that enable you to tailor grammars to leverage features across recognizers.
// Build Grammar
ChantGrammar = ChantGM.CreateGrammar("Colors")
ChantGrammar.AddComment("This is the list of colors")
ChantGrammarRule = ChantGrammar.AddRule("Color")
ChantGrammarList = ChantGrammarRule.AddList("colorlist")
ChantGrammarList.AddPhrase("red")
ChantGrammarList.AddPhrase("blue")
ChantGrammarList.AddPhrase("green")
ChantGrammarList.AddPhrase("yellow")
ChantGrammarList.AddPhrase("pink")
ChantGrammarList.AddPhrase("purple")
ChantGrammarList.AddPhrase("brown")
ChantGrammarList.AddPhrase("orange")
// Generate Grammar Syntax for Microsoft SAPI 5
GrammarSyntax = ChantGrammar.GenerateGrammar(CGSSAPI5XML)
// Compile grammar
ChantGM.CompileResource(GrammarSyntax,GrammarSyntax.Length,CCOText,CGSSAPI5XML,NULL,CCRNone,0,CCPAsynchronous)
Refer to programming language specific syntax in the help file Class Library Reference.
Chant GrammarKit Developer Edition Compile sample has been updated to illustrate generating a grammar. Look in the Samples directory of the component library you are using to locate the Compile sample.
Build and launch the sample compile grammar application. You can compile grammars for various grammar compiler formats that are installed on your system.
Select a grammar compiler to use from the list. Only the grammar compilers installed on your system are listed. If you do not see any grammar compilers listed, see the help file section entitled Grammar Compiler Installation.
The grammar syntax is generated for that grammar compiler and appears in the text box.
Press the compile button. The compilation, warning, and error messages are displayed.