Class: Sequitur::SequiturGrammar
- Inherits:
-
DynamicGrammar
- Object
- DynamicGrammar
- Sequitur::SequiturGrammar
- Defined in:
- lib/sequitur/sequitur_grammar.rb
Instance Attribute Summary collapse
-
#digrams ⇒ Object
readonly
A hash with pairs of the form: digram key => digram.
-
#parsed ⇒ Object
readonly
The input.
Attributes inherited from DynamicGrammar
Instance Method Summary collapse
-
#add_token(aToken) ⇒ Object
Add the given token to the grammar.
-
#check_unicity ⇒ Object
Check the invariant: Every digram appearing in a rhs must occur at most once in the grammar.
-
#initialize(anEnum) ⇒ SequiturGrammar
constructor
Constructor.
Methods inherited from DynamicGrammar
Constructor Details
#initialize(anEnum) ⇒ SequiturGrammar
Constructor. Build the grammar from an enumerator of tokens
13 14 15 16 17 18 19 20 21 |
# File 'lib/sequitur/sequitur_grammar.rb', line 13 def initialize(anEnum) super() # Make start production compliant with utility rule 2.times { root.incr_refcount } @digrams = {} @parsed = [] anEnum.each { |a_token| add_token(a_token) } end |
Instance Attribute Details
#digrams ⇒ Object (readonly)
A hash with pairs of the form: digram key => digram
7 8 9 |
# File 'lib/sequitur/sequitur_grammar.rb', line 7 def digrams @digrams end |
#parsed ⇒ Object (readonly)
The input
10 11 12 |
# File 'lib/sequitur/sequitur_grammar.rb', line 10 def parsed @parsed end |
Instance Method Details
#add_token(aToken) ⇒ Object
Add the given token to the grammar.
26 27 28 29 |
# File 'lib/sequitur/sequitur_grammar.rb', line 26 def add_token(aToken) parsed << aToken super end |
#check_unicity ⇒ Object
Check the invariant: Every digram appearing in a rhs must occur at most once in the grammar.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/sequitur/sequitur_grammar.rb', line 33 def check_unicity() all_digrams = {} productions.each do |a_prod| prod_digrams = a_prod.digrams prod_digrams.each_with_index do |a_digram, index| next if index && a_digram == a_prod.digrams[index - 1] if all_digrams.include? a_digram.key msg = "Digram #{a_digram.symbols} occurs twice!" colliding = all_digrams[a_digram.key] msg << "\nOnce in production #{colliding.production.object_id}" msg << "\nSecond in production #{a_prod.object_id}" msg << "\n#{to_string}" fail StandardError, msg unless colliding == a_prod else all_digrams[a_digram.key] = a_digram end end end end |