-
Notifications
You must be signed in to change notification settings - Fork 0
/
constant_int.c
114 lines (98 loc) · 2.61 KB
/
constant_int.c
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
#include "constant_int.h"
#include <ctype.h>
#include <stdlib.h>
#include "reg_exp.h"
typedef enum
{
ZERO,
DIGIT,
X,
OTHER
} signal;
lexeme *find_dec_constant(char *seq);
lexeme *find_oct_constant(char *seq);
lexeme *find_hex_constant(char *seq);
lexeme *find_int_constant(char *seq)
{
lexeme *a[3];
a[0] = find_dec_constant(seq);
a[1] = find_oct_constant(seq);
a[1] = find_hex_constant(seq);
return get_longest(a, 3);
}
lexeme *find_dec_constant(char *seq)
{
int get_signal(char a)
{
if (a == '0')
return ZERO;
else if (isdigit(a))
return DIGIT;
else
return OTHER;
}
static state transitions[MAX_STATES][MAX_SIGNALS] = {
[STATE_0][OTHER] = STATE_9,
[STATE_0][ZERO] = STATE_9,
[STATE_0][DIGIT] = STATE_1,
[STATE_1][OTHER] = STATE_2,
[STATE_1][ZERO] = STATE_1,
[STATE_1][DIGIT] = STATE_1
};
static state finals[1] = {STATE_1};
return reg_exp(seq, get_signal, transitions, INT_CONSTANT, finals, 1);
}
lexeme *find_oct_constant(char *seq)
{
int get_signal(char a)
{
if (a == '0')
return ZERO;
else if (a >= '0' && a <= '7')
return DIGIT;
else
return OTHER;
}
static state transitions[MAX_STATES][MAX_SIGNALS] = {
[STATE_0][OTHER] = STATE_9,
[STATE_0][ZERO] = STATE_1,
[STATE_0][DIGIT] = STATE_9,
[STATE_1][OTHER] = STATE_9,
[STATE_1][ZERO] = STATE_1,
[STATE_1][DIGIT] = STATE_1
};
static state finals[1] = {STATE_1};
return reg_exp(seq, get_signal, transitions, INT_CONSTANT, finals, 1);
}
lexeme *find_hex_constant(char *seq)
{
int get_signal(char a)
{
if (a == '0')
return ZERO;
else if (a == 'X' || a == 'x')
return X;
else if (isdigit(a) ||
(a > 'a' && a < 'f') ||
(a > 'A' && a < 'F'))
return DIGIT;
else
return OTHER;
}
static state transitions[MAX_STATES][MAX_SIGNALS] = {
[STATE_0][OTHER] = STATE_9,
[STATE_0][ZERO] = STATE_1,
[STATE_0][DIGIT] = STATE_9,
[STATE_0][X] = STATE_9,
[STATE_1][OTHER] = STATE_9,
[STATE_1][ZERO] = STATE_9,
[STATE_1][DIGIT] = STATE_9,
[STATE_1][X] = STATE_2,
[STATE_2][OTHER] = STATE_9,
[STATE_2][ZERO] = STATE_2,
[STATE_2][DIGIT] = STATE_2,
[STATE_2][X] = STATE_9,
};
static state finals[1] = {STATE_2};
return reg_exp(seq, get_signal, transitions, INT_CONSTANT, finals, 1);
}