Class: Sequitur::DynamicGrammar
- Inherits:
-
Object
- Object
- Sequitur::DynamicGrammar
- Defined in:
- lib/sequitur/dynamic_grammar.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#productions ⇒ Object
readonly
The set of production rules of the grammar.
-
#root ⇒ Object
readonly
Link to the root - start - production.
-
#trace ⇒ Object
nodoc Trace the execution of the algorithm.
Instance Method Summary collapse
-
#add_production(aProduction) ⇒ Object
Add a production to the grammar.
-
#add_token(aToken) ⇒ Object
Add the given token to the grammar.
-
#delete_production(anIndex) ⇒ Object
Remove a production from the grammar.
-
#initialize ⇒ DynamicGrammar
constructor
Constructor.
-
#to_string ⇒ Object
Emit a text representation of the grammar.
Constructor Details
#initialize ⇒ DynamicGrammar
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
#productions ⇒ Object (readonly)
The set of production rules of the grammar
10 11 12 |
# File 'lib/sequitur/dynamic_grammar.rb', line 10 def productions @productions end |
#root ⇒ Object (readonly)
Link to the root - start - production.
7 8 9 |
# File 'lib/sequitur/dynamic_grammar.rb', line 7 def root @root end |
#trace ⇒ Object
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_string ⇒ Object
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 |