Class: Mapper::Grammar

Inherits:
Hash
  • Object
show all
Defined in:
../lib/grammar.rb

Overview

The internal representation of the language syntax, used for the genotype->phenotype mapping of Grammatical Evolution. The genotype is basically the array of fixnums, the phenotype is the source code in some programming language, or, in general, any construct following the syntax. See www.grammatical-evolution.org/

A Grammar instance is created by the parser (e.g. Abnf::Parser) and passed as a main constructor argument of objects subclassing Mapper::Base. The instance of the Grammar class is basically a Hash of { symbol1 => Rule1, symbol2 => Rule2 … } assignments. each_pair of the hash maps the nonterminal symbol[i] to RuleAlt[i]. The Mapper::RuleAlt object is the array of all rule alternatives expanding the given symbol).

The Grammar has to define a start_symbol for successful mapping.

Direct Known Subclasses

Abnf::FileLoader

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Grammar) initialize(src = nil, start = nil)

Initialize a Grammar. The src argument is the instance of any Hash subclass (for deep copying), the start argument is the start symbol definition.



29
30
31
32
33
34
35
36
37
38
# File '../lib/grammar.rb', line 29

def initialize( src=nil, start=nil )
  super()
  update src unless src.nil?

  if start.nil? 
    @start_symbol = src.start_symbol if src.kind_of? Grammar
  else
    @start_symbol = start
  end
end

Instance Attribute Details

- (Object) start_symbol

Start symbol of the genotype-phenotype mapping.



53
54
55
# File '../lib/grammar.rb', line 53

def start_symbol
  @start_symbol
end

Instance Method Details

- (Object) deep_copy

Return a deep copy of the instance.



41
42
43
44
45
# File '../lib/grammar.rb', line 41

def deep_copy
  copy = Grammar.new( nil, @start_symbol )
  each_pair {|symb, alt| copy[symb] = alt.deep_copy }
  copy 
end

- (Object) symbols

Return the array of all defined the symbols, sorted.



48
49
50
# File '../lib/grammar.rb', line 48

def symbols
  keys.sort
end