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