-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.rb
105 lines (83 loc) · 1.72 KB
/
utilities.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!macruby
# coding: utf-8
#
# utilities.rb
#
# Created by Erik Österlund on 1/14/10.
# Copyright 2010 Växjö Universitet. All rights reserved.
#
require 'parser'
module LALR
class Rule
attr_accessor :name, :productions
def initialize(name, productions)
@name, @productions = name, productions
end
def to_s
str = @name.to_s + " -> "
@productions.each do |p|
str << p.to_s + " "
end
str
end
def hash()
@name.hash + @productions.hash
end
def ==(arg)
@name == arg.name and @productions == arg.productions
end
def eql?(arg)
@name == arg.name and @productions == arg.productions
end
def epsilon?()
(productions.include?(Epsilon.instance)) or productions.length == 0
end
end
def shift(num)
Action.new :shift, num
end
def reduce(num)
Action.new :reduce, num
end
def accept()
Action.new :accept
end
class Action
attr_accessor :action, :number
def initialize(action, number = nil)
@number, @action = number, action
end
def exec(sender)
if @number
sender.send @action, @number
else
sender.send @action
end
end
def to_s
@action[0].to_s + (@number != nil ? @number.to_s : "")
end
def eql?(arg)
@action == arg.action and @number == arg.number
end
def ==(arg)
return false if arg == nil
@action == arg.action and @number == arg.number
end
def hash
@action.hash
end
end
class EOF
include Singleton
def to_s
"$"
end
end
class Epsilon
include Singleton
def to_s
"ε"
end
end
end