Rule References

You can reference grammar rules inside rule expansions, as we have already seen (in our simple yes or no grammar, the root rule referenced the $yes and $no rules). You can reference another rule simply by including the rulename as part of a rule expansion:

$yesorno = $yes | $no;

When the grammar is parsed, the contents of the $yes and $no rules are included as part of the $yesorno rule. A rule is considered the child of any rule that references it. This hierarchical parsing of grammars is what builds the parse tree, and understanding this becomes very important when working with semantic interpretation.

You can also reference external grammar files -- or rules within external files -- to create more complex grammars, and re-use existing grammars. As an example, suppose you had a simple phone number grammar in a remote location that looked like this:

http://www.mycompany.com/phone_number.gram

#ABNF 1.0;
language en-US;
mode voice;
root $phone_number;

$phone_number = [$area_code] $number;

$digit = one | two | three | four | five | six | seven | eight | nine | zero;
$area_code = [one | area code] $digit<3>;
$number = $digit<7>;

You can use this grammar in another grammar by specifying its location, inside of angle brackets, as a rulename:

#ABNF 1.0;
language en-US;
mode voice;
root $main;

$main = (my | the) [phone] number is $<http://www.mycompany.com/phone_number.gram>;

The above grammar is using the root rule of the phone_number grammar in its $main rule. You can reference grammar files using http, ftp, or your operating system's local or network file descriptors. When writing grammars that use external grammar files, it's usually a good idea to specify a base URI in your grammar header.

To use a single rule in an external grammar, append the grammar name with the "#" symbol.

Example

#ABNF 1.0;
language en-US;
mode voice;
root $main;

$main = (my | the) area code is $<http://www.mycompany.com/phone_number.gram#area_code>;

In this example, $main is referencing only the $area_code rule from the phone_number grammar.

In addition to referencing external grammar files, you can also reference any of the LumenVox built-in grammars.

Example

Example
#ABNF 1.0;
language en-US;
mode voice;
root $main;

$main = (my | the) [phone] number is $<builtin:grammar/phone>;

The next part of the grammar tutorial covers writing Special Rules.

© 2012 LumenVox LLC. All rights reserved.