Dela via


Grammar Rule Name Referencing

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

ECMAScript expressions that are contained in tag elements generate semantic values that are associated with the Rule Variable of a rule element. Script expressions that are contained in a tag element can evaluate values and assign values to the Rule Variable of the tag element's containing rule element. This Rule Variable of the containing rule element is called the Grammar Rule Name (GRN) Rule Variable. Scripts evaluate or assign values to the GRN Rule Variable using GRN referencing.

The GRN Rule Variable is identified by a dollar sign ("$"), and predefined as an object before any script expressions are processed. The GRN Rule Variable contains two predefined properties: _attributes and _value (represented by $._attributes and $._value). These properties can be used to set semantic information to be returned as the text content and element attribute-value pairs in the Semantic Markup Language (SML) output. For more information about how the recognizer serializes the Rule Variable and its properties, see Serialization of the Root Rule Variable and Serialization of Developer-defined Rule Variable Properties.

Separate tag elements can contain individual script expressions. A single tag element can also contain a concatenated series of multiple script expressions. Script expressions cannot assign values to reserved words.

Creating Child Properties

Because the GRN Rule Variable is predefined as an object, direct child properties of it can be created without using the new operator. Use $.propname (where propname is the name of the property) to identify properties of the GRN Rule Variable.

In the following example, the GRN Rule Variable of the airport rule is predefined as an empty object. The script expressions contained in the tag elements create two properties (code and location) of the airport Rule Variable and assign values to these properties. The script expressions assign the value, LHR, to the code property and the value, England, to the location property.

<rule id="airport">
  Heathrow
    <tag>$.code= "LHR";</tag>
    <tag>$.location= "England";</tag>
</rule>

Creating Grandchild Properties

ECMAScript is not a strongly typed language, meaning that when a property is initialized, it is not predefined as a specific type. Instead, assigning a value to the property sets the property type to the same type as the assigned value. In the preceding example, because the property location is assigned a string value, the property is defined as a string.

The way that type is set in ECMAScript has implications for creating grandchild properties. Before grandchild properties can be created, script writers must first initialize a child property of the type object to contain the grandchild properties. Initialize an object using the new operator ("{}") as shown in the following example.

<rule id="airport">
  Heathrow
    <tag>$.location={};</tag>
    <tag>$.location.country= "England";</tag>
    <tag>$.location.city= "London";</tag>
    <tag>$.location.elevation= "80 ft. MSL";</tag>
</rule>

In this example, the script initializes the location property as an object. The grandchild properties country, city, and elevation are then initialized as direct properties of the child property location and assigned the values England, London, and 80 ft. MSL, respectively.

Order of Assignment

The way that type is set in ECMAScript also has implications for the order in which script writers can assign values to the Rule Variable or its properties. The previous examples illustrate how a script writer can initialize the location property as a string or an object. Although the Rule Variable is predefined to be an object, the Rule Variable can also be changed to a different type through simple assignment. In the following example, the script assigns a string value to the Rule Variable, thus changing the type of the Rule Variable from an object to a string.

<rule id="airport">
  Heathrow
    <tag>$ = "A major airport near London";</tag>
</rule>

If the property type or the Rule Variable is changed, any values that already exist in the property or Rule Variable is lost. In the following example, the script initializes the code and location child properties and assigns the values LHR and England to them, respectively. However, in the next line, these properties and their values are destroyed when the Rule Variable is assigned a string value.

<rule id="airport">
  Heathrow
    <tag>$.code = "LHR";</tag>
    <tag>$.location = "England";</tag>
    <tag>$ = "A major airport near London";</tag>
</rule>

In response to the utterance "Heathrow," this rule returns the semantic value "A major airport near London" and not the values LHR and England.