xref: /minix3/external/bsd/flex/dist/examples/manual/numbers.lex (revision 357f1050293be536ca8309aae20889945ce99fc1)
1*357f1050SThomas Veerman /*
2*357f1050SThomas Veerman  * numbers.lex : An example of the definitions and techniques
3*357f1050SThomas Veerman  *               for scanning numbers
4*357f1050SThomas Veerman  */
5*357f1050SThomas Veerman 
6*357f1050SThomas Veerman %{
7*357f1050SThomas Veerman #include <stdio.h>
8*357f1050SThomas Veerman 
9*357f1050SThomas Veerman #define UNSIGNED_LONG_SYM   1
10*357f1050SThomas Veerman #define SIGNED_LONG_SYM     2
11*357f1050SThomas Veerman #define UNSIGNED_SYM        3
12*357f1050SThomas Veerman #define SIGNED_SYM          4
13*357f1050SThomas Veerman #define LONG_DOUBLE_SYM     5
14*357f1050SThomas Veerman #define FLOAT_SYM           6
15*357f1050SThomas Veerman 
16*357f1050SThomas Veerman union _yylval {
17*357f1050SThomas Veerman   long double    ylong_double;
18*357f1050SThomas Veerman   float          yfloat;
19*357f1050SThomas Veerman   unsigned long  yunsigned_long;
20*357f1050SThomas Veerman   unsigned       yunsigned;
21*357f1050SThomas Veerman   long           ysigned_long;
22*357f1050SThomas Veerman   int            ysigned;
23*357f1050SThomas Veerman } yylval;
24*357f1050SThomas Veerman 
25*357f1050SThomas Veerman %}
26*357f1050SThomas Veerman 
27*357f1050SThomas Veerman digit             [0-9]
28*357f1050SThomas Veerman hex_digit         [0-9a-fA-F]
29*357f1050SThomas Veerman oct_digit         [0-7]
30*357f1050SThomas Veerman 
31*357f1050SThomas Veerman exponent          [eE][+-]?{digit}+
32*357f1050SThomas Veerman i                 {digit}+
33*357f1050SThomas Veerman float_constant    ({i}\.{i}?|{i}?\.{i}){exponent}?
34*357f1050SThomas Veerman hex_constant      0[xX]{hex_digit}+
35*357f1050SThomas Veerman oct_constant      0{oct_digit}*
36*357f1050SThomas Veerman int_constant      {digit}+
37*357f1050SThomas Veerman long_ext          [lL]
38*357f1050SThomas Veerman unsigned_ext      [uU]
39*357f1050SThomas Veerman float_ext         [fF]
40*357f1050SThomas Veerman ulong_ext         {long_ext}{unsigned_ext}|{unsigned_ext}{long_ext}
41*357f1050SThomas Veerman 
42*357f1050SThomas Veerman %%
43*357f1050SThomas Veerman 
44*357f1050SThomas Veerman {hex_constant}{ulong_ext} {  /* we need to skip the "0x" part */
45*357f1050SThomas Veerman                              sscanf(&yytext[2],"%lx",&yylval.yunsigned_long);
46*357f1050SThomas Veerman                              return(UNSIGNED_LONG_SYM);
47*357f1050SThomas Veerman                           }
48*357f1050SThomas Veerman {hex_constant}{long_ext}  {
49*357f1050SThomas Veerman                              sscanf(&yytext[2],"%lx",&yylval.ysigned_long);
50*357f1050SThomas Veerman                              return(SIGNED_LONG_SYM);
51*357f1050SThomas Veerman                           }
52*357f1050SThomas Veerman {hex_constant}{unsigned_ext}  {
53*357f1050SThomas Veerman                              sscanf(&yytext[2],"%x",&yylval.yunsigned);
54*357f1050SThomas Veerman                              return(UNSIGNED_SYM);
55*357f1050SThomas Veerman                           }
56*357f1050SThomas Veerman {hex_constant}            { /* use %lx to protect against overflow */
57*357f1050SThomas Veerman                              sscanf(&yytext[2],"%lx",&yylval.ysigned_long);
58*357f1050SThomas Veerman                              return(SIGNED_LONG_SYM);
59*357f1050SThomas Veerman                           }
60*357f1050SThomas Veerman {oct_constant}{ulong_ext} {
61*357f1050SThomas Veerman                              sscanf(yytext,"%lo",&yylval.yunsigned_long);
62*357f1050SThomas Veerman                              return(UNSIGNED_LONG_SYM);
63*357f1050SThomas Veerman                           }
64*357f1050SThomas Veerman {oct_constant}{long_ext}  {
65*357f1050SThomas Veerman                              sscanf(yytext,"%lo",&yylval.ysigned_long);
66*357f1050SThomas Veerman                              return(SIGNED_LONG_SYM);
67*357f1050SThomas Veerman                           }
68*357f1050SThomas Veerman {oct_constant}{unsigned_ext}  {
69*357f1050SThomas Veerman                              sscanf(yytext,"%o",&yylval.yunsigned);
70*357f1050SThomas Veerman                              return(UNSIGNED_SYM);
71*357f1050SThomas Veerman                           }
72*357f1050SThomas Veerman {oct_constant}            { /* use %lo to protect against overflow */
73*357f1050SThomas Veerman                              sscanf(yytext,"%lo",&yylval.ysigned_long);
74*357f1050SThomas Veerman                              return(SIGNED_LONG_SYM);
75*357f1050SThomas Veerman                           }
76*357f1050SThomas Veerman {int_constant}{ulong_ext} {
77*357f1050SThomas Veerman                              sscanf(yytext,"%ld",&yylval.yunsigned_long);
78*357f1050SThomas Veerman                              return(UNSIGNED_LONG_SYM);
79*357f1050SThomas Veerman                           }
80*357f1050SThomas Veerman {int_constant}{long_ext}  {
81*357f1050SThomas Veerman                              sscanf(yytext,"%ld",&yylval.ysigned_long);
82*357f1050SThomas Veerman                              return(SIGNED_LONG_SYM);
83*357f1050SThomas Veerman                           }
84*357f1050SThomas Veerman {int_constant}{unsigned_ext}  {
85*357f1050SThomas Veerman                              sscanf(yytext,"%d",&yylval.yunsigned);
86*357f1050SThomas Veerman                              return(UNSIGNED_SYM);
87*357f1050SThomas Veerman                           }
88*357f1050SThomas Veerman {int_constant}            { /* use %ld to protect against overflow */
89*357f1050SThomas Veerman                              sscanf(yytext,"%ld",&yylval.ysigned_long);
90*357f1050SThomas Veerman                              return(SIGNED_LONG_SYM);
91*357f1050SThomas Veerman                           }
92*357f1050SThomas Veerman {float_constant}{long_ext}  {
93*357f1050SThomas Veerman                              sscanf(yytext,"%lf",&yylval.ylong_double);
94*357f1050SThomas Veerman                              return(LONG_DOUBLE_SYM);
95*357f1050SThomas Veerman                           }
96*357f1050SThomas Veerman {float_constant}{float_ext}  {
97*357f1050SThomas Veerman                              sscanf(yytext,"%f",&yylval.yfloat);
98*357f1050SThomas Veerman                              return(FLOAT_SYM);
99*357f1050SThomas Veerman                           }
100*357f1050SThomas Veerman {float_constant}          { /* use %lf to protect against overflow */
101*357f1050SThomas Veerman                              sscanf(yytext,"%lf",&yylval.ylong_double);
102*357f1050SThomas Veerman                              return(LONG_DOUBLE_SYM);
103*357f1050SThomas Veerman                           }
104*357f1050SThomas Veerman %%
105*357f1050SThomas Veerman 
106*357f1050SThomas Veerman int main(void)
107*357f1050SThomas Veerman {
108*357f1050SThomas Veerman   int code;
109*357f1050SThomas Veerman 
110*357f1050SThomas Veerman   while((code = yylex())){
111*357f1050SThomas Veerman     printf("yytext          : %s\n",yytext);
112*357f1050SThomas Veerman     switch(code){
113*357f1050SThomas Veerman     case UNSIGNED_LONG_SYM:
114*357f1050SThomas Veerman        printf("Type of number  : UNSIGNED LONG\n");
115*357f1050SThomas Veerman        printf("Value of number : %lu\n",yylval.yunsigned_long);
116*357f1050SThomas Veerman        break;
117*357f1050SThomas Veerman     case SIGNED_LONG_SYM:
118*357f1050SThomas Veerman        printf("Type of number  : SIGNED LONG\n");
119*357f1050SThomas Veerman        printf("Value of number : %ld\n",yylval.ysigned_long);
120*357f1050SThomas Veerman        break;
121*357f1050SThomas Veerman     case UNSIGNED_SYM:
122*357f1050SThomas Veerman        printf("Type of number  : UNSIGNED\n");
123*357f1050SThomas Veerman        printf("Value of number : %u\n",yylval.yunsigned);
124*357f1050SThomas Veerman        break;
125*357f1050SThomas Veerman     case SIGNED_SYM:
126*357f1050SThomas Veerman        printf("Type of number  : SIGNED\n");
127*357f1050SThomas Veerman        printf("Value of number : %d\n",yylval.ysigned);
128*357f1050SThomas Veerman        break;
129*357f1050SThomas Veerman     case LONG_DOUBLE_SYM:
130*357f1050SThomas Veerman        printf("Type of number  : LONG DOUBLE\n");
131*357f1050SThomas Veerman        printf("Value of number : %lf\n",yylval.ylong_double);
132*357f1050SThomas Veerman        break;
133*357f1050SThomas Veerman     case FLOAT_SYM:
134*357f1050SThomas Veerman        printf("Type of number  : FLOAT\n");
135*357f1050SThomas Veerman        printf("Value of number : %f\n",yylval.yfloat);
136*357f1050SThomas Veerman        break;
137*357f1050SThomas Veerman     default:
138*357f1050SThomas Veerman        printf("Type of number  : UNDEFINED\n");
139*357f1050SThomas Veerman        printf("Value of number : UNDEFINED\n");
140*357f1050SThomas Veerman        break;
141*357f1050SThomas Veerman     }
142*357f1050SThomas Veerman   }
143*357f1050SThomas Veerman   return(0);
144*357f1050SThomas Veerman }
145*357f1050SThomas Veerman 
146