Grammar syntax

A complete discussion of grammar syntax is beyond the scope of this Programmer's Guide. The information provided in this section is merely to acquaint you with the basics of writing grammars. The VoiceXML browser supports both SRGS XML format and SRGS ABNF. The examples in this section are in the XML format.

Grammar header

An SRGS XML format grammar consists of a grammar header and a grammar body. The grammar header declares the various attributes of the grammar, including the version of the grammar, its mode and its root rule. When included within a VoiceXML 2.1 document, a grammar header might look like this:
<grammar version="1.0" type="application/srgs+xml" root="startHere" mode="dtmf">
A standalone grammar header would include additional information, including the XML namespace:
<grammar version="1.0" xmlns="http://www.w3.org/2001/06/grammar"
mode="voice" root="citystate" tag-format="semantics/1.0">

For external grammars, the mode attribute must be specified on the grammar tag within the VoiceXML document as well as in the header of the external grammar.

Grammar body

The grammar body consists of one or more rules that define the valid set of utterances. The syntax for grammar rules is:
<rule id="rulename" scope="rule-scope">
   rule contents
</rule>
where:
rulename
is the name the rule will be referenced by from other grammars and VoiceXML applications.
scope
is either public or private. Publicly scoped rules can be referenced from any grammar or VoiceXML application; privately scoped rules are only accessible within the grammar file in which they are declared.
rule contents
are the various items that make up the rule itself. For example:
<rule id="direction" scope="public">
   <one-of>
      <item>left</item>
      <item>right</item>
   </one-of>
</rule>
identifies a rule that, when activated, will let someone say either “left” or “right”.

Comments in grammars

Comments within a grammar are the same as they are anywhere in an XML document. They start with the characters<!-- and end with -->. For example:

<!-- this is a comment -->

External and inline grammars

Grammars can either be defined inline, within a VoiceXML document, or the VoiceXML document can reference an external grammar file. When specified inline, the <grammar> tag must identify the root rule to activate, using the root attribute. When an external grammar is referenced from within a VoiceXML document, the document does not need to specify the root rule to activate. If none is specified, the root rule identified in the header of the grammar itself is activated. You can also activate a specific rule within the external grammar from the VoiceXML document, by appending a fragment identifier to the URI of the external grammar. For example:
<grammar src="citystate.grxml#citylist"/>
In this example, the rule named citylist in the grammar citysytate.grxml would be activated.

DTMF grammars

The following example defines an inline DTMF grammar that enables the user to make a selection by pressing the numbers 1 through 4, the asterisk or the pound sign.

<grammar version="1.0" xmlns="http://www.w3.org/2001/06/grammar"
mode="dtmf" root="digits" tag-format="semantics/1.0">
     <rule id="digits">
          <one-of>
               <item>1</item>
               <item>2</item>
               <item>3</item>
               <item>4</item>
               <item>*</item>
               <item>#</item>
           </one-of>
     </rule>
 </grammar>             
If you are using external grammars, make sure you include the mode=“dtmf” attribute in the <grammar> element.
Note: DTMF grammars used with Blueworx Voice Response do not support the use of hotword barge-in. This means that if a prompt is played with bargeintype="hotword" and a DTMF grammar is active, then the prompt will stop as soon as any DTMF key is detected and the prompt will be played as though it was set to bargeintype="speech". This occurs regardless of whether or not any speech grammars are also active.

You can also use the bargein=dtmf_only attribute of the <prompt> element to enable the user to stop the audio output by pressing any DTMF key. See Comparing barge-in detection methods