From 1651cfb83e0d95f9ada0b9acae6ede84e605720b Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Thu, 12 Jun 2014 16:25:33 +0200 Subject: save some memory --- grammar.rb | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'grammar.rb') diff --git a/grammar.rb b/grammar.rb index a8ec07c..6c95de2 100644 --- a/grammar.rb +++ b/grammar.rb @@ -41,11 +41,11 @@ end class Rule attr_accessor :lhs, :rhs, :target, :map, :f - def initialize lhs=nil, rhs=nil, target=nil, map=nil, f=SparseVector.new + def initialize lhs=NT.new, rhs=[], target=[], map=[], f=SparseVector.new @lhs = lhs @rhs = rhs @target = target - @map = (map ? map : []) + @map = map @f = f @arity_ = nil end @@ -59,30 +59,30 @@ class Rule return @arity_ end - def read_right_ s, fill_map=false - _ = [] + def read_right_ s, create_map=false + a = [] s.split.each { |x| x.strip! - if x[0]=='[' && x[x.size-1] == ']' - _ << NT.from_s(x) - @map << _.last.index if fill_map + if x[0] == '[' && x[x.size-1] == ']' + a << NT.from_s(x) + @map << a.last.index if create_map else - _ << T.new(x) + a << T.new(x) end } - return _ + return a end def from_s s lhs, rhs, target, f = splitpipe s, 3 - @lhs = NT.from_s lhs - @rhs = read_right_ rhs + @lhs = NT.from_s lhs + @rhs = read_right_ rhs @target = read_right_ target, true - @f = (f ? SparseVector.from_kv(f) : nil) + @f = (f ? SparseVector.from_kv(f) : nil) end - def self.from_s_x s - r = self.new + def self.from_s s + r = Rule.new r.from_s s return r end @@ -110,9 +110,7 @@ class Grammar end def to_s - s = '' - @rules.each { |r| s += r.to_s+"\n" } - return s + @rules.map { |r| r.to_s }.join "\n" end def add_glue_rules -- cgit v1.2.3