class Racc::SymbolTable

Attributes

anchor[R]
dummy[R]
error[R]
nt_base[R]
symbols[R]
to_a[R]

Public Class Methods

new() click to toggle source
# File lib/racc/grammar.rb, line 868
def initialize
  @symbols = []   # :: [Racc::Sym]
  @cache   = {}   # :: {(String|Symbol) => Racc::Sym}
  @dummy  = intern(:$start, true)
  @anchor = intern(false, true)     # Symbol ID = 0
  @error  = intern(:error, false)   # Symbol ID = 1
end

Public Instance Methods

[](id) click to toggle source
# File lib/racc/grammar.rb, line 880
def [](id)
  @symbols[id]
end
delete(sym) click to toggle source
# File lib/racc/grammar.rb, line 896
def delete(sym)
  @symbols.delete sym
  @cache.delete sym.value
end
each(&block) click to toggle source
# File lib/racc/grammar.rb, line 907
def each(&block)
  @symbols.each(&block)
end
each_nonterminal(&block) click to toggle source
# File lib/racc/grammar.rb, line 923
def each_nonterminal(&block)
  @nterms.each(&block)
end
each_terminal(&block) click to toggle source
# File lib/racc/grammar.rb, line 915
def each_terminal(&block)
  @terms.each(&block)
end
fix() click to toggle source
# File lib/racc/grammar.rb, line 927
def fix
  terms, nterms = @symbols.partition {|s| s.terminal? }
  @symbols = terms + nterms
  @terms = terms
  @nterms = nterms
  @nt_base = terms.size
  fix_ident
  check_terminals
end
intern(val, dummy = false) click to toggle source
# File lib/racc/grammar.rb, line 884
def intern(val, dummy = false)
  @cache[val] ||=
      begin
        sym = Sym.new(val, dummy)
        @symbols.push sym
        sym
      end
end
nonterminals() click to toggle source
# File lib/racc/grammar.rb, line 919
def nonterminals
  @symbols[@nt_base, @symbols.size - @nt_base]
end
nt_max() click to toggle source
# File lib/racc/grammar.rb, line 903
def nt_max
  @symbols.size
end
terminals(&block) click to toggle source
# File lib/racc/grammar.rb, line 911
def terminals(&block)
  @symbols[0, @nt_base]
end

Private Instance Methods

check_terminals() click to toggle source
# File lib/racc/grammar.rb, line 945
def check_terminals
  return unless @symbols.any? {|s| s.should_terminal? }
  @anchor.should_terminal
  @error.should_terminal
  each_terminal do |t|
    t.should_terminal if t.string_symbol?
  end
  each do |s|
    s.should_terminal if s.assoc
  end
  terminals().reject {|t| t.should_terminal? }.each do |t|
    raise CompileError, "terminal #{t} not declared as terminal"
  end
  nonterminals().select {|n| n.should_terminal? }.each do |n|
    raise CompileError, "symbol #{n} declared as terminal but is not terminal"
  end
end
fix_ident() click to toggle source
# File lib/racc/grammar.rb, line 939
def fix_ident
  @symbols.each_with_index do |t, i|
    t.ident = i
  end
end