blabla
[tastes.git] / goyacc / expr / expr.go
blobfbd831a06f32850d4dddb45560f7f6b94ecce976
1 // Code generated by goyacc -o expr.go -p expr expr.y. DO NOT EDIT.
3 //line expr.y:13
5 package main
7 import __yyfmt__ "fmt"
9 //line expr.y:14
11 import (
12 "bufio"
13 "bytes"
14 "fmt"
15 "io"
16 "log"
17 "math/big"
18 "os"
19 "unicode/utf8"
22 //line expr.y:29
23 type exprSymType struct {
24 yys int
25 num *big.Rat
28 const NUM = 57346
30 var exprToknames = [...]string{
31 "$end",
32 "error",
33 "$unk",
34 "'+'",
35 "'-'",
36 "'*'",
37 "'/'",
38 "'('",
39 "')'",
40 "NUM",
43 var exprStatenames = [...]string{}
45 const exprEofCode = 1
46 const exprErrCode = 2
47 const exprInitialStackSize = 16
49 //line expr.y:92
51 // The parser expects the lexer to return 0 on EOF. Give it a name
52 // for clarity.
53 const eof = 0
55 // The parser uses the type <prefix>Lex as a lexer. It must provide
56 // the methods Lex(*<prefix>SymType) int and Error(string).
57 type exprLex struct {
58 line []byte
59 peek rune
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 {
65 for {
66 c := x.next()
67 switch c {
68 case eof:
69 return eof
70 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
71 return x.num(c, yylval)
72 case '+', '-', '*', '/', '(', ')':
73 return int(c)
75 // Recognize Unicode multiplication and division
76 // symbols, returning what the parser expects.
77 case '×':
78 return '*'
79 case '÷':
80 return '/'
82 case ' ', '\t', '\n', '\r':
83 default:
84 log.Printf("unrecognized character %q", c)
89 // Lex a number.
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)
96 var b bytes.Buffer
97 add(&b, c)
99 for {
100 c = x.next()
101 switch c {
102 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'e', 'E':
103 add(&b, c)
104 default:
105 break L
108 if c != eof {
109 x.peek = c
111 yylval.num = &big.Rat{}
112 _, ok := yylval.num.SetString(b.String())
113 if !ok {
114 log.Printf("bad number %q", b.String())
115 return eof
117 return NUM
120 // Return the next rune for the lexer.
121 func (x *exprLex) next() rune {
122 if x.peek != eof {
123 r := x.peek
124 x.peek = eof
125 return r
127 if len(x.line) == 0 {
128 return eof
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")
134 return x.next()
136 return c
139 // The parser calls this method on a parse error.
140 func (x *exprLex) Error(s string) {
141 log.Printf("parse error: %s", s)
144 func main() {
145 in := bufio.NewReader(os.Stdin)
146 for {
147 if _, err := os.Stdout.WriteString("> "); err != nil {
148 log.Fatalf("WriteString: %s", err)
150 line, err := in.ReadBytes('\n')
151 if err == io.EOF {
152 return
154 if err != nil {
155 log.Fatalf("ReadBytes: %s", err)
158 exprParse(&exprLex{line: line})
162 //line yacctab:1
163 var exprExca = [...]int{
164 -1, 1,
165 1, -1,
166 -2, 0,
169 const exprPrivate = 57344
171 const exprLast = 23
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,
176 15, 10, 11,
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,
182 -1000, -1000,
185 var exprPgo = [...]int{
186 0, 3, 14, 6, 0, 11,
189 var exprR1 = [...]int{
190 0, 5, 1, 1, 1, 2, 2, 2, 3, 3,
191 3, 4, 4,
194 var exprR2 = [...]int{
195 0, 1, 1, 2, 2, 1, 3, 3, 1, 3,
196 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,
202 -4, 9,
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,
208 10, 12,
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{
220 2, 3, 10,
223 var exprTok3 = [...]int{
227 var exprErrorMessages = [...]struct {
228 state int
229 token int
230 msg string
233 //line yaccpar:1
235 /* parser for yacc output */
237 var (
238 exprDebug = 0
239 exprErrorVerbose = false
242 type exprLexer interface {
243 Lex(lval *exprSymType) int
244 Error(s string)
247 type exprParser interface {
248 Parse(exprLexer) int
249 Lookahead() int
252 type exprParserImpl struct {
253 lval exprSymType
254 stack [exprInitialStackSize]exprSymType
255 char int
258 func (p *exprParserImpl) Lookahead() int {
259 return p.char
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 {
287 const TOKSTART = 4
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) {
309 return res
311 expected = append(expected, tok)
315 if exprDef[state] == -2 {
316 i := 0
317 for exprExca[i] != -1 || exprExca[i+1] != state {
318 i += 2
321 // Look for tokens that we accept or reduce.
322 for i += 2; exprExca[i] >= 0; i += 2 {
323 tok := exprExca[i]
324 if tok < TOKSTART || exprExca[i+1] == 0 {
325 continue
327 if len(expected) == cap(expected) {
328 return res
330 expected = append(expected, tok)
333 // If the default action is to accept or reduce, give up.
334 if exprExca[i+1] != 0 {
335 return res
339 for i, tok := range expected {
340 if i == 0 {
341 res += ", expecting "
342 } else {
343 res += " or "
345 res += exprTokname(tok)
347 return res
350 func exprlex1(lex exprLexer, lval *exprSymType) (char, token int) {
351 token = 0
352 char = lex.Lex(lval)
353 if char <= 0 {
354 token = exprTok1[0]
355 goto out
357 if char < len(exprTok1) {
358 token = exprTok1[char]
359 goto out
361 if char >= exprPrivate {
362 if char < exprPrivate+len(exprTok2) {
363 token = exprTok2[char-exprPrivate]
364 goto out
367 for i := 0; i < len(exprTok3); i += 2 {
368 token = exprTok3[i+0]
369 if token == char {
370 token = exprTok3[i+1]
371 goto out
375 out:
376 if token == 0 {
377 token = exprTok2[1] /* unknown char */
379 if exprDebug >= 3 {
380 __yyfmt__.Printf("lex %s(%d)\n", exprTokname(token), uint(char))
382 return char, token
385 func exprParse(exprlex exprLexer) int {
386 return exprNewParser().Parse(exprlex)
389 func (exprrcvr *exprParserImpl) Parse(exprlex exprLexer) int {
390 var exprn 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 */
398 exprstate := 0
399 exprrcvr.char = -1
400 exprtoken := -1 // exprrcvr.char translated into internal numbering
401 defer func() {
402 // Make sure we report no lookahead when not parsing.
403 exprstate = -1
404 exprrcvr.char = -1
405 exprtoken = -1
407 exprp := -1
408 goto exprstack
410 ret0:
411 return 0
413 ret1:
414 return 1
416 exprstack:
417 /* put a state and value onto the stack */
418 if exprDebug >= 4 {
419 __yyfmt__.Printf("char %v in %v\n", exprTokname(exprtoken), exprStatname(exprstate))
422 exprp++
423 if exprp >= len(exprS) {
424 nyys := make([]exprSymType, len(exprS)*2)
425 copy(nyys, exprS)
426 exprS = nyys
428 exprS[exprp] = exprVAL
429 exprS[exprp].yys = exprstate
431 exprnewstate:
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)
439 exprn += exprtoken
440 if exprn < 0 || exprn >= exprLast {
441 goto exprdefault
443 exprn = exprAct[exprn]
444 if exprChk[exprn] == exprtoken { /* valid shift */
445 exprrcvr.char = -1
446 exprtoken = -1
447 exprVAL = exprrcvr.lval
448 exprstate = exprn
449 if Errflag > 0 {
450 Errflag--
452 goto exprstack
455 exprdefault:
456 /* default state action */
457 exprn = exprDef[exprstate]
458 if exprn == -2 {
459 if exprrcvr.char < 0 {
460 exprrcvr.char, exprtoken = exprlex1(exprlex, &exprrcvr.lval)
463 /* look through exception table */
464 xi := 0
465 for {
466 if exprExca[xi+0] == -1 && exprExca[xi+1] == exprstate {
467 break
469 xi += 2
471 for xi += 2; ; xi += 2 {
472 exprn = exprExca[xi+0]
473 if exprn < 0 || exprn == exprtoken {
474 break
477 exprn = exprExca[xi+1]
478 if exprn < 0 {
479 goto ret0
482 if exprn == 0 {
483 /* error ... attempt to resume parsing */
484 switch Errflag {
485 case 0: /* brand new error */
486 exprlex.Error(exprErrorMessage(exprstate, exprtoken))
487 Nerrs++
488 if exprDebug >= 1 {
489 __yyfmt__.Printf("%s", exprStatname(exprstate))
490 __yyfmt__.Printf(" saw %s\n", exprTokname(exprtoken))
492 fallthrough
494 case 1, 2: /* incompletely recovered error ... try again */
495 Errflag = 3
497 /* find a state where "error" is a legal shift action */
498 for exprp >= 0 {
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 {
503 goto exprstack
507 /* the current p has no shift on "error", pop stack */
508 if exprDebug >= 2 {
509 __yyfmt__.Printf("error recovery pops state %d\n", exprS[exprp].yys)
511 exprp--
513 /* there is no state on the stack with an error shift ... abort */
514 goto ret1
516 case 3: /* no shift yet; clobber input char */
517 if exprDebug >= 2 {
518 __yyfmt__.Printf("error recovery discards %s\n", exprTokname(exprtoken))
520 if exprtoken == exprEofCode {
521 goto ret1
523 exprrcvr.char = -1
524 exprtoken = -1
525 goto exprnewstate /* try again in the same state */
529 /* reduction by production exprn */
530 if exprDebug >= 2 {
531 __yyfmt__.Printf("reduce %v in:\n\t%v\n", exprn, exprStatname(exprstate))
534 exprnt := exprn
535 exprpt := exprp
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)
543 copy(nyys, exprS)
544 exprS = nyys
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]
555 } else {
556 exprstate = exprAct[exprj]
557 if exprChk[exprstate] != -exprn {
558 exprstate = exprAct[exprg]
561 // dummy call; replaced with literal code
562 switch exprnt {
564 case 1:
565 exprDollar = exprS[exprpt-1 : exprpt+1]
566 //line expr.y:43
568 if exprDollar[1].num.IsInt() {
569 fmt.Println(exprDollar[1].num.Num().String())
570 } else {
571 fmt.Println(exprDollar[1].num.String())
574 case 3:
575 exprDollar = exprS[exprpt-2 : exprpt+1]
576 //line expr.y:54
578 exprVAL.num = exprDollar[2].num
580 case 4:
581 exprDollar = exprS[exprpt-2 : exprpt+1]
582 //line expr.y:58
584 exprVAL.num = exprDollar[2].num.Neg(exprDollar[2].num)
586 case 6:
587 exprDollar = exprS[exprpt-3 : exprpt+1]
588 //line expr.y:65
590 exprVAL.num = exprDollar[1].num.Add(exprDollar[1].num, exprDollar[3].num)
592 case 7:
593 exprDollar = exprS[exprpt-3 : exprpt+1]
594 //line expr.y:69
596 exprVAL.num = exprDollar[1].num.Sub(exprDollar[1].num, exprDollar[3].num)
598 case 9:
599 exprDollar = exprS[exprpt-3 : exprpt+1]
600 //line expr.y:76
602 exprVAL.num = exprDollar[1].num.Mul(exprDollar[1].num, exprDollar[3].num)
604 case 10:
605 exprDollar = exprS[exprpt-3 : exprpt+1]
606 //line expr.y:80
608 exprVAL.num = exprDollar[1].num.Quo(exprDollar[1].num, exprDollar[3].num)
610 case 12:
611 exprDollar = exprS[exprpt-3 : exprpt+1]
612 //line expr.y:87
614 exprVAL.num = exprDollar[2].num
617 goto exprstack /* stack new state and value */