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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
function dbg(x)
{
printf("dbg: <%s>\n", x)
return x
}
function empty(s)
{
return s == ""
}
function eat_char(s)
{
return substr(s, 2)
}
function eat_chars(s, n)
{
return substr(s, n + 1)
}
function matches(s, pfx)
{
return substr(s, 1, length(pfx)) == pfx
}
function match_and_eat(s, pfx)
{
if (matches(s, pfx))
return eat_chars(s, length(pfx))
return s
}
function match_and_eat_else(s, pfx, e)
{
if (matches(s, pfx))
return eat_chars(s, length(pfx))
return e
}
function eat_rchar(c)
{
if (c ~ /^\\./)
return eat_chars(c, 2)
if (c == "$")
return c
if (c !~ /^[\[\*\+\?{}\(\)|]/)
return eat_char(c)
return c
}
function eat_bchar(c)
{
if (c ~ /^\\]|\\-|\\\\/)
return eat_chars(c, 2)
if (c !~ /^[\-\[]/)
return eat_char(c)
return c
}
function eat_class(c)
{
c = match_and_eat(c, "[:alnum:]")
c = match_and_eat(c, "[:alpha:]")
c = match_and_eat(c, "[:blank:]")
c = match_and_eat(c, "[:cntrl:]")
c = match_and_eat(c, "[:digit:]")
c = match_and_eat(c, "[:graph:]")
c = match_and_eat(c, "[:lower:]")
c = match_and_eat(c, "[:print:]")
c = match_and_eat(c, "[:punct:]")
c = match_and_eat(c, "[:space:]")
c = match_and_eat(c, "[:upper:]")
return match_and_eat(c, "[:xdigit:]")
}
function eat_bracket_exp(e,
#local
f, o)
{
o = e
e = eat_char(e)
for (;;) {
if (matches(e, "]")) {
return eat_char(e)
}
if (matches(e, "[")) {
f = eat_class(e)
if (f == e)
return o
e = f
continue;
}
f = eat_bchar(e)
if (f == e)
return o
e = f
if (matches(e, "-")) {
e = eat_char(e)
f = eat_bchar(e)
if (f == e)
return o
e = f
}
}
}
function eat_rep_notation(n,
# local
o)
{
o = n
n = eat_char(n)
if (n !~ /^[0-9]/)
return o
while (n ~ /^[0-9]/)
n = eat_char(n)
if (matches(n, "}"))
return eat_char(n)
if (!matches(n, ","))
return o
n = eat_char(n)
if (matches(n, "}"))
return eat_char(n)
if (n !~ /^[0-9]/)
return o
while (n ~ /^[0-9]/)
n = eat_char(n)
return match_and_eat_else(n, "}", o)
}
function eat_factor(f)
{
if (matches(f, "("))
return match_and_eat_else(eat_regex(eat_char(f)), ")", f)
if (matches(f, "["))
return eat_bracket_exp(f)
return eat_rchar(f)
}
function eat_term(t,
#local
s)
{
s = eat_factor(t)
if (empty(s) || s == t)
return s
t = s
if (t ~ /^[?+*]/)
return eat_char(t)
if (matches(t, "{"))
return eat_rep_notation(t)
return t
}
function eat_regex(r,
#locals
s)
{
if (empty(r))
return r
s = eat_term(r)
if (empty(s) || s == r)
return s
r = s;
if (matches(r, "|"))
r = eat_char(r)
return eat_regex(r)
}
function regex_check(r)
{
if (matches(r, "^"))
r = eat_char(r)
if (empty(r))
return r
r = eat_regex(r)
if (r == "$")
r = ""
return r
}
function is_regex(r)
{
return empty(regex_check(r))
}
{
ok = is_regex($0)
printf("is_regex(\"%s\") = %d", $0, ir)
if (!ok)
printf(", junk = \"%s\"", regex_check($0))
printf("\n")
}
|