-
Notifications
You must be signed in to change notification settings - Fork 34
/
y.go
483 lines (425 loc) Β· 8.84 KB
/
y.go
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//line expr.y:17
package main
import __yyfmt__ "fmt"
//line expr.y:18
import (
"bytes"
"errors"
"fmt"
"log"
"unicode/utf8"
)
var result_value int
var result_error string
//line expr.y:33
type exprSymType struct {
yys int
num int
}
const NUM = 57346
var exprToknames = []string{
"'+'",
"'-'",
"'*'",
"'/'",
"'('",
"')'",
"NUM",
}
var exprStatenames = []string{}
const exprEofCode = 1
const exprErrCode = 2
const exprMaxDepth = 200
//line expr.y:92
// The parser expects the lexer to return 0 on EOF. Give it a name
// for clarity.
const eof = 0
// The parser uses the type <prefix>Lex as a lexer. It must provide
// the methods Lex(*<prefix>SymType) int and Error(string).
type exprLex struct {
line []byte
peek rune
}
// The parser calls this method to get each new token. This
// implementation returns operators and NUM.
func (x *exprLex) Lex(yylval *exprSymType) int {
for {
c := x.next()
switch c {
case eof:
return eof
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return x.num(c, yylval)
case '+', '-', '*', '/', '(', ')':
return int(c)
case ' ', '\t', '\n', '\r':
default:
log.Printf("unrecognized character %q", c)
}
}
}
// Lex a number.
func (x *exprLex) num(c rune, yylval *exprSymType) int {
add := func(b *bytes.Buffer, c rune) {
if _, err := b.WriteRune(c); err != nil {
log.Fatalf("WriteRune: %s", err)
}
}
var b bytes.Buffer
add(&b, c)
L:
for {
c = x.next()
switch c {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'x', 'a', 'b', 'c', 'd', 'e', 'f':
add(&b, c)
default:
break L
}
}
if c != eof {
x.peek = c
}
value := 0
if n, _ := fmt.Sscanf(b.String(), "%v", &value); n != 1 {
log.Printf("bad number %q", b.String())
return eof
}
yylval.num = value
return NUM
}
// Return the next rune for the lexer.
func (x *exprLex) next() rune {
if x.peek != eof {
r := x.peek
x.peek = eof
return r
}
if len(x.line) == 0 {
return eof
}
c, size := utf8.DecodeRune(x.line)
x.line = x.line[size:]
if c == utf8.RuneError && size == 1 {
log.Print("invalid utf8")
return x.next()
}
return c
}
// The parser calls this method on a parse error.
func (x *exprLex) Error(s string) {
result_error = s
}
func evaluateExpression(line string) (int, error) {
result_value = -1
result_error = ""
exprParse(&exprLex{line: []byte(line)})
if result_error == "" {
return result_value, nil
}
return result_value, errors.New(result_error)
}
//line yacctab:1
var exprExca = []int{
-1, 1,
1, -1,
-2, 0,
}
const exprNprod = 13
const exprPrivate = 57344
var exprTokenNames []string
var exprStates []string
const exprLast = 23
var exprAct = []int{
7, 4, 5, 2, 21, 9, 6, 8, 12, 13,
9, 1, 8, 16, 3, 19, 20, 17, 18, 14,
15, 10, 11,
}
var exprPact = []int{
-3, -1000, -1000, 17, -3, -3, 13, -1000, -1000, -3,
2, 2, -1000, -1000, 2, 2, -5, 13, 13, -1000,
-1000, -1000,
}
var exprPgo = []int{
0, 3, 14, 6, 0, 11,
}
var exprR1 = []int{
0, 5, 1, 1, 1, 2, 2, 2, 3, 3,
3, 4, 4,
}
var exprR2 = []int{
0, 1, 1, 2, 2, 1, 3, 3, 1, 3,
3, 1, 3,
}
var exprChk = []int{
-1000, -5, -1, -2, 4, 5, -3, -4, 10, 8,
4, 5, -1, -1, 6, 7, -1, -3, -3, -4,
-4, 9,
}
var exprDef = []int{
0, -2, 1, 2, 0, 0, 5, 8, 11, 0,
0, 0, 3, 4, 0, 0, 0, 6, 7, 9,
10, 12,
}
var exprTok1 = []int{
1, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
8, 9, 6, 4, 3, 5, 3, 7,
}
var exprTok2 = []int{
2, 3, 10,
}
var exprTok3 = []int{
0,
}
//line yaccpar:1
/* parser for yacc output */
var exprDebug = 0
type exprLexer interface {
Lex(lval *exprSymType) int
Error(s string)
}
const exprFlag = -1000
func exprTokname(c int) string {
// 4 is TOKSTART above
if c >= 4 && c-4 < len(exprToknames) {
if exprToknames[c-4] != "" {
return exprToknames[c-4]
}
}
return __yyfmt__.Sprintf("tok-%v", c)
}
func exprStatname(s int) string {
if s >= 0 && s < len(exprStatenames) {
if exprStatenames[s] != "" {
return exprStatenames[s]
}
}
return __yyfmt__.Sprintf("state-%v", s)
}
func exprlex1(lex exprLexer, lval *exprSymType) int {
c := 0
char := lex.Lex(lval)
if char <= 0 {
c = exprTok1[0]
goto out
}
if char < len(exprTok1) {
c = exprTok1[char]
goto out
}
if char >= exprPrivate {
if char < exprPrivate+len(exprTok2) {
c = exprTok2[char-exprPrivate]
goto out
}
}
for i := 0; i < len(exprTok3); i += 2 {
c = exprTok3[i+0]
if c == char {
c = exprTok3[i+1]
goto out
}
}
out:
if c == 0 {
c = exprTok2[1] /* unknown char */
}
if exprDebug >= 3 {
__yyfmt__.Printf("lex %s(%d)\n", exprTokname(c), uint(char))
}
return c
}
func exprParse(exprlex exprLexer) int {
var exprn int
var exprlval exprSymType
var exprVAL exprSymType
exprS := make([]exprSymType, exprMaxDepth)
Nerrs := 0 /* number of errors */
Errflag := 0 /* error recovery flag */
exprstate := 0
exprchar := -1
exprp := -1
goto exprstack
ret0:
return 0
ret1:
return 1
exprstack:
/* put a state and value onto the stack */
if exprDebug >= 4 {
__yyfmt__.Printf("char %v in %v\n", exprTokname(exprchar), exprStatname(exprstate))
}
exprp++
if exprp >= len(exprS) {
nyys := make([]exprSymType, len(exprS)*2)
copy(nyys, exprS)
exprS = nyys
}
exprS[exprp] = exprVAL
exprS[exprp].yys = exprstate
exprnewstate:
exprn = exprPact[exprstate]
if exprn <= exprFlag {
goto exprdefault /* simple state */
}
if exprchar < 0 {
exprchar = exprlex1(exprlex, &exprlval)
}
exprn += exprchar
if exprn < 0 || exprn >= exprLast {
goto exprdefault
}
exprn = exprAct[exprn]
if exprChk[exprn] == exprchar { /* valid shift */
exprchar = -1
exprVAL = exprlval
exprstate = exprn
if Errflag > 0 {
Errflag--
}
goto exprstack
}
exprdefault:
/* default state action */
exprn = exprDef[exprstate]
if exprn == -2 {
if exprchar < 0 {
exprchar = exprlex1(exprlex, &exprlval)
}
/* look through exception table */
xi := 0
for {
if exprExca[xi+0] == -1 && exprExca[xi+1] == exprstate {
break
}
xi += 2
}
for xi += 2; ; xi += 2 {
exprn = exprExca[xi+0]
if exprn < 0 || exprn == exprchar {
break
}
}
exprn = exprExca[xi+1]
if exprn < 0 {
goto ret0
}
}
if exprn == 0 {
/* error ... attempt to resume parsing */
switch Errflag {
case 0: /* brand new error */
exprlex.Error("syntax error")
Nerrs++
if exprDebug >= 1 {
__yyfmt__.Printf("%s", exprStatname(exprstate))
__yyfmt__.Printf(" saw %s\n", exprTokname(exprchar))
}
fallthrough
case 1, 2: /* incompletely recovered error ... try again */
Errflag = 3
/* find a state where "error" is a legal shift action */
for exprp >= 0 {
exprn = exprPact[exprS[exprp].yys] + exprErrCode
if exprn >= 0 && exprn < exprLast {
exprstate = exprAct[exprn] /* simulate a shift of "error" */
if exprChk[exprstate] == exprErrCode {
goto exprstack
}
}
/* the current p has no shift on "error", pop stack */
if exprDebug >= 2 {
__yyfmt__.Printf("error recovery pops state %d\n", exprS[exprp].yys)
}
exprp--
}
/* there is no state on the stack with an error shift ... abort */
goto ret1
case 3: /* no shift yet; clobber input char */
if exprDebug >= 2 {
__yyfmt__.Printf("error recovery discards %s\n", exprTokname(exprchar))
}
if exprchar == exprEofCode {
goto ret1
}
exprchar = -1
goto exprnewstate /* try again in the same state */
}
}
/* reduction by production exprn */
if exprDebug >= 2 {
__yyfmt__.Printf("reduce %v in:\n\t%v\n", exprn, exprStatname(exprstate))
}
exprnt := exprn
exprpt := exprp
_ = exprpt // guard against "declared and not used"
exprp -= exprR2[exprn]
exprVAL = exprS[exprp+1]
/* consult goto table to find next state */
exprn = exprR1[exprn]
exprg := exprPgo[exprn]
exprj := exprg + exprS[exprp].yys + 1
if exprj >= exprLast {
exprstate = exprAct[exprg]
} else {
exprstate = exprAct[exprj]
if exprChk[exprstate] != -exprn {
exprstate = exprAct[exprg]
}
}
// dummy call; replaced with literal code
switch exprnt {
case 1:
//line expr.y:47
{
result_value = exprS[exprpt-0].num
}
case 2:
exprVAL.num = exprS[exprpt-0].num
case 3:
//line expr.y:54
{
exprVAL.num = exprS[exprpt-0].num
}
case 4:
//line expr.y:58
{
exprVAL.num = -exprS[exprpt-0].num
}
case 5:
exprVAL.num = exprS[exprpt-0].num
case 6:
//line expr.y:65
{
exprVAL.num = exprS[exprpt-2].num + exprS[exprpt-0].num
}
case 7:
//line expr.y:69
{
exprVAL.num = exprS[exprpt-2].num - exprS[exprpt-0].num
}
case 8:
exprVAL.num = exprS[exprpt-0].num
case 9:
//line expr.y:76
{
exprVAL.num = exprS[exprpt-2].num * exprS[exprpt-0].num
}
case 10:
//line expr.y:80
{
exprVAL.num = exprS[exprpt-2].num / exprS[exprpt-0].num
}
case 11:
exprVAL.num = exprS[exprpt-0].num
case 12:
//line expr.y:87
{
exprVAL.num = exprS[exprpt-1].num
}
}
goto exprstack /* stack new state and value */
}