1*3c3a7b76Schristos /* 2*3c3a7b76Schristos * dates.lex: An example of using start states to 3*3c3a7b76Schristos * distinguish between different date formats. 4*3c3a7b76Schristos */ 5*3c3a7b76Schristos 6*3c3a7b76Schristos %{ 7*3c3a7b76Schristos #include <ctype.h> 8*3c3a7b76Schristos 9*3c3a7b76Schristos char month[20],dow[20],day[20],year[20]; 10*3c3a7b76Schristos 11*3c3a7b76Schristos %} 12*3c3a7b76Schristos 13*3c3a7b76Schristos skip of|the|[ \t,]* 14*3c3a7b76Schristos 15*3c3a7b76Schristos mon (mon(day)?) 16*3c3a7b76Schristos tue (tue(sday)?) 17*3c3a7b76Schristos wed (wed(nesday)?) 18*3c3a7b76Schristos thu (thu(rsday)?) 19*3c3a7b76Schristos fri (fri(day)?) 20*3c3a7b76Schristos sat (sat(urday)?) 21*3c3a7b76Schristos sun (sun(day)?) 22*3c3a7b76Schristos 23*3c3a7b76Schristos day_of_the_week ({mon}|{tue}|{wed}|{thu}|{fri}|{sat}|{sun}) 24*3c3a7b76Schristos 25*3c3a7b76Schristos jan (jan(uary)?) 26*3c3a7b76Schristos feb (feb(ruary)?) 27*3c3a7b76Schristos mar (mar(ch)?) 28*3c3a7b76Schristos apr (apr(il)?) 29*3c3a7b76Schristos may (may) 30*3c3a7b76Schristos jun (jun(e)?) 31*3c3a7b76Schristos jul (jul(y)?) 32*3c3a7b76Schristos aug (aug(ust)?) 33*3c3a7b76Schristos sep (sep(tember)?) 34*3c3a7b76Schristos oct (oct(ober)?) 35*3c3a7b76Schristos nov (nov(ember)?) 36*3c3a7b76Schristos dec (dec(ember)?) 37*3c3a7b76Schristos 38*3c3a7b76Schristos first_half ({jan}|{feb}|{mar}|{apr}|{may}|{jun}) 39*3c3a7b76Schristos second_half ({jul}|{aug}|{sep}|{oct}|{nov}|{dec}) 40*3c3a7b76Schristos month {first_half}|{second_half} 41*3c3a7b76Schristos 42*3c3a7b76Schristos nday [1-9]|[1-2][0-9]|3[0-1] 43*3c3a7b76Schristos nmonth [1-9]|1[0-2] 44*3c3a7b76Schristos nyear [0-9]{1,4} 45*3c3a7b76Schristos 46*3c3a7b76Schristos year_ext (ad|AD|bc|BC)? 47*3c3a7b76Schristos day_ext (st|nd|rd|th)? 48*3c3a7b76Schristos 49*3c3a7b76Schristos %s LONG SHORT 50*3c3a7b76Schristos %s DAY DAY_FIRST YEAR_FIRST YEAR_LAST YFMONTH YLMONTH 51*3c3a7b76Schristos 52*3c3a7b76Schristos %% 53*3c3a7b76Schristos 54*3c3a7b76Schristos /* the default is month-day-year */ 55*3c3a7b76Schristos 56*3c3a7b76Schristos <LONG>{day_of_the_week} strcpy(dow,yytext); 57*3c3a7b76Schristos <LONG>{month} strcpy(month,yytext); BEGIN(DAY); 58*3c3a7b76Schristos 59*3c3a7b76Schristos /* handle the form: day-month-year */ 60*3c3a7b76Schristos 61*3c3a7b76Schristos <LONG>{nday}{day_ext} strcpy(day,yytext); BEGIN(DAY_FIRST); 62*3c3a7b76Schristos <DAY_FIRST>{month} strcpy(month,yytext); BEGIN(LONG); 63*3c3a7b76Schristos <DAY>{nday}{day_ext} strcpy(day,yytext); BEGIN(LONG); 64*3c3a7b76Schristos 65*3c3a7b76Schristos <LONG>{nyear}{year_ext} { 66*3c3a7b76Schristos printf("Long:\n"); 67*3c3a7b76Schristos printf(" DOW : %s \n",dow); 68*3c3a7b76Schristos printf(" Day : %s \n",day); 69*3c3a7b76Schristos printf(" Month : %s \n",month); 70*3c3a7b76Schristos printf(" Year : %s \n",yytext); 71*3c3a7b76Schristos strcpy(dow,""); 72*3c3a7b76Schristos strcpy(day,""); 73*3c3a7b76Schristos strcpy(month,""); 74*3c3a7b76Schristos } 75*3c3a7b76Schristos 76*3c3a7b76Schristos /* handle dates of the form: day-month-year */ 77*3c3a7b76Schristos 78*3c3a7b76Schristos <SHORT>{nday} strcpy(day,yytext); BEGIN(YEAR_LAST); 79*3c3a7b76Schristos <YEAR_LAST>{nmonth} strcpy(month,yytext);BEGIN(YLMONTH); 80*3c3a7b76Schristos <YLMONTH>{nyear} strcpy(year,yytext); BEGIN(SHORT); 81*3c3a7b76Schristos 82*3c3a7b76Schristos /* handle dates of the form: year-month-day */ 83*3c3a7b76Schristos 84*3c3a7b76Schristos <SHORT>{nyear} strcpy(year,yytext); BEGIN(YEAR_FIRST); 85*3c3a7b76Schristos <YEAR_FIRST>{nmonth} strcpy(month,yytext);BEGIN(YFMONTH); 86*3c3a7b76Schristos <YFMONTH>{nday} strcpy(day,yytext); BEGIN(SHORT); 87*3c3a7b76Schristos 88*3c3a7b76Schristos 89*3c3a7b76Schristos <SHORT>\n { 90*3c3a7b76Schristos printf("Short:\n"); 91*3c3a7b76Schristos printf(" Day : %s \n",day); 92*3c3a7b76Schristos printf(" Month : %s \n",month); 93*3c3a7b76Schristos printf(" Year : %s \n",year); 94*3c3a7b76Schristos strcpy(year,""); 95*3c3a7b76Schristos strcpy(day,""); 96*3c3a7b76Schristos strcpy(month,""); 97*3c3a7b76Schristos } 98*3c3a7b76Schristos 99*3c3a7b76Schristos long\n BEGIN(LONG); 100*3c3a7b76Schristos short\n BEGIN(SHORT); 101*3c3a7b76Schristos 102*3c3a7b76Schristos {skip}* 103*3c3a7b76Schristos \n 104*3c3a7b76Schristos . 105*3c3a7b76Schristos 106*3c3a7b76Schristos 107