Sha256: 5624b8d8d0bf5dd4b2e80f0c2540355f75401cecbe803be92aebaf90556e26a3

Contents?: true

Size: 1.66 KB

Versions: 35

Compression:

Stored size: 1.66 KB

Contents

class Calculator

  prechigh
    left '*' '/'
    left '+' '-'
  preclow

  convert
    NUMBER 'Number'
  end

rule

  target : exp
         | /* none */ { result = 0 }

  exp    : exp '+' exp { result += val[2]; a = 'plus' }
         | exp '-' exp { result -= val[2]; a = "string test" }
         | exp '*' exp { result *= val[2] }
         | exp '/' exp { result /= val[2] }
         | '(' { $emb = true } exp ')'
             {
               raise 'must not happen' unless $emb
               result = val[2]
             }
         | '-' NUMBER  { result = -val[1] }
         | NUMBER

----header

class Number
end

----inner

  def initialize
    @racc_debug_out = $stdout
    @yydebug = false
  end

  def validate(expected, src)
    result = parse(src)
    unless result == expected
      raise "test #{@test_number} fail"
    end
    @test_number += 1
  end

  def parse(src)
    @src = src
    @test_number = 1
    yyparse self, :scan
  end

  def scan(&block)
    @src.each(&block)
  end

----footer

calc = Calculator.new

calc.validate(9, [[Number, 9], nil])

calc.validate(-3,
    [[Number, 5],
     ['*',   '*'],
     [Number, 1],
     ['-',   '*'],
     [Number, 1],
     ['*',   '*'],
     [Number, 8],
     nil])

calc.validate(-1,
    [[Number, 5],
     ['+',   '+'],
     [Number, 2],
     ['-',   '-'],
     [Number, 5],
     ['+',   '+'],
     [Number, 2],
     ['-',   '-'],
     [Number, 5],
     nil])

calc.validate(-4,
    [['-',    'UMINUS'],
     [Number, 4],
     nil])

calc.validate(40,
    [[Number, 7],
     ['*',   '*'],
     ['(',   '('],
     [Number, 4],
     ['+',   '+'],
     [Number, 3],
     [')',   ')'],
     ['-',   '-'],
     [Number, 9],
     nil])

Version data entries

35 entries across 30 versions & 5 rubygems

Version Path
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/racc-1.5.2/test/assets/ichk.y
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/racc-1.5.2/test/assets/ichk.y
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/racc-1.5.2/test/assets/ichk.y
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/racc-1.5.2/test/assets/ichk.y
mumukit-content-type-1.11.1 vendor/bundle/ruby/2.6.0/gems/racc-1.5.2/test/assets/ichk.y
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/racc-1.5.2/test/assets/ichk.y
tdiary-5.1.6 vendor/bundle/ruby/3.0.0/gems/racc-1.5.2/test/assets/ichk.y
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/racc-1.5.2/test/assets/ichk.y
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/2.7.0/gems/racc-1.5.2/test/assets/ichk.y
tdiary-5.1.5 vendor/bundle/ruby/2.7.0/gems/racc-1.5.2/test/assets/ichk.y
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/racc-1.5.2/test/assets/ichk.y
racc-1.5.2-java test/assets/ichk.y
racc-1.5.2 test/assets/ichk.y
racc-1.5.1-java test/assets/ichk.y
racc-1.5.1 test/assets/ichk.y
racc-1.5.0-java test/assets/ichk.y
racc-1.5.0 test/assets/ichk.y
racc-1.4.16-java test/assets/ichk.y
racc-1.4.16 test/assets/ichk.y
racc-1.4.15-java test/assets/ichk.y