Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(719)

Side by Side Diff: tools/lexer_generator/dfa_minimizer.py

Issue 169523003: Experimental parser: split and rename some files (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/lexer_generator/dfa.py ('k') | tools/lexer_generator/dfa_optimizer.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2013 the V8 project authors. All rights reserved. 1 # Copyright 2014 the V8 project authors. All rights reserved.
2 # Redistribution and use in source and binary forms, with or without 2 # Redistribution and use in source and binary forms, with or without
3 # modification, are permitted provided that the following conditions are 3 # modification, are permitted provided that the following conditions are
4 # met: 4 # met:
5 # 5 #
6 # * Redistributions of source code must retain the above copyright 6 # * Redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer. 7 # notice, this list of conditions and the following disclaimer.
8 # * Redistributions in binary form must reproduce the above 8 # * Redistributions in binary form must reproduce the above
9 # copyright notice, this list of conditions and the following 9 # copyright notice, this list of conditions and the following
10 # disclaimer in the documentation and/or other materials provided 10 # disclaimer in the documentation and/or other materials provided
11 # with the distribution. 11 # with the distribution.
12 # * Neither the name of Google Inc. nor the names of its 12 # * Neither the name of Google Inc. nor the names of its
13 # contributors may be used to endorse or promote products derived 13 # contributors may be used to endorse or promote products derived
14 # from this software without specific prior written permission. 14 # from this software without specific prior written permission.
15 # 15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 from itertools import chain 28 from itertools import chain
29 from automaton import * 29 from dfa import Dfa
30 from transition_keys import TransitionKey 30 from transition_key import TransitionKey
31
32 class DfaState(AutomatonState):
33
34 def __init__(self, name, action, transitions):
35 super(DfaState, self).__init__()
36 self.__name = name
37 self.__transitions = transitions
38 self.__action = action
39 assert isinstance(action, Action)
40
41 def name(self):
42 return self.__name
43
44 def action(self):
45 return self.__action
46
47 def transition_count(self):
48 return len(self.__transitions)
49
50 def omega_transition(self):
51 if TransitionKey.omega() in self.__transitions:
52 return self.__transitions[TransitionKey.omega()]
53 return None
54
55 def epsilon_closure_iter(self):
56 return iter([])
57
58 def transition_state_for_key(self, value):
59 matches = list(self.transition_state_iter_for_key(value))
60 assert len(matches) <= 1
61 return matches[0] if matches else None
62
63 def key_state_iter(
64 self,
65 key_filter = lambda x: True,
66 state_filter = lambda x: True,
67 match_func = lambda x, y: True,
68 yield_func = lambda x, y: (x, y)):
69 for key, state in self.__transitions.items():
70 if key_filter(key) and state_filter(state) and match_func(key, state):
71 yield yield_func(key, state)
72
73 class Dfa(Automaton):
74
75 @staticmethod
76 def __add_transition(transitions, key, state):
77 assert key != None
78 assert not key == TransitionKey.epsilon()
79 assert not transitions.has_key(key)
80 transitions[key] = state
81
82 def __init__(self, encoding, start_name, mapping):
83 super(Dfa, self).__init__(encoding)
84 self.__terminal_set = set()
85 name_map = {}
86 for name, node_data in mapping.items():
87 transitions = {}
88 node = DfaState(name, node_data['action'], transitions)
89 name_map[name] = (node, transitions)
90 if node_data['terminal']:
91 self.__terminal_set.add(node)
92 for name, node_data in mapping.items():
93 (node, transitions) = name_map[name]
94 inversion = {}
95 for key, state in node_data['transitions'].items():
96 if not state in inversion:
97 inversion[state] = []
98 inversion[state].append(key)
99 for state, keys in inversion.items():
100 merged_key = TransitionKey.merged_key(encoding, keys)
101 self.__add_transition(transitions, merged_key, name_map[state][0])
102 self.__start = name_map[start_name][0]
103 self.__node_count = len(mapping)
104 self.__verify()
105
106 def __verify(self):
107 assert self.__terminal_set
108 state_count = self.visit_all_states(lambda state, count: count + 1, 0)
109 assert self.__node_count == state_count
110
111 def node_count(self):
112 return self.__node_count
113
114 def start_state(self):
115 return self.__start
116
117 def terminal_set(self):
118 return set(self.__terminal_set)
119
120 def minimize(self):
121 return DfaMinimizer(self).minimize()
122 31
123 class StatePartition(object): 32 class StatePartition(object):
124 33
125 def __init__(self, node_numbers): 34 def __init__(self, node_numbers):
126 self.__node_numbers = node_numbers 35 self.__node_numbers = node_numbers
127 assert self.__node_numbers 36 assert self.__node_numbers
128 self.__hash = None 37 self.__hash = None
129 38
130 def set(self): 39 def set(self):
131 return self.__node_numbers 40 return self.__node_numbers
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 elif len(intersection) <= len(difference): 293 elif len(intersection) <= len(difference):
385 working_set.add(intersection) 294 working_set.add(intersection)
386 else: 295 else:
387 working_set.add(difference) 296 working_set.add(difference)
388 if old_partitions: 297 if old_partitions:
389 partitions -= old_partitions 298 partitions -= old_partitions
390 if new_partitions: 299 if new_partitions:
391 partitions |= new_partitions 300 partitions |= new_partitions
392 (start_name, mapping) = self.__create_states_from_partitions(partitions) 301 (start_name, mapping) = self.__create_states_from_partitions(partitions)
393 return Dfa(self.__dfa.encoding(), start_name, mapping) 302 return Dfa(self.__dfa.encoding(), start_name, mapping)
OLDNEW
« no previous file with comments | « tools/lexer_generator/dfa.py ('k') | tools/lexer_generator/dfa_optimizer.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698