Skip to content

Commit

Permalink
Fix chain sorting predicate; extent debug log; extend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
generall committed May 5, 2015
1 parent 07014ce commit 5abfb22
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 46 deletions.
41 changes: 27 additions & 14 deletions LR_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
require './fsm.rb'
require './expression.rb'

require 'colorize' if ARGV.member?("debug")

require 'colorize' if $DEBUG_project > 0
require 'pry' if $DEBUG_project > 0

class Grammar

Expand Down Expand Up @@ -150,7 +150,7 @@ def generate_FSM(grammar, axiom)
add_next_vertex(grammar, fsm, rule, vertex_by_set, 0);
end

if @Debug
if @Debug >= 2
vertex_by_set.each{|x|
p x
}
Expand All @@ -160,7 +160,7 @@ def generate_FSM(grammar, axiom)

class Nonterm
def initialize(sym = nil, obj = nil, is_reducible = false)
@Debug = true & false;
@Debug = 0;
@symbol = sym;
@object = obj;
@is_reducible = is_reducible;
Expand Down Expand Up @@ -246,11 +246,11 @@ def parse(fsm, expr, grammar)
end
end

if @Debug then
puts "is_acceptable: " + is_acceptable .to_s;
if @Debug >= 2 then
puts "expr: " + expr .to_s;
puts "signal: " + signal .to_s;
puts "is_acceptable: " + is_acceptable .to_s;
puts "expr.last: " + expr.last .to_s;
puts "expr: " + expr .to_s;
puts "state_stack: " + state_stack .to_s;
puts "current_state " + fsm.current_vertex.to_s;
puts "processed: " + processed .to_s;
Expand All @@ -269,9 +269,9 @@ def parse(fsm, expr, grammar)
to_production = fsm.get_value[0];
r_s = rule.size # Rule_Size

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

is_appropriate = true;
Expand Down Expand Up @@ -303,7 +303,7 @@ def parse(fsm, expr, grammar)

end
end
p "-------" if @Debug
p "-------" if @Debug >= 2
end
return expr.last;
end
Expand All @@ -316,7 +316,7 @@ def tree_to_metaexpression(tree, meta)

class LR_parser
def initialize(type)
@Debug = ARGV.member?("debug");
@Debug = $DEBUG_project

TokenTemplate.set_ltype type

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

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


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

if @Debug > 0
print "expr: "
p expr
end

metaexpr = MetaExpression.new();
parse(@grammar_machina, expr, @@grammar).make_metaexpr(metaexpr);

ast = parse(@grammar_machina, expr, @@grammar);

#binding.pry if $DEBUG_project > 0
ast.print_tree if @Debug > 1

ast.make_metaexpr(metaexpr);


return metaexpr;
end
end
Expand Down
4 changes: 4 additions & 0 deletions align.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def test_aligment(input_strings, type)

# no indent is observed
def align_group(input_strings, type)
p "start align_group" if $DEBUG_project > 0
p = LR_parser.new(type)
metas = []
input_strings.each { |str| metas.push(p.parse_meta(str)); }
Expand All @@ -60,6 +61,8 @@ def align_group(input_strings, type)
for i in 0..metas.size-2 do
pairs_array.push(matcher.generate_pairs(metas[i].value, metas[i+1].value));
end

metas.each{|x| x.print_tree } if $DEBUG_project > 1
r = Recreator.new(type)
chains = r.generate_chains(pairs_array);
lines = r.multiline_reconstruction(metas, chains)
Expand Down Expand Up @@ -96,6 +99,7 @@ def align(input_strings, type)
end
end

puts "groups.size: #{groups.size}"if $DEBUG_project > 0
result = [];
groups.each_with_index do |group, i|
if group.size > 1 then
Expand Down
4 changes: 2 additions & 2 deletions expression.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

require "./staff.rb"
require 'colorize' if ARGV.member?("debug")
require 'colorize' if $DEBUG_project > 0


class MetaExpression
Expand All @@ -25,7 +25,7 @@ def print_tree(n = 0)
@value.each do |token|
if token.class == Token then
print " "*n*4;
p token.value;
p token;
else
token.print_tree(n+1)
end
Expand Down
11 changes: 10 additions & 1 deletion pipe_launch.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
require 'optparse'

$DEBUG_project = 0;

options = {}
OptionParser.new do |opt|
opt.on('--debug [LEVEL]') { |o| $DEBUG_project = (o || 1).to_i }
end.parse!

require "./align.rb"

DEBUG_pipe = false

DEBUG_pipe = false
type = :default;
case ARGV[0]
when "C99"
Expand Down
112 changes: 91 additions & 21 deletions recreator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,27 @@

class Range
def intersection(other)
return nil if (self.max < other.begin or other.max < self.begin)
[self.begin, other.begin].max..[self.max, other.max].min
return (1..0) if (self.last < other.begin or other.last < self.begin)
[self.begin, other.begin].last..[self.last, other.last].min
end

def shift_left(indx)
if indx.class == Fixnum then
return (self.begin - indx)..(self.last - indx)
end
end

def shift_right(indx)
if indx.class == Fixnum then
return (self.begin + indx)..(self.last + indx)
end
end


alias_method :&, :intersection
alias_method :+, :shift_right
alias_method :-, :shift_left

end


Expand All @@ -23,6 +40,22 @@ def bubble_sort!(&closure)
end
end
end
end
end

class Pairs
def initialize(arr)
@arr = arr
end

def print(sp = 0)
@arr.each do |pair|
end
end
end

class Chain
def initialize(arr)

end
end
Expand All @@ -31,7 +64,7 @@ class Recreator

def initialize(type)
@type = type
@Debug = true & false;
@Debug = $DEBUG_project;
@sc = SpaceConf.new(type)
end

Expand All @@ -46,7 +79,7 @@ def get_string_from_meta(meta)
meta.value.each do |token|
if token.class == MetaExpression then
min_spaces = @sc.get_min(prev_min, token.get_first_token)
if @Debug then
if @Debug > 0 then
p "from ", prev_min.type
p "to", token.get_first_token.type
p min_spaces
Expand All @@ -57,7 +90,7 @@ def get_string_from_meta(meta)
prev_min = token.get_last_token;
else
min_spaces = @sc.get_min(prev_min, token)
if @Debug then
if @Debug > 0 then
p "debug:"
p "from ", prev_min
p "to", token
Expand Down Expand Up @@ -92,7 +125,7 @@ def multiline_reconstruction(meta_array, chains)
begin
if t_token.class == Token then
min_spaces = @sc.get_min(@prev_tokens[line_index], t_token)
if @Debug then
if @Debug > 0 then
p "from ", @prev_tokens[line_index].type
p "to", t_token.type
p min_spaces
Expand All @@ -102,7 +135,7 @@ def multiline_reconstruction(meta_array, chains)
@prev_tokens[line_index] = t_token;
else
min_spaces = @sc.get_min(@prev_tokens[line_index], t_token.get_first_token)
if @Debug then
if @Debug > 0 then
p "from ", @prev_tokens[line_index].type
p "to", t_token.get_first_token.type
p min_spaces
Expand All @@ -113,7 +146,7 @@ def multiline_reconstruction(meta_array, chains)
end
rescue Exception => e
p e
p e.backtrace
e.backtrace.each{|x| p x}
p "Exceprion: "
p "line_index: " + line_index.to_s
p "token_index: " + indexes[line_index].to_s
Expand All @@ -127,6 +160,10 @@ def multiline_reconstruction(meta_array, chains)
end
# chain processing

if @Debug > 0
puts "chains"
chains.each {|x| p x}
end
chains.each do |chain|

begin_line = chain[0];
Expand Down Expand Up @@ -176,7 +213,7 @@ def multiline_reconstruction(meta_array, chains)
params.push(t2.str_index);
limit = @sc.get_max(t1, t2, params);
accept = delta[i] < limit;
if @Debug & false then
if @Debug > 1 then
p "prev: " + t1.type .to_s;
p "next: " + t2.type .to_s;
p "delta:" + delta[i].to_s;
Expand Down Expand Up @@ -218,8 +255,13 @@ def multiline_reconstruction(meta_array, chains)

# input [ [ [index, index], [i, i], ... ], ...]
# output [ [line_id, [[token-id, token-id], [t-id, t-id], ...]], .... ]

# line_id - is first line of chain
def generate_chains(pairs_array)

if @Debug > 0
puts "generate_chains(pairs_array): "
pairs_array.each{|x| p x}
end
n = pairs_array.size();
used_indexes = n.times.map{{}};
curr_indexes = [ 0 ] * n;
Expand Down Expand Up @@ -273,28 +315,56 @@ def generate_chains(pairs_array)
end

# sort
chains.bubble_sort! do |x,y|

chains.sort! do |x,y|

# TODO add intersection here! Add convertation to line!
x_range = x[0]..(x[0] + x[1].size)
y_range = y[0]..(y[0] + y[1].size)
x_range = x[0]..(x[0] + x[1].size )
y_range = y[0]..(y[0] + y[1].size )


str_inter = x_range & y_range;
str_inter = x_range & y_range; # intersection of ranges

x_min = 0;
y_min = 0;

x_by_lines = x[1].map{|i| i[0]} + [x[1].last[1]]
y_by_lines = y[1].map{|i| i[0]} + [y[1].last[1]]

res = [x_by_lines[str_inter - x[0]].min, -x[0]] <=> [y_by_lines[str_inter - y[0]].min, -y[0]]


if str_inter != nil
x_range = str_inter;
y_range = str_inter;
if @Debug > 1
p "#{x} < #{y}" if res == -1
p "#{x} > #{y}" if res == 1
p "#{x} = #{y}" if res == 0
p "#{x} ??? #{y}"if res == nil

if res == nil
p [x_by_lines[str_inter - x[0]].min, x[0]]
p [y_by_lines[str_inter - y[0]].min, y[0]]
p str_inter
p "x_by_lines: #{x_by_lines}"
p "y_by_lines: #{y_by_lines}"
p "x_range: #{x_range}"
p "y_range: #{y_range}"

end
end

x_range.each{ |i| x_min = [x_min, x_by_lines[i - x[0]]].max }
y_range.each{ |i| y_min = [y_min, y_by_lines[i - y[0]]].max }
x_min <=> y_min;


#p "str_inter: ", str_inter

#p x_by_lines[str_inter]
#p y_by_lines[str_inter]
#if str_inter != nil
# x_range = str_inter;
# y_range = str_inter;
#end
#x_range.each{ |i| x_min = [x_min, x_by_lines[i - x[0]]].max }
#y_range.each{ |i| y_min = [y_min, y_by_lines[i - y[0]]].max }
#x_min <=> y_min;
res
end

return chains;
Expand Down
Loading

0 comments on commit 5abfb22

Please sign in to comment.