-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar_symbol.rb
43 lines (37 loc) · 986 Bytes
/
grammar_symbol.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class GrammarSymbol
# represents a symbol in a grammar
# type - :terminal / :nonterminal
# value - actual value of the symbol
# character (terminal)
# number (non-terminal)
attr_reader :type, :value
def initialize(type, value)
if [:terminal, :nonterminal].include? type
@type = type
else
raise "#{type} is not a legal symbol type"
end
if value.class == String
@value = value
else
raise "NT value needs to be of type String"
end
end
def to_s
case @type
when :terminal
"#{@value}"
when :nonterminal
"NT#{@value}"
end
end
def eql?(other)
other.instance_of?(self.class) && other.value == @value && other.type == @type
end
def hash
return @value.hash - 7 * @type.hash
end
def copy
return GrammarSymbol.new(@type, @value)
end
end