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

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

Issue 68523003: Experimental lexer generator: fix actions in graphs. (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 | 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 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 def generate_dot(start_node, terminal_set, edge_iterator, state_iterator): 60 def generate_dot(start_node, terminal_set, edge_iterator, state_iterator):
61 61
62 def escape(v): 62 def escape(v):
63 v = str(v).replace('\r', '\\\\r') 63 v = str(v).replace('\r', '\\\\r')
64 v = str(v).replace('\t', '\\\\t') 64 v = str(v).replace('\t', '\\\\t')
65 v = str(v).replace('\n', '\\\\n') 65 v = str(v).replace('\n', '\\\\n')
66 v = str(v).replace('\\', '\\\\') 66 v = str(v).replace('\\', '\\\\')
67 v = str(v).replace('\"', '\\\"') 67 v = str(v).replace('\"', '\\\"')
68 return v 68 return v
69 69
70 def f(node, node_content): 70 def f(node, content):
71 (node_content, edge_content) = content
72 if node.action():
73 action_text = node.action()[1].split('\n')[0]
74 node_content.append(' S_l%s[shape = box, label="%s"];' %
75 (node.node_number(), action_text))
76 node_content.append(' S_%s -> S_l%s [arrowhead = none];' %
77 (node.node_number(), node.node_number()))
71 for key, values in node.transitions().items(): 78 for key, values in node.transitions().items():
72 if key == TransitionKey.epsilon(): 79 if key == TransitionKey.epsilon():
73 key = "ε" 80 key = "ε"
74 for state in state_iterator(values): 81 for state in state_iterator(values):
75 action = state.action() 82 edge_content.append(" S_%s -> S_%s [ label = \"%s\" ];" % (
76 if action: 83 node.node_number(), state.node_number(), escape(key)))
77 content = " S_%s -> S_%s [ label = \"%s {%s}:%d\" ];" % ( 84 return (node_content, edge_content)
78 node.node_number(),
79 state.node_number(),
80 escape(key),
81 escape(action[1]),
82 action[0])
83 else:
84 content = " S_%s -> S_%s [ label = \"%s\" ];" % (
85 node.node_number(), state.node_number(), escape(key))
86 node_content.append(content)
87 return node_content
88 85
89 node_content = edge_iterator(f, [])
90 terminals = ["S_%d;" % x.node_number() for x in terminal_set] 86 terminals = ["S_%d;" % x.node_number() for x in terminal_set]
87 (node_content, edge_content) = edge_iterator(f, ([], []))
91 start_number = start_node.node_number() 88 start_number = start_node.node_number()
92 start_shape = "circle" 89 start_shape = "circle"
93 if start_node in terminal_set: 90 if start_node in terminal_set:
94 start_shape = "doublecircle" 91 start_shape = "doublecircle"
95 92
96 return ''' 93 return '''
97 digraph finite_state_machine { 94 digraph finite_state_machine {
98 rankdir=LR; 95 rankdir=LR;
99 node [shape = %s, style=filled, bgcolor=lightgrey]; S_%s 96 node [shape = %s, style=filled, bgcolor=lightgrey]; S_%s
100 node [shape = doublecircle, style=unfilled]; %s 97 node [shape = doublecircle, style=unfilled]; %s
101 node [shape = circle]; 98 node [shape = circle];
102 %s 99 %s
100 %s
103 } 101 }
104 ''' % (start_shape, 102 ''' % (start_shape,
105 start_number, 103 start_number,
106 " ".join(terminals), 104 " ".join(terminals),
105 "\n".join(edge_content),
107 "\n".join(node_content)) 106 "\n".join(node_content))
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698