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



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

def productions
  @productions
end

#startObject (readonly)

Link to the start production.



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

def start
  @start
end

#traceObject (readonly)

nodoc Trace the execution of the algorithm.



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

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:



79
80
81
82
83
84
85
86
# File 'lib/sequitur/dynamic_grammar.rb', line 79

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:



46
47
48
49
50
51
# File 'lib/sequitur/dynamic_grammar.rb', line 46

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



72
73
74
# File 'lib/sequitur/dynamic_grammar.rb', line 72

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.



57
58
59
60
61
62
63
64
65
66
# File 'lib/sequitur/dynamic_grammar.rb', line 57

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)


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

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:



90
91
92
# File 'lib/sequitur/dynamic_grammar.rb', line 90

def visitor()
  return GrammarVisitor.new(self)
end