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

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

Issue 85413006: Experimental parser: dfa optimization to remove transitions from keyword lexing (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 7 years 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/generator.py ('k') | tools/lexer_generator/transition_keys.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 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 13 matching lines...) Expand all
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 import ply.yacc as yacc 28 import ply.yacc as yacc
29 from automaton import Action 29 from automaton import Action
30 from rule_lexer import RuleLexer 30 from rule_lexer import RuleLexer
31 from regex_parser import RegexParser 31 from regex_parser import RegexParser
32 from nfa_builder import NfaBuilder 32 from nfa_builder import NfaBuilder
33 from dfa import Dfa 33 from dfa import Dfa
34 from dfa_optimizer import DfaOptimizer
34 from transition_keys import TransitionKey, KeyEncoding 35 from transition_keys import TransitionKey, KeyEncoding
35 36
36 class RuleParserState: 37 class RuleParserState:
37 38
38 def __init__(self, encoding): 39 def __init__(self, encoding):
39 self.aliases = {} 40 self.aliases = {}
40 self.character_classes = {} 41 self.character_classes = {}
41 self.current_state = None 42 self.current_state = None
42 self.rules = {} 43 self.rules = {}
43 self.transitions = set() 44 self.transitions = set()
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 if not self.__nfa: 287 if not self.__nfa:
287 self.__nfa = self.__builder.nfa(self.__graph) 288 self.__nfa = self.__builder.nfa(self.__graph)
288 return self.__nfa 289 return self.__nfa
289 290
290 def dfa(self): 291 def dfa(self):
291 if not self.__dfa: 292 if not self.__dfa:
292 (start, dfa_nodes) = self.nfa().compute_dfa() 293 (start, dfa_nodes) = self.nfa().compute_dfa()
293 self.__dfa = Dfa(self.nfa().encoding(), start, dfa_nodes) 294 self.__dfa = Dfa(self.nfa().encoding(), start, dfa_nodes)
294 return self.__dfa 295 return self.__dfa
295 296
297 def optimize_dfa(self, log = False):
298 assert not self.__dfa
299 self.__dfa = DfaOptimizer.optimize(self.dfa(), log)
300
296 def minimal_dfa(self): 301 def minimal_dfa(self):
297 if not self.__minimial_dfa: 302 if not self.__minimial_dfa:
298 self.__minimial_dfa = self.dfa().minimize() 303 self.__minimial_dfa = self.dfa().minimize()
299 return self.__minimial_dfa 304 return self.__minimial_dfa
300 305
301 def __process_parser_state(self, parser_state): 306 def __process_parser_state(self, parser_state):
302 rule_map = {} 307 rule_map = {}
303 builder = NfaBuilder(parser_state.encoding, parser_state.character_classes) 308 builder = NfaBuilder(parser_state.encoding, parser_state.character_classes)
304 assert 'default' in parser_state.rules 309 assert 'default' in parser_state.rules
305 def process(subgraph, v): 310 def process(subgraph, v):
(...skipping 27 matching lines...) Expand all
333 for k, v in parser_state.rules.items(): 338 for k, v in parser_state.rules.items():
334 if k == 'default': continue 339 if k == 'default': continue
335 process(k, v) 340 process(k, v)
336 process('default', parser_state.rules['default']) 341 process('default', parser_state.rules['default'])
337 # build the automata 342 # build the automata
338 for rule_name, graph in rule_map.items(): 343 for rule_name, graph in rule_map.items():
339 self.__automata[rule_name] = RuleProcessor.Automata(builder, graph) 344 self.__automata[rule_name] = RuleProcessor.Automata(builder, graph)
340 # process default_action 345 # process default_action
341 default_action = parser_state.rules['default']['default_action'] 346 default_action = parser_state.rules['default']['default_action']
342 self.default_action = Action(None, default_action[1]) if default_action else None 347 self.default_action = Action(None, default_action[1]) if default_action else None
OLDNEW
« no previous file with comments | « tools/lexer_generator/generator.py ('k') | tools/lexer_generator/transition_keys.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698