Class: Sequitur::DynamicGrammar

Inherits:
Object
  • Object
show all
Defined in:
lib/sequitur/dynamic_grammar.rb

Direct Known Subclasses

SequiturGrammar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDynamicGrammar

Constructor. Build a grammar with one empty rule as start/root rule



18
19
20
21
22
# File 'lib/sequitur/dynamic_grammar.rb', line 18

def initialize()
  @root = Production.new
  @productions = [ root ]
  @trace = false
end

Instance Attribute Details

#productionsObject (readonly)

The set of production rules of the grammar



10
11
12
# File 'lib/sequitur/dynamic_grammar.rb', line 10

def productions
  @productions
end

#rootObject (readonly)

Link to the root - start - production.



7
8
9
# File 'lib/sequitur/dynamic_grammar.rb', line 7

def root
  @root
end

#traceObject

nodoc Trace the execution of the algorithm.



13
14
15
# File 'lib/sequitur/dynamic_grammar.rb', line 13

def trace
  @trace
end

Instance Method Details

#add_production(aProduction) ⇒ Object

Add a production to the grammar.



35
36
37
38
39
40
41
# File 'lib/sequitur/dynamic_grammar.rb', line 35

def add_production(aProduction)
  # TODO: remove output
  puts "Adding #{aProduction.object_id}" if trace
  puts aProduction.to_string if trace
  check_rhs_of(aProduction) # TODO: configurable check
  productions << aProduction
end

#add_token(aToken) ⇒ Object

Add the given token to the grammar.



60
61
62
# File 'lib/sequitur/dynamic_grammar.rb', line 60

def add_token(aToken)
  append_symbol_to(root, aToken)
end

#delete_production(anIndex) ⇒ Object

Remove a production from the grammar



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sequitur/dynamic_grammar.rb', line 45

def delete_production(anIndex)
  puts "Before production removal #{productions[anIndex].object_id}" if trace
  puts to_string if trace
  prod = productions.delete_at(anIndex)
  # TODO: remove output
  puts prod.to_string if trace
  prod.clear_rhs

  check_backrefs  # TODO: configurable check

  return prod
end

#to_stringObject

Emit a text representation of the grammar. Each production rule is emitted per line.



28
29
30
31
# File 'lib/sequitur/dynamic_grammar.rb', line 28

def to_string()
  rule_text = productions.map(&:to_string).join("\n")
  return rule_text
end