Rule Variables
Every matched rule in an SRGS grammar carries a value — its rule variable — that holds the rule’s contribution to the semantic result. Understanding how that value is set, what type it takes, and which other rules can read it is the heart of working with semantic interpretation.
String Literals
In the string-literals tag format, whatever appears inside a tag becomes the rule variable for the current rule, verbatim. The format does no computation — there is no way to perform operations on the value; it is a literal assignment and nothing more.
Rule Visibility and Inheritance
When a rule references another rule and that rule matches, the referenced rule becomes a child of the referencing rule. Consider a simple boolean grammar:
| #ABNF 1.0 UTF-8; language en-US; mode voice; tag-format <semantics/1.0-literals>; root $boolean; $boolean = $yes | $no; $yes = yes {true}; $no = no {false}; |
Only a rule’s direct children are visible to it: a rule can read the variables of its immediate children, but not those of deeper descendants.
Visibility also drives inheritance. If a matched rule contains no semantic interpretation tags of its own, its variable inherits the value of the last visible rule’s variable. In the grammar above, $boolean has no tag, so its value is inherited from whichever of $yes or $no matched.
SI Script
Visibility and inheritance matter even more with SI Script, the tag format that allows the full range of ECMAScript operations rather than literal assignment alone.
The object out refers to the current rule’s variable; you set and modify it with standard ECMAScript operators. Here is the same boolean grammar written with SI Script:
| #ABNF 1.0 UTF-8; language en-US; mode voice; tag-format <semantics/1.0>; root $boolean; $boolean = $yes | $no; $yes = yes {out="true"}; $no = no {out="false"}; |
Variable Types
A rule variable can hold any type available in ECMAScript. Until a value is assigned, it has no explicit type.
Because the type follows the value, keep in mind how operators behave on each. Adding two strings concatenates them into a single string, while adding two numbers performs arithmetic — the same + does very different things depending on the operands, which is a common source of subtle interpretation bugs.
Accessing Other Rule Variables
SI Script exposes an object called rules whose properties are the variables of all currently visible rules. Reference a specific one with rules.rulename.
This grammar recognizes a binary string and assembles it one digit at a time:
| root $binary; $binary = {out=""} ($one {out+=rules.one} | $zero {out+=rules.zero}) <1->; $one = one {out="1"}; $zero = zero {out="0"}; |
Each time $one or $zero matches, its variable ("1" or "0") is appended to out. Note the {out=""} tag, which initializes out as a string before the repeat begins.
When you simply want the most recently matched rule, rules.latest() always refers to it — which lets you collapse the two branches into one:
| root $binary; $binary = (($one | $zero) {out+=rules.latest()})<1->; $one = one {out="1"}; $zero = zero {out="0"}; |
Meta Variables
Beyond its value, each rule has an associated meta variable that carries information from the recognizer about how the rule was matched. You can reach it three ways:
meta.rulename— a specific rulemeta.latest()— the last rule matchedmeta.current()— the current rule
Each meta variable exposes two properties:
text— the raw text the recognizer matchedscore— the confidence score
For example, this rule records the interpretation alongside the confidence and raw text of the match:
| $yes = yes {out.interpretation="true"; out.confidence=meta.current().score; out.rawtext=meta.current().text;}; |
