# # DO NOT MODIFY!!!! # This file is automatically generated by Racc 1.4.9 # from Racc grammer file "". # require 'racc/parser.rb' # Generated grammar file. module Maths class Calculator < Racc::Parser module_eval(<<'...end calc.y/module_eval...', 'calc.y', 39) def parse(str) @q = [] until str.empty? case str # ignore whitespace when /\A\s+/ # percentage handling when /\Aas a percentage of/ @q.push [:PERCENTAGE_OF, $&] when /\Aas a % of/ @q.push [:PERCENTAGE_OF, $&] when /\Aas % of/ @q.push [:PERCENTAGE_OF, $&] when /\Aas % of/ @q.push [:PERCENTAGE_OF, $&] # special terms when /\Api/ @q.push [:NUMBER, BigDecimal.new("3.14159265358979323846264338327")] # variables and functions when /\A[A-Za-z]+[0-9]*/ if Maths::Functions::FUNCTIONS.has_key?($&) @q.push [:FUNC, $&] else @q.push [:VARIABLE, $&] end when /\A\$/ @q.push [:VARIABLE, $&] # numbers when /\A\d+(\.\d+)?([Ee]\d+)?/ @q.push [:NUMBER, BigDecimal.new($&)] # when /\A\d+/ # @q.push [:NUMBER, BigDecimal.new($&)] when /\A.|\n/o s = $& @q.push [s, s] end str = $' end @q.push [false, '$end'] do_parse end def next_token @q.shift end ...end calc.y/module_eval... ##### State transition tables begin ### racc_action_table = [ 9, 16, 13, 14, 15, 21, 6, 10, 9, 4, 22, 5, 8, 7, 6, 24, 9, 4, nil, 5, 8, 7, 6, nil, 9, 4, nil, 5, 8, 19, 6, nil, 9, 4, nil, 5, 8, 19, 6, nil, 9, 4, nil, 5, 8, 19, 6, nil, 9, 4, nil, 5, 8, 19, 6, nil, 9, 4, nil, 5, 8, 19, 6, nil, 9, 4, nil, 5, 8, 19, 6, nil, 9, 4, nil, 5, 8, 19, 6, nil, 9, 4, nil, 5, 8, 19, 6, nil, 9, 4, nil, 5, 8, 19, 6, nil, nil, 4, nil, 5, 8, 19, 16, 13, 14, 15, 11, 12, 17, nil, nil, nil, 33, 16, 13, 14, 15, 11, 12, 17, nil, nil, 32, 16, 13, 14, 15, 11, 12, 17, 16, 13, 14, 15, 11, 12, 17, 16, 13, 14, 15, 11, 12, 17, 16, 13, 14, 15 ] racc_action_check = [ 0, 26, 26, 26, 26, 6, 0, 1, 22, 0, 7, 0, 0, 0, 22, 10, 17, 22, nil, 22, 22, 22, 17, nil, 4, 17, nil, 17, 17, 17, 4, nil, 5, 4, nil, 4, 4, 4, 5, nil, 16, 5, nil, 5, 5, 5, 16, nil, 15, 16, nil, 16, 16, 16, 15, nil, 9, 15, nil, 15, 15, 15, 9, nil, 14, 9, nil, 9, 9, 9, 14, nil, 11, 14, nil, 14, 14, 14, 11, nil, 12, 11, nil, 11, 11, 11, 12, nil, 13, 12, nil, 12, 12, 12, 13, nil, nil, 13, nil, 13, 13, 13, 20, 20, 20, 20, 20, 20, 20, nil, nil, nil, 20, 18, 18, 18, 18, 18, 18, 18, nil, nil, 18, 31, 31, 31, 31, 31, 31, 31, 2, 2, 2, 2, 2, 2, 2, 34, 34, 34, 34, 34, 34, 34, 25, 25, 25, 25 ] racc_action_pointer = [ -3, 7, 126, nil, 21, 29, -10, -1, nil, 53, 15, 69, 77, 85, 61, 45, 37, 13, 109, nil, 98, nil, 5, nil, nil, 140, -3, nil, nil, nil, nil, 119, nil, nil, 133, nil ] racc_action_default = [ -3, -19, -1, -2, -19, -19, -19, -13, -14, -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, -13, -19, -12, -19, -15, 36, -4, -5, -6, -7, -8, -9, -16, -10, -11, -17, -18 ] racc_goto_table = [ 3, 2, 1, nil, nil, 18, 20, nil, nil, nil, 23, nil, 25, 26, 27, 28, 29, 30, 31, nil, nil, nil, 35, 34 ] racc_goto_check = [ 3, 2, 1, nil, nil, 2, 2, nil, nil, nil, 2, nil, 2, 2, 2, 2, 2, 2, 2, nil, nil, nil, 3, 2 ] racc_goto_pointer = [ nil, 2, 1, 0 ] racc_goto_default = [ nil, nil, nil, nil ] racc_reduce_table = [ 0, 0, :racc_error, 1, 18, :_reduce_none, 1, 18, :_reduce_none, 0, 18, :_reduce_3, 3, 19, :_reduce_4, 3, 19, :_reduce_5, 3, 19, :_reduce_6, 3, 19, :_reduce_7, 3, 19, :_reduce_8, 3, 19, :_reduce_9, 3, 19, :_reduce_10, 3, 19, :_reduce_11, 2, 19, :_reduce_12, 1, 19, :_reduce_13, 1, 19, :_reduce_none, 2, 19, :_reduce_15, 3, 19, :_reduce_16, 3, 20, :_reduce_17, 3, 20, :_reduce_18 ] racc_reduce_n = 19 racc_shift_n = 36 racc_token_table = { false => 0, :error => 1, :UMINUS => 2, :FUNC => 3, "^" => 4, "*" => 5, "/" => 6, "%" => 7, "+" => 8, "-" => 9, :PERCENTAGE_OF => 10, "=" => 11, "(" => 12, ")" => 13, "|" => 14, :NUMBER => 15, :VARIABLE => 16 } racc_nt_base = 17 racc_use_result_var = true Racc_arg = [ racc_action_table, racc_action_check, racc_action_default, racc_action_pointer, racc_goto_table, racc_goto_check, racc_goto_default, racc_goto_pointer, racc_nt_base, racc_reduce_table, racc_token_table, racc_shift_n, racc_reduce_n, racc_use_result_var ] Racc_token_to_s_table = [ "$end", "error", "UMINUS", "FUNC", "\"^\"", "\"*\"", "\"/\"", "\"%\"", "\"+\"", "\"-\"", "PERCENTAGE_OF", "\"=\"", "\"(\"", "\")\"", "\"|\"", "NUMBER", "VARIABLE", "$start", "target", "exp", "assign" ] Racc_debug_parser = false ##### State transition tables end ##### # reduce 0 omitted # reduce 1 omitted # reduce 2 omitted module_eval(<<'.,.,', 'calc.y', 13) def _reduce_3(val, _values, result) result = 0 result end .,., module_eval(<<'.,.,', 'calc.y', 15) def _reduce_4(val, _values, result) result += val[2] result end .,., module_eval(<<'.,.,', 'calc.y', 16) def _reduce_5(val, _values, result) result -= val[2] result end .,., module_eval(<<'.,.,', 'calc.y', 17) def _reduce_6(val, _values, result) result *= val[2] result end .,., module_eval(<<'.,.,', 'calc.y', 18) def _reduce_7(val, _values, result) result /= val[2] result end .,., module_eval(<<'.,.,', 'calc.y', 19) def _reduce_8(val, _values, result) result %= val[2] result end .,., module_eval(<<'.,.,', 'calc.y', 20) def _reduce_9(val, _values, result) result = result.power(val[2]) result end .,., module_eval(<<'.,.,', 'calc.y', 21) def _reduce_10(val, _values, result) result = val[1] result end .,., module_eval(<<'.,.,', 'calc.y', 22) def _reduce_11(val, _values, result) result = val[1].abs result end .,., module_eval(<<'.,.,', 'calc.y', 23) def _reduce_12(val, _values, result) result = -val[1] result end .,., module_eval(<<'.,.,', 'calc.y', 24) def _reduce_13(val, _values, result) result = Brain.lookup(val[0]) result end .,., # reduce 14 omitted module_eval(<<'.,.,', 'calc.y', 26) def _reduce_15(val, _values, result) result = Maths::Functions.exec(val[0], val[1]) result end .,., module_eval(<<'.,.,', 'calc.y', 27) def _reduce_16(val, _values, result) result = Percentage.build(val[0], val[2]) result end .,., module_eval(<<'.,.,', 'calc.y', 29) def _reduce_17(val, _values, result) result = Brain.assign(val[0], val[2]) result end .,., module_eval(<<'.,.,', 'calc.y', 30) def _reduce_18(val, _values, result) result = Brain.assign(val[0], val[2]) result end .,., def _reduce_none(val, _values, result) val[0] end end # class Calculator end