expr.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. //line expr.y:13
  2. package main
  3. import __yyfmt__ "fmt"
  4. //line expr.y:14
  5. import (
  6. "bufio"
  7. "bytes"
  8. "fmt"
  9. "io"
  10. "log"
  11. "math/big"
  12. "os"
  13. "unicode/utf8"
  14. )
  15. //line expr.y:29
  16. type exprSymType struct {
  17. yys int
  18. num *big.Rat
  19. }
  20. const NUM = 57346
  21. var exprToknames = [...]string{
  22. "$end",
  23. "error",
  24. "$unk",
  25. "'+'",
  26. "'-'",
  27. "'*'",
  28. "'/'",
  29. "'('",
  30. "')'",
  31. "NUM",
  32. }
  33. var exprStatenames = [...]string{}
  34. const exprEofCode = 1
  35. const exprErrCode = 2
  36. const exprInitialStackSize = 16
  37. //line expr.y:92
  38. // The parser expects the lexer to return 0 on EOF. Give it a name
  39. // for clarity.
  40. const eof = 0
  41. // The parser uses the type <prefix>Lex as a lexer. It must provide
  42. // the methods Lex(*<prefix>SymType) int and Error(string).
  43. type exprLex struct {
  44. line []byte
  45. peek rune
  46. }
  47. // The parser calls this method to get each new token. This
  48. // implementation returns operators and NUM.
  49. func (x *exprLex) Lex(yylval *exprSymType) int {
  50. for {
  51. c := x.next()
  52. switch c {
  53. case eof:
  54. return eof
  55. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  56. return x.num(c, yylval)
  57. case '+', '-', '*', '/', '(', ')':
  58. return int(c)
  59. // Recognize Unicode multiplication and division
  60. // symbols, returning what the parser expects.
  61. case '×':
  62. return '*'
  63. case '÷':
  64. return '/'
  65. case ' ', '\t', '\n', '\r':
  66. default:
  67. log.Printf("unrecognized character %q", c)
  68. }
  69. }
  70. }
  71. // Lex a number.
  72. func (x *exprLex) num(c rune, yylval *exprSymType) int {
  73. add := func(b *bytes.Buffer, c rune) {
  74. if _, err := b.WriteRune(c); err != nil {
  75. log.Fatalf("WriteRune: %s", err)
  76. }
  77. }
  78. var b bytes.Buffer
  79. add(&b, c)
  80. L:
  81. for {
  82. c = x.next()
  83. switch c {
  84. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'e', 'E':
  85. add(&b, c)
  86. default:
  87. break L
  88. }
  89. }
  90. if c != eof {
  91. x.peek = c
  92. }
  93. yylval.num = &big.Rat{}
  94. _, ok := yylval.num.SetString(b.String())
  95. if !ok {
  96. log.Printf("bad number %q", b.String())
  97. return eof
  98. }
  99. return NUM
  100. }
  101. // Return the next rune for the lexer.
  102. func (x *exprLex) next() rune {
  103. if x.peek != eof {
  104. r := x.peek
  105. x.peek = eof
  106. return r
  107. }
  108. if len(x.line) == 0 {
  109. return eof
  110. }
  111. c, size := utf8.DecodeRune(x.line)
  112. x.line = x.line[size:]
  113. if c == utf8.RuneError && size == 1 {
  114. log.Print("invalid utf8")
  115. return x.next()
  116. }
  117. return c
  118. }
  119. // The parser calls this method on a parse error.
  120. func (x *exprLex) Error(s string) {
  121. log.Printf("parse error: %s", s)
  122. }
  123. func main() {
  124. in := bufio.NewReader(os.Stdin)
  125. for {
  126. if _, err := os.Stdout.WriteString("> "); err != nil {
  127. log.Fatalf("WriteString: %s", err)
  128. }
  129. line, err := in.ReadBytes('\n')
  130. if err == io.EOF {
  131. return
  132. }
  133. if err != nil {
  134. log.Fatalf("ReadBytes: %s", err)
  135. }
  136. exprParse(&exprLex{line: line})
  137. }
  138. }
  139. //line yacctab:1
  140. var exprExca = [...]int{
  141. -1, 1,
  142. 1, -1,
  143. -2, 0,
  144. }
  145. const exprPrivate = 57344
  146. const exprLast = 23
  147. var exprAct = [...]int{
  148. 7, 4, 5, 2, 21, 9, 6, 8, 12, 13,
  149. 9, 1, 8, 16, 3, 19, 20, 17, 18, 14,
  150. 15, 10, 11,
  151. }
  152. var exprPact = [...]int{
  153. -3, -1000, -1000, 17, -3, -3, 13, -1000, -1000, -3,
  154. 2, 2, -1000, -1000, 2, 2, -5, 13, 13, -1000,
  155. -1000, -1000,
  156. }
  157. var exprPgo = [...]int{
  158. 0, 3, 14, 6, 0, 11,
  159. }
  160. var exprR1 = [...]int{
  161. 0, 5, 1, 1, 1, 2, 2, 2, 3, 3,
  162. 3, 4, 4,
  163. }
  164. var exprR2 = [...]int{
  165. 0, 1, 1, 2, 2, 1, 3, 3, 1, 3,
  166. 3, 1, 3,
  167. }
  168. var exprChk = [...]int{
  169. -1000, -5, -1, -2, 4, 5, -3, -4, 10, 8,
  170. 4, 5, -1, -1, 6, 7, -1, -3, -3, -4,
  171. -4, 9,
  172. }
  173. var exprDef = [...]int{
  174. 0, -2, 1, 2, 0, 0, 5, 8, 11, 0,
  175. 0, 0, 3, 4, 0, 0, 0, 6, 7, 9,
  176. 10, 12,
  177. }
  178. var exprTok1 = [...]int{
  179. 1, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  180. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  181. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  182. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  183. 8, 9, 6, 4, 3, 5, 3, 7,
  184. }
  185. var exprTok2 = [...]int{
  186. 2, 3, 10,
  187. }
  188. var exprTok3 = [...]int{
  189. 0,
  190. }
  191. var exprErrorMessages = [...]struct {
  192. state int
  193. token int
  194. msg string
  195. }{}
  196. //line yaccpar:1
  197. /* parser for yacc output */
  198. var (
  199. exprDebug = 0
  200. exprErrorVerbose = false
  201. )
  202. type exprLexer interface {
  203. Lex(lval *exprSymType) int
  204. Error(s string)
  205. }
  206. type exprParser interface {
  207. Parse(exprLexer) int
  208. Lookahead() int
  209. }
  210. type exprParserImpl struct {
  211. lval exprSymType
  212. stack [exprInitialStackSize]exprSymType
  213. char int
  214. }
  215. func (p *exprParserImpl) Lookahead() int {
  216. return p.char
  217. }
  218. func exprNewParser() exprParser {
  219. return &exprParserImpl{}
  220. }
  221. const exprFlag = -1000
  222. func exprTokname(c int) string {
  223. if c >= 1 && c-1 < len(exprToknames) {
  224. if exprToknames[c-1] != "" {
  225. return exprToknames[c-1]
  226. }
  227. }
  228. return __yyfmt__.Sprintf("tok-%v", c)
  229. }
  230. func exprStatname(s int) string {
  231. if s >= 0 && s < len(exprStatenames) {
  232. if exprStatenames[s] != "" {
  233. return exprStatenames[s]
  234. }
  235. }
  236. return __yyfmt__.Sprintf("state-%v", s)
  237. }
  238. func exprErrorMessage(state, lookAhead int) string {
  239. const TOKSTART = 4
  240. if !exprErrorVerbose {
  241. return "syntax error"
  242. }
  243. for _, e := range exprErrorMessages {
  244. if e.state == state && e.token == lookAhead {
  245. return "syntax error: " + e.msg
  246. }
  247. }
  248. res := "syntax error: unexpected " + exprTokname(lookAhead)
  249. // To match Bison, suggest at most four expected tokens.
  250. expected := make([]int, 0, 4)
  251. // Look for shiftable tokens.
  252. base := exprPact[state]
  253. for tok := TOKSTART; tok-1 < len(exprToknames); tok++ {
  254. if n := base + tok; n >= 0 && n < exprLast && exprChk[exprAct[n]] == tok {
  255. if len(expected) == cap(expected) {
  256. return res
  257. }
  258. expected = append(expected, tok)
  259. }
  260. }
  261. if exprDef[state] == -2 {
  262. i := 0
  263. for exprExca[i] != -1 || exprExca[i+1] != state {
  264. i += 2
  265. }
  266. // Look for tokens that we accept or reduce.
  267. for i += 2; exprExca[i] >= 0; i += 2 {
  268. tok := exprExca[i]
  269. if tok < TOKSTART || exprExca[i+1] == 0 {
  270. continue
  271. }
  272. if len(expected) == cap(expected) {
  273. return res
  274. }
  275. expected = append(expected, tok)
  276. }
  277. // If the default action is to accept or reduce, give up.
  278. if exprExca[i+1] != 0 {
  279. return res
  280. }
  281. }
  282. for i, tok := range expected {
  283. if i == 0 {
  284. res += ", expecting "
  285. } else {
  286. res += " or "
  287. }
  288. res += exprTokname(tok)
  289. }
  290. return res
  291. }
  292. func exprlex1(lex exprLexer, lval *exprSymType) (char, token int) {
  293. token = 0
  294. char = lex.Lex(lval)
  295. if char <= 0 {
  296. token = exprTok1[0]
  297. goto out
  298. }
  299. if char < len(exprTok1) {
  300. token = exprTok1[char]
  301. goto out
  302. }
  303. if char >= exprPrivate {
  304. if char < exprPrivate+len(exprTok2) {
  305. token = exprTok2[char-exprPrivate]
  306. goto out
  307. }
  308. }
  309. for i := 0; i < len(exprTok3); i += 2 {
  310. token = exprTok3[i+0]
  311. if token == char {
  312. token = exprTok3[i+1]
  313. goto out
  314. }
  315. }
  316. out:
  317. if token == 0 {
  318. token = exprTok2[1] /* unknown char */
  319. }
  320. if exprDebug >= 3 {
  321. __yyfmt__.Printf("lex %s(%d)\n", exprTokname(token), uint(char))
  322. }
  323. return char, token
  324. }
  325. func exprParse(exprlex exprLexer) int {
  326. return exprNewParser().Parse(exprlex)
  327. }
  328. func (exprrcvr *exprParserImpl) Parse(exprlex exprLexer) int {
  329. var exprn int
  330. var exprVAL exprSymType
  331. var exprDollar []exprSymType
  332. _ = exprDollar // silence set and not used
  333. exprS := exprrcvr.stack[:]
  334. Nerrs := 0 /* number of errors */
  335. Errflag := 0 /* error recovery flag */
  336. exprstate := 0
  337. exprrcvr.char = -1
  338. exprtoken := -1 // exprrcvr.char translated into internal numbering
  339. defer func() {
  340. // Make sure we report no lookahead when not parsing.
  341. exprstate = -1
  342. exprrcvr.char = -1
  343. exprtoken = -1
  344. }()
  345. exprp := -1
  346. goto exprstack
  347. ret0:
  348. return 0
  349. ret1:
  350. return 1
  351. exprstack:
  352. /* put a state and value onto the stack */
  353. if exprDebug >= 4 {
  354. __yyfmt__.Printf("char %v in %v\n", exprTokname(exprtoken), exprStatname(exprstate))
  355. }
  356. exprp++
  357. if exprp >= len(exprS) {
  358. nyys := make([]exprSymType, len(exprS)*2)
  359. copy(nyys, exprS)
  360. exprS = nyys
  361. }
  362. exprS[exprp] = exprVAL
  363. exprS[exprp].yys = exprstate
  364. exprnewstate:
  365. exprn = exprPact[exprstate]
  366. if exprn <= exprFlag {
  367. goto exprdefault /* simple state */
  368. }
  369. if exprrcvr.char < 0 {
  370. exprrcvr.char, exprtoken = exprlex1(exprlex, &exprrcvr.lval)
  371. }
  372. exprn += exprtoken
  373. if exprn < 0 || exprn >= exprLast {
  374. goto exprdefault
  375. }
  376. exprn = exprAct[exprn]
  377. if exprChk[exprn] == exprtoken { /* valid shift */
  378. exprrcvr.char = -1
  379. exprtoken = -1
  380. exprVAL = exprrcvr.lval
  381. exprstate = exprn
  382. if Errflag > 0 {
  383. Errflag--
  384. }
  385. goto exprstack
  386. }
  387. exprdefault:
  388. /* default state action */
  389. exprn = exprDef[exprstate]
  390. if exprn == -2 {
  391. if exprrcvr.char < 0 {
  392. exprrcvr.char, exprtoken = exprlex1(exprlex, &exprrcvr.lval)
  393. }
  394. /* look through exception table */
  395. xi := 0
  396. for {
  397. if exprExca[xi+0] == -1 && exprExca[xi+1] == exprstate {
  398. break
  399. }
  400. xi += 2
  401. }
  402. for xi += 2; ; xi += 2 {
  403. exprn = exprExca[xi+0]
  404. if exprn < 0 || exprn == exprtoken {
  405. break
  406. }
  407. }
  408. exprn = exprExca[xi+1]
  409. if exprn < 0 {
  410. goto ret0
  411. }
  412. }
  413. if exprn == 0 {
  414. /* error ... attempt to resume parsing */
  415. switch Errflag {
  416. case 0: /* brand new error */
  417. exprlex.Error(exprErrorMessage(exprstate, exprtoken))
  418. Nerrs++
  419. if exprDebug >= 1 {
  420. __yyfmt__.Printf("%s", exprStatname(exprstate))
  421. __yyfmt__.Printf(" saw %s\n", exprTokname(exprtoken))
  422. }
  423. fallthrough
  424. case 1, 2: /* incompletely recovered error ... try again */
  425. Errflag = 3
  426. /* find a state where "error" is a legal shift action */
  427. for exprp >= 0 {
  428. exprn = exprPact[exprS[exprp].yys] + exprErrCode
  429. if exprn >= 0 && exprn < exprLast {
  430. exprstate = exprAct[exprn] /* simulate a shift of "error" */
  431. if exprChk[exprstate] == exprErrCode {
  432. goto exprstack
  433. }
  434. }
  435. /* the current p has no shift on "error", pop stack */
  436. if exprDebug >= 2 {
  437. __yyfmt__.Printf("error recovery pops state %d\n", exprS[exprp].yys)
  438. }
  439. exprp--
  440. }
  441. /* there is no state on the stack with an error shift ... abort */
  442. goto ret1
  443. case 3: /* no shift yet; clobber input char */
  444. if exprDebug >= 2 {
  445. __yyfmt__.Printf("error recovery discards %s\n", exprTokname(exprtoken))
  446. }
  447. if exprtoken == exprEofCode {
  448. goto ret1
  449. }
  450. exprrcvr.char = -1
  451. exprtoken = -1
  452. goto exprnewstate /* try again in the same state */
  453. }
  454. }
  455. /* reduction by production exprn */
  456. if exprDebug >= 2 {
  457. __yyfmt__.Printf("reduce %v in:\n\t%v\n", exprn, exprStatname(exprstate))
  458. }
  459. exprnt := exprn
  460. exprpt := exprp
  461. _ = exprpt // guard against "declared and not used"
  462. exprp -= exprR2[exprn]
  463. // exprp is now the index of $0. Perform the default action. Iff the
  464. // reduced production is ε, $1 is possibly out of range.
  465. if exprp+1 >= len(exprS) {
  466. nyys := make([]exprSymType, len(exprS)*2)
  467. copy(nyys, exprS)
  468. exprS = nyys
  469. }
  470. exprVAL = exprS[exprp+1]
  471. /* consult goto table to find next state */
  472. exprn = exprR1[exprn]
  473. exprg := exprPgo[exprn]
  474. exprj := exprg + exprS[exprp].yys + 1
  475. if exprj >= exprLast {
  476. exprstate = exprAct[exprg]
  477. } else {
  478. exprstate = exprAct[exprj]
  479. if exprChk[exprstate] != -exprn {
  480. exprstate = exprAct[exprg]
  481. }
  482. }
  483. // dummy call; replaced with literal code
  484. switch exprnt {
  485. case 1:
  486. exprDollar = exprS[exprpt-1 : exprpt+1]
  487. //line expr.y:43
  488. {
  489. if exprDollar[1].num.IsInt() {
  490. fmt.Println(exprDollar[1].num.Num().String())
  491. } else {
  492. fmt.Println(exprDollar[1].num.String())
  493. }
  494. }
  495. case 3:
  496. exprDollar = exprS[exprpt-2 : exprpt+1]
  497. //line expr.y:54
  498. {
  499. exprVAL.num = exprDollar[2].num
  500. }
  501. case 4:
  502. exprDollar = exprS[exprpt-2 : exprpt+1]
  503. //line expr.y:58
  504. {
  505. exprVAL.num = exprDollar[2].num.Neg(exprDollar[2].num)
  506. }
  507. case 6:
  508. exprDollar = exprS[exprpt-3 : exprpt+1]
  509. //line expr.y:65
  510. {
  511. exprVAL.num = exprDollar[1].num.Add(exprDollar[1].num, exprDollar[3].num)
  512. }
  513. case 7:
  514. exprDollar = exprS[exprpt-3 : exprpt+1]
  515. //line expr.y:69
  516. {
  517. exprVAL.num = exprDollar[1].num.Sub(exprDollar[1].num, exprDollar[3].num)
  518. }
  519. case 9:
  520. exprDollar = exprS[exprpt-3 : exprpt+1]
  521. //line expr.y:76
  522. {
  523. exprVAL.num = exprDollar[1].num.Mul(exprDollar[1].num, exprDollar[3].num)
  524. }
  525. case 10:
  526. exprDollar = exprS[exprpt-3 : exprpt+1]
  527. //line expr.y:80
  528. {
  529. exprVAL.num = exprDollar[1].num.Quo(exprDollar[1].num, exprDollar[3].num)
  530. }
  531. case 12:
  532. exprDollar = exprS[exprpt-3 : exprpt+1]
  533. //line expr.y:87
  534. {
  535. exprVAL.num = exprDollar[2].num
  536. }
  537. }
  538. goto exprstack /* stack new state and value */
  539. }