Skip to content
This repository has been archived by the owner on Sep 13, 2017. It is now read-only.

Commit

Permalink
various fixes for NFA -> GTG graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Oct 12, 2011
1 parent 83e80d4 commit 1347a42
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/journey/gtg/transition_table.rb
Expand Up @@ -114,7 +114,7 @@ def visualizer paths, title = 'FSM'

def []= from, to, sym
case sym
when String, NilClass
when String
@string_states[from][sym] = to
when Regexp
@regexp_states[from][sym] = to
Expand Down
23 changes: 10 additions & 13 deletions lib/journey/nfa/dot.rb
Expand Up @@ -8,16 +8,16 @@ def to_dot
" #{from} -> #{to} [label=\"#{sym || 'ε'}\"];"
}

memo_nodes = memos.values.flatten.map { |n|
label = n
if Journey::Route === n
label = "#{n.verb.source} #{n.path.spec}"
end
" #{n.object_id} [label=\"#{label}\", shape=box];"
}
memo_edges = memos.map { |k, memos|
memos.map { |v| " #{k} -> #{v.object_id};" }
}.flatten
#memo_nodes = memos.values.flatten.map { |n|
# label = n
# if Journey::Route === n
# label = "#{n.verb.source} #{n.path.spec}"
# end
# " #{n.object_id} [label=\"#{label}\", shape=box];"
#}
#memo_edges = memos.map { |k, memos|
# (memos || []).map { |v| " #{k} -> #{v.object_id};" }
#}.flatten.uniq

<<-eodot
digraph nfa {
Expand All @@ -26,9 +26,6 @@ def to_dot
#{accepting_states.join ' '};
node [shape = circle];
#{edges.join "\n"}
#{memo_nodes.join "\n"}
#{memo_edges.join "\n"}
}
eodot
end
Expand Down
8 changes: 5 additions & 3 deletions lib/journey/nfa/transition_table.rb
Expand Up @@ -19,6 +19,10 @@ def accepting? state
accepting == state
end

def accepting_states
[accepting]
end

def add_memo idx, memo
@memos[idx] = memo
end
Expand Down Expand Up @@ -77,16 +81,14 @@ def generalized_table
final_groups.each do |states|
id = state_id[states]

gt[id, final_state] = nil
gt.add_accepting id
save = states.find { |s|
@memos.key?(s) && eclosure(s).sort.last == accepting
}

gt.add_memo id, memo(save)
end

gt.add_accepting final_state

gt
end

Expand Down
8 changes: 7 additions & 1 deletion lib/journey/visitors.rb
Expand Up @@ -141,7 +141,13 @@ def binary node
end
super
end
alias :nary :binary

def nary node
node.children.each do |c|
@edges << "#{node.object_id} -> #{c.object_id};"
end
super
end

def unary node
@edges << "#{node.object_id} -> #{node.left.object_id};"
Expand Down

0 comments on commit 1347a42

Please sign in to comment.