source: trunk/flex/examples/manual/dates.lex@ 3032

Last change on this file since 3032 was 3031, checked in by bird, 18 years ago

flex 2.5.33.

File size: 2.8 KB
Line 
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
9char month[20],dow[20],day[20],year[20];
10
11%}
12
13skip of|the|[ \t,]*
14
15mon (mon(day)?)
16tue (tue(sday)?)
17wed (wed(nesday)?)
18thu (thu(rsday)?)
19fri (fri(day)?)
20sat (sat(urday)?)
21sun (sun(day)?)
22
23day_of_the_week ({mon}|{tue}|{wed}|{thu}|{fri}|{sat}|{sun})
24
25jan (jan(uary)?)
26feb (feb(ruary)?)
27mar (mar(ch)?)
28apr (apr(il)?)
29may (may)
30jun (jun(e)?)
31jul (jul(y)?)
32aug (aug(ust)?)
33sep (sep(tember)?)
34oct (oct(ober)?)
35nov (nov(ember)?)
36dec (dec(ember)?)
37
38first_half ({jan}|{feb}|{mar}|{apr}|{may}|{jun})
39second_half ({jul}|{aug}|{sep}|{oct}|{nov}|{dec})
40month {first_half}|{second_half}
41
42nday [1-9]|[1-2][0-9]|3[0-1]
43nmonth [1-9]|1[0-2]
44nyear [0-9]{1,4}
45
46year_ext (ad|AD|bc|BC)?
47day_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
99long\n BEGIN(LONG);
100short\n BEGIN(SHORT);
101
102{skip}*
103\n
104.
105
106
Note: See TracBrowser for help on using the repository browser.