1 // Code generated by goyacc -o expr.go -p expr expr.y. DO NOT EDIT.
23 type exprSymType
struct {
30 var exprToknames
= [...]string{
43 var exprStatenames
= [...]string{}
47 const exprInitialStackSize
= 16
51 // The parser expects the lexer to return 0 on EOF. Give it a name
55 // The parser uses the type <prefix>Lex as a lexer. It must provide
56 // the methods Lex(*<prefix>SymType) int and Error(string).
62 // The parser calls this method to get each new token. This
63 // implementation returns operators and NUM.
64 func (x
*exprLex
) Lex(yylval
*exprSymType
) int {
70 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
71 return x
.num(c
, yylval
)
72 case '+', '-', '*', '/', '(', ')':
75 // Recognize Unicode multiplication and division
76 // symbols, returning what the parser expects.
82 case ' ', '\t', '\n', '\r':
84 log
.Printf("unrecognized character %q", c
)
90 func (x
*exprLex
) num(c rune
, yylval
*exprSymType
) int {
91 add
:= func(b
*bytes
.Buffer
, c rune
) {
92 if _
, err
:= b
.WriteRune(c
); err
!= nil {
93 log
.Fatalf("WriteRune: %s", err
)
102 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'e', 'E':
111 yylval
.num
= &big
.Rat
{}
112 _
, ok
:= yylval
.num
.SetString(b
.String())
114 log
.Printf("bad number %q", b
.String())
120 // Return the next rune for the lexer.
121 func (x
*exprLex
) next() rune
{
127 if len(x
.line
) == 0 {
130 c
, size
:= utf8
.DecodeRune(x
.line
)
131 x
.line
= x
.line
[size
:]
132 if c
== utf8
.RuneError
&& size
== 1 {
133 log
.Print("invalid utf8")
139 // The parser calls this method on a parse error.
140 func (x
*exprLex
) Error(s
string) {
141 log
.Printf("parse error: %s", s
)
145 in
:= bufio
.NewReader(os
.Stdin
)
147 if _
, err
:= os
.Stdout
.WriteString("> "); err
!= nil {
148 log
.Fatalf("WriteString: %s", err
)
150 line
, err
:= in
.ReadBytes('\n')
155 log
.Fatalf("ReadBytes: %s", err
)
158 exprParse(&exprLex
{line
: line
})
163 var exprExca
= [...]int{
169 const exprPrivate
= 57344
173 var exprAct
= [...]int{
174 7, 4, 5, 2, 21, 9, 6, 8, 12, 13,
175 9, 1, 8, 16, 3, 19, 20, 17, 18, 14,
179 var exprPact
= [...]int{
180 -3, -1000, -1000, 17, -3, -3, 13, -1000, -1000, -3,
181 2, 2, -1000, -1000, 2, 2, -5, 13, 13, -1000,
185 var exprPgo
= [...]int{
189 var exprR1
= [...]int{
190 0, 5, 1, 1, 1, 2, 2, 2, 3, 3,
194 var exprR2
= [...]int{
195 0, 1, 1, 2, 2, 1, 3, 3, 1, 3,
199 var exprChk
= [...]int{
200 -1000, -5, -1, -2, 4, 5, -3, -4, 10, 8,
201 4, 5, -1, -1, 6, 7, -1, -3, -3, -4,
205 var exprDef
= [...]int{
206 0, -2, 1, 2, 0, 0, 5, 8, 11, 0,
207 0, 0, 3, 4, 0, 0, 0, 6, 7, 9,
211 var exprTok1
= [...]int{
212 1, 3, 3, 3, 3, 3, 3, 3, 3, 3,
213 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
214 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
215 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
216 8, 9, 6, 4, 3, 5, 3, 7,
219 var exprTok2
= [...]int{
223 var exprTok3
= [...]int{
227 var exprErrorMessages
= [...]struct {
235 /* parser for yacc output */
239 exprErrorVerbose
= false
242 type exprLexer
interface {
243 Lex(lval
*exprSymType
) int
247 type exprParser
interface {
252 type exprParserImpl
struct {
254 stack
[exprInitialStackSize
]exprSymType
258 func (p
*exprParserImpl
) Lookahead() int {
262 func exprNewParser() exprParser
{
263 return &exprParserImpl
{}
266 const exprFlag
= -1000
268 func exprTokname(c
int) string {
269 if c
>= 1 && c
-1 < len(exprToknames
) {
270 if exprToknames
[c
-1] != "" {
271 return exprToknames
[c
-1]
274 return __yyfmt__
.Sprintf("tok-%v", c
)
277 func exprStatname(s
int) string {
278 if s
>= 0 && s
< len(exprStatenames
) {
279 if exprStatenames
[s
] != "" {
280 return exprStatenames
[s
]
283 return __yyfmt__
.Sprintf("state-%v", s
)
286 func exprErrorMessage(state
, lookAhead
int) string {
289 if !exprErrorVerbose
{
290 return "syntax error"
293 for _
, e
:= range exprErrorMessages
{
294 if e
.state
== state
&& e
.token
== lookAhead
{
295 return "syntax error: " + e
.msg
299 res
:= "syntax error: unexpected " + exprTokname(lookAhead
)
301 // To match Bison, suggest at most four expected tokens.
302 expected
:= make([]int, 0, 4)
304 // Look for shiftable tokens.
305 base
:= exprPact
[state
]
306 for tok
:= TOKSTART
; tok
-1 < len(exprToknames
); tok
++ {
307 if n
:= base
+ tok
; n
>= 0 && n
< exprLast
&& exprChk
[exprAct
[n
]] == tok
{
308 if len(expected
) == cap(expected
) {
311 expected
= append(expected
, tok
)
315 if exprDef
[state
] == -2 {
317 for exprExca
[i
] != -1 || exprExca
[i
+1] != state
{
321 // Look for tokens that we accept or reduce.
322 for i
+= 2; exprExca
[i
] >= 0; i
+= 2 {
324 if tok
< TOKSTART || exprExca
[i
+1] == 0 {
327 if len(expected
) == cap(expected
) {
330 expected
= append(expected
, tok
)
333 // If the default action is to accept or reduce, give up.
334 if exprExca
[i
+1] != 0 {
339 for i
, tok
:= range expected
{
341 res
+= ", expecting "
345 res
+= exprTokname(tok
)
350 func exprlex1(lex exprLexer
, lval
*exprSymType
) (char
, token
int) {
357 if char
< len(exprTok1
) {
358 token
= exprTok1
[char
]
361 if char
>= exprPrivate
{
362 if char
< exprPrivate
+len(exprTok2
) {
363 token
= exprTok2
[char
-exprPrivate
]
367 for i
:= 0; i
< len(exprTok3
); i
+= 2 {
368 token
= exprTok3
[i
+0]
370 token
= exprTok3
[i
+1]
377 token
= exprTok2
[1] /* unknown char */
380 __yyfmt__
.Printf("lex %s(%d)\n", exprTokname(token
), uint(char
))
385 func exprParse(exprlex exprLexer
) int {
386 return exprNewParser().Parse(exprlex
)
389 func (exprrcvr
*exprParserImpl
) Parse(exprlex exprLexer
) int {
391 var exprVAL exprSymType
392 var exprDollar
[]exprSymType
393 _
= exprDollar
// silence set and not used
394 exprS
:= exprrcvr
.stack
[:]
396 Nerrs
:= 0 /* number of errors */
397 Errflag
:= 0 /* error recovery flag */
400 exprtoken
:= -1 // exprrcvr.char translated into internal numbering
402 // Make sure we report no lookahead when not parsing.
417 /* put a state and value onto the stack */
419 __yyfmt__
.Printf("char %v in %v\n", exprTokname(exprtoken
), exprStatname(exprstate
))
423 if exprp
>= len(exprS
) {
424 nyys
:= make([]exprSymType
, len(exprS
)*2)
428 exprS
[exprp
] = exprVAL
429 exprS
[exprp
].yys
= exprstate
432 exprn
= exprPact
[exprstate
]
433 if exprn
<= exprFlag
{
434 goto exprdefault
/* simple state */
436 if exprrcvr
.char
< 0 {
437 exprrcvr
.char
, exprtoken
= exprlex1(exprlex
, &exprrcvr
.lval
)
440 if exprn
< 0 || exprn
>= exprLast
{
443 exprn
= exprAct
[exprn
]
444 if exprChk
[exprn
] == exprtoken
{ /* valid shift */
447 exprVAL
= exprrcvr
.lval
456 /* default state action */
457 exprn
= exprDef
[exprstate
]
459 if exprrcvr
.char
< 0 {
460 exprrcvr
.char
, exprtoken
= exprlex1(exprlex
, &exprrcvr
.lval
)
463 /* look through exception table */
466 if exprExca
[xi
+0] == -1 && exprExca
[xi
+1] == exprstate
{
471 for xi
+= 2; ; xi
+= 2 {
472 exprn
= exprExca
[xi
+0]
473 if exprn
< 0 || exprn
== exprtoken
{
477 exprn
= exprExca
[xi
+1]
483 /* error ... attempt to resume parsing */
485 case 0: /* brand new error */
486 exprlex
.Error(exprErrorMessage(exprstate
, exprtoken
))
489 __yyfmt__
.Printf("%s", exprStatname(exprstate
))
490 __yyfmt__
.Printf(" saw %s\n", exprTokname(exprtoken
))
494 case 1, 2: /* incompletely recovered error ... try again */
497 /* find a state where "error" is a legal shift action */
499 exprn
= exprPact
[exprS
[exprp
].yys
] + exprErrCode
500 if exprn
>= 0 && exprn
< exprLast
{
501 exprstate
= exprAct
[exprn
] /* simulate a shift of "error" */
502 if exprChk
[exprstate
] == exprErrCode
{
507 /* the current p has no shift on "error", pop stack */
509 __yyfmt__
.Printf("error recovery pops state %d\n", exprS
[exprp
].yys
)
513 /* there is no state on the stack with an error shift ... abort */
516 case 3: /* no shift yet; clobber input char */
518 __yyfmt__
.Printf("error recovery discards %s\n", exprTokname(exprtoken
))
520 if exprtoken
== exprEofCode
{
525 goto exprnewstate
/* try again in the same state */
529 /* reduction by production exprn */
531 __yyfmt__
.Printf("reduce %v in:\n\t%v\n", exprn
, exprStatname(exprstate
))
536 _
= exprpt
// guard against "declared and not used"
538 exprp
-= exprR2
[exprn
]
539 // exprp is now the index of $0. Perform the default action. Iff the
540 // reduced production is ε, $1 is possibly out of range.
541 if exprp
+1 >= len(exprS
) {
542 nyys
:= make([]exprSymType
, len(exprS
)*2)
546 exprVAL
= exprS
[exprp
+1]
548 /* consult goto table to find next state */
549 exprn
= exprR1
[exprn
]
550 exprg
:= exprPgo
[exprn
]
551 exprj
:= exprg
+ exprS
[exprp
].yys
+ 1
553 if exprj
>= exprLast
{
554 exprstate
= exprAct
[exprg
]
556 exprstate
= exprAct
[exprj
]
557 if exprChk
[exprstate
] != -exprn
{
558 exprstate
= exprAct
[exprg
]
561 // dummy call; replaced with literal code
565 exprDollar
= exprS
[exprpt
-1 : exprpt
+1]
568 if exprDollar
[1].num
.IsInt() {
569 fmt
.Println(exprDollar
[1].num
.Num().String())
571 fmt
.Println(exprDollar
[1].num
.String())
575 exprDollar
= exprS
[exprpt
-2 : exprpt
+1]
578 exprVAL
.num
= exprDollar
[2].num
581 exprDollar
= exprS
[exprpt
-2 : exprpt
+1]
584 exprVAL
.num
= exprDollar
[2].num
.Neg(exprDollar
[2].num
)
587 exprDollar
= exprS
[exprpt
-3 : exprpt
+1]
590 exprVAL
.num
= exprDollar
[1].num
.Add(exprDollar
[1].num
, exprDollar
[3].num
)
593 exprDollar
= exprS
[exprpt
-3 : exprpt
+1]
596 exprVAL
.num
= exprDollar
[1].num
.Sub(exprDollar
[1].num
, exprDollar
[3].num
)
599 exprDollar
= exprS
[exprpt
-3 : exprpt
+1]
602 exprVAL
.num
= exprDollar
[1].num
.Mul(exprDollar
[1].num
, exprDollar
[3].num
)
605 exprDollar
= exprS
[exprpt
-3 : exprpt
+1]
608 exprVAL
.num
= exprDollar
[1].num
.Quo(exprDollar
[1].num
, exprDollar
[3].num
)
611 exprDollar
= exprS
[exprpt
-3 : exprpt
+1]
614 exprVAL
.num
= exprDollar
[2].num
617 goto exprstack
/* stack new state and value */