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