Parent

Included Modules

Dhaka::Parser

The parser generator. To generate a parser from a grammar specification ArithmeticPrecedenceGrammar, one would write:

parser = Dhaka::Parser.new(ArithmeticPrecedenceGrammar)

To compile this parser to Ruby source as ArithmeticPrecedenceParser:

parser.compile_to_ruby_source_as(:ArithmeticPrecedenceParser)

which returns a string of Ruby code.

Attributes

grammar[R]

Public Class Methods

new(grammar, logger = nil) click to toggle source

Creates a new parser from the given grammar. Messages are logged by default to STDOUT and the log level is WARN. Shift-reduce conflicts are reported at WARN and reduce-reduce conflicts at ERROR. You may pass in your own logger. Logging at DEBUG shows a lot of progress output.

# File lib/dhaka/parser/parser.rb, line 16
def initialize(grammar, logger = nil)
  @shift_actions  = Hash.new {|hash, state| hash[state] = ShiftAction.new(state)}
  @reduce_actions = Hash.new {|hash, production| hash[production] = ReduceAction.new(production)}
  @logger         = logger || default_logger
  @transitions    = Hash.new {|hash, state| hash[state] = {}}
  @grammar        = grammar
  @channels       = Hash.new {|hash, start_item| hash[start_item] = []}
  @states = Hash.new do |hash, kernel|
      closure, channels = grammar.closure(kernel)
      channels.each do |start_item, channel_set|
        @channels[start_item].concat channel_set.to_a
      end
      new_state    = ParserState.new(self, closure)
      hash[kernel] = new_state
      @logger.debug("Created #{new_state.unique_name}.")
      new_state.transition_items.each do |symbol, items|
        destination_kernel = ItemSet.new(items.collect{|item| item.next_item})
        destination_state  = hash[destination_kernel]
        items.each {|item| @channels[item] << grammar.passive_channel(item, destination_state.items[item.next_item])}
        @transitions[new_state][symbol] = destination_state
      end
      new_state
  end
  initialize_states
end

Public Instance Methods

compile_to_ruby_source_as(parser_class_name) click to toggle source

Returns the Ruby source of the generated parser compiled as parser_class_name. This can be written out to a file.

# File lib/dhaka/parser/parser.rb, line 43
def compile_to_ruby_source_as parser_class_name
  result = "class #{parser_class_name} < Dhaka::CompiledParser\n\n"
  result << "  self.grammar = #{grammar.name}\n\n"
  result << "  start_with #{start_state.id}\n\n"
  states.each do |state|
    result << "#{state.compile_to_ruby_source}\n\n"
  end
  result << "end"
  result
end
inspect() click to toggle source
# File lib/dhaka/parser/parser.rb, line 68
def inspect
  "<Dhaka::Parser grammar : #{grammar}>"
end
to_dot(options = {}) click to toggle source

Returns the dot representation of the parser. If :hide_lookaheads is set to true in the options hash, lookaheads are not written out to the parser states, which is helpful when there are dozens of lookahead symbols for every item in every state.

# File lib/dhaka/parser/parser.rb, line 57
def to_dot(options = {})
  Dot::Digraph.new(:fontsize => 10, :shape => :box, :size => 5) do |g|
    states.each do |state|
      g.node(state, :label => state.items.values.collect{|item| item.to_s(options)}.join("\n"))
      @transitions[state].each do |symbol, dest_state|
        g.edge(state, dest_state, :label => symbol.name)
      end
    end
  end.to_dot
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.