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
readonly
nodoc Trace the execution of the algorithm.
Instance Method Summary collapse
-
#add_production(aProduction) ⇒ Object
Add a given production to the grammar.
-
#add_token(aToken) ⇒ Object
Add the given token to the grammar.
-
#initialize ⇒ DynamicGrammar
constructor
Constructor.
-
#remove_production(anIndex) ⇒ Object
Remove a production from the grammar.
-
#to_string ⇒ Object
Emit a text representation of the grammar.
-
#visitor ⇒ Object
Factory method.
Constructor Details
#initialize ⇒ DynamicGrammar
Constructor. Build a grammar with one empty rule as start/root rule
19 20 21 22 23 |
# File 'lib/sequitur/dynamic_grammar.rb', line 19 def initialize() @root = Production.new @productions = [ root ] @trace = false end |
Instance Attribute Details
#productions ⇒ Object (readonly)
The set of production rules of the grammar
11 12 13 |
# File 'lib/sequitur/dynamic_grammar.rb', line 11 def productions @productions end |
#root ⇒ Object (readonly)
Link to the root - start - production.
8 9 10 |
# File 'lib/sequitur/dynamic_grammar.rb', line 8 def root @root end |
#trace ⇒ Object (readonly)
nodoc Trace the execution of the algorithm.
14 15 16 |
# File 'lib/sequitur/dynamic_grammar.rb', line 14 def trace @trace end |
Instance Method Details
#add_production(aProduction) ⇒ Object
Add a given production to the grammar.
36 37 38 39 40 41 42 |
# File 'lib/sequitur/dynamic_grammar.rb', line 36 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.
59 60 61 |
# File 'lib/sequitur/dynamic_grammar.rb', line 59 def add_token(aToken) append_symbol_to(root, aToken) end |
#remove_production(anIndex) ⇒ Object
Remove a production from the grammar
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/sequitur/dynamic_grammar.rb', line 46 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_string ⇒ Object
Emit a text representation of the grammar. Each production rule is emitted per line.
29 30 31 32 |
# File 'lib/sequitur/dynamic_grammar.rb', line 29 def to_string() rule_text = productions.map(&:to_string).join("\n") return rule_text end |
#visitor ⇒ Object
Factory method. Returns a visitor for this grammar.
72 73 74 |
# File 'lib/sequitur/dynamic_grammar.rb', line 72 def visitor() return GrammarVisitor.new(self) end |