<grammar version="1.0" xmlns="http://www.w3.org/2001/06/grammar" xml:lang="en-US" tag-format="semantics/1.0" root="captureDate"> <!-- Header definition for US language and the root rule "captureDate" to start with-->

	<rule id="captureDate" scope="public"><!-- Rule to define accepted input and call subrules-->
		<tag>out=""</tag> <!-- Define output object-->
		<one-of>
			<item>
				<one-of>
					<item><!-- Capture input in the format: the 25th of December -->
						<item repeat="0-1">the</item>
						<one-of>
							<item><ruleref uri="#digits"/><tag>day = rules.digits;</tag></item>
							<item><ruleref uri="#teens"/><tag>day = rules.teens;</tag></item>
							<item><ruleref uri="#above_twenty"/><tag>day = rules.above_twenty;</tag></item>
						</one-of>
						<item repeat="0-1">of</item>
						<item><ruleref uri="#MonthName"/><tag>month=rules.MonthName;</tag></item>
					</item>
					<item><!-- Capture input in the format: the December the 25th -->
						<item><ruleref uri="#MonthName"/><tag>month=rules.MonthName;</tag></item>
						<item repeat="0-1">the</item>
						<one-of>
							<item><ruleref uri="#digits"/><tag>day = rules.digits;</tag></item>
							<item><ruleref uri="#teens"/><tag>day = rules.teens;</tag></item>
							<item><ruleref uri="#above_twenty"/><tag>day = rules.above_twenty;</tag></item>
						</one-of>
					</item>
				</one-of>
				<one-of><!-- Capture input in the format: Year YY / YYYY -->
					<item><ruleref uri="#year2"/><tag>year = rules.year2;var thisYear = new Date(); 
						if((thisYear.getYear()-year) > 100){year = year + 2000} else {year = year + 1900};</tag></item> <!-- Use ecma script to adjust 2 digit years under the assumption that caller are younger than 100-->
					<item><ruleref uri="#year4"/><tag>year = rules.year4;</tag></item>
				</one-of>
			<tag><!--Prepare a different output format strings--> 
			if(10 > day){day="0"+day.toString()}; if(10 > month){month="0"+month.toString()};
			out  = year.toString() + "-" + month.toString() + "-" + day.toString();</tag> 
			</item>
			<item>
				<!-- DTMF Date in the format: d d m m y y y y / 2 1 0 8 1 9 8 5 -->
				<item><ruleref uri="#dayDTMF"/><tag>out += rules.dayDTMF</tag></item>
				<item><ruleref uri="#monthDTMF"/><tag>out = out + "/" + rules.monthDTMF + "/"</tag></item>
				<one-of>
					<item>1 9<ruleref uri="#yearDTMF"/><tag>out += 1900 + rules.yearDTMF</tag></item>
					<item>2 0<ruleref uri="#yearDTMF"/><tag>out += 2000 + rules.yearDTMF;</tag></item>
				</one-of>				
			</item>			
			<item><ruleref uri="#agent"/><tag>out=rules.agent;</tag></item><!--Exit point to route the caller to an agent--> 
		</one-of>
	</rule>
	
	<!-- DTMF INPUT -->
	
	<rule id="yearDTMF" scope="public"> <!--Rule to manage DTMF input for year -->
        <one-of>
			<item>0 0<tag>out = 0;</tag></item>
			<item>0<ruleref uri="#yearDTMF1_9"/><tag> out=rules.yearDTMF1_9;</tag></item>
			<item>1 0<tag>out = 10;</tag></item>
			<item>1<ruleref uri="#yearDTMF1_9"/><tag>out=rules.yearDTMF1_9+10;</tag></item>
			<item>2 0<tag>out = 20;</tag></item>
			<item>2<ruleref uri="#yearDTMF1_9"/><tag>out=rules.yearDTMF1_9+20;</tag></item>
			<item>3 0<tag>out = 30;</tag></item>
			<item>3<ruleref uri="#yearDTMF1_9"/><tag>out=rules.yearDTMF1_9+30;</tag></item>
			<item>4 0<tag>out = 40;</tag></item>
			<item>4<ruleref uri="#yearDTMF1_9"/><tag>out=rules.yearDTMF1_9+40;</tag></item>
			<item>5 0<tag>out = 50;</tag></item>
			<item>5<ruleref uri="#yearDTMF1_9"/><tag>out=rules.yearDTMF1_9+50;</tag></item>
			<item>6 0<tag>out = 60;</tag></item>
			<item>6<ruleref uri="#yearDTMF1_9"/><tag>out=rules.yearDTMF1_9+60;</tag></item>
			<item>7 0<tag>out = 70;</tag></item>
			<item>7<ruleref uri="#yearDTMF1_9"/><tag>out=rules.yearDTMF1_9+70;</tag></item>
			<item>8 0<tag>out = 80;</tag></item>
			<item>8<ruleref uri="#yearDTMF1_9"/><tag>out=rules.yearDTMF1_9+80;</tag></item>
			<item>9 0<tag>out = 90;</tag></item>
			<item>9<ruleref uri="#yearDTMF1_9"/><tag>out=rules.yearDTMF1_9+90;</tag></item>
		</one-of>
	</rule>
	
    <rule id="yearDTMF1_9" scope="public"> <!--Subrule to manage DTMF input for year between 1 and 9 -->
        <one-of>
			<item>1<tag>out = 1;</tag></item>
			<item>2<tag>out = 2;</tag></item>
			<item>3<tag>out = 3;</tag></item>
			<item>4<tag>out = 4;</tag></item>
			<item>5<tag>out = 5;</tag></item>
			<item>6<tag>out = 6;</tag></item>
			<item>7<tag>out = 7;</tag></item>
			<item>8<tag>out = 8;</tag></item>
			<item>9<tag>out = 9;</tag></item>
        </one-of>
    </rule>

	<rule id="dayDTMF" scope="public"> <!--Rule to manage DTMF input for day -->
        <one-of>
			<item>0<ruleref uri="#dayDTMF1_9"/><tag>out= "0" + rules.dayDTMF1_9</tag></item>
			<item>1 0<tag>out = 10;</tag></item>
			<item>1<ruleref uri="#dayDTMF1_9"/><tag>out=rules.dayDTMF1_9+10;</tag></item>
			<item>2 0<tag>out = 20;</tag></item>
			<item>2<ruleref uri="#dayDTMF1_9"/><tag>out=rules.dayDTMF1_9+20;</tag></item>
			<item>3 0<tag>out = 30;</tag></item>
			<item>3 1<tag>out = 31;</tag></item>
		</one-of>
	</rule>

    <rule id="dayDTMF1_9" scope="public"><!--Subrule to manage DTMF input for day between 1 and 9 -->
        <one-of>
			<item>1<tag>out = 1;</tag></item>
			<item>2<tag>out = 2;</tag></item>
			<item>3<tag>out = 3;</tag></item>
			<item>4<tag>out = 4;</tag></item>
			<item>5<tag>out = 5;</tag></item>
			<item>6<tag>out = 6;</tag></item>
			<item>7<tag>out = 7;</tag></item>
			<item>8<tag>out = 8;</tag></item>
			<item>9<tag>out = 9;</tag></item>
        </one-of>
    </rule>
 
	<rule id="monthDTMF" scope="public"><!--Rule to manage DTMF input for month -->
        <one-of>
			<item>0<ruleref uri="#monthDTMF1_9"/><tag>out = rules.monthDTMF1_9</tag></item>
			<item>1 0<tag>out = "10";</tag></item>
			<item>1 1<tag>out = "11";</tag></item>
			<item>1 2<tag>out = "12";</tag></item>
		</one-of>
	</rule>
	
    <rule id="monthDTMF1_9" scope="public"><!--Subrule to manage DTMF input for month between 1 and 9 -->
        <one-of>
			<item>1<tag>out = "01";</tag></item>
			<item>2<tag>out = "02;</tag></item>
			<item>3<tag>out = "03";</tag></item>
			<item>4<tag>out = "04";</tag></item>
			<item>5<tag>out = "05";</tag></item>
			<item>6<tag>out = "06";</tag></item>
			<item>7<tag>out = "07";</tag></item>
			<item>8<tag>out = "08";</tag></item>
			<item>9<tag>out = "09";</tag></item>
        </one-of>
    </rule>
	
	<!-- Voice INPUT -->

	<rule id="MonthName" scope="public"> <!--Rule to capture the name of the month-->
		<one-of>
			<item>january<tag>out=1;</tag></item>
			<item>february<tag>out=2;</tag></item>
			<item>march<tag>out=3;</tag></item>
			<item>april<tag>out=4;</tag></item>
			<item>may<tag>out=5;</tag></item>
			<item>june<tag>out=6;</tag></item>
			<item>july<tag>out=7;</tag></item>
			<item>august<tag>out=8;</tag></item>
			<item>september<tag>out=9;</tag></item>
			<item>october<tag>out=10;</tag></item>
			<item>november<tag>out=11;</tag></item>
			<item>december<tag>out=12;</tag></item>
			<item>jan<tag>out=1;</tag></item>
			<item>feb<tag>out=2;</tag></item>
			<item>aug<tag>out=8;</tag></item>
			<item>sept<tag>out=9;</tag></item>
			<item>oct<tag>out=10;</tag></item>
			<item>nov<tag>out=11;</tag></item>
			<item>dec<tag>out=12;</tag></item>
		</one-of>
	</rule>

	<rule id="digits"> <!--Rule to manage input for values between 0 and 9 -->
		<one-of>
			<item>0<tag>out=0;</tag></item>
			<item>1<tag>out=1;</tag></item>
			<item>2<tag>out=2;</tag></item>
			<item>3<tag>out=3;</tag></item>
			<item>4<tag>out=4;</tag></item>
			<item>5<tag>out=5;</tag></item>
			<item>6<tag>out=6;</tag></item>
			<item>7<tag>out=7;</tag></item>
			<item>8<tag>out=8;</tag></item>
			<item>9<tag>out=9;</tag></item>
			<item>first<tag>out=1;</tag></item>
			<item>second<tag>out=2;</tag></item>
			<item>third<tag>out=3;</tag></item>
			<item>fourth<tag>out=4;</tag></item>
			<item>fifth<tag>out=5;</tag></item>
			<item>sixth<tag>out=6;</tag></item>
			<item>seventh<tag>out=7;</tag></item>
			<item>eighth<tag>out=8;</tag></item>
			<item>ninth<tag>out=9;</tag></item>
		</one-of>
	</rule>

	<rule id="teens"> <!--Rule to manage input for values between 10 and 19-->
		<one-of>
			<item>ten<tag>out=10;</tag></item>
			<item>tenth<tag>out=10;</tag></item>
			<item>eleven<tag>out=11;</tag></item>
			<item>twelve<tag>out=12;</tag></item>
			<item>thirteen<tag>out=13;</tag></item>
			<item>fourteen<tag>out=14;</tag></item>
			<item>fifteen<tag>out=15;</tag></item>
			<item>sixteen<tag>out=16;</tag></item>
			<item>seventeen<tag>out=17;</tag></item>
			<item>eighteen<tag>out=18;</tag></item>
			<item>nineteen<tag>out=19;</tag></item>
			<item>tenth<tag>out=10;</tag></item>
			<item>eleventh<tag>out=11;</tag></item>
			<item>twelveth<tag>out=12;</tag></item>
			<item>thirteenth<tag>out=13;</tag></item>
			<item>fourteenth<tag>out=14;</tag></item>
			<item>fifteenth<tag>out=15;</tag></item>
			<item>sixteenth<tag>out=16;</tag></item>
			<item>seventeenth<tag>out=17;</tag></item>
			<item>eighteenth<tag>out=18;</tag></item>
			<item>nineteenth<tag>out=19;</tag></item>
		</one-of>
	</rule>

	<rule id="above_twenty"> <!--Rule to manage input for values above twenties-->
		<one-of>
			<item>twenty<tag>out=20;</tag></item>
			<item>thirty<tag>out=30;</tag></item>
		</one-of>
		<item repeat="0-1"><ruleref uri="#digits"/><tag>out += rules.digits;</tag></item>
	</rule>
	
    <rule id="year2" scope="public"> <!--Rule to manage input for 2 digit year and call subrules-->
        <one-of>
            <item><ruleref uri="#YearDigit0_99" /><tag>out = rules.latest();</tag></item>
            <item>oh<ruleref uri="#YearDigit1_9" /><tag>out = rules.latest();</tag></item>
        </one-of>
    </rule>
	
	<rule id="year4" scope="public"> <!--Rule to manage input for 4 digit year and call subrules-->
		<one-of>
			<item>nineteen hundred<tag>out = 1900;</tag></item>
			<item><!-- nineteen  -->
				<item>nineteen</item>
				<item repeat="0-1">hundred and</item>
				<one-of>
					<item>oh<ruleref uri="#YearDigit1_9" /></item>
					<item><ruleref uri="#YearDigit1_9" /></item>
					<item><ruleref uri="#YearDigit0_99" /></item>
				</one-of>
				<tag>out = 1900 + rules.latest();</tag>
			</item>
			<item>two thousand<tag>out = 2000;</tag></item>
			<item><!-- two thousand -->
				<item>two thousand</item>
				<item repeat="0-1">and</item>
				<one-of>
					<item><ruleref uri="#YearDigit1_9" /></item>
					<item><ruleref uri="#YearDigit0_99" /></item>
				</one-of>
				<tag>out = 2000 + rules.latest();</tag>
			</item>
		</one-of>
	</rule>

	<rule id="YearDigit0_99" scope="public"> <!--Rule to capture numbers from 10 to 99-->
        <one-of>
			<item>ten<tag>out=10;</tag></item>
			<item>eleven<tag>out=11;</tag></item>
			<item>twelve<tag>out=12;</tag></item>
			<item>thirteen<tag>out=13;</tag></item>
			<item>fourteen<tag>out=14;</tag></item>
			<item>fifteen<tag>out=15;</tag></item>
			<item>sixteen<tag>out=16;</tag></item>
			<item>seventeen<tag>out=17;</tag></item>
			<item>eighteen<tag>out=18;</tag></item>
			<item>nineteen<tag>out=19;</tag></item>
			<item>twenty<tag>out=20;</tag></item>			
			<item>twenty<ruleref uri="#YearDigit1_9"/><tag>out=rules.latest()+20;</tag></item>
			<item>thirty<tag>out30;</tag></item>
			<item>thirty<ruleref uri="#YearDigit1_9"/><tag>out=rules.latest()+30;</tag></item>
			<item>forty<tag>out=40;</tag></item>
			<item>forty<ruleref uri="#YearDigit1_9"/><tag>out=rules.latest()+40;</tag></item>
			<item>fifty<tag>out=50;</tag></item>
			<item>fifty<ruleref uri="#YearDigit1_9"/><tag>out=rules.latest()+50;</tag></item>
			<item>sixty<tag>out=60;</tag></item>
			<item>sixty<ruleref uri="#YearDigit1_9"/><tag>out=rules.latest()+60;</tag></item>
			<item>seventy<tag>out=70;</tag></item>
			<item>seventy<ruleref uri="#YearDigit1_9"/><tag>out=rules.latest()+70;</tag></item>
			<item>eighty<tag>out=80;</tag></item>
			<item>eighty<ruleref uri="#YearDigit1_9"/><tag>out=rules.latest()+80;</tag></item>
			<item>ninety<tag>out=90;</tag></item>
			<item>ninety<ruleref uri="#YearDigit1_9"/><tag>out=rules.latest()+90;</tag></item>			
        </one-of>
    </rule>

    <rule id="YearDigit1_9" scope="public"> <!--Rule to capture numbers from 1 to 9-->
        <one-of>
			<item>one<tag>out=1;</tag></item>
			<item>two<tag>out=2;</tag></item>
			<item>three<tag>out=3;</tag></item>
			<item>four<tag>out=4;</tag></item>
			<item>five<tag>out=5;</tag></item>
			<item>six<tag>out=6;</tag></item>
			<item>seven<tag>out=7;</tag></item>
			<item>eight<tag>out=8;</tag></item>
			<item>nine<tag>out=9;</tag></item>
        </one-of>
    </rule>	
	
	<rule id="agent" scope="public"><!-- Exit point to talk to an agent-->
		<item>
			<item repeat="0-1">i</item>
			<item repeat="0-1">want to</item>
			<one-of>
				<item repeat="0-1">speak</item>
				<item repeat="0-1">talk</item>
			</one-of>
			<one-of>
				<item repeat="0-1">to an</item>
				<item repeat="0-1">with an</item>
			</one-of>
			<one-of>
				<item>agent<tag>out="agent"</tag></item>
				<item>employee<tag>out="agent"</tag></item>
			</one-of>
		</item>
    </rule>
</grammar>