File: calc3.racc

package info (click to toggle)
ruby-oedipus-lex 2.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 240 kB
  • sloc: ruby: 1,218; lisp: 12; ansic: 5; makefile: 4
file content (54 lines) | stat: -rw-r--r-- 907 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#
# A simple calculator, version 3.
#

class Calculator3
  prechigh
    nonassoc UMINUS
    left '*' '/'
    left '+' '-'
  preclow
  options no_result_var
rule
  target  : exp
          | /* none */ { 0 }

  exp     : exp '+' exp { val[0] + val[2] }
          | exp '-' exp { val[0] - val[2] }
          | exp '*' exp { val[0] * val[2] }
          | exp '/' exp { val[0] / val[2] }
          | '(' exp ')' { val[1] }
          | '-' NUMBER  =UMINUS { -(val[1]) }
          | NUMBER
end

---- header ----
#
# generated by racc
#
require 'calc3.rex'

---- inner ----

---- footer ----

if $stdin.tty? then
  puts 'sample calc'
  puts '"q" to quit.'
end
calc = Calculator3.new

while true
  if $stdin.tty? then
    print '>>> '; $stdout.flush
  end
  str = $stdin.gets.strip
  break if /q/i === str
  begin
    p calc.parse str
  rescue ParseError
    puts 'parse error'
  end

  break unless $stdin.tty?
end