Class: Racc::LogFileGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/racc/logfilegenerator.rb

Instance Method Summary collapse

Constructor Details

#initialize(states, debug_flags = DebugFlags.new) ⇒ LogFileGenerator

Returns a new instance of LogFileGenerator.


16
17
18
19
20
# File 'lib/racc/logfilegenerator.rb', line 16

def initialize(states, debug_flags = DebugFlags.new)
  @states = states
  @grammar = states.grammar
  @debug_flags = debug_flags
end

Instance Method Details

#action_out(f, state) ⇒ Object


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
# File 'lib/racc/logfilegenerator.rb', line 91

def action_out(f, state)
  sr = state.srconf && state.srconf.dup
  rr = state.rrconf && state.rrconf.dup
  acts = state.action
  keys = acts.keys
  keys.sort! {|a,b| a.ident <=> b.ident }

  [ Shift, Reduce, Error, Accept ].each do |klass|
    keys.delete_if do |tok|
      act = acts[tok]
      if act.kind_of?(klass)
        outact f, tok, act
        if sr and c = sr.delete(tok)
          outsrconf f, c
        end
        if rr and c = rr.delete(tok)
          outrrconf f, c
        end

        true
      else
        false
      end
    end
  end
  sr.each {|tok, c| outsrconf f, c } if sr
  rr.each {|tok, c| outrrconf f, c } if rr

  act = state.defact
  if not act.kind_of?(Error) or @debug_flags.any?
    outact f, '$default', act
  end

  f.puts
  state.goto_table.each do |t, st|
    if t.nonterminal?
      f.printf "  %-12s  go to state %d\n", t.to_s, st.ident
    end
  end
end

#outact(f, t, act) ⇒ Object


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/racc/logfilegenerator.rb', line 132

def outact(f, t, act)
  case act
  when Shift
    f.printf "  %-12s  shift, and go to state %d\n", 
             t.to_s, act.goto_id
  when Reduce
    f.printf "  %-12s  reduce using rule %d (%s)\n",
             t.to_s, act.ruleid, act.rule.target.to_s
  when Accept
    f.printf "  %-12s  accept\n", t.to_s
  when Error
    f.printf "  %-12s  error\n", t.to_s
  else
    raise "racc: fatal: wrong act for outact: act=#{act}(#{act.class})"
  end
end

#output(out) ⇒ Object


22
23
24
25
26
27
28
# File 'lib/racc/logfilegenerator.rb', line 22

def output(out)
  output_conflict out; out.puts
  output_useless  out; out.puts
  output_rule     out; out.puts
  output_token    out; out.puts
  output_state    out
end

#output_conflict(out) ⇒ Object

Warnings


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/racc/logfilegenerator.rb', line 34

def output_conflict(out)
  @states.each do |state|
    if state.srconf
      out.printf "state %d contains %d shift/reduce conflicts\n",
                 state.stateid, state.srconf.size
    end
    if state.rrconf
      out.printf "state %d contains %d reduce/reduce conflicts\n",
                 state.stateid, state.rrconf.size
    end
  end
end

#output_rule(out) ⇒ Object

Rules


169
170
171
172
173
174
175
176
177
# File 'lib/racc/logfilegenerator.rb', line 169

def output_rule(out)
  out.print "-------- Grammar --------\n\n"
  @grammar.each do |rl|
    if @debug_flags.any? or rl.ident != 0
      out.printf "rule %d %s: %s\n",
                 rl.ident, rl.target.to_s, rl.symbols.join(' ')
    end
  end
end

#output_state(out) ⇒ Object

States


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/racc/logfilegenerator.rb', line 65

def output_state(out)
  out << "--------- State ---------\n"

  showall = @debug_flags.la || @debug_flags.state
  @states.each do |state|
    out << "\nstate #{state.ident}\n\n"

    (showall ? state.closure : state.core).each do |ptr|
      pointer_out(out, ptr) if ptr.rule.ident != 0 or showall
    end
    out << "\n"

    action_out out, state
  end
end

#output_token(out) ⇒ Object

Tokens


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/racc/logfilegenerator.rb', line 183

def output_token(out)
  out.print "------- Symbols -------\n\n"

  out.print "**Nonterminals, with rules where they appear\n\n"
  @grammar.each_nonterminal do |t|
    tmp = <<SRC
  %s (%d)
on right: %s
on left : %s
SRC
    out.printf tmp, t.to_s, t.ident,
               symbol_locations(t.locate).join(' '),
               symbol_locations(t.heads).join(' ')
  end

  out.print "\n**Terminals, with rules where they appear\n\n"
  @grammar.each_terminal do |t|
    out.printf "  %s (%d) %s\n",
               t.to_s, t.ident, symbol_locations(t.locate).join(' ')
  end
end

#output_useless(out) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/racc/logfilegenerator.rb', line 47

def output_useless(out)
  @grammar.each do |rl|
    if rl.useless?
      out.printf "rule %d (%s) never reduced\n",
                 rl.ident, rl.target.to_s
    end
  end
  @grammar.each_nonterminal do |t|
    if t.useless?
      out.printf "useless nonterminal %s\n", t.to_s
    end
  end
end

#outrrconf(f, confs) ⇒ Object


157
158
159
160
161
162
163
# File 'lib/racc/logfilegenerator.rb', line 157

def outrrconf(f, confs)
  confs.each do |c|
    r = c.low_prec
    f.printf "  %-12s  [reduce using rule %d (%s)]\n",
             c.token.to_s, r.ident, r.target.to_s
  end
end

#outsrconf(f, confs) ⇒ Object


149
150
151
152
153
154
155
# File 'lib/racc/logfilegenerator.rb', line 149

def outsrconf(f, confs)
  confs.each do |c|
    r = c.reduce
    f.printf "  %-12s  [reduce using rule %d (%s)]\n",
             c.shift.to_s, r.ident, r.target.to_s
  end
end

#pointer_out(out, ptr) ⇒ Object


81
82
83
84
85
86
87
88
89
# File 'lib/racc/logfilegenerator.rb', line 81

def pointer_out(out, ptr)
  buf = sprintf("%4d) %s :", ptr.rule.ident, ptr.rule.target.to_s)
  ptr.rule.symbols.each_with_index do |tok, idx|
    buf << ' _' if idx == ptr.index
    buf << ' ' << tok.to_s
  end
  buf << ' _' if ptr.reduce?
  out.puts buf
end

#symbol_locations(locs) ⇒ Object


205
206
207
# File 'lib/racc/logfilegenerator.rb', line 205

def symbol_locations(locs)
  locs.map {|loc| loc.rule.ident }.reject {|n| n == 0 }.uniq
end