expr.y 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // This is an example of a goyacc program.
  5. // To build it:
  6. // goyacc -p "expr" expr.y (produces y.go)
  7. // go build -o expr y.go
  8. // expr
  9. // > <type an expression>
  10. %{
  11. package main
  12. import (
  13. "bufio"
  14. "bytes"
  15. "fmt"
  16. "io"
  17. "log"
  18. "math/big"
  19. "os"
  20. "unicode/utf8"
  21. )
  22. %}
  23. %union {
  24. num *big.Rat
  25. }
  26. %type <num> expr expr1 expr2 expr3
  27. %token '+' '-' '*' '/' '(' ')'
  28. %token <num> NUM
  29. %%
  30. top:
  31. expr
  32. {
  33. if $1.IsInt() {
  34. fmt.Println($1.Num().String())
  35. } else {
  36. fmt.Println($1.String())
  37. }
  38. }
  39. expr:
  40. expr1
  41. | '+' expr
  42. {
  43. $$ = $2
  44. }
  45. | '-' expr
  46. {
  47. $$ = $2.Neg($2)
  48. }
  49. expr1:
  50. expr2
  51. | expr1 '+' expr2
  52. {
  53. $$ = $1.Add($1, $3)
  54. }
  55. | expr1 '-' expr2
  56. {
  57. $$ = $1.Sub($1, $3)
  58. }
  59. expr2:
  60. expr3
  61. | expr2 '*' expr3
  62. {
  63. $$ = $1.Mul($1, $3)
  64. }
  65. | expr2 '/' expr3
  66. {
  67. $$ = $1.Quo($1, $3)
  68. }
  69. expr3:
  70. NUM
  71. | '(' expr ')'
  72. {
  73. $$ = $2
  74. }
  75. %%
  76. // The parser expects the lexer to return 0 on EOF. Give it a name
  77. // for clarity.
  78. const eof = 0
  79. // The parser uses the type <prefix>Lex as a lexer. It must provide
  80. // the methods Lex(*<prefix>SymType) int and Error(string).
  81. type exprLex struct {
  82. line []byte
  83. peek rune
  84. }
  85. // The parser calls this method to get each new token. This
  86. // implementation returns operators and NUM.
  87. func (x *exprLex) Lex(yylval *exprSymType) int {
  88. for {
  89. c := x.next()
  90. switch c {
  91. case eof:
  92. return eof
  93. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  94. return x.num(c, yylval)
  95. case '+', '-', '*', '/', '(', ')':
  96. return int(c)
  97. // Recognize Unicode multiplication and division
  98. // symbols, returning what the parser expects.
  99. case '×':
  100. return '*'
  101. case '÷':
  102. return '/'
  103. case ' ', '\t', '\n', '\r':
  104. default:
  105. log.Printf("unrecognized character %q", c)
  106. }
  107. }
  108. }
  109. // Lex a number.
  110. func (x *exprLex) num(c rune, yylval *exprSymType) int {
  111. add := func(b *bytes.Buffer, c rune) {
  112. if _, err := b.WriteRune(c); err != nil {
  113. log.Fatalf("WriteRune: %s", err)
  114. }
  115. }
  116. var b bytes.Buffer
  117. add(&b, c)
  118. L: for {
  119. c = x.next()
  120. switch c {
  121. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'e', 'E':
  122. add(&b, c)
  123. default:
  124. break L
  125. }
  126. }
  127. if c != eof {
  128. x.peek = c
  129. }
  130. yylval.num = &big.Rat{}
  131. _, ok := yylval.num.SetString(b.String())
  132. if !ok {
  133. log.Printf("bad number %q", b.String())
  134. return eof
  135. }
  136. return NUM
  137. }
  138. // Return the next rune for the lexer.
  139. func (x *exprLex) next() rune {
  140. if x.peek != eof {
  141. r := x.peek
  142. x.peek = eof
  143. return r
  144. }
  145. if len(x.line) == 0 {
  146. return eof
  147. }
  148. c, size := utf8.DecodeRune(x.line)
  149. x.line = x.line[size:]
  150. if c == utf8.RuneError && size == 1 {
  151. log.Print("invalid utf8")
  152. return x.next()
  153. }
  154. return c
  155. }
  156. // The parser calls this method on a parse error.
  157. func (x *exprLex) Error(s string) {
  158. log.Printf("parse error: %s", s)
  159. }
  160. func main() {
  161. in := bufio.NewReader(os.Stdin)
  162. for {
  163. if _, err := os.Stdout.WriteString("> "); err != nil {
  164. log.Fatalf("WriteString: %s", err)
  165. }
  166. line, err := in.ReadBytes('\n')
  167. if err == io.EOF {
  168. return
  169. }
  170. if err != nil {
  171. log.Fatalf("ReadBytes: %s", err)
  172. }
  173. exprParse(&exprLex{line: line})
  174. }
  175. }