Tonton San wrote in post #1136122:
> I'm trying to evaluate an arithmetic operation of prefix type,
> "add(multiply(123,432),subtract(add(19,21),124))", can you make it so
> the regex engine scans it backwards?

why scans backward ?

s="add(multiply(2,4),substract(add(add(add(1,2),9),add(2,1)),1))"

l=s.split(/([)(,\-])/).reject {|a| a=='(' || a==')' || a==',' || a==""}
p l
reg=[]

def add(a,b)       (a.to_i+b.to_i).to_s end
def multiply(a,b)  (a.to_i*b.to_i).to_s end
def substract(a,b) (b.to_i-a.to_i).to_s end

def is_num(a) a=~/^-?\d+$/ end
def is_op(a) a=~/^[a-z]+$/ end

loop do
  if is_num(l[-1]) && is_num(l[-2]) && is_op(l[-3])
    a,b,op=l.pop,l.pop,l.pop
    l.push send(op,a,b)
  end
  if is_num(l[-1]) && is_num(l[-2]) && is_num(l[-3])
    reg << l.pop
  end
  if is_num(l[-1]) && is_op(l[-2]) && reg.size>0
    l.push reg.pop
  end
  puts "#{l.inspect} || #{reg.inspect}"
  break if l.size==1 && reg.size==0
end
p l.last


Perhaps you mix lexical analysis with syntax analysis and evaluator  ?

-- 
Posted via http://www.ruby-forum.com/.