Class: Sequitur::DynamicGrammar

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

Overview

A dynamic grammar is a context-free grammar that can be built incrementally. Formally, a grammar has: One start production Zero or more other productions Each production has a rhs that is a sequence of grammar symbols. Grammar symbols are categorized into -terminal symbols (i.e. String, Ruby Symbol,...) -non-terminal symbols (i.e. ProductionRef)

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/start rule.



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

def initialize
  @start = Production.new
  @productions = [start]
  @trace = false
end

Instance Attribute Details

#productionsObject (readonly)

The set of production rules of the grammar



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

def productions
  @productions
end

#startObject (readonly)

Link to the start production.



17
18
19
# File 'lib/sequitur/dynamic_grammar.rb', line 17

def start
  @start
end

#traceObject

nodoc Trace the execution of the algorithm.



23
24
25
# File 'lib/sequitur/dynamic_grammar.rb', line 23

def trace
  @trace
end

Instance Method Details

#accept(aVisitor) ⇒ Object

Part of the 'visitee' role in the Visitor design pattern. A visitee is expected to accept the visit from a visitor object

Parameters:



74
75
76
77
78
79
80
81
# File 'lib/sequitur/dynamic_grammar.rb', line 74

def accept(aVisitor)
  aVisitor.start_visit_grammar(self)

  # Let's proceed with the visit of productions
  productions.each { |prod| prod.accept(aVisitor) }

  aVisitor.end_visit_grammar(self)
end

#add_production(aProduction) ⇒ Object

Add a given production to the grammar.

Parameters:



43
44
45
46
47
48
# File 'lib/sequitur/dynamic_grammar.rb', line 43

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

#add_token(aToken) ⇒ Object

Add the given token to the grammar. Append the token to the rhs of the start/start rule.

Parameters:

  • aToken (Object)

    input token to add



67
68
69
# File 'lib/sequitur/dynamic_grammar.rb', line 67

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

#remove_production(anIndex) ⇒ Production

Remove a production with given index from the grammar

Parameters:

  • anIndex (Fixnum)

Returns:

  • (Production)

    the production removed from the grammar.



53
54
55
56
57
58
59
60
61
62
# File 'lib/sequitur/dynamic_grammar.rb', line 53

def remove_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('Removed: ' + prod.to_string) if trace
  prod.clear_rhs

  return prod
end

#to_stringString

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

Returns:

  • (String)


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

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

#visitorGrammarVisitor

Factory method. Returns a visitor for this grammar.

Returns:



85
86
87
# File 'lib/sequitur/dynamic_grammar.rb', line 85

def visitor
  return GrammarVisitor.new(self)
end