37 付録: ABNF形式とXML形式の文法の例

以下に示すのは、「open a file.」「please move the window.」といったコマンドに対応した簡単な文法である。この文法は、別々定義されたさらに上級の文法(ここには示さないが)を参照する。

ABNF形式

	#ABNF 1.0 ISO-8859-1;
	
	language en;mode voice;
	root $basicCmd;
	alias $(http://www.sayplease.com/politeness.gram) $$polite;
	
	
	meta "author" is "Stephanie Williams";
	
	/**
	 * Basic command.
	 * @example please move the window
	 * @example open a file
	 */
	
	public $basicCmd = 
	          $$polite#startPolite $command $$polite#endPolite;
	
	$command = $action $object;
	$action = /10/ open {'OPEN'} | /2/ close {'CLOSE'} 
	                 | /1/ delete {'DELETE'} | /1/ move {'MOVE'};
	$object = [the | a] (window | file | menu);

XML形式

	<?xml version="1.0"?>
	
	<grammar xmlns="http://www.w3.org/2001/06/grammar" 
	        xml:lang="en" version="1.0" mode="voice" root="basicCmd">
	
	<alias name="polite"
	        uri="http://www.sayplease.com/politeness.xml"/>
	
	<meta name="author" content="Stephanie Williams"/>
	
	<rule id="basicCmd" scope="public">
	  <example> please move the window </example>
	  <example> open a file </example>
	
	  <ruleref alias="polite#startPolite"/>
          <ruleref uri="#command"/>
	  <ruleref alias="polite#endPolite"/>
	
	</rule>
	
	<rule id="command">
	  <ruleref uri="#action"/> <ruleref uri="#object"/>
	</rule>
	
	<rule id="action">
	   <one-of>
  	      <item weight="10"> open   <tag>'OPEN'</tag> </item>
	      <item weight="2">  close  <tag>'CLOSE'</tag> </item>
	      <item weight="1">  delete <tag>'DELETE'</tag> </item>
	      <item weight="1">  move   <tag>'MOVE'</tag> </item>
	    </one-of>
	</rule>
	
	<rule id="object">
	 <item repeat="0-1">
	
	    <one-of>
	      <item> the </item>
	      <item> a </item>
	    </one-of>
	 </item>
	
	 <one-of>
	      <item> window </item>
	      <item> file </item>
	      <item> menu </item>
	 </one-of>
	</rule>
	
	</grammar>

次の2つの文法は、XML形式およびABNF形式の両文法においての参照を示している。

ABNF:http://www.example.com/places.gram

	#ABNF 1.0 ISO-8859-1;
	
	language en;mode voice;
	root $city_state;
	// No aliases  in this referenced grammar.
	
	public $city = Boston | Philadelphia | Fargo;
	
	public $state = Florida | North Dakota | New York;
	
	// References to local rules
	// Artificial example allows "Boston, Florida!"
	
	public $city_state = $city $state;

ABNF:http://www.example.com/booking.gram

	#ABNF 1.0 ISO-8859-1;
	
	language en;
	mode voice;
	
	alias $(http://www.example.com/places.gram) $$someplaces;
	
	
	// Reference by URI syntax
	$flight = I want to fly to
	   $(http://www.example.com/places.gram#city);
	
	// Reference using alias name
	$exercise = I want to walk to $$someplaces#state;
	
	// Reference to root rule using an alias reference
	$wet = I want to swim to $$someplaces;

XML:http://www.example.com/places.xml

	<?xml version="1.0"?>
	
	<grammar xmlns="http://www.w3.org/2001/06/grammar" 
	        xml:lang="en" version="1.0" root="city_state" mode="voice">
	
	   <rule id="city" scope="public">
	     <one-of>
	       <item>Boston</item>
	       <item>Philadelphia</item>
	       <item>Fargo</item>
	     </one-of>
	   </rule>
	
	   <rule id="state" scope="public">
	     <one-of>
	       <item>Florida</item>
	       <item>North Dakota</item>
	       <item>New York</item>
	     </one-of>
	   </rule>
	
	   <!-- Reference by URI to a local rule -->
	   <!-- Artificial example allows "Boston, Florida"! -->
	   <rule id="city_state" scope="public">
	     <ruleref uri="#city"/> <ruleref uri="#state"/>
	   </rule>
	</grammar>

XML:http://www.example.com/booking.xml

	<?xml version="1.0"?>
	
	<grammar xmlns="http://www.w3.org/2001/06/grammar" 
	        xml:lang="en" version="1.0" mode="voice">

 	<alias name="someplaces"
 	        uri="http://www.example.com/places.xml"/>
	
	
	   <!-- Using URI syntax -->
	   <rule id="flight">
	     I want to fly to 
	     <ruleref uri="http://www.example.com/places.xml#city"/>
	   </rule>
	
	   <!-- Using alias syntax -->
	   <rule id="exercise">
	     I want to walk to 
	     <ruleref alias="someplaces#state"/>
	   </rule>
	
	   <!-- Reference to root rule of a grammar by alias -->
	   <rule id="wet">
	     I want to swim to 
	     <ruleref alias="someplaces"/>
	   </rule>
	</grammar>