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