-
Notifications
You must be signed in to change notification settings - Fork 4
/
calc.y.go
540 lines (473 loc) · 10.4 KB
/
calc.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// Code generated by goyacc -o calc.y.go -p calc calc.y. DO NOT EDIT.
//line calc.y:8
package main
import __yyfmt__ "fmt"
//line calc.y:8
import (
"fmt"
)
var idValueMap = map[string]int{}
//line calc.y:17
type calcSymType struct {
yys int
value int
id string
}
const NUMBER = 57346
const ID = 57347
const ADD = 57348
const SUB = 57349
const MUL = 57350
const DIV = 57351
const ABS = 57352
const LPAREN = 57353
const RPAREN = 57354
const ASSIGN = 57355
const EOL = 57356
var calcToknames = [...]string{
"$end",
"error",
"$unk",
"NUMBER",
"ID",
"ADD",
"SUB",
"MUL",
"DIV",
"ABS",
"LPAREN",
"RPAREN",
"ASSIGN",
"EOL",
}
var calcStatenames = [...]string{}
const calcEofCode = 1
const calcErrCode = 2
const calcInitialStackSize = 16
//line calc.y:63
//line yacctab:1
var calcExca = [...]int{
-1, 1,
1, -1,
-2, 0,
}
const calcPrivate = 57344
const calcLast = 39
var calcAct = [...]int{
5, 10, 11, 10, 11, 12, 10, 11, 15, 24,
1, 9, 23, 0, 21, 22, 6, 16, 6, 3,
2, 0, 7, 8, 7, 8, 4, 13, 14, 17,
0, 0, 0, 20, 0, 0, 0, 18, 19,
}
var calcPact = [...]int{
-1000, 14, -3, -8, 19, -1000, -1000, 12, 12, -1000,
12, 12, 12, 12, 12, -1000, -1000, 0, 19, 19,
-5, -1000, -1000, -1000, -1000,
}
var calcPgo = [...]int{
0, 20, 26, 0, 10,
}
var calcR1 = [...]int{
0, 4, 4, 4, 1, 1, 1, 2, 2, 2,
3, 3, 3, 3,
}
var calcR2 = [...]int{
0, 0, 3, 5, 1, 3, 3, 1, 3, 3,
1, 1, 2, 3,
}
var calcChk = [...]int{
-1000, -4, -1, 5, -2, -3, 4, 10, 11, 14,
6, 7, 13, 8, 9, -3, 5, -1, -2, -2,
-1, -3, -3, 12, 14,
}
var calcDef = [...]int{
1, -2, 0, 11, 4, 7, 10, 0, 0, 2,
0, 0, 0, 0, 0, 12, 11, 0, 5, 6,
0, 8, 9, 13, 3,
}
var calcTok1 = [...]int{
1,
}
var calcTok2 = [...]int{
2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14,
}
var calcTok3 = [...]int{
0,
}
var calcErrorMessages = [...]struct {
state int
token int
msg string
}{}
//line yaccpar:1
/* parser for yacc output */
var (
calcDebug = 0
calcErrorVerbose = false
)
type calcLexer interface {
Lex(lval *calcSymType) int
Error(s string)
}
type calcParser interface {
Parse(calcLexer) int
Lookahead() int
}
type calcParserImpl struct {
lval calcSymType
stack [calcInitialStackSize]calcSymType
char int
}
func (p *calcParserImpl) Lookahead() int {
return p.char
}
func calcNewParser() calcParser {
return &calcParserImpl{}
}
const calcFlag = -1000
func calcTokname(c int) string {
if c >= 1 && c-1 < len(calcToknames) {
if calcToknames[c-1] != "" {
return calcToknames[c-1]
}
}
return __yyfmt__.Sprintf("tok-%v", c)
}
func calcStatname(s int) string {
if s >= 0 && s < len(calcStatenames) {
if calcStatenames[s] != "" {
return calcStatenames[s]
}
}
return __yyfmt__.Sprintf("state-%v", s)
}
func calcErrorMessage(state, lookAhead int) string {
const TOKSTART = 4
if !calcErrorVerbose {
return "syntax error"
}
for _, e := range calcErrorMessages {
if e.state == state && e.token == lookAhead {
return "syntax error: " + e.msg
}
}
res := "syntax error: unexpected " + calcTokname(lookAhead)
// To match Bison, suggest at most four expected tokens.
expected := make([]int, 0, 4)
// Look for shiftable tokens.
base := calcPact[state]
for tok := TOKSTART; tok-1 < len(calcToknames); tok++ {
if n := base + tok; n >= 0 && n < calcLast && calcChk[calcAct[n]] == tok {
if len(expected) == cap(expected) {
return res
}
expected = append(expected, tok)
}
}
if calcDef[state] == -2 {
i := 0
for calcExca[i] != -1 || calcExca[i+1] != state {
i += 2
}
// Look for tokens that we accept or reduce.
for i += 2; calcExca[i] >= 0; i += 2 {
tok := calcExca[i]
if tok < TOKSTART || calcExca[i+1] == 0 {
continue
}
if len(expected) == cap(expected) {
return res
}
expected = append(expected, tok)
}
// If the default action is to accept or reduce, give up.
if calcExca[i+1] != 0 {
return res
}
}
for i, tok := range expected {
if i == 0 {
res += ", expecting "
} else {
res += " or "
}
res += calcTokname(tok)
}
return res
}
func calclex1(lex calcLexer, lval *calcSymType) (char, token int) {
token = 0
char = lex.Lex(lval)
if char <= 0 {
token = calcTok1[0]
goto out
}
if char < len(calcTok1) {
token = calcTok1[char]
goto out
}
if char >= calcPrivate {
if char < calcPrivate+len(calcTok2) {
token = calcTok2[char-calcPrivate]
goto out
}
}
for i := 0; i < len(calcTok3); i += 2 {
token = calcTok3[i+0]
if token == char {
token = calcTok3[i+1]
goto out
}
}
out:
if token == 0 {
token = calcTok2[1] /* unknown char */
}
if calcDebug >= 3 {
__yyfmt__.Printf("lex %s(%d)\n", calcTokname(token), uint(char))
}
return char, token
}
func calcParse(calclex calcLexer) int {
return calcNewParser().Parse(calclex)
}
func (calcrcvr *calcParserImpl) Parse(calclex calcLexer) int {
var calcn int
var calcVAL calcSymType
var calcDollar []calcSymType
_ = calcDollar // silence set and not used
calcS := calcrcvr.stack[:]
Nerrs := 0 /* number of errors */
Errflag := 0 /* error recovery flag */
calcstate := 0
calcrcvr.char = -1
calctoken := -1 // calcrcvr.char translated into internal numbering
defer func() {
// Make sure we report no lookahead when not parsing.
calcstate = -1
calcrcvr.char = -1
calctoken = -1
}()
calcp := -1
goto calcstack
ret0:
return 0
ret1:
return 1
calcstack:
/* put a state and value onto the stack */
if calcDebug >= 4 {
__yyfmt__.Printf("char %v in %v\n", calcTokname(calctoken), calcStatname(calcstate))
}
calcp++
if calcp >= len(calcS) {
nyys := make([]calcSymType, len(calcS)*2)
copy(nyys, calcS)
calcS = nyys
}
calcS[calcp] = calcVAL
calcS[calcp].yys = calcstate
calcnewstate:
calcn = calcPact[calcstate]
if calcn <= calcFlag {
goto calcdefault /* simple state */
}
if calcrcvr.char < 0 {
calcrcvr.char, calctoken = calclex1(calclex, &calcrcvr.lval)
}
calcn += calctoken
if calcn < 0 || calcn >= calcLast {
goto calcdefault
}
calcn = calcAct[calcn]
if calcChk[calcn] == calctoken { /* valid shift */
calcrcvr.char = -1
calctoken = -1
calcVAL = calcrcvr.lval
calcstate = calcn
if Errflag > 0 {
Errflag--
}
goto calcstack
}
calcdefault:
/* default state action */
calcn = calcDef[calcstate]
if calcn == -2 {
if calcrcvr.char < 0 {
calcrcvr.char, calctoken = calclex1(calclex, &calcrcvr.lval)
}
/* look through exception table */
xi := 0
for {
if calcExca[xi+0] == -1 && calcExca[xi+1] == calcstate {
break
}
xi += 2
}
for xi += 2; ; xi += 2 {
calcn = calcExca[xi+0]
if calcn < 0 || calcn == calctoken {
break
}
}
calcn = calcExca[xi+1]
if calcn < 0 {
goto ret0
}
}
if calcn == 0 {
/* error ... attempt to resume parsing */
switch Errflag {
case 0: /* brand new error */
calclex.Error(calcErrorMessage(calcstate, calctoken))
Nerrs++
if calcDebug >= 1 {
__yyfmt__.Printf("%s", calcStatname(calcstate))
__yyfmt__.Printf(" saw %s\n", calcTokname(calctoken))
}
fallthrough
case 1, 2: /* incompletely recovered error ... try again */
Errflag = 3
/* find a state where "error" is a legal shift action */
for calcp >= 0 {
calcn = calcPact[calcS[calcp].yys] + calcErrCode
if calcn >= 0 && calcn < calcLast {
calcstate = calcAct[calcn] /* simulate a shift of "error" */
if calcChk[calcstate] == calcErrCode {
goto calcstack
}
}
/* the current p has no shift on "error", pop stack */
if calcDebug >= 2 {
__yyfmt__.Printf("error recovery pops state %d\n", calcS[calcp].yys)
}
calcp--
}
/* there is no state on the stack with an error shift ... abort */
goto ret1
case 3: /* no shift yet; clobber input char */
if calcDebug >= 2 {
__yyfmt__.Printf("error recovery discards %s\n", calcTokname(calctoken))
}
if calctoken == calcEofCode {
goto ret1
}
calcrcvr.char = -1
calctoken = -1
goto calcnewstate /* try again in the same state */
}
}
/* reduction by production calcn */
if calcDebug >= 2 {
__yyfmt__.Printf("reduce %v in:\n\t%v\n", calcn, calcStatname(calcstate))
}
calcnt := calcn
calcpt := calcp
_ = calcpt // guard against "declared and not used"
calcp -= calcR2[calcn]
// calcp is now the index of $0. Perform the default action. Iff the
// reduced production is ε, $1 is possibly out of range.
if calcp+1 >= len(calcS) {
nyys := make([]calcSymType, len(calcS)*2)
copy(nyys, calcS)
calcS = nyys
}
calcVAL = calcS[calcp+1]
/* consult goto table to find next state */
calcn = calcR1[calcn]
calcg := calcPgo[calcn]
calcj := calcg + calcS[calcp].yys + 1
if calcj >= calcLast {
calcstate = calcAct[calcg]
} else {
calcstate = calcAct[calcj]
if calcChk[calcstate] != -calcn {
calcstate = calcAct[calcg]
}
}
// dummy call; replaced with literal code
switch calcnt {
case 2:
calcDollar = calcS[calcpt-3 : calcpt+1]
//line calc.y:33
{
idValueMap["_"] = calcDollar[2].value
fmt.Printf("= %v\n", calcDollar[2].value)
}
case 3:
calcDollar = calcS[calcpt-5 : calcpt+1]
//line calc.y:37
{
idValueMap["_"] = calcDollar[4].value
idValueMap[calcDollar[2].id] = calcDollar[4].value
fmt.Printf("= %v\n", calcDollar[4].value)
}
case 4:
calcDollar = calcS[calcpt-1 : calcpt+1]
//line calc.y:45
{
calcVAL.value = calcDollar[1].value
}
case 5:
calcDollar = calcS[calcpt-3 : calcpt+1]
//line calc.y:46
{
calcVAL.value = calcDollar[1].value + calcDollar[3].value
}
case 6:
calcDollar = calcS[calcpt-3 : calcpt+1]
//line calc.y:47
{
calcVAL.value = calcDollar[1].value - calcDollar[3].value
}
case 7:
calcDollar = calcS[calcpt-1 : calcpt+1]
//line calc.y:51
{
calcVAL.value = calcDollar[1].value
}
case 8:
calcDollar = calcS[calcpt-3 : calcpt+1]
//line calc.y:52
{
calcVAL.value = calcDollar[1].value * calcDollar[3].value
}
case 9:
calcDollar = calcS[calcpt-3 : calcpt+1]
//line calc.y:53
{
calcVAL.value = calcDollar[1].value / calcDollar[3].value
}
case 10:
calcDollar = calcS[calcpt-1 : calcpt+1]
//line calc.y:57
{
calcVAL.value = calcDollar[1].value
}
case 11:
calcDollar = calcS[calcpt-1 : calcpt+1]
//line calc.y:58
{
calcVAL.value = idValueMap[calcDollar[1].id]
}
case 12:
calcDollar = calcS[calcpt-2 : calcpt+1]
//line calc.y:59
{
if calcDollar[2].value >= 0 {
calcVAL.value = calcDollar[2].value
} else {
calcVAL.value = -calcDollar[2].value
}
}
case 13:
calcDollar = calcS[calcpt-3 : calcpt+1]
//line calc.y:60
{
calcVAL.value = calcDollar[2].value
}
}
goto calcstack /* stack new state and value */
}