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

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

Issue 169583003: Experimental parser: more verification (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 2014 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
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 82
83 # First we partition the states into the following groups: 83 # First we partition the states into the following groups:
84 # - terminal states without action 84 # - terminal states without action
85 # - nonterminal states without action 85 # - nonterminal states without action
86 # - one group per action: terminal states which have the action 86 # - one group per action: terminal states which have the action
87 # - one group per action: nonterminal states which have the action 87 # - one group per action: nonterminal states which have the action
88 # These are the keys of initial_partitions. The values are lists of state 88 # These are the keys of initial_partitions. The values are lists of state
89 # IDs. 89 # IDs.
90 initial_partitions = {} 90 initial_partitions = {}
91 terminal_set = self.__dfa.terminal_set() 91 terminal_set = self.__dfa.terminal_set()
92 all_keys = [] # Will contain all TransitionKeys in the dfa.
93 92
94 # f fills in initial_partitions, id_to_state and all_keys. 93 # f fills in initial_partitions, id_to_state and all_keys.
95 def f(state, visitor_state): 94 def f(state, visitor_state):
96 state_id = len(id_to_state) 95 state_id = len(id_to_state)
97 id_to_state[state_id] = state 96 id_to_state[state_id] = state
98 action = state.action() 97 action = state.action()
99 all_keys.append(state.key_iter())
100 if state in terminal_set: 98 if state in terminal_set:
101 key = ("terminal set", action) 99 key = ("terminal set", action)
102 else: 100 else:
103 # Match actions are valid only if we have matched the whole token, not 101 # Match actions are valid only if we have matched the whole token, not
104 # just some sub-part of it. 102 # just some sub-part of it.
105 # TODO(dcarney): restore 103 # TODO(dcarney): restore
106 # assert not action.is_match_action() 104 # assert not action.is_match_action()
107 key = ("nonterminal set", action) 105 key = ("nonterminal set", action)
108 if not key in initial_partitions: 106 if not key in initial_partitions:
109 initial_partitions[key] = set() 107 initial_partitions[key] = set()
(...skipping 16 matching lines...) Expand all
126 # two states of the dfa can be in the same partition. See the doc string of 124 # two states of the dfa can be in the same partition. See the doc string of
127 # TransitionKey.disjoint_keys. 125 # TransitionKey.disjoint_keys.
128 126
129 # For example, if the TransitionKeys are {[a-d], [c-h]}, the alphabet is 127 # For example, if the TransitionKeys are {[a-d], [c-h]}, the alphabet is
130 # {[a-b], [c-d], [e-h]}. If state S1 has a transition to S2 with 128 # {[a-b], [c-d], [e-h]}. If state S1 has a transition to S2 with
131 # TransitionKey [a-d], and state S3 has a transition to S4 with 129 # TransitionKey [a-d], and state S3 has a transition to S4 with
132 # TransitionKey [c-h], S1 and S3 cannot be in the same partition. This will 130 # TransitionKey [c-h], S1 and S3 cannot be in the same partition. This will
133 # become clear when we check the transition for TransitionKey [c-d] (S1 has 131 # become clear when we check the transition for TransitionKey [c-d] (S1 has
134 # a transition to S2, S3 has a transition to S4). 132 # a transition to S2, S3 has a transition to S4).
135 encoding = self.__dfa.encoding() 133 encoding = self.__dfa.encoding()
136 self.__alphabet = list( 134 self.__alphabet = list(self.__dfa.disjoint_keys_iter())
137 TransitionKey.disjoint_keys(encoding, chain(*all_keys)))
138 135
139 # For each state and each TransitionKey in the alphabet, find out which 136 # For each state and each TransitionKey in the alphabet, find out which
140 # transition we take from the state with the TransitionKey. 137 # transition we take from the state with the TransitionKey.
141 transitions = {} 138 transitions = {}
142 for state_id, state in id_to_state.items(): 139 for state_id, state in id_to_state.items():
143 def f((key_id, key)): 140 def f((key_id, key)):
144 transition_state = state.transition_state_for_key(key) 141 transition_state = state.transition_state_for_key(key)
145 if transition_state: 142 if transition_state:
146 return state_to_id[transition_state] 143 return state_to_id[transition_state]
147 return None 144 return None
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 elif len(intersection) <= len(difference): 290 elif len(intersection) <= len(difference):
294 working_set.add(intersection) 291 working_set.add(intersection)
295 else: 292 else:
296 working_set.add(difference) 293 working_set.add(difference)
297 if old_partitions: 294 if old_partitions:
298 partitions -= old_partitions 295 partitions -= old_partitions
299 if new_partitions: 296 if new_partitions:
300 partitions |= new_partitions 297 partitions |= new_partitions
301 (start_name, mapping) = self.__create_states_from_partitions(partitions) 298 (start_name, mapping) = self.__create_states_from_partitions(partitions)
302 return Dfa(self.__dfa.encoding(), start_name, mapping) 299 return Dfa(self.__dfa.encoding(), start_name, mapping)
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