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

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

Issue 54303002: Experimental parser: some unit tests (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 | « tools/lexer_generator/automata_test.py ('k') | 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 visited = set() 71 visited = set()
72 while edge: 72 while edge:
73 f = lambda (next_edge, state), node: ( 73 f = lambda (next_edge, state), node: (
74 next_edge | set(node.transitions().values()), 74 next_edge | set(node.transitions().values()),
75 visitor(node, state)) 75 visitor(node, state))
76 (next_edge, state) = reduce(f, edge, (set(), state)) 76 (next_edge, state) = reduce(f, edge, (set(), state))
77 visited |= edge 77 visited |= edge
78 edge = next_edge - visited 78 edge = next_edge - visited
79 return state 79 return state
80 80
81 def matches(self, string):
82 state = self.__start
83 for c in string:
84 next_state = None
85 for key, transition_state in state.transitions().items():
86 if key.matches_char(c):
87 next_state = transition_state
88 break
89 if not next_state:
90 return False
91 state = next_state
92 return state in self.__terminal_set
93
81 def to_dot(self): 94 def to_dot(self):
82 95
83 def f(node, node_content): 96 def f(node, node_content):
84 for key, value in node.transitions().items(): 97 for key, value in node.transitions().items():
85 node_content.append( 98 node_content.append(
86 " S_%s -> S_%s [ label = \"%s\" ];" % 99 " S_%s -> S_%s [ label = \"%s\" ];" %
87 (node.node_number(), value.node_number(), key)) 100 (node.node_number(), value.node_number(), key))
88 return node_content 101 return node_content
89 102
90 node_content = self.__visit_edges(self.__start, f, []) 103 node_content = self.__visit_edges(self.__start, f, [])
91 terminals = ["S_%d;" % x.node_number() for x in self.__terminal_set] 104 terminals = ["S_%d;" % x.node_number() for x in self.__terminal_set]
92 start_number = self.__start.node_number() 105 start_number = self.__start.node_number()
93 start_shape = "circle" 106 start_shape = "circle"
94 if self.__start in self.__terminal_set: 107 if self.__start in self.__terminal_set:
95 start_shape = "doublecircle" 108 start_shape = "doublecircle"
96 109
97 return ''' 110 return '''
98 digraph finite_state_machine { 111 digraph finite_state_machine {
99 rankdir=LR; 112 rankdir=LR;
100 node [shape = %s, style=filled, bgcolor=lightgrey]; S_%s 113 node [shape = %s, style=filled, bgcolor=lightgrey]; S_%s
101 node [shape = doublecircle, style=unfilled]; %s 114 node [shape = doublecircle, style=unfilled]; %s
102 node [shape = circle]; 115 node [shape = circle];
103 %s 116 %s
104 } 117 }
105 ''' % (start_shape, 118 ''' % (start_shape,
106 start_number, 119 start_number,
107 " ".join(terminals), 120 " ".join(terminals),
108 "\n".join(node_content)) 121 "\n".join(node_content))
OLDNEW
« no previous file with comments | « tools/lexer_generator/automata_test.py ('k') | tools/lexer_generator/nfa.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698