summaryrefslogtreecommitdiff
path: root/prototype/grammar.rb
blob: 05224f4593699c8702c7a126eb1bd10d6e491a90 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
module Grammar

class T
  attr_accessor :word

  def initialize word
    @word = word
  end

  def to_s
    @word
  end
end

class NT
  attr_accessor :symbol, :index

  def initialize symbol=nil, index=-1
    @symbol = symbol
    @index  = index
  end

  #FIXME? symbol should not contain [, ] or ",
  #       else we're in trouble
  def from_s s
    @symbol, @index = s.delete('[]').split ','
    @symbol.strip!
    @index = @index.to_i-1
  end

  def self.from_s s
    new = NT.new
    new.from_s s
    return new
  end

  #FIXME? indexed by 1
  def to_s
    return "[#{@symbol},#{@index+1}]" if @index>=0
    return "[#{@symbol}]"
  end
end

class Rule
  attr_accessor :lhs, :rhs, :target, :map, :f

  def initialize lhs=NT.new, rhs=[], target=[], map=[], f=SparseVector.new, arity=0
    @lhs    = lhs
    @rhs    = rhs
    @target = target
    @map    = map
    @f      = f
    @arity  = arity
  end

  def read_rhs_ s, make_meta=false
    a = []
    s.split.map { |x|
      x.strip!
      if x[0] == '[' && x[x.size-1] == ']'
        a << NT.from_s(x)
        if make_meta
          @map << a.last.index
          @arity += 1
        end
      else
        a << T.new(x)
      end
    }
    return a
  end

  def from_s s
    lhs, rhs, target, f = splitpipe s, 3
    @lhs    = NT.from_s lhs
    @rhs    = read_rhs_ rhs, true
    @target = read_rhs_ target
    @f      = (f ? SparseVector.from_kv(f, '=', ' ') : SparseVector.new)
  end

  def self.from_s s
    r = Rule.new
    r.from_s s
    return r
  end

  def to_s
    "#{@lhs.to_s} ||| #{@rhs.map { |x| x.to_s }.join ' '} ||| #{@target.map { |x| x.to_s }.join ' '}"
  end
end

class Grammar
  attr_accessor :rules, :start_nt, :start_t, :flat

  def initialize fn
    @rules = []; @start_nt = []; @start_t = []; @flat = []
    n = 0
    ReadFile.readlines_strip(fn).each_with_index { |s,i|
      STDERR.write '.'; STDERR.write " #{i+1}\n" if (i+1)%40==0
      n += 1
      @rules << Rule.from_s(s)
      if @rules.last.rhs.first.class == NT
        @start_nt << @rules.last
      else
        if rules.last.arity == 0
          @flat << @rules.last
        else
          @start_t << @rules.last
        end
      end
    }
    STDERR.write " #{n}\n"
  end

  def to_s
    @rules.map { |r| r.to_s }.join "\n"
  end

  def add_glue
    @rules.map { |r| r.lhs.symbol }.select { |s| s != 'S' }.uniq.each { |symbol|
      @rules << Rule.new(NT.new('S'), [NT.new(symbol, 0)], [NT.new(symbol, 0)], [0])
      @start_nt << @rules.last
      @rules << Rule.new(NT.new('S'), [NT.new('S', 0), NT.new('X', 1)], [NT.new('S', 0), NT.new('X', 1)], [0, 1])
      @start_nt << @rules.last
    }
  end

  def add_pass_through a
    return if !a
    a.each { |word|
      @rules << Rule.new(NT.new('X'), [T.new(word)], [T.new(word)])
      @flat << @rules.last
    }
  end
end

end #module