A Simple Grammar

We will begin our look at writing SRGS grammars with a simple grammar that lets the engine recognize the words "yes" or "no".  Yes or no grammars are the "hello world" of grammar writing.

Example

#ABNF 1.0;
language en-US; //use the American English pronunciation dictionary.
mode voice;  //the input for this grammar will be spoken words (as opposed to DTMF)

root $yesorno;

$yes = yes;
$no = no;
$yesorno = $yes | $no;

This grammar contains most of the elements of any grammar you will write.  Let's take it apart.

The Grammar Identifier

Any SRGS grammar written in ABNF notation must begin with the line

#ABNF 1.0;

With no additional characters.  This identifies to the LumenVox grammar compiler that the file being read is an ABNF grammar, as opposed to an SRGS XML grammar, or other future supported grammar formats.

The Grammar Header

Following the identifier, a well formed grammar will contain information about the language the grammar is written in, the expected interaction mode, and the name of a rule where the engine will begin its search (the root rule).  In addition, the header may contain one or more tags, and an identifier describing the tag format for this grammar.  Tags will be discussed later in this tutorial.

The contents of the grammar header may be in any order, but no header data may occur in the file after the first rule is written.

Comments

ABNF grammars may contain comments anywhere in their body (with the exception of the first line, containing the grammar identifier).  The comment format is the same one used by the C, C++, and Java programming languages.

Rules

The rules of a grammar specify what word combinations the engine may recognize.  They are the heart of the grammar.  Each rule has a name, appearing on the left hand side of an "=" sign, and a rule expansion, appearing on the right hand side.  

The rule name starts with a "$", then a letter followed by additional letters, numbers, or underscore characters.

The rule expansion describes to the engine what sequences of words will allow a rule to be matched.  An entire grammar is matched if its root rule is matched.

The first rule in the above grammar is matched if the engine detects the word "yes" being spoken.  The second rule is matched if the word "no" is detected.  The third rule contains a "|" symbol, which is a logical "or" operator.  So the third rule is matched if the $yes or $no rules are matched.

Most of the rest of this tutorial will be concerned with writing more and more expressive rule expansions.

How the Speech Engine Uses a Grammar

When the engine begins decoding your audio, it starts at the root rule of the provided grammar, in this case the rule $yesorno.  It then steps through all legal expansions, looking for the first words it's allowed to listen for.  It moves into the rules $yes and $no, since it's allowed to match against either rule.  Since the first words in the rules $yes and $no are "yes" and "no", the engine knows that it is allowed to recognize either word.

If the engine detects "yes" as a possibility, it then looks for the next word it can recognize in the $yes rule.  Since there are no more words in the $yes rule, the rule is matched.  And since the $yes rule is matched, the $yesorno root rule is matched, so the entire grammar is matched.

Next Rule Expansions