-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.py
114 lines (104 loc) · 2.57 KB
/
Test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# ------------------------------------------------------------
# calclex.py
#
# tokenizer for a simple expression evaluator for
# numbers and +,-,*,/
# ------------------------------------------------------------
import os
import ply.lex as lex
import github
with open("ejemploTopo.xml","r") as file: data=file.read().replace('\n', '')
# List of token names. This is always required
tokens = (
'TOPO',
'TOPOC',
'SWITCHES',
'SWITCHESC',
'SWITCH',
'SWITCHC',
'DPID',
'DPIDC',
'NAME',
'NAMEC',
'LINKS',
'LINKSC',
'SWITCHTOSWITCH',
'SWITCHTOSWITCHC',
'LINK',
'LINKC',
'PORT',
'PORTC',
'DESCRIPTION',
'DESCRIPTIONC',
'SWITCHTOOTHER',
'SWITCHTOOTHERC',
'NETWORKS',
'NETWORKSC',
'NETWORK',
'NETWORKC',
'ID',
'IDC',
'SUBNET',
'SUBNETC',
'NETMASK',
'NETMASKC',
'LETTERS',
'IP',
'NUMBER'
)
# Regular expression rules for simple tokens
t_TOPO = r'<topo>'
t_TOPOC = r'</topo>'
t_SWITCHES = r'<switches>'
t_SWITCHESC = r'</switches>'
t_SWITCH = r'<switch>'
t_SWITCHC = r'</switch>'
t_DPID = r'<dpid>'
t_DPIDC = r'</dpid>'
t_NAME = r'<name>'
t_NAMEC = r'</name>'
t_LINKS = r'<links>'
t_LINKSC = r'</links>'
t_SWITCHTOSWITCH = r'<switch-to-switch>'
t_SWITCHTOSWITCHC = r'</switch-to-switch>'
t_LINK = r'<link>'
t_LINKC = r'</link>'
t_PORT = r'<port>'
t_PORTC = r'</port>'
t_DESCRIPTION = r'<description>'
t_DESCRIPTIONC = r'</description>'
t_SWITCHTOOTHER = r'<switch-to-other>'
t_SWITCHTOOTHERC = r'</switch-to-other>'
t_NETWORKS = r'<networks>'
t_NETWORKSC = r'</networks>'
t_NETWORK = r'<network>'
t_NETWORKC = r'</network>'
t_ID = r'<id>'
t_IDC = r'</id>'
t_SUBNET = r'<subnet>'
t_SUBNETC = r'</subnet>'
t_NETMASK = r'<netmask>'
t_NETMASKC = r'</netmask>'
t_LETTERS = r'[a-zA-Z ]+'
t_IP = r'(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?){1,3}\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?){1,3}\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?){1,3}\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?){1,3}'
t_NUMBER = r'\d+'
# Define a rule so we can track line numbers
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
# A string containing ignored characters (spaces and tabs)
t_ignore = ' \t'
# Error handling rule
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Build the lexer
lexer = lex.lex()
# Give the lexer some input
lexer.input(data)
# Tokenize
while True:
tok = lexer.token()
if not tok:
break # No more input
print(tok)