Phone_Number.gram

This example ABNF grammar recognizes a spoken North American phone number — an optional area code followed by a seven-digit number — and uses semantic interpretation to return a structured result with two string members, areacode and number. When the caller does not speak an area code, it defaults to 858. The grammar demonstrates several common techniques: mapping spoken digit words to their numeric values, using the repeat operator to require a fixed number of digits, and building a structured semantic result with the out variable.

Example

#ABNF 1.0;
mode voice;
language en-US;

tag-format <semantics/1.0>;

root $PhoneNumber;

$Digit = (ONE {out = "1"} |
TWO {out = "2"} |
THREE {out = "3"} |
FOUR {out = "4"} |
FIVE {out = "5"} |
SIX {out = "6"} |
SEVEN {out = "7"} |
EIGHT {out = "8"} |
NINE {out = "9"} |
(ZERO | O) {out = "0"} );

/*
* $AreaCode resolves to a three digit string
* after semantic interpretation.
*/

$AreaCode = { out = "" } ( $Digit { out += rules.Digit } ) <3>;

/*
* $Number resolves to a seven digit string
* after semantic interpretation.
*/

$Number = { out = "" } ( $Digit { out += rules.latest() } ) <7>;

/*
* After semantic interpretation,
* $PhoneNumber resolves to a structure with two member variable strings,
* areacode (which defaults to "858"), and number.
*/

$PhoneNumber =
(
[AREA CODE | ONE]
$AreaCode { out.areacode = rules.latest() }
$Number { out.number = rules.latest() }
) |
( $Number ) { out.areacode = "858"; out.number = rules.latest() };

Related articles


Was this article helpful?