| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2013 the V8 project authors. All rights reserved. |
| 2 # Redistribution and use in source and binary forms, with or without |
| 3 # modification, are permitted provided that the following conditions are |
| 4 # met: |
| 5 # |
| 6 # * Redistributions of source code must retain the above copyright |
| 7 # notice, this list of conditions and the following disclaimer. |
| 8 # * Redistributions in binary form must reproduce the above |
| 9 # copyright notice, this list of conditions and the following |
| 10 # disclaimer in the documentation and/or other materials provided |
| 11 # with the distribution. |
| 12 # * Neither the name of Google Inc. nor the names of its |
| 13 # contributors may be used to endorse or promote products derived |
| 14 # from this software without specific prior written permission. |
| 15 # |
| 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 from nfa import Nfa |
| 29 |
| 30 class DfaState: |
| 31 |
| 32 def __init__(self, name, node_number): |
| 33 self.__name = name |
| 34 self.__node_number = node_number |
| 35 self.__transitions = {} |
| 36 |
| 37 def name(self): |
| 38 return self.__name |
| 39 |
| 40 def node_number(self): |
| 41 return self.__node_number |
| 42 |
| 43 def add_transition(self, key, state): |
| 44 assert not self.__transitions.has_key(key) |
| 45 self.__transitions[key] = state |
| 46 |
| 47 def transitions(self): |
| 48 return self.__transitions |
| 49 |
| 50 |
| 51 class Dfa: |
| 52 |
| 53 def __init__(self, start_name, mapping, end_names): |
| 54 name_map = {} |
| 55 offset = 0 |
| 56 self.__terminal_set = set() |
| 57 for name in mapping.keys(): |
| 58 dfa_state = DfaState(name, offset) |
| 59 name_map[name] = dfa_state |
| 60 offset = offset + 1 |
| 61 if name in end_names: |
| 62 self.__terminal_set.add(dfa_state) |
| 63 for name, values in mapping.items(): |
| 64 for key, value in values.items(): |
| 65 name_map[name].add_transition(key, name_map[value]) |
| 66 self.__start = name_map[start_name] |
| 67 assert self.__terminal_set |
| 68 |
| 69 @staticmethod |
| 70 def __visit_edges(start, function, state): |
| 71 edge = set([start]) |
| 72 visited = set() |
| 73 while edge: |
| 74 next_edge = set() |
| 75 for node in edge: |
| 76 next_edge = next_edge | set(node.transitions().values()) |
| 77 state = function(node, state) |
| 78 visited = visited | edge |
| 79 edge = next_edge - visited |
| 80 return state |
| 81 |
| 82 def to_dot(self): |
| 83 |
| 84 def f(node, node_content): |
| 85 for key, value in node.transitions().items(): |
| 86 node_content.append( |
| 87 " S_%s -> S_%s [ label = \"%s\" ];" % |
| 88 (node.node_number(), value.node_number(), key)) |
| 89 return node_content |
| 90 |
| 91 node_content = self.__visit_edges(self.__start, f, []) |
| 92 terminals = ["S_%d;" % x.node_number() for x in self.__terminal_set] |
| 93 start_number = self.__start.node_number() |
| 94 start_shape = "circle" |
| 95 if self.__start in self.__terminal_set: |
| 96 start_shape = "doublecircle" |
| 97 |
| 98 return ''' |
| 99 digraph finite_state_machine { |
| 100 rankdir=LR; |
| 101 node [shape = %s, style=filled, bgcolor=lightgrey]; S_%s |
| 102 node [shape = doublecircle, style=unfilled]; %s |
| 103 node [shape = circle]; |
| 104 %s |
| 105 } |
| 106 ''' % (start_shape, start_number, " ".join(terminals), "\n".join(node_conten
t)) |
| 107 |
| OLD | NEW |