@@ -51,6 +51,7 @@ import (
51
51
"fmt"
52
52
"go/format"
53
53
"io/ioutil"
54
+ "math"
54
55
"os"
55
56
"strconv"
56
57
"strings"
@@ -2157,7 +2158,7 @@ func output() {
2157
2158
if ! lflag {
2158
2159
fmt .Fprintf (ftable , "\n //line yacctab:1" )
2159
2160
}
2160
- fmt . Fprintf ( ftable , " \n var %sExca = [... ]int{ \n " , prefix )
2161
+ var actions [ ]int
2161
2162
2162
2163
if len (errors ) > 0 {
2163
2164
stateTable = make ([]Row , nstate )
@@ -2230,10 +2231,11 @@ func output() {
2230
2231
}
2231
2232
}
2232
2233
}
2233
- wract ( i )
2234
+ actions = addActions ( actions , i )
2234
2235
}
2235
2236
2236
- fmt .Fprintf (ftable , "}\n " )
2237
+ arrayOutColumns ("Exca" , actions , 2 , false )
2238
+ fmt .Fprintf (ftable , "\n " )
2237
2239
ftable .WriteRune ('\n' )
2238
2240
fmt .Fprintf (ftable , "const %sPrivate = %v\n " , prefix , PRIVATE )
2239
2241
}
@@ -2278,7 +2280,7 @@ func precftn(r, t, s int) {
2278
2280
// output state i
2279
2281
// temp1 has the actions, lastred the default
2280
2282
//
2281
- func wract ( i int ) {
2283
+ func addActions ( act [] int , i int ) [] int {
2282
2284
var p , p1 int
2283
2285
2284
2286
// find the best choice for lastred
@@ -2351,18 +2353,19 @@ func wract(i int) {
2351
2353
continue
2352
2354
}
2353
2355
if flag == 0 {
2354
- fmt . Fprintf ( ftable , " \t -1, %v, \n " , i )
2356
+ act = append ( act , - 1 , i )
2355
2357
}
2356
2358
flag ++
2357
- fmt . Fprintf ( ftable , " \t %v, %v, \n " , p , p1 )
2359
+ act = append ( act , p , p1 )
2358
2360
zzexcp ++
2359
2361
}
2360
2362
}
2361
2363
if flag != 0 {
2362
2364
defact [i ] = - 2
2363
- fmt . Fprintf ( ftable , " \t -2, %v, \n " , lastred )
2365
+ act = append ( act , - 2 , lastred )
2364
2366
}
2365
2367
optst [i ] = os
2368
+ return act
2366
2369
}
2367
2370
2368
2371
//
@@ -2855,7 +2858,7 @@ func others() {
2855
2858
}
2856
2859
}
2857
2860
arout ("Chk" , temp1 , nstate )
2858
- arout ("Def" , defact , nstate )
2861
+ arrayOutColumns ("Def" , defact [: nstate ], 10 , false )
2859
2862
2860
2863
// put out token translation tables
2861
2864
// table 1 has 0-256
@@ -2903,8 +2906,7 @@ func others() {
2903
2906
2904
2907
// table 3 has everything else
2905
2908
ftable .WriteRune ('\n' )
2906
- fmt .Fprintf (ftable , "var %sTok3 = [...]int{\n \t " , prefix )
2907
- c = 0
2909
+ var v []int
2908
2910
for i = 1 ; i <= ntokens ; i ++ {
2909
2911
j = tokset [i ].value
2910
2912
if j >= 0 && j < 256 {
@@ -2914,19 +2916,11 @@ func others() {
2914
2916
continue
2915
2917
}
2916
2918
2917
- if c % 5 != 0 {
2918
- ftable .WriteRune (' ' )
2919
- }
2920
- fmt .Fprintf (ftable , "%d, %d," , j , i )
2921
- c ++
2922
- if c % 5 == 0 {
2923
- fmt .Fprint (ftable , "\n \t " )
2924
- }
2919
+ v = append (v , j , i )
2925
2920
}
2926
- if c % 5 != 0 {
2927
- ftable .WriteRune (' ' )
2928
- }
2929
- fmt .Fprintf (ftable , "%d,\n }\n " , 0 )
2921
+ v = append (v , 0 )
2922
+ arout ("Tok3" , v , len (v ))
2923
+ fmt .Fprintf (ftable , "\n " )
2930
2924
2931
2925
// Custom error messages.
2932
2926
fmt .Fprintf (ftable , "\n " )
@@ -3013,21 +3007,65 @@ Loop:
3013
3007
}
3014
3008
}
3015
3009
3016
- func arout (s string , v []int , n int ) {
3010
+ func minMax (v []int ) (min , max int ) {
3011
+ if len (v ) == 0 {
3012
+ return
3013
+ }
3014
+ min = v [0 ]
3015
+ max = v [0 ]
3016
+ for _ , i := range v {
3017
+ if i < min {
3018
+ min = i
3019
+ }
3020
+ if i > max {
3021
+ max = i
3022
+ }
3023
+ }
3024
+ return
3025
+ }
3026
+
3027
+ // return the smaller integral base type to store the values in v
3028
+ func minType (v []int , allowUnsigned bool ) (typ string ) {
3029
+ typ = "int"
3030
+ typeLen := 8
3031
+ min , max := minMax (v )
3032
+ checkType := func (name string , size , minType , maxType int ) {
3033
+ if min >= minType && max <= maxType && typeLen > size {
3034
+ typ = name
3035
+ typeLen = size
3036
+ }
3037
+ }
3038
+ checkType ("int32" , 4 , math .MinInt32 , math .MaxInt32 )
3039
+ checkType ("int16" , 2 , math .MinInt16 , math .MaxInt16 )
3040
+ checkType ("int8" , 1 , math .MinInt8 , math .MaxInt8 )
3041
+ if allowUnsigned {
3042
+ // Do not check for uint32, not worth and won't compile on 32 bit systems
3043
+ checkType ("uint16" , 2 , 0 , math .MaxUint16 )
3044
+ checkType ("uint8" , 1 , 0 , math .MaxUint8 )
3045
+ }
3046
+ return
3047
+ }
3048
+
3049
+ func arrayOutColumns (s string , v []int , columns int , allowUnsigned bool ) {
3017
3050
s = prefix + s
3018
3051
ftable .WriteRune ('\n' )
3019
- fmt .Fprintf (ftable , "var %v = [...]int{" , s )
3020
- for i := 0 ; i < n ; i ++ {
3021
- if i % 10 == 0 {
3052
+ minType := minType (v , allowUnsigned )
3053
+ fmt .Fprintf (ftable , "var %v = [...]%s{" , s , minType )
3054
+ for i , val := range v {
3055
+ if i % columns == 0 {
3022
3056
fmt .Fprintf (ftable , "\n \t " )
3023
3057
} else {
3024
3058
ftable .WriteRune (' ' )
3025
3059
}
3026
- fmt .Fprintf (ftable , "%d," , v [ i ] )
3060
+ fmt .Fprintf (ftable , "%d," , val )
3027
3061
}
3028
3062
fmt .Fprintf (ftable , "\n }\n " )
3029
3063
}
3030
3064
3065
+ func arout (s string , v []int , n int ) {
3066
+ arrayOutColumns (s , v [:n ], 10 , true )
3067
+ }
3068
+
3031
3069
//
3032
3070
// output the summary on y.output
3033
3071
//
@@ -3332,9 +3370,9 @@ func $$ErrorMessage(state, lookAhead int) string {
3332
3370
expected := make([]int, 0, 4)
3333
3371
3334
3372
// Look for shiftable tokens.
3335
- base := $$Pact[state]
3373
+ base := int( $$Pact[state])
3336
3374
for tok := TOKSTART; tok-1 < len($$Toknames); tok++ {
3337
- if n := base + tok; n >= 0 && n < $$Last && $$Chk[$$Act[n]] == tok {
3375
+ if n := base + tok; n >= 0 && n < $$Last && int( $$Chk[int( $$Act[n])]) == tok {
3338
3376
if len(expected) == cap(expected) {
3339
3377
return res
3340
3378
}
@@ -3344,13 +3382,13 @@ func $$ErrorMessage(state, lookAhead int) string {
3344
3382
3345
3383
if $$Def[state] == -2 {
3346
3384
i := 0
3347
- for $$Exca[i] != -1 || $$Exca[i+1] != state {
3385
+ for $$Exca[i] != -1 || int( $$Exca[i+1]) != state {
3348
3386
i += 2
3349
3387
}
3350
3388
3351
3389
// Look for tokens that we accept or reduce.
3352
3390
for i += 2; $$Exca[i] >= 0; i += 2 {
3353
- tok := $$Exca[i]
3391
+ tok := int( $$Exca[i])
3354
3392
if tok < TOKSTART || $$Exca[i+1] == 0 {
3355
3393
continue
3356
3394
}
@@ -3381,30 +3419,30 @@ func $$lex1(lex $$Lexer, lval *$$SymType) (char, token int) {
3381
3419
token = 0
3382
3420
char = lex.Lex(lval)
3383
3421
if char <= 0 {
3384
- token = $$Tok1[0]
3422
+ token = int( $$Tok1[0])
3385
3423
goto out
3386
3424
}
3387
3425
if char < len($$Tok1) {
3388
- token = $$Tok1[char]
3426
+ token = int( $$Tok1[char])
3389
3427
goto out
3390
3428
}
3391
3429
if char >= $$Private {
3392
3430
if char < $$Private+len($$Tok2) {
3393
- token = $$Tok2[char-$$Private]
3431
+ token = int( $$Tok2[char-$$Private])
3394
3432
goto out
3395
3433
}
3396
3434
}
3397
3435
for i := 0; i < len($$Tok3); i += 2 {
3398
- token = $$Tok3[i+0]
3436
+ token = int( $$Tok3[i+0])
3399
3437
if token == char {
3400
- token = $$Tok3[i+1]
3438
+ token = int( $$Tok3[i+1])
3401
3439
goto out
3402
3440
}
3403
3441
}
3404
3442
3405
3443
out:
3406
3444
if token == 0 {
3407
- token = $$Tok2[1] /* unknown char */
3445
+ token = int( $$Tok2[1]) /* unknown char */
3408
3446
}
3409
3447
if $$Debug >= 3 {
3410
3448
__yyfmt__.Printf("lex %s(%d)\n", $$Tokname(token), uint(char))
@@ -3459,7 +3497,7 @@ $$stack:
3459
3497
$$S[$$p].yys = $$state
3460
3498
3461
3499
$$newstate:
3462
- $$n = $$Pact[$$state]
3500
+ $$n = int( $$Pact[$$state])
3463
3501
if $$n <= $$Flag {
3464
3502
goto $$default /* simple state */
3465
3503
}
@@ -3470,8 +3508,8 @@ $$newstate:
3470
3508
if $$n < 0 || $$n >= $$Last {
3471
3509
goto $$default
3472
3510
}
3473
- $$n = $$Act[$$n]
3474
- if $$Chk[$$n] == $$token { /* valid shift */
3511
+ $$n = int( $$Act[$$n])
3512
+ if int( $$Chk[$$n]) == $$token { /* valid shift */
3475
3513
$$rcvr.char = -1
3476
3514
$$token = -1
3477
3515
$$VAL = $$rcvr.lval
@@ -3484,7 +3522,7 @@ $$newstate:
3484
3522
3485
3523
$$default:
3486
3524
/* default state action */
3487
- $$n = $$Def[$$state]
3525
+ $$n = int( $$Def[$$state])
3488
3526
if $$n == -2 {
3489
3527
if $$rcvr.char < 0 {
3490
3528
$$rcvr.char, $$token = $$lex1($$lex, &$$rcvr.lval)
@@ -3493,18 +3531,18 @@ $$default:
3493
3531
/* look through exception table */
3494
3532
xi := 0
3495
3533
for {
3496
- if $$Exca[xi+0] == -1 && $$Exca[xi+1] == $$state {
3534
+ if $$Exca[xi+0] == -1 && int( $$Exca[xi+1]) == $$state {
3497
3535
break
3498
3536
}
3499
3537
xi += 2
3500
3538
}
3501
3539
for xi += 2; ; xi += 2 {
3502
- $$n = $$Exca[xi+0]
3540
+ $$n = int( $$Exca[xi+0])
3503
3541
if $$n < 0 || $$n == $$token {
3504
3542
break
3505
3543
}
3506
3544
}
3507
- $$n = $$Exca[xi+1]
3545
+ $$n = int( $$Exca[xi+1])
3508
3546
if $$n < 0 {
3509
3547
goto ret0
3510
3548
}
@@ -3526,10 +3564,10 @@ $$default:
3526
3564
3527
3565
/* find a state where "error" is a legal shift action */
3528
3566
for $$p >= 0 {
3529
- $$n = $$Pact[$$S[$$p].yys] + $$ErrCode
3567
+ $$n = int( $$Pact[$$S[$$p].yys]) + $$ErrCode
3530
3568
if $$n >= 0 && $$n < $$Last {
3531
- $$state = $$Act[$$n] /* simulate a shift of "error" */
3532
- if $$Chk[$$state] == $$ErrCode {
3569
+ $$state = int( $$Act[$$n]) /* simulate a shift of "error" */
3570
+ if int( $$Chk[$$state]) == $$ErrCode {
3533
3571
goto $$stack
3534
3572
}
3535
3573
}
@@ -3565,7 +3603,7 @@ $$default:
3565
3603
$$pt := $$p
3566
3604
_ = $$pt // guard against "declared and not used"
3567
3605
3568
- $$p -= $$R2[$$n]
3606
+ $$p -= int( $$R2[$$n])
3569
3607
// $$p is now the index of $0. Perform the default action. Iff the
3570
3608
// reduced production is ε, $1 is possibly out of range.
3571
3609
if $$p+1 >= len($$S) {
@@ -3576,16 +3614,16 @@ $$default:
3576
3614
$$VAL = $$S[$$p+1]
3577
3615
3578
3616
/* consult goto table to find next state */
3579
- $$n = $$R1[$$n]
3580
- $$g := $$Pgo[$$n]
3617
+ $$n = int( $$R1[$$n])
3618
+ $$g := int( $$Pgo[$$n])
3581
3619
$$j := $$g + $$S[$$p].yys + 1
3582
3620
3583
3621
if $$j >= $$Last {
3584
- $$state = $$Act[$$g]
3622
+ $$state = int( $$Act[$$g])
3585
3623
} else {
3586
- $$state = $$Act[$$j]
3587
- if $$Chk[$$state] != -$$n {
3588
- $$state = $$Act[$$g]
3624
+ $$state = int( $$Act[$$j])
3625
+ if int( $$Chk[$$state]) != -$$n {
3626
+ $$state = int( $$Act[$$g])
3589
3627
}
3590
3628
}
3591
3629
// dummy call; replaced with literal code
0 commit comments