Class: Sequitur::DynamicGrammar
- Inherits:
-
Object
- Object
- Sequitur::DynamicGrammar
- 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
Instance Attribute Summary collapse
-
#productions ⇒ Object
readonly
The set of production rules of the grammar.
-
#start ⇒ Object
readonly
Link to the start production.
-
#trace ⇒ Object
nodoc Trace the execution of the algorithm.
Instance Method Summary collapse
-
#accept(aVisitor) ⇒ Object
Part of the 'visitee' role in the Visitor design pattern.
-
#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) ⇒ Production
Remove a production with given index from the grammar.
-
#to_string ⇒ String
Emit a text representation of the grammar.
-
#visitor ⇒ GrammarVisitor
Factory method.
Constructor Details
#initialize ⇒ DynamicGrammar
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
#productions ⇒ Object (readonly)
The set of production rules of the grammar
20 21 22 |
# File 'lib/sequitur/dynamic_grammar.rb', line 20 def productions @productions end |
#start ⇒ Object (readonly)
Link to the start production.
17 18 19 |
# File 'lib/sequitur/dynamic_grammar.rb', line 17 def start @start end |
#trace ⇒ Object
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
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.
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.
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
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_string ⇒ String
Emit a text representation of the grammar. Each production rule is emitted per line.
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 |
#visitor ⇒ GrammarVisitor
Factory method. Returns a visitor for this grammar.
85 86 87 |
# File 'lib/sequitur/dynamic_grammar.rb', line 85 def visitor return GrammarVisitor.new(self) end |