2 * numbers.lex : An example of the definitions and techniques
9 #define UNSIGNED_LONG_SYM 1
10 #define SIGNED_LONG_SYM 2
11 #define UNSIGNED_SYM 3
13 #define LONG_DOUBLE_SYM 5
17 long double ylong_double;
19 unsigned long yunsigned_long;
31 exponent [eE][+-]?{digit}+
33 float_constant ({i}\.{i}?|{i}?\.{i}){exponent}?
34 hex_constant 0[xX]{hex_digit}+
35 oct_constant 0{oct_digit}*
40 ulong_ext {long_ext}{unsigned_ext}|{unsigned_ext}{long_ext}
44 {hex_constant}{ulong_ext} { /* we need to skip the "0x" part */
45 sscanf(&yytext[2],"%lx",&yylval.yunsigned_long);
46 return(UNSIGNED_LONG_SYM);
48 {hex_constant}{long_ext} {
49 sscanf(&yytext[2],"%lx",&yylval.ysigned_long);
50 return(SIGNED_LONG_SYM);
52 {hex_constant}{unsigned_ext} {
53 sscanf(&yytext[2],"%x",&yylval.yunsigned);
56 {hex_constant} { /* use %lx to protect against overflow */
57 sscanf(&yytext[2],"%lx",&yylval.ysigned_long);
58 return(SIGNED_LONG_SYM);
60 {oct_constant}{ulong_ext} {
61 sscanf(yytext,"%lo",&yylval.yunsigned_long);
62 return(UNSIGNED_LONG_SYM);
64 {oct_constant}{long_ext} {
65 sscanf(yytext,"%lo",&yylval.ysigned_long);
66 return(SIGNED_LONG_SYM);
68 {oct_constant}{unsigned_ext} {
69 sscanf(yytext,"%o",&yylval.yunsigned);
72 {oct_constant} { /* use %lo to protect against overflow */
73 sscanf(yytext,"%lo",&yylval.ysigned_long);
74 return(SIGNED_LONG_SYM);
76 {int_constant}{ulong_ext} {
77 sscanf(yytext,"%ld",&yylval.yunsigned_long);
78 return(UNSIGNED_LONG_SYM);
80 {int_constant}{long_ext} {
81 sscanf(yytext,"%ld",&yylval.ysigned_long);
82 return(SIGNED_LONG_SYM);
84 {int_constant}{unsigned_ext} {
85 sscanf(yytext,"%d",&yylval.yunsigned);
88 {int_constant} { /* use %ld to protect against overflow */
89 sscanf(yytext,"%ld",&yylval.ysigned_long);
90 return(SIGNED_LONG_SYM);
92 {float_constant}{long_ext} {
93 sscanf(yytext,"%lf",&yylval.ylong_double);
94 return(LONG_DOUBLE_SYM);
96 {float_constant}{float_ext} {
97 sscanf(yytext,"%f",&yylval.yfloat);
100 {float_constant} { /* use %lf to protect against overflow */
101 sscanf(yytext,"%lf",&yylval.ylong_double);
102 return(LONG_DOUBLE_SYM);
110 while((code = yylex())){
111 printf("yytext : %s\n",yytext);
113 case UNSIGNED_LONG_SYM:
114 printf("Type of number : UNSIGNED LONG\n");
115 printf("Value of number : %lu\n",yylval.yunsigned_long);
117 case SIGNED_LONG_SYM:
118 printf("Type of number : SIGNED LONG\n");
119 printf("Value of number : %ld\n",yylval.ysigned_long);
122 printf("Type of number : UNSIGNED\n");
123 printf("Value of number : %u\n",yylval.yunsigned);
126 printf("Type of number : SIGNED\n");
127 printf("Value of number : %d\n",yylval.ysigned);
129 case LONG_DOUBLE_SYM:
130 printf("Type of number : LONG DOUBLE\n");
131 printf("Value of number : %lf\n",yylval.ylong_double);
134 printf("Type of number : FLOAT\n");
135 printf("Value of number : %f\n",yylval.yfloat);
138 printf("Type of number : UNDEFINED\n");
139 printf("Value of number : UNDEFINED\n");