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

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

Issue 51043003: Experimental Parser: move key functions to TransitionKey class (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: 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 | « no previous file | tools/lexer_generator/nfa.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 29 matching lines...) Expand all
40 def node_number(self): 40 def node_number(self):
41 return self.__node_number 41 return self.__node_number
42 42
43 def add_transition(self, key, state): 43 def add_transition(self, key, state):
44 assert not self.__transitions.has_key(key) 44 assert not self.__transitions.has_key(key)
45 self.__transitions[key] = state 45 self.__transitions[key] = state
46 46
47 def transitions(self): 47 def transitions(self):
48 return self.__transitions 48 return self.__transitions
49 49
50
51 class Dfa: 50 class Dfa:
52 51
53 def __init__(self, start_name, mapping, end_names): 52 def __init__(self, start_name, mapping, end_names):
54 name_map = {} 53 name_map = {}
55 offset = 0 54 offset = 0
56 self.__terminal_set = set() 55 self.__terminal_set = set()
57 for name in mapping.keys(): 56 for name in mapping.keys():
58 dfa_state = DfaState(name, offset) 57 dfa_state = DfaState(name, offset)
59 name_map[name] = dfa_state 58 name_map[name] = dfa_state
60 offset = offset + 1 59 offset = offset + 1
61 if name in end_names: 60 if name in end_names:
62 self.__terminal_set.add(dfa_state) 61 self.__terminal_set.add(dfa_state)
63 for name, values in mapping.items(): 62 for name, values in mapping.items():
64 for key, value in values.items(): 63 for key, value in values.items():
65 name_map[name].add_transition(key, name_map[value]) 64 name_map[name].add_transition(key, name_map[value])
66 self.__start = name_map[start_name] 65 self.__start = name_map[start_name]
67 assert self.__terminal_set 66 assert self.__terminal_set
68 67
69 @staticmethod 68 @staticmethod
70 def __visit_edges(start, function, state): 69 def __visit_edges(start, function, state):
71 edge = set([start]) 70 edge = set([start])
72 visited = set() 71 visited = set()
73 while edge: 72 while edge:
74 next_edge = set() 73 next_edge = set()
75 for node in edge: 74 for node in edge:
76 next_edge = next_edge | set(node.transitions().values()) 75 next_edge |= set(node.transitions().values())
77 state = function(node, state) 76 state = function(node, state)
78 visited = visited | edge 77 visited |= edge
79 edge = next_edge - visited 78 edge = next_edge - visited
80 return state 79 return state
81 80
82 def to_dot(self): 81 def to_dot(self):
83 82
84 def f(node, node_content): 83 def f(node, node_content):
85 for key, value in node.transitions().items(): 84 for key, value in node.transitions().items():
86 node_content.append( 85 node_content.append(
87 " S_%s -> S_%s [ label = \"%s\" ];" % 86 " S_%s -> S_%s [ label = \"%s\" ];" %
88 (node.node_number(), value.node_number(), key)) 87 (node.node_number(), value.node_number(), key))
89 return node_content 88 return node_content
90 89
91 node_content = self.__visit_edges(self.__start, f, []) 90 node_content = self.__visit_edges(self.__start, f, [])
92 terminals = ["S_%d;" % x.node_number() for x in self.__terminal_set] 91 terminals = ["S_%d;" % x.node_number() for x in self.__terminal_set]
93 start_number = self.__start.node_number() 92 start_number = self.__start.node_number()
94 start_shape = "circle" 93 start_shape = "circle"
95 if self.__start in self.__terminal_set: 94 if self.__start in self.__terminal_set:
96 start_shape = "doublecircle" 95 start_shape = "doublecircle"
97 96
98 return ''' 97 return '''
99 digraph finite_state_machine { 98 digraph finite_state_machine {
100 rankdir=LR; 99 rankdir=LR;
101 node [shape = %s, style=filled, bgcolor=lightgrey]; S_%s 100 node [shape = %s, style=filled, bgcolor=lightgrey]; S_%s
102 node [shape = doublecircle, style=unfilled]; %s 101 node [shape = doublecircle, style=unfilled]; %s
103 node [shape = circle]; 102 node [shape = circle];
104 %s 103 %s
105 } 104 }
106 ''' % (start_shape, start_number, " ".join(terminals), "\n".join(node_conten t)) 105 ''' % (start_shape,
107 106 start_number,
107 " ".join(terminals),
108 "\n".join(node_content))
OLDNEW
« no previous file with comments | « no previous file | tools/lexer_generator/nfa.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698