summaryrefslogtreecommitdiff
path: root/examples/manual/numbers.lex
blob: 6484d6ee35b9e37aa8189077441a090183f72a8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * numbers.lex : An example of the definitions and techniques
 *               for scanning numbers
 */

%{
#include <stdio.h>

#define UNSIGNED_LONG_SYM   1
#define SIGNED_LONG_SYM     2
#define UNSIGNED_SYM        3
#define SIGNED_SYM          4
#define LONG_DOUBLE_SYM     5
#define FLOAT_SYM           6

union _yylval {
  long double    ylong_double;
  float          yfloat; 
  unsigned long  yunsigned_long;
  unsigned       yunsigned;
  long           ysigned_long;
  int            ysigned;
} yylval;

%}

digit             [0-9]
hex_digit         [0-9a-fA-F]
oct_digit         [0-7]

exponent          [eE][+-]?{digit}+
i                 {digit}+
float_constant    ({i}\.{i}?|{i}?\.{i}){exponent}?
hex_constant      0[xX]{hex_digit}+
oct_constant      0{oct_digit}*
int_constant      {digit}+
long_ext          [lL]
unsigned_ext      [uU]
float_ext         [fF]
ulong_ext         {long_ext}{unsigned_ext}|{unsigned_ext}{long_ext}

%%

{hex_constant}{ulong_ext} {  /* we need to skip the "0x" part */
                             sscanf(&yytext[2],"%lx",&yylval.yunsigned_long); 
                             return(UNSIGNED_LONG_SYM);
                          }
{hex_constant}{long_ext}  {  
                             sscanf(&yytext[2],"%lx",&yylval.ysigned_long); 
                             return(SIGNED_LONG_SYM);
                          }
{hex_constant}{unsigned_ext}  { 
                             sscanf(&yytext[2],"%x",&yylval.yunsigned); 
                             return(UNSIGNED_SYM);
                          }
{hex_constant}            { /* use %lx to protect against overflow */
                             sscanf(&yytext[2],"%lx",&yylval.ysigned_long); 
                             return(SIGNED_LONG_SYM);
                          }
{oct_constant}{ulong_ext} {
                             sscanf(yytext,"%lo",&yylval.yunsigned_long); 
                             return(UNSIGNED_LONG_SYM);
                          }
{oct_constant}{long_ext}  {
                             sscanf(yytext,"%lo",&yylval.ysigned_long); 
                             return(SIGNED_LONG_SYM);
                          }
{oct_constant}{unsigned_ext}  {
                             sscanf(yytext,"%o",&yylval.yunsigned); 
                             return(UNSIGNED_SYM);
                          }
{oct_constant}            { /* use %lo to protect against overflow */
                             sscanf(yytext,"%lo",&yylval.ysigned_long); 
                             return(SIGNED_LONG_SYM);
                          }
{int_constant}{ulong_ext} {
                             sscanf(yytext,"%ld",&yylval.yunsigned_long); 
                             return(UNSIGNED_LONG_SYM);
                          }
{int_constant}{long_ext}  {
                             sscanf(yytext,"%ld",&yylval.ysigned_long); 
                             return(SIGNED_LONG_SYM);
                          }
{int_constant}{unsigned_ext}  {
                             sscanf(yytext,"%d",&yylval.yunsigned); 
                             return(UNSIGNED_SYM);
                          }
{int_constant}            { /* use %ld to protect against overflow */
                             sscanf(yytext,"%ld",&yylval.ysigned_long); 
                             return(SIGNED_LONG_SYM);
                          }
{float_constant}{long_ext}  {
                             sscanf(yytext,"%lf",&yylval.ylong_double); 
                             return(LONG_DOUBLE_SYM);
                          }
{float_constant}{float_ext}  {
                             sscanf(yytext,"%f",&yylval.yfloat); 
                             return(FLOAT_SYM);
                          }
{float_constant}          { /* use %lf to protect against overflow */
                             sscanf(yytext,"%lf",&yylval.ylong_double); 
                             return(LONG_DOUBLE_SYM);
                          }
%%

int main(void)
{
  int code;

  while((code = yylex())){
    printf("yytext          : %s\n",yytext);
    switch(code){
    case UNSIGNED_LONG_SYM:
       printf("Type of number  : UNSIGNED LONG\n");
       printf("Value of number : %lu\n",yylval.yunsigned_long);
       break;
    case SIGNED_LONG_SYM:  
       printf("Type of number  : SIGNED LONG\n");
       printf("Value of number : %ld\n",yylval.ysigned_long);
       break;
    case UNSIGNED_SYM:     
       printf("Type of number  : UNSIGNED\n");
       printf("Value of number : %u\n",yylval.yunsigned);
       break;
    case SIGNED_SYM:       
       printf("Type of number  : SIGNED\n");
       printf("Value of number : %d\n",yylval.ysigned);
       break;
    case LONG_DOUBLE_SYM:  
       printf("Type of number  : LONG DOUBLE\n");
       printf("Value of number : %lf\n",yylval.ylong_double);
       break;
    case FLOAT_SYM:        
       printf("Type of number  : FLOAT\n");
       printf("Value of number : %f\n",yylval.yfloat);
       break;
    default:
       printf("Type of number  : UNDEFINED\n");
       printf("Value of number : UNDEFINED\n");
       break;
    }
  }
  return(0);
}