Title / Description
Code require 'pp' P={ "q0"=>[["a","q1"]], "q1"=>[["b","q2"],["b","q1"]], "q2"=>[["b","q0"],["a","q3"]], "q3"=>[["a","q4"]], "q4"=>[["a","q0"]] } sigma=["a","b"] s=["q0"] f=["q3"] i=0 P1=Hash.new() q=[] q<<s while q.size>i do current=q[i] puts "current at start = #{current}" temp=[] puts "current=#{current}" sigma.each do |terminal| puts "terminal=#{terminal}" new_current=[] current.each do |stare| puts "stare=#{stare}" if P.has_key?(stare) P[stare].each do |item| if item[0]==terminal new_current<<item[1] end end if new_current!=[] if temp!=[] if temp.last[0]==terminal temp.pop end end temp<<[terminal, new_current.join] end end puts "new_current=#{new_current}" if(new_current.size>0)&&( q.index(new_current)==nil ) q<<new_current end P1[current.join]=temp end end puts "q final=#{q}" i+=1 puts end puts puts pp P1 =begin P1.each do |stare, prods| prods.uniq.each do |prod| puts "#{stare}----#{prod[0]}--->#{prod[1]}" end end =end
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code