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.



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

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.



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

def action
  @action
end

#closureObject (readonly)

Returns the value of attribute closure.



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

def closure
  @closure
end

#coreObject (readonly)

Returns the value of attribute core.



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

def core
  @core
end

#defactObject

default action



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

def defact
  @defact
end

#goto_tableObject (readonly)

Returns the value of attribute goto_table.



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

def goto_table
  @goto_table
end

#gotosObject (readonly)

Returns the value of attribute gotos.



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

def gotos
  @gotos
end

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

Returns the value of attribute ident.



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

def ident
  @ident
end

#ritemsObject (readonly)

Returns the value of attribute ritems.



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

def ritems
  @ritems
end

#rrconfObject (readonly)

Returns the value of attribute rrconf.



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

def rrconf
  @rrconf
end

#rrulesObject (readonly)

Returns the value of attribute rrules.



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

def rrules
  @rrules
end

#srconfObject (readonly)

Returns the value of attribute srconf.



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

def srconf
  @srconf
end

#stokensObject (readonly)

Returns the value of attribute stokens.



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

def stokens
  @stokens
end

Instance Method Details

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



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

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

#check_la(la_rules) ⇒ Object



666
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
# File 'lib/racc/state.rb', line 666

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)


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

def conflict?
  @conflict
end

#inspectObject Also known as: to_s



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

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

#la=(la) ⇒ Object



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

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



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

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



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

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

#n_srconflictsObject



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

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

#rr_conflict(high, low, ctok) ⇒ Object



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

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



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

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



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

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