Skip to content

Commit 5abfb22

Browse files
committed
Fix chain sorting predicate; extent debug log; extend tests
1 parent 07014ce commit 5abfb22

File tree

8 files changed

+154
-46
lines changed

8 files changed

+154
-46
lines changed

LR_parser.rb

+27-14
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
require './fsm.rb'
33
require './expression.rb'
44

5-
require 'colorize' if ARGV.member?("debug")
6-
5+
require 'colorize' if $DEBUG_project > 0
6+
require 'pry' if $DEBUG_project > 0
77

88
class Grammar
99

@@ -150,7 +150,7 @@ def generate_FSM(grammar, axiom)
150150
add_next_vertex(grammar, fsm, rule, vertex_by_set, 0);
151151
end
152152

153-
if @Debug
153+
if @Debug >= 2
154154
vertex_by_set.each{|x|
155155
p x
156156
}
@@ -160,7 +160,7 @@ def generate_FSM(grammar, axiom)
160160

161161
class Nonterm
162162
def initialize(sym = nil, obj = nil, is_reducible = false)
163-
@Debug = true & false;
163+
@Debug = 0;
164164
@symbol = sym;
165165
@object = obj;
166166
@is_reducible = is_reducible;
@@ -246,11 +246,11 @@ def parse(fsm, expr, grammar)
246246
end
247247
end
248248

249-
if @Debug then
250-
puts "is_acceptable: " + is_acceptable .to_s;
249+
if @Debug >= 2 then
250+
puts "expr: " + expr .to_s;
251251
puts "signal: " + signal .to_s;
252+
puts "is_acceptable: " + is_acceptable .to_s;
252253
puts "expr.last: " + expr.last .to_s;
253-
puts "expr: " + expr .to_s;
254254
puts "state_stack: " + state_stack .to_s;
255255
puts "current_state " + fsm.current_vertex.to_s;
256256
puts "processed: " + processed .to_s;
@@ -269,9 +269,9 @@ def parse(fsm, expr, grammar)
269269
to_production = fsm.get_value[0];
270270
r_s = rule.size # Rule_Size
271271

272-
if @Debug then
273-
puts "apply rule: " + to_production.to_s + "->" + rule.to_s;
274-
puts "processed[top]: " + to_production.to_s + "->" + processed[-r_s..-1] .to_s;
272+
if @Debug >= 2 then
273+
puts "apply rule: " + to_production.to_s + " -> " + rule.to_s;
274+
puts "processed[top]: " + to_production.to_s + " -> " + processed[-r_s..-1] .to_s;
275275
end
276276

277277
is_appropriate = true;
@@ -303,7 +303,7 @@ def parse(fsm, expr, grammar)
303303

304304
end
305305
end
306-
p "-------" if @Debug
306+
p "-------" if @Debug >= 2
307307
end
308308
return expr.last;
309309
end
@@ -316,7 +316,7 @@ def tree_to_metaexpression(tree, meta)
316316

317317
class LR_parser
318318
def initialize(type)
319-
@Debug = ARGV.member?("debug");
319+
@Debug = $DEBUG_project
320320

321321
TokenTemplate.set_ltype type
322322

@@ -332,7 +332,7 @@ def initialize(type)
332332
@grammar_machina = generate_FSM(@@grammar, [:main, [:expr], 0]);
333333
@grammar_machina.set_current(0);
334334

335-
if @Debug then
335+
if @Debug >= 2 then
336336
@grammar_machina.vertex_set.each{
337337
|x|
338338
print x[0]
@@ -357,12 +357,25 @@ def parse_meta(input_string, erase_ins = true)
357357
e = Expression.new(input_string);
358358
e.erase_insignificant_tokens! if erase_ins
359359

360+
360361
expr = e.tokens.clone
361362
expr += [Token.new(:eof, :eof, true, 0, 0, 0)]
362363

364+
if @Debug > 0
365+
print "expr: "
366+
p expr
367+
end
368+
363369
metaexpr = MetaExpression.new();
364-
parse(@grammar_machina, expr, @@grammar).make_metaexpr(metaexpr);
365370

371+
ast = parse(@grammar_machina, expr, @@grammar);
372+
373+
#binding.pry if $DEBUG_project > 0
374+
ast.print_tree if @Debug > 1
375+
376+
ast.make_metaexpr(metaexpr);
377+
378+
366379
return metaexpr;
367380
end
368381
end

align.rb

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def test_aligment(input_strings, type)
5151

5252
# no indent is observed
5353
def align_group(input_strings, type)
54+
p "start align_group" if $DEBUG_project > 0
5455
p = LR_parser.new(type)
5556
metas = []
5657
input_strings.each { |str| metas.push(p.parse_meta(str)); }
@@ -60,6 +61,8 @@ def align_group(input_strings, type)
6061
for i in 0..metas.size-2 do
6162
pairs_array.push(matcher.generate_pairs(metas[i].value, metas[i+1].value));
6263
end
64+
65+
metas.each{|x| x.print_tree } if $DEBUG_project > 1
6366
r = Recreator.new(type)
6467
chains = r.generate_chains(pairs_array);
6568
lines = r.multiline_reconstruction(metas, chains)
@@ -96,6 +99,7 @@ def align(input_strings, type)
9699
end
97100
end
98101

102+
puts "groups.size: #{groups.size}"if $DEBUG_project > 0
99103
result = [];
100104
groups.each_with_index do |group, i|
101105
if group.size > 1 then

expression.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
require "./staff.rb"
3-
require 'colorize' if ARGV.member?("debug")
3+
require 'colorize' if $DEBUG_project > 0
44

55

66
class MetaExpression
@@ -25,7 +25,7 @@ def print_tree(n = 0)
2525
@value.each do |token|
2626
if token.class == Token then
2727
print " "*n*4;
28-
p token.value;
28+
p token;
2929
else
3030
token.print_tree(n+1)
3131
end

pipe_launch.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
require 'optparse'
2+
3+
$DEBUG_project = 0;
4+
5+
options = {}
6+
OptionParser.new do |opt|
7+
opt.on('--debug [LEVEL]') { |o| $DEBUG_project = (o || 1).to_i }
8+
end.parse!
9+
110
require "./align.rb"
211

3-
DEBUG_pipe = false
412

13+
DEBUG_pipe = false
514
type = :default;
615
case ARGV[0]
716
when "C99"

recreator.rb

+91-21
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,27 @@
44

55
class Range
66
def intersection(other)
7-
return nil if (self.max < other.begin or other.max < self.begin)
8-
[self.begin, other.begin].max..[self.max, other.max].min
7+
return (1..0) if (self.last < other.begin or other.last < self.begin)
8+
[self.begin, other.begin].last..[self.last, other.last].min
99
end
10+
11+
def shift_left(indx)
12+
if indx.class == Fixnum then
13+
return (self.begin - indx)..(self.last - indx)
14+
end
15+
end
16+
17+
def shift_right(indx)
18+
if indx.class == Fixnum then
19+
return (self.begin + indx)..(self.last + indx)
20+
end
21+
end
22+
23+
1024
alias_method :&, :intersection
25+
alias_method :+, :shift_right
26+
alias_method :-, :shift_left
27+
1128
end
1229

1330

@@ -23,6 +40,22 @@ def bubble_sort!(&closure)
2340
end
2441
end
2542
end
43+
end
44+
end
45+
46+
class Pairs
47+
def initialize(arr)
48+
@arr = arr
49+
end
50+
51+
def print(sp = 0)
52+
@arr.each do |pair|
53+
end
54+
end
55+
end
56+
57+
class Chain
58+
def initialize(arr)
2659

2760
end
2861
end
@@ -31,7 +64,7 @@ class Recreator
3164

3265
def initialize(type)
3366
@type = type
34-
@Debug = true & false;
67+
@Debug = $DEBUG_project;
3568
@sc = SpaceConf.new(type)
3669
end
3770

@@ -46,7 +79,7 @@ def get_string_from_meta(meta)
4679
meta.value.each do |token|
4780
if token.class == MetaExpression then
4881
min_spaces = @sc.get_min(prev_min, token.get_first_token)
49-
if @Debug then
82+
if @Debug > 0 then
5083
p "from ", prev_min.type
5184
p "to", token.get_first_token.type
5285
p min_spaces
@@ -57,7 +90,7 @@ def get_string_from_meta(meta)
5790
prev_min = token.get_last_token;
5891
else
5992
min_spaces = @sc.get_min(prev_min, token)
60-
if @Debug then
93+
if @Debug > 0 then
6194
p "debug:"
6295
p "from ", prev_min
6396
p "to", token
@@ -92,7 +125,7 @@ def multiline_reconstruction(meta_array, chains)
92125
begin
93126
if t_token.class == Token then
94127
min_spaces = @sc.get_min(@prev_tokens[line_index], t_token)
95-
if @Debug then
128+
if @Debug > 0 then
96129
p "from ", @prev_tokens[line_index].type
97130
p "to", t_token.type
98131
p min_spaces
@@ -102,7 +135,7 @@ def multiline_reconstruction(meta_array, chains)
102135
@prev_tokens[line_index] = t_token;
103136
else
104137
min_spaces = @sc.get_min(@prev_tokens[line_index], t_token.get_first_token)
105-
if @Debug then
138+
if @Debug > 0 then
106139
p "from ", @prev_tokens[line_index].type
107140
p "to", t_token.get_first_token.type
108141
p min_spaces
@@ -113,7 +146,7 @@ def multiline_reconstruction(meta_array, chains)
113146
end
114147
rescue Exception => e
115148
p e
116-
p e.backtrace
149+
e.backtrace.each{|x| p x}
117150
p "Exceprion: "
118151
p "line_index: " + line_index.to_s
119152
p "token_index: " + indexes[line_index].to_s
@@ -127,6 +160,10 @@ def multiline_reconstruction(meta_array, chains)
127160
end
128161
# chain processing
129162

163+
if @Debug > 0
164+
puts "chains"
165+
chains.each {|x| p x}
166+
end
130167
chains.each do |chain|
131168

132169
begin_line = chain[0];
@@ -176,7 +213,7 @@ def multiline_reconstruction(meta_array, chains)
176213
params.push(t2.str_index);
177214
limit = @sc.get_max(t1, t2, params);
178215
accept = delta[i] < limit;
179-
if @Debug & false then
216+
if @Debug > 1 then
180217
p "prev: " + t1.type .to_s;
181218
p "next: " + t2.type .to_s;
182219
p "delta:" + delta[i].to_s;
@@ -218,8 +255,13 @@ def multiline_reconstruction(meta_array, chains)
218255

219256
# input [ [ [index, index], [i, i], ... ], ...]
220257
# output [ [line_id, [[token-id, token-id], [t-id, t-id], ...]], .... ]
221-
258+
# line_id - is first line of chain
222259
def generate_chains(pairs_array)
260+
261+
if @Debug > 0
262+
puts "generate_chains(pairs_array): "
263+
pairs_array.each{|x| p x}
264+
end
223265
n = pairs_array.size();
224266
used_indexes = n.times.map{{}};
225267
curr_indexes = [ 0 ] * n;
@@ -273,28 +315,56 @@ def generate_chains(pairs_array)
273315
end
274316

275317
# sort
276-
chains.bubble_sort! do |x,y|
318+
319+
chains.sort! do |x,y|
320+
277321
# TODO add intersection here! Add convertation to line!
278-
x_range = x[0]..(x[0] + x[1].size)
279-
y_range = y[0]..(y[0] + y[1].size)
322+
x_range = x[0]..(x[0] + x[1].size )
323+
y_range = y[0]..(y[0] + y[1].size )
280324

281325

282-
str_inter = x_range & y_range;
326+
str_inter = x_range & y_range; # intersection of ranges
283327

284328
x_min = 0;
285329
y_min = 0;
286330

287331
x_by_lines = x[1].map{|i| i[0]} + [x[1].last[1]]
288332
y_by_lines = y[1].map{|i| i[0]} + [y[1].last[1]]
333+
334+
res = [x_by_lines[str_inter - x[0]].min, -x[0]] <=> [y_by_lines[str_inter - y[0]].min, -y[0]]
335+
289336

290-
if str_inter != nil
291-
x_range = str_inter;
292-
y_range = str_inter;
337+
if @Debug > 1
338+
p "#{x} < #{y}" if res == -1
339+
p "#{x} > #{y}" if res == 1
340+
p "#{x} = #{y}" if res == 0
341+
p "#{x} ??? #{y}"if res == nil
342+
343+
if res == nil
344+
p [x_by_lines[str_inter - x[0]].min, x[0]]
345+
p [y_by_lines[str_inter - y[0]].min, y[0]]
346+
p str_inter
347+
p "x_by_lines: #{x_by_lines}"
348+
p "y_by_lines: #{y_by_lines}"
349+
p "x_range: #{x_range}"
350+
p "y_range: #{y_range}"
351+
352+
end
293353
end
294-
295-
x_range.each{ |i| x_min = [x_min, x_by_lines[i - x[0]]].max }
296-
y_range.each{ |i| y_min = [y_min, y_by_lines[i - y[0]]].max }
297-
x_min <=> y_min;
354+
355+
356+
#p "str_inter: ", str_inter
357+
358+
#p x_by_lines[str_inter]
359+
#p y_by_lines[str_inter]
360+
#if str_inter != nil
361+
# x_range = str_inter;
362+
# y_range = str_inter;
363+
#end
364+
#x_range.each{ |i| x_min = [x_min, x_by_lines[i - x[0]]].max }
365+
#y_range.each{ |i| y_min = [y_min, y_by_lines[i - y[0]]].max }
366+
#x_min <=> y_min;
367+
res
298368
end
299369

300370
return chains;

0 commit comments

Comments
 (0)