Home > Articles > Web Services > XML

Introduction to Voice XML Part 3: Voice XML Grammars

In grade school, learning the rules of grammar is often painful but pays off when one needs to write and speak coherent sentences. When building voice applications, grammars play a similar role as the basis for user communication. Frank Coyle shows why understanding the rules of voice grammars opens the door to constructing robust voice applications that model your users' speech patterns and improves the quality of the voice interface.
Like this article? We recommend

In grade school, learning the rules of grammar is often painful but pays off when one needs to write and speak coherent sentences. When building voice applications, grammars play a similar role as the basis for user communication. Understanding the rules of voice grammars opens the door to constructing robust voice applications that model your users’ speech patterns and improves the quality of the voice interface.

In this article, we’ll look at how to take your Voice XML applications to the next level of sophistication through the use of grammars. A grammar defines the words and word phrases that a user can say at any point in a conversational dialogue. Working in conjunction with your voice server, the words and phrases defined in a voice grammar are processed by the server’s speech recognition engine to identify permissible sequences of words and to attach meaning to them.

The art of grammar authoring is critical to the development of robust, usable telephony speech applications. When an application grammar accurately models the speech patterns of callers, everyone wins. The usability of the application is enhanced, and caller satisfaction increases. With growth in the market for speech technology, grammar authoring is emerging as an important area of specialization.

Grammars are built around tokens and rules. Tokens are the basic units of a grammar, either spoken words or touch-tone digits (dtmf). Rules are what we use to string together tokens (or other rules) to build phrases that a Voice XML engine can recognize and respond to.

As an example, imagine a voice application linked to your computerized home control system. You’ve been working late and are heading home. You dial your Voice XML app and say this:

Hey, turn on the Jacuzzi. I’ll be home at 9 P. Set the temp to 102.

A well-architected grammar should also be able to understand something like this:

Yo, Jacuzzi on, heat it to 102, be home in 2 hours.

If the grammar is set up correctly, the voice server should parse either of the above phrases and perhaps generate some XML that looks like this:


It can notify your home server using an HTTP get request:


Then, when you arrive home, just step into the heated Jacuzzi that’s ready for you after a hard day at work.

Voice XML Grammar Options

There is no one standard for writing grammars; different providers typically support several options. Currently, there are four major grammar formats in use today, described in the following sections.

GSL (Nuance)

GSL is the format used by the popular Nuance advanced speech recognition system (ASR). GSL has been incorporated by Voice XML development platforms such as BeVocal Café, Nuance, Tellme Studio, and others. GSL allows rule composition using constructs such as optional, sequences, alternatives, and probabilities. Since GSL has been around for a while, there are numerous web sites where you can find examples and tutorials on GSL.

Since GSL is quite compact and easier to write than XML grammars, many developers prefer GSL. The first examples in this article use GSL.

Java Speech Grammar Format (JSGF)

JSGF is Sun Microsystems’ platform and vendor-independent format for use in speech recognition. JSGF is part of the Java Speech API initiative and is now available as a W3C Note. JSGF leverages the Java programming language to provide naming conventions for grammar and package names.

Similar to the Java programming language, JSGF supports the convenient concept of documentation comments. With Sun’s move into the Java phone cellular space, expect to see JSGF in wide use.

Speech Recognition Grammar Specification (SRGS)

The Speech Recognition Specification (SRGS) is part of the W3C’s effort to develop standards to allow speech-based interaction with Web-based applications. In setting up SRGS, the W3C actually specified two grammar formats: an XML-based syntax (with an associated DTD) and a non-XML syntax called ABNF.

The ABNF format is based on Backus-Naur Form (BNF), which has been used for decades to specify context-free grammars for computer languages and protocols. SRGS supports rule definitions and, as part of the current working draft, an XSLT stylesheet that can convert the XML grammars to their corresponding ABNF form.

The choice between the non-XML and XML formats is up to the developer, and depends on what is supported by your voice server. While non-XML grammars may be easier to write, there are two strong arguments for using XML. The first is the W3C Voice XML 2.0 Recommendation which requires compliant implementations to support the XML format and only optionally support ABNF.

The second and most compelling reason to become familiar at least with the XML grammar is the existence of an associated DTD and XML Schema that, when used with an XML editor, makes the task of grammar writing less error-prone. Even if you write an XML grammar with just a text editor, you can always run a quick validity check and reduce potential lost time trying to eyeball a grammar error.

So let’s jump in and take a look at grammar construction by working through a sample application that asks users for a name and returns a phone extension. It should be noted that these examples are a bit contrived to illustrate grammar construction. All the lookup takes place within the Voice XML document. While this works fine for a very small company, in practice you would want to pass the user request to a server and return some Voice XML that reported the extension.

With that in mind, let’s start our grammar explorations by looking at the popular and widely supported Nuance GSL grammar. After we get our feet wet with GSL, we’ll take a look at how to do similar things with the SGRS XML grammar.

Example 1: Working with Grammars

Let’s begin by looking at some sample dialogues that can be used to retrieve the telephone extension of employees.

Example 1 Dialogue:

S: Whose extension would you like me to look up?

U: Bob Kirin.

S: Their extension is 8 3 0 6.

S: Whose extension would you like me to look up?

U: Bob.

S: Their extension is 8 3 0 6.

S: Whose extension would you like me to look up?

U: Kirin.

S: Their extension is 8 3 0 6.

Here the user can use first name, last name, or both to get the extension.

Listing 1 Simple Voice XML form with inline grammar

1  <vxml version="2.0" xmlns=’http://www.w3.org/2001/vxml’

2   <form id="F1">
3   <field name="worker">

4   <prompt>
5       Whose extension would you like me to look up?
6    </prompt>

7   <grammar type="application/x-nuance-gsl" mode="voice">
8   <![CDATA[
9   [
10    [bob kirin]    {<worker "8 3 0 6">}
11    [roger whitney]  {<worker "9 2 3 4">}
12    [wilma rudolph]  {<worker "8 1 2 1">}
13    [customer service] {<worker "6 2 3 4">}
14   ]
15   ]]>
16   </grammar>

17   <filled >
18    <prompt>
19     Their extension is <value expr="worker"/>
20    </prompt>
21   </filled>

22   </field>
23   </form>
24   </vxml>

Listing 1 shows the complete Voice XML document that supports the dialogues for this example. It contains a form with a field. Within that field are a prompt (what the user first hears), a grammar that determines acceptable input, and a filled element that is executed when there is a match between spoken input and the grammar element.

If there is no match, the default no-match handler reports a do-not-understand message and returns the user to the form.

Let’s deconstruct the Voice XML in Listing 1:

  • Line 3: The field is called "worker". Our goal is to determine which worker the user is interested in.
  • Line 7: The grammar begins. Note that the grammar element is a subelement of field. The type attribute specifies the kind of grammar (Nuance GSL), and the mode attribute specifies voice (dtmf is also an option).
  • Line 8: The grammar is enclosed in a CDATA section. Most non-XML grammars are wrapped inside CDATA since they might include characters that cause trouble for an XML parser.
  • Line 10: The [] defines what the speech recognizer is looking for (i.e. Bob Kirin). The {} specifies the meaning when Bob Kirin is spoken. In this case, the value for the field worker is set to the string "8 3 0 6". We include spaces here so that the speech generator will pronounce each number individually.
  • Line 17: When a match is made against one of the workers, the filled element is executed. In this case, we use a prompt to report the worker’s extension, which is made available by retrieving the value of the field variable worker.

Example 2: Alternate Options

In Example 1, users have the option of speaking the complete name or just the first or last name. But if two individuals have the same first or last name, this creates a problem.

To force a sequence of word tokens to be spoken, grammar writers can enclose the required words in parentheses; for example: (roger rabbit). Listing 2 illustrates a grammar that will support the following dialogue.

Example 2 Dialogue:

S: Whose extension would you like me to look up?

U: Roger.

S: I did not understand what you said.

U: Roger Rabbit.

S: Their extension is 9 2 8 8.

S: Whose extension would you like me to look up?

U: The big fat boss.

S: Their extension is 8 3 0 6.

S: Whose extension would you like me to look up?

U: Kirin.

S: Their extension is 8 3 0 6.

Listing 2 A GSL grammar with options

1   <grammar type="application/x-nuance-gsl" mode="voice">
2   <![CDATA[
3   [
4    [bob kirin (the big fat boss)] {<worker "8 3 0 6">}
5    [(roger whitney) whitney]    {<worker "9 2 3 4">}
6    [wilma rudolph]         {<worker "8 1 2 1">}
7    [(roger rabbit) rabbit]     {<worker "9 2 8 8">}
8    [customer service]       {<worker "6 2 3 7">}
9   ]
10   ]]>
11   </grammar>

Some details about Listing 2:

  • Line 4: We can ask for bob, kirin, bob kirin, or the big fat boss.
  • Lines 5 and 7: To distinguish workers with the same first names, we surround the full names with ().

Example 3: Wildcards

The wildcard operators provide additional control over the options for spoken input. Similar to regular expression and DTD syntax, GSL supports the following wildcard style operators:

  • Zero or more occurrences: *
  • One or more occurrences: +
  • Optional: ?

For example, consider the following alternative ways of getting the numbers.

Example 3 Dialogue:

S: Whose extension would you like me to look up?

U: Bob.

S: Their extension is 8 3 0 6.


U: The big fat boss.

S: Their extension is 8 3 0 6.


U: big boss.

S: Their extension is 8 3 0 6.


U: boss.

S: Their extension is 8 3 0 6.

S: Whose extension would you like me to look up?

U: Customer Service.

S: Their extension is 6 2 3 7.


U: Help.

S: Their extension is 6 2 3 7.


U: I need help.

S: Their extension is 6 2 3 7.


U: I really really really really need help.

S: Their extension is 6 2 3 7.

Listing 3 shows the grammar for the Example 3 dialogue.

Listing 3 A GSL grammar with wildcards

1   <grammar type="application/x-nuance-gsl" mode="voice">
2   <![CDATA[
3   [
4    [bob kirin (?the ?big ?fat boss)]    {<worker "8 3 0 6">}
5    [(roger whitney) whitney]         {<worker "9 2 3 4">}
6    [wilma rudolph]              {<worker "8 1 2 1">}
7    [(roger rabbit) rabbit]          {<worker "9 2 3 8 8">}
8    [customer service (?i *really ?need help) ]  {<worker "6 2 3 7">}
9   ]
10   ]]>
11   </grammar>

Note the following for Listing 3:

  • Line 4: Using ?the ?big ?fat makes each word optional.
  • Line 8: Using *really lets users say really in sequence as many times as they like, including none.

As you can see, the Nuance GSL grammar is straightforward and relatively easy to understand. Let’s now move on to the XML grammar defined as part of the W3C’s SGRS.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020