Class: Racc::State

Inherits:
Object show all
Defined in:
lib/racc/state.rb

Overview

A LALR state.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ident, core) ⇒ State

Returns a new instance of State.



609
610
611
612
613
614
615
616
617
618
619
620
621
622
# File 'lib/racc/state.rb', line 609

def initialize(ident, core)
  @ident = ident
  @core = core
  @goto_table = {}
  @gotos = {}
  @stokens = nil
  @ritems = nil
  @action = {}
  @defact = nil
  @rrconf = nil
  @srconf = nil

  @closure = make_closure(@core)
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



638
639
640
# File 'lib/racc/state.rb', line 638

def action
  @action
end

#closureObject (readonly)

Returns the value of attribute closure.



629
630
631
# File 'lib/racc/state.rb', line 629

def closure
  @closure
end

#coreObject (readonly)

Returns the value of attribute core.



628
629
630
# File 'lib/racc/state.rb', line 628

def core
  @core
end

#defactObject

default action



639
640
641
# File 'lib/racc/state.rb', line 639

def defact
  @defact
end

#goto_tableObject (readonly)

Returns the value of attribute goto_table.



631
632
633
# File 'lib/racc/state.rb', line 631

def goto_table
  @goto_table
end

#gotosObject (readonly)

Returns the value of attribute gotos.



632
633
634
# File 'lib/racc/state.rb', line 632

def gotos
  @gotos
end

#identObject (readonly) Also known as: stateid, hash

Returns the value of attribute ident.



624
625
626
# File 'lib/racc/state.rb', line 624

def ident
  @ident
end

#ritemsObject (readonly)

Returns the value of attribute ritems.



635
636
637
# File 'lib/racc/state.rb', line 635

def ritems
  @ritems
end

#rrconfObject (readonly)

Returns the value of attribute rrconf.



641
642
643
# File 'lib/racc/state.rb', line 641

def rrconf
  @rrconf
end

#rrulesObject (readonly)

Returns the value of attribute rrules.



636
637
638
# File 'lib/racc/state.rb', line 636

def rrules
  @rrules
end

#srconfObject (readonly)

Returns the value of attribute srconf.



642
643
644
# File 'lib/racc/state.rb', line 642

def srconf
  @srconf
end

#stokensObject (readonly)

Returns the value of attribute stokens.



634
635
636
# File 'lib/racc/state.rb', line 634

def stokens
  @stokens
end

Instance Method Details

#==(oth) ⇒ Object Also known as: eql?



650
651
652
# File 'lib/racc/state.rb', line 650

def ==(oth)
  @ident == oth.ident
end

#check_la(la_rules) ⇒ Object



667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'lib/racc/state.rb', line 667

def check_la(la_rules)
  @conflict = false
  s = []
  r = []
  @closure.each do |ptr|
    if t = ptr.dereference
      if t.terminal?
        s[t.ident] = t
        if t.ident == 1    # $error
          @conflict = true
        end
      end
    else
      r.push ptr.rule
    end
  end
  unless r.empty?
    if not s.empty? or r.size > 1
      @conflict = true
    end
  end
  s.compact!
  @stokens  = s
  @rrules = r

  if @conflict
    @la_rules_i = la_rules.size
    @la_rules = r.map {|i| i.ident }
    la_rules.concat r
  else
    @la_rules_i = @la_rules = nil
  end
end

#conflict?Boolean

Returns:

  • (Boolean)


701
702
703
# File 'lib/racc/state.rb', line 701

def conflict?
  @conflict
end

#inspectObject Also known as: to_s



644
645
646
# File 'lib/racc/state.rb', line 644

def inspect
  "<state #{@ident}>"
end

#la=(la) ⇒ Object



718
719
720
721
722
723
724
725
726
# File 'lib/racc/state.rb', line 718

def la=(la)
  return unless @conflict
  i = @la_rules_i
  @ritems = r = []
  @rrules.each do |rule|
    r.push Item.new(rule, la[i])
    i += 1
  end
end

#make_closure(core) ⇒ Object



656
657
658
659
660
661
662
663
664
665
# File 'lib/racc/state.rb', line 656

def make_closure(core)
  set = ISet.new
  core.each do |ptr|
    set.add ptr
    if t = ptr.dereference and t.nonterminal?
      set.update_a t.expand
    end
  end
  set.to_a
end

#n_rrconflictsObject



754
755
756
# File 'lib/racc/state.rb', line 754

def n_rrconflicts
  @rrconf ? @rrconf.size : 0
end

#n_srconflictsObject



750
751
752
# File 'lib/racc/state.rb', line 750

def n_srconflicts
  @srconf ? @srconf.size : 0
end

#rr_conflict(high, low, ctok) ⇒ Object



728
729
730
731
732
733
734
735
736
737
# File 'lib/racc/state.rb', line 728

def rr_conflict(high, low, ctok)
  c = RRconflict.new(@ident, high, low, ctok)

  @rrconf ||= {}
  if a = @rrconf[ctok]
    a.push c
  else
    @rrconf[ctok] = [c]
  end
end

#rruleid(rule) ⇒ Object



705
706
707
708
709
710
711
712
713
714
715
716
# File 'lib/racc/state.rb', line 705

def rruleid(rule)
  if i = @la_rules.index(rule.ident)
    @la_rules_i + i
  else
    puts '/// rruleid'
    p self
    p rule
    p @rrules
    p @la_rules_i
    raise 'racc: fatal: cannot get reduce rule id'
  end
end

#sr_conflict(shift, reduce) ⇒ Object



739
740
741
742
743
744
745
746
747
748
# File 'lib/racc/state.rb', line 739

def sr_conflict(shift, reduce)
  c = SRconflict.new(@ident, shift, reduce)

  @srconf ||= {}
  if a = @srconf[shift]
    a.push c
  else
    @srconf[shift] = [c]
  end
end