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

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

Issue 62223002: Experimental lexer generator: transfer actions across epsilon transitions correctly when constructi… (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: code review (dcarney) Created 7 years, 1 month 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/rule_parser.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 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 name = ".".join(str(x.node_number()) for x in sorted(nfa_state_set)) 326 name = ".".join(str(x.node_number()) for x in sorted(nfa_state_set))
327 if name in dfa_nodes: 327 if name in dfa_nodes:
328 return name 328 return name
329 dfa_nodes[name] = { 329 dfa_nodes[name] = {
330 'transitions': {}, 330 'transitions': {},
331 'terminal': end_node in nfa_state_set} 331 'terminal': end_node in nfa_state_set}
332 for key in NfaState.gather_transition_keys(nfa_state_set): 332 for key in NfaState.gather_transition_keys(nfa_state_set):
333 f = lambda acc, state: acc | state.key_matches(key) 333 f = lambda acc, state: acc | state.key_matches(key)
334 transitions = reduce(f, nfa_state_set, set()) 334 transitions = reduce(f, nfa_state_set, set())
335 match_states = set() 335 match_states = set()
336 actions = set() 336 actions = []
337 for (state, action) in transitions: 337 for (state, action) in transitions:
338 match_states.add(state) 338 match_states.add(state)
339 if action: 339 if action:
340 actions.add(action) 340 actions.append(action)
341
342 # Pull in actions which can be taken with an epsilon transition from the
343 # match state.
344 e = TransitionKey.epsilon()
345 if e in state.transitions():
346 for e_trans in state.transitions()[e]:
347 if e_trans[1]:
348 actions.append(e_trans[1])
349
341 assert len(match_states) == len(transitions) 350 assert len(match_states) == len(transitions)
342 assert not actions or len(actions) == 1 351
343 action = iter(actions).next() if actions else None 352 actions.sort()
353 action = actions[0] if actions else None
344 transition_state = Nfa.__to_dfa(match_states, dfa_nodes, end_node) 354 transition_state = Nfa.__to_dfa(match_states, dfa_nodes, end_node)
345 dfa_nodes[name]['transitions'][key] = (transition_state, action) 355 dfa_nodes[name]['transitions'][key] = (transition_state, action)
346 return name 356 return name
347 357
348 def compute_dfa(self): 358 def compute_dfa(self):
349 self.__compute_epsilon_closures() 359 self.__compute_epsilon_closures()
350 dfa_nodes = {} 360 dfa_nodes = {}
351 start_name = self.__to_dfa(set([self.__start]), dfa_nodes, self.__end) 361 start_name = self.__to_dfa(set([self.__start]), dfa_nodes, self.__end)
352 return (start_name, dfa_nodes) 362 return (start_name, dfa_nodes)
353 363
(...skipping 22 matching lines...) Expand all
376 digraph finite_state_machine { 386 digraph finite_state_machine {
377 rankdir=LR; 387 rankdir=LR;
378 node [shape = circle, style=filled, bgcolor=lightgrey]; S_%s 388 node [shape = circle, style=filled, bgcolor=lightgrey]; S_%s
379 node [shape = doublecircle, style=unfilled]; S_%s 389 node [shape = doublecircle, style=unfilled]; S_%s
380 node [shape = circle]; 390 node [shape = circle];
381 %s 391 %s
382 } 392 }
383 ''' % (self.__start.node_number(), 393 ''' % (self.__start.node_number(),
384 self.__end.node_number(), 394 self.__end.node_number(),
385 "\n".join(node_content)) 395 "\n".join(node_content))
OLDNEW
« no previous file with comments | « tools/lexer_generator/generator.py ('k') | tools/lexer_generator/rule_parser.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698