Class: Dhaka::Parser

Inherits:
Object
  • Object
show all
Includes:
ParserMethods
Defined in:
lib/parser/parser.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ParserMethods

#parse

Constructor Details

#initialize(grammar, logger = nil) ⇒ Parser

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.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/parser/parser.rb', line 20

def initialize(grammar, logger = nil)
  if logger
    @logger = logger
  else
    @logger = Logger.new(STDOUT)
    @logger.level = Logger::WARN
  end
  @transitions = Hash.new {|hash, state| hash[state] = {}}
  @grammar = grammar
  @channels = []
  @states = Hash.new do |hash, kernel|
      channels, closure = @grammar.closure(kernel)
      @channels += channels.to_a
      new_state = ParserState.new(self, closure)
      hash[kernel] = new_state
      @logger.debug("Created #{new_state}.")
      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 << @grammar.passive_channel(item, destination_state.items[item.next_item]) }
        @transitions[new_state][symbol] = destination_state
      end
      new_state
  end
  initialize_states
end

Instance Attribute Details

#grammarObject (readonly)

Returns the value of attribute grammar.



15
16
17
# File 'lib/parser/parser.rb', line 15

def grammar
  @grammar
end

Instance Method Details

#compile_to_ruby_source_as(parser_class_name) ⇒ Object

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



48
49
50
51
52
53
54
55
56
57
# File 'lib/parser/parser.rb', line 48

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

#to_dot(options = {}) ⇒ Object

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.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/parser/parser.rb', line 62

def to_dot(options = {})
  result = ["digraph x {", "node [fontsize=\"10\" shape=box size=\"5\"]"]
  result += states.collect { |state| state.to_dot(options) }
  states.each { |state| 
      @transitions[state].each { |symbol, dest_state| 
          result << "#{state.dot_name} -> #{dest_state.dot_name} [label=\"#{symbol.name}\"]"
         }
     }
  result << ['}']
  result.join("\n")
end