class Sequitur::DynamicGrammar

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)

Attributes

productions[R]

@return [Array<Sequitur::Production>] The set of production rules of the grammar

start[R]

@return [Sequitur::Production] Link to the start production.

trace[RW]

@return [TrueClass, FalseClass] Trace the execution of the algorithm.

Public Class Methods

new() click to toggle source

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

# File lib/sequitur/dynamic_grammar.rb, line 27
def initialize
  @start = Production.new
  @productions = [start]
  @trace = false
end

Public Instance Methods

accept(aVisitor) click to toggle source

Part of the ‘visitee’ role in the Visitor design pattern.

A visitee is expected to accept the visit from a visitor object

@param aVisitor [Sequitur::GrammarVisitor] the visitor object

# 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) click to toggle source

Add a given production to the grammar. @param aProduction [Sequitur::Production] @return [Array<Sequitur::Production>]

# 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) click to toggle source

Add the given token to the grammar.

Append the token to the rhs of the start/start rule.

@param aToken [Object] input token to add

# File lib/sequitur/dynamic_grammar.rb, line 67
def add_token(aToken)
  append_symbol_to(start, aToken)
end
remove_production(anIndex) click to toggle source

Remove a production with given index from the grammar @param anIndex [Integer] @return [Sequitur::Production] the production removed from the grammar.

# 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

  prod
end
to_string() click to toggle source

Emit a text representation of the grammar. Each production rule is emitted per line. @return [String]

# File lib/sequitur/dynamic_grammar.rb, line 36
def to_string
  productions.map(&:to_string).join("\n")
end
visitor() click to toggle source

Factory method. Returns a visitor for this grammar. @return [Sequitur::GrammarVisitor]

# File lib/sequitur/dynamic_grammar.rb, line 85
def visitor
  GrammarVisitor.new(self)
end

Protected Instance Methods

append_symbol_to(aProduction, aSymbol) click to toggle source

Append a given symbol to the rhs of passed production. @param aProduction [Production] @param aSymbol [Object]

# File lib/sequitur/dynamic_grammar.rb, line 94
def append_symbol_to(aProduction, aSymbol)
  aProduction.append_symbol(aSymbol)
end