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

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

Issue 153993014: Experimental parser: cleanup dfa.py (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') | no next file » | 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 2013 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
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 return count + 1 123 return count + 1
124 count = self.visit_all_states(f, 0) 124 count = self.visit_all_states(f, 0)
125 assert count == nodes_created 125 assert count == nodes_created
126 126
127 @staticmethod 127 @staticmethod
128 def __gather_transition_keys(encoding, state_set): 128 def __gather_transition_keys(encoding, state_set):
129 keys = set(chain(*map(lambda state: state.key_iter(), state_set))) 129 keys = set(chain(*map(lambda state: state.key_iter(), state_set)))
130 keys.discard(TransitionKey.epsilon()) 130 keys.discard(TransitionKey.epsilon())
131 return TransitionKey.disjoint_keys(encoding, keys) 131 return TransitionKey.disjoint_keys(encoding, keys)
132 132
133 def __to_dfa(self, nfa_state_set, dfa_nodes, end_node): 133 def __to_dfa(self, nfa_state_set, dfa_nodes):
134 nfa_state_set = Automaton.epsilon_closure(nfa_state_set) 134 nfa_state_set = Automaton.epsilon_closure(nfa_state_set)
135 # nfa_state_set will be a state in the dfa. 135 # nfa_state_set will be a state in the dfa.
136 assert nfa_state_set 136 assert nfa_state_set
137 name = ".".join(str(x.node_number()) for x in sorted(nfa_state_set)) 137 name = ".".join(str(x.node_number()) for x in sorted(nfa_state_set))
138 if name in dfa_nodes: 138 if name in dfa_nodes:
139 return name 139 return name
140 dfa_nodes[name] = { 140 dfa_nodes[name] = {
141 'transitions': {}, 141 'transitions': {},
142 'terminal': end_node in nfa_state_set, 142 'terminal': self.__end in nfa_state_set,
143 'action' : Action.dominant_action(nfa_state_set)} 143 'action' : Action.dominant_action(nfa_state_set)}
144 # Gather the set of transition keys for which the dfa state will have 144 # Gather the set of transition keys for which the dfa state will have
145 # transitions (the disjoint set of all the transition keys from all the 145 # transitions (the disjoint set of all the transition keys from all the
146 # states combined). For example, if a state in the state set has a 146 # states combined). For example, if a state in the state set has a
147 # transition for key [a-c], and another state for [b-d], the dfa state will 147 # transition for key [a-c], and another state for [b-d], the dfa state will
148 # have transitions with keys ([a], [b-c], [d]). 148 # have transitions with keys ([a], [b-c], [d]).
149 for key in Nfa.__gather_transition_keys(self.encoding(), nfa_state_set): 149 for key in Nfa.__gather_transition_keys(self.encoding(), nfa_state_set):
150 # Find out which states we can reach with "key", starting from any of the 150 # Find out which states we can reach with "key", starting from any of the
151 # states in "nfa_state_set". The corresponding dfa state will have a 151 # states in "nfa_state_set". The corresponding dfa state will have a
152 # transition with "key" to a state which corresponds to the set of the 152 # transition with "key" to a state which corresponds to the set of the
153 # states ("match_states") (more accurately, its epsilon closure). 153 # states ("match_states") (more accurately, its epsilon closure).
154 f = lambda state: state.transition_state_iter_for_key(key) 154 f = lambda state: state.transition_state_iter_for_key(key)
155 match_states = set(chain(*map(f, nfa_state_set))) 155 match_states = set(chain(*map(f, nfa_state_set)))
156 transition_state = self.__to_dfa(match_states, dfa_nodes, end_node) 156 transition_state = self.__to_dfa(match_states, dfa_nodes)
157 dfa_nodes[name]['transitions'][key] = transition_state 157 dfa_nodes[name]['transitions'][key] = transition_state
158 return name 158 return name
159 159
160 def compute_dfa(self): 160 def compute_dfa(self):
161 dfa_nodes = {} 161 dfa_nodes = {}
162 start_name = self.__to_dfa(set([self.__start]), dfa_nodes, self.__end) 162 start_name = self.__to_dfa(set([self.__start]), dfa_nodes)
163 return (start_name, dfa_nodes) 163 return (start_name, dfa_nodes)
OLDNEW
« no previous file with comments | « tools/lexer_generator/dfa.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698