Class: RLSM::RegExp

Inherits:
Object
  • Object
show all
Defined in:
lib/rlsm/regexp.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description) ⇒ RegExp

Creates a new RegExp. The description is a string consiting of latin letters, numbers and the following special characters

  1. (, ) for grouping subexpressions

  2. | for union of regular expressions

  3. * for the Kleene-Closure of a regular expression

  4. @ the empty word.

Whitspaces will be ignored and the empty string represents the empty language.



30
31
32
33
# File 'lib/rlsm/regexp.rb', line 30

def initialize(description)
  @parse_tree = RLSM::RE::Parser[ description ]
  @string = @parse_tree.to_s
end

Instance Attribute Details

#parse_treeObject (readonly)

Returns the value of attribute parse_tree.



35
36
37
# File 'lib/rlsm/regexp.rb', line 35

def parse_tree
  @parse_tree
end

#stringObject (readonly)

Returns the value of attribute string.



35
36
37
# File 'lib/rlsm/regexp.rb', line 35

def string
  @string
end

Class Method Details

.[](description) ⇒ Object

Synonym for new.



19
20
21
# File 'lib/rlsm/regexp.rb', line 19

def self.[](description)
  new(description)
end

.empty_setObject

Returns a RegExp which represents the empty language.



14
15
16
# File 'lib/rlsm/regexp.rb', line 14

def self.empty_set
  new ''
end

.empty_wordObject

Returns a RegExp which is the empty word.



9
10
11
# File 'lib/rlsm/regexp.rb', line 9

def self.empty_word
  new RLSM::RE::ParserHelpers::EmptyWordSymbol
end

Instance Method Details

#+(other) ⇒ Object

Concatenate self with other.



38
39
40
# File 'lib/rlsm/regexp.rb', line 38

def +(other)
  RLSM::RegExp.new "(#@string)(#{other.string})"
end

#==(other) ⇒ Object

Checks if self is equal to other, i.e. they represent the same language.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rlsm/regexp.rb', line 68

def ==(other)
  return true if @string == other.string

  first = @parse_tree.first.map { |pos| pos.to_s }.uniq 
  other_first = other.parse_tree.first.map { |pos| pos.to_s }.uniq
  return false if first != other_first

  last = @parse_tree.last.map { |pos| pos.to_s }.uniq 
  other_last = other.parse_tree.last.map { |pos| pos.to_s }.uniq
  return false if last != other_last

  to_dfa =~ other.to_dfa
end

#starObject

Returns the Kleene closure of self.



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

def star
  RLSM::RegExp.new "(#@string)*"
end

#to_dfaObject

Calculates a minimal DFA which represents the same languge as self.



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

def to_dfa
  RLSM::DFA.new(subset_construction).minimize!
end

#to_monoidObject

Calculates the syntactic monoid of the represented language.



63
64
65
# File 'lib/rlsm/regexp.rb', line 63

def to_monoid
  to_dfa.to_monoid
end

#to_regexpObject

Simply returns self.



58
59
60
# File 'lib/rlsm/regexp.rb', line 58

def to_regexp
  self
end

#|(other) ⇒ Object

Returns the union of self and other



43
44
45
# File 'lib/rlsm/regexp.rb', line 43

def |(other)
  RLSM::RegExp.new "#@string|#{other.string}"
end