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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
from sly import Lexer, Parser
class TimedWordLexer(Lexer):
tokens = {LPAREN, RPAREN, IDENTIFIER, NUMBER, ARGSEP, FUNCTIONSEP}
ignore = ' \t'
LPAREN = r'\('
RPAREN = r'\)'
IDENTIFIER = r'[a-zA-Z_][a-zA-Z0-9_]*'
NUMBER = r'[0-9e.]+'
ARGSEP = r','
FUNCTIONSEP = r';'
class TimedSequenceLexer(Lexer):
tokens = {LPAREN, RPAREN, LBRACE, RBRACE, CYCLE, IDENTIFIER, NUMBER, ARGSEP, FUNCTIONSEP}
ignore = ' \t'
LPAREN = r'\('
RPAREN = r'\)'
LBRACE = r'\{'
RBRACE = r'\}'
CYCLE = r'cycle'
IDENTIFIER = r'[a-zA-Z_][a-zA-Z0-9_]*'
NUMBER = r'[0-9e.]+'
ARGSEP = r','
FUNCTIONSEP = r';'
def error(self, t):
print("Illegal character '%s'" % t.value[0])
if t.value[0] == '{' and t.value.find('}'):
self.index += 1 + t.value.find('}')
else:
self.index += 1
class TimedWordParser(Parser):
tokens = TimedWordLexer.tokens
@_('timedSymbol FUNCTIONSEP timedWord')
def timedWord(self, p):
ret = [p.timedSymbol]
ret.extend(p.timedWord)
return ret
@_('timedSymbol FUNCTIONSEP', 'timedSymbol')
def timedWord(self, p):
return [p.timedSymbol]
@_('IDENTIFIER', 'IDENTIFIER LPAREN RPAREN')
def timedSymbol(self, p):
return (p.IDENTIFIER,)
@_('IDENTIFIER LPAREN args RPAREN')
def timedSymbol(self, p):
return (p.IDENTIFIER, *p.args)
@_('arg ARGSEP args')
def args(self, p):
ret = [p.arg]
ret.extend(p.args)
return ret
@_('arg')
def args(self, p):
return [p.arg]
@_('NUMBER')
def arg(self, p):
return float(p.NUMBER)
@_('IDENTIFIER')
def arg(self, p):
return p.IDENTIFIER
class TimedSequenceParser(Parser):
tokens = TimedSequenceLexer.tokens
@_('timedSequenceL', 'timedSequenceW')
def timedSequence(self, p):
return p[0]
@_('loop')
def timedSequenceL(self, p):
return [p.loop]
@_('loop timedSequenceW')
def timedSequenceL(self, p):
ret = [p.loop]
ret.extend(p.timedSequenceW)
return ret
@_('timedWord')
def timedSequenceW(self, p):
return [p.timedWord]
@_('timedWord timedSequenceL')
def timedSequenceW(self, p):
ret = [p.timedWord]
ret.extend(p.timedSequenceL)
return ret
@_('timedSymbol FUNCTIONSEP timedWord')
def timedWord(self, p):
p.timedWord.word.insert(0, p.timedSymbol)
return p.timedWord
@_('timedSymbol FUNCTIONSEP')
def timedWord(self, p):
return TimedWord(word=[p.timedSymbol])
@_('CYCLE LPAREN IDENTIFIER RPAREN LBRACE timedWord RBRACE')
def loop(self, p):
return Workload(p.IDENTIFIER, p.timedWord)
@_('IDENTIFIER', 'IDENTIFIER LPAREN RPAREN')
def timedSymbol(self, p):
return (p.IDENTIFIER,)
@_('IDENTIFIER LPAREN args RPAREN')
def timedSymbol(self, p):
return (p.IDENTIFIER, *p.args)
@_('arg ARGSEP args')
def args(self, p):
ret = [p.arg]
ret.extend(p.args)
return ret
@_('arg')
def args(self, p):
return [p.arg]
@_('NUMBER')
def arg(self, p):
return float(p.NUMBER)
@_('IDENTIFIER')
def arg(self, p):
return p.IDENTIFIER
def error(self, p):
if p:
print("Syntax error at token", p.type)
# Just discard the token and tell the parser it's okay.
self.errok()
else:
print("Syntax error at EOF")
class TimedWord:
def __init__(self, word_string=None, word=list()):
if word_string is not None:
lexer = TimedWordLexer()
parser = TimedWordParser()
self.word = parser.parse(lexer.tokenize(word_string))
else:
self.word = word
def __getitem__(self, item):
return self.word[item]
def __repr__(self):
ret = list()
for symbol in self.word:
ret.append('{}({})'.format(symbol[0], ', '.join(map(str, symbol[1:]))))
return 'TimedWord<"{}">'.format('; '.join(ret))
class Workload:
def __init__(self, name, word):
self.name = name
self.word = word
def __getitem__(self, item):
return self.word[item]
def __repr__(self):
return 'Workload("{}", {})'.format(self.name, self.word)
class TimedSequence:
def __init__(self, seq_string=None, seq=list()):
if seq_string is not None:
lexer = TimedSequenceLexer()
parser = TimedSequenceParser()
self.seq = parser.parse(lexer.tokenize(seq_string))
else:
self.seq = seq
def __getitem__(self, item):
return self.seq[item]
def __repr__(self):
ret = list()
for symbol in self.seq:
ret.append('{}'.format(symbol))
return 'TimedSequence(seq=[{}])'.format(', '.join(ret))
|