parser.py (6863:21fbf0412e0d) parser.py (6877:2a1a3d916ca8)
1# Copyright (c) 2009 The Hewlett-Packard Development Company
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Authors: Nathan Binkert
28
29import os.path
30import re
31import sys
32
33from m5.util.grammar import Grammar, TokenError, ParseError
34
35import slicc.ast as ast
36import slicc.util as util
37from slicc.symbols import SymbolTable
38
39def read_slicc(sources):
40 if not isinstance(sources, (list,tuple)):
41 sources = [ sources ]
42
43 for source in sources:
44 for sm_file in file(source, "r"):
45 sm_file = sm_file.strip()
46 if not sm_file:
47 continue
48 if sm_file.startswith("#"):
49 continue
50 yield sm_file
51
52class SLICC(Grammar):
53 def __init__(self, **kwargs):
54 super(SLICC, self).__init__(**kwargs)
55 self.decl_list_vec = []
56 self.current_file = None
57 self.symtab = SymbolTable()
58
59 def parse(self, filename):
60 self.current_file = filename
61 f = file(filename, 'r')
62 text = f.read()
63 try:
64 decl_list = super(SLICC, self).parse(text)
65 except (TokenError, ParseError), e:
66 sys.exit("%s: %s:%d" % (e, filename, e.token.lineno))
67 self.decl_list_vec.append(decl_list)
68 self.current_file = None
69
70 def _load(self, *filenames):
71 filenames = list(filenames)
72 while filenames:
73 f = filenames.pop(0)
74 if isinstance(f, (list, tuple)):
75 filenames[0:0] = list(f)
76 continue
77
78 yield f
79 if f.endswith(".slicc"):
80 dirname,basename = os.path.split(f)
81 filenames[0:0] = [ os.path.join(dirname, x) \
82 for x in read_slicc(f)]
83 else:
84 assert f.endswith(".sm")
85 self.parse(f)
86
87 def load(self, *filenames, **kwargs):
88 verbose = kwargs.pop("verbose", False)
89 if kwargs:
90 raise TypeError
91
92 gen = self._load(*filenames)
93 if verbose:
94 return gen
95 else:
96 # Run out the generator if we don't want the verbosity
97 for foo in gen:
98 pass
99
100 def findMachines(self):
101 for decl_list in self.decl_list_vec:
102 decl_list.findMachines()
103
104 def generate(self):
105 for decl_list in self.decl_list_vec:
106 decl_list.generate()
107
108 def writeCodeFiles(self, code_path):
109 util.makeDir(code_path)
110 self.symtab.writeCodeFiles(code_path)
111
112 def writeHTMLFiles(self, code_path):
113 util.makeDir(code_path)
114 self.symtab.writeHTMLFiles(code_path)
115
116 def files(self):
117 f = set([
1# Copyright (c) 2009 The Hewlett-Packard Development Company
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Authors: Nathan Binkert
28
29import os.path
30import re
31import sys
32
33from m5.util.grammar import Grammar, TokenError, ParseError
34
35import slicc.ast as ast
36import slicc.util as util
37from slicc.symbols import SymbolTable
38
39def read_slicc(sources):
40 if not isinstance(sources, (list,tuple)):
41 sources = [ sources ]
42
43 for source in sources:
44 for sm_file in file(source, "r"):
45 sm_file = sm_file.strip()
46 if not sm_file:
47 continue
48 if sm_file.startswith("#"):
49 continue
50 yield sm_file
51
52class SLICC(Grammar):
53 def __init__(self, **kwargs):
54 super(SLICC, self).__init__(**kwargs)
55 self.decl_list_vec = []
56 self.current_file = None
57 self.symtab = SymbolTable()
58
59 def parse(self, filename):
60 self.current_file = filename
61 f = file(filename, 'r')
62 text = f.read()
63 try:
64 decl_list = super(SLICC, self).parse(text)
65 except (TokenError, ParseError), e:
66 sys.exit("%s: %s:%d" % (e, filename, e.token.lineno))
67 self.decl_list_vec.append(decl_list)
68 self.current_file = None
69
70 def _load(self, *filenames):
71 filenames = list(filenames)
72 while filenames:
73 f = filenames.pop(0)
74 if isinstance(f, (list, tuple)):
75 filenames[0:0] = list(f)
76 continue
77
78 yield f
79 if f.endswith(".slicc"):
80 dirname,basename = os.path.split(f)
81 filenames[0:0] = [ os.path.join(dirname, x) \
82 for x in read_slicc(f)]
83 else:
84 assert f.endswith(".sm")
85 self.parse(f)
86
87 def load(self, *filenames, **kwargs):
88 verbose = kwargs.pop("verbose", False)
89 if kwargs:
90 raise TypeError
91
92 gen = self._load(*filenames)
93 if verbose:
94 return gen
95 else:
96 # Run out the generator if we don't want the verbosity
97 for foo in gen:
98 pass
99
100 def findMachines(self):
101 for decl_list in self.decl_list_vec:
102 decl_list.findMachines()
103
104 def generate(self):
105 for decl_list in self.decl_list_vec:
106 decl_list.generate()
107
108 def writeCodeFiles(self, code_path):
109 util.makeDir(code_path)
110 self.symtab.writeCodeFiles(code_path)
111
112 def writeHTMLFiles(self, code_path):
113 util.makeDir(code_path)
114 self.symtab.writeHTMLFiles(code_path)
115
116 def files(self):
117 f = set([
118 'ControllerFactory.cc',
119 'ControllerFactory.hh',
120 'MachineType.cc',
121 'MachineType.hh',
122 'Types.hh' ])
123
124 for decl_list in self.decl_list_vec:
125 f |= decl_list.files()
126
127 return f
128
129 t_ignore = '\t '
130
131 # C or C++ comment (ignore)
132 def t_c_comment(self, t):
133 r'/\*(.|\n)*?\*/'
134 t.lexer.lineno += t.value.count('\n')
135
136 def t_cpp_comment(self, t):
137 r'//.*'
138
139 # Define a rule so we can track line numbers
140 def t_newline(self, t):
141 r'\n+'
142 t.lexer.lineno += len(t.value)
143
144 reserved = {
145 'global' : 'GLOBAL',
146 'machine' : 'MACHINE',
147 'in_port' : 'IN_PORT',
148 'out_port' : 'OUT_PORT',
149 'action' : 'ACTION',
150 'transition' : 'TRANS',
151 'structure' : 'STRUCT',
152 'external_type' : 'EXTERN_TYPE',
153 'enumeration' : 'ENUM',
154 'peek' : 'PEEK',
155 'enqueue' : 'ENQUEUE',
156 'copy_head' : 'COPY_HEAD',
157 'check_allocate' : 'CHECK_ALLOCATE',
158 'check_stop_slots' : 'CHECK_STOP_SLOTS',
159 'if' : 'IF',
160 'else' : 'ELSE',
161 'return' : 'RETURN',
162 'THIS' : 'THIS',
163 'CHIP' : 'CHIP',
164 'void' : 'VOID',
165 'new' : 'NEW',
166 }
167
168 literals = ':[]{}(),='
169
170 tokens = [ 'EQ', 'NE', 'LT', 'GT', 'LE', 'GE',
171 'LEFTSHIFT', 'RIGHTSHIFT',
172 'NOT', 'AND', 'OR',
173 'PLUS', 'DASH', 'STAR', 'SLASH',
174 'DOUBLE_COLON', 'SEMI',
175 'ASSIGN', 'DOT',
176 'IDENT', 'LIT_BOOL', 'FLOATNUMBER', 'NUMBER', 'STRING' ]
177 tokens += reserved.values()
178
179 t_EQ = r'=='
180 t_NE = r'!='
181 t_LT = r'<'
182 t_GT = r'>'
183 t_LE = r'<='
184 t_GE = r'>='
185 t_LEFTSHIFT = r'<<'
186 t_RIGHTSHIFT = r'>>'
187 t_NOT = r'!'
188 t_AND = r'&&'
189 t_OR = r'\|\|'
190 t_PLUS = r'\+'
191 t_DASH = r'-'
192 t_STAR = r'\*'
193 t_SLASH = r'/'
194 t_DOUBLE_COLON = r'::'
195 t_SEMI = r';'
196 t_ASSIGN = r':='
197 t_DOT = r'\.'
198
199 precedence = (
200 ('left', 'AND', 'OR'),
201 ('left', 'EQ', 'NE'),
202 ('left', 'LT', 'GT', 'LE', 'GE'),
203 ('left', 'RIGHTSHIFT', 'LEFTSHIFT'),
204 ('left', 'PLUS', 'DASH'),
205 ('left', 'STAR', 'SLASH'),
206 ('right', 'NOT', 'UMINUS'),
207 )
208
209 def t_IDENT(self, t):
210 r'[a-zA-Z_][a-zA-Z_0-9]*'
211 if t.value == 'true':
212 t.type = 'LIT_BOOL'
213 t.value = True
214 return t
215
216 if t.value == 'false':
217 t.type = 'LIT_BOOL'
218 t.value = False
219 return t
220
221 # Check for reserved words
222 t.type = self.reserved.get(t.value, 'IDENT')
223 return t
224
225 def t_FLOATNUMBER(self, t):
226 '[0-9]+[.][0-9]+'
227 try:
228 t.value = float(t.value)
229 except ValueError:
230 raise TokenError("Illegal float", t)
231 return t
232
233 def t_NUMBER(self, t):
234 r'[0-9]+'
235 try:
236 t.value = int(t.value)
237 except ValueError:
238 raise TokenError("Illegal number", t)
239 return t
240
241 def t_STRING1(self, t):
242 r'\"[^"\n]*\"'
243 t.type = 'STRING'
244 t.value = t.value[1:-1]
245 return t
246
247 def t_STRING2(self, t):
248 r"\'[^'\n]*\'"
249 t.type = 'STRING'
250 t.value = t.value[1:-1]
251 return t
252
253 def p_file(self, p):
254 "file : decls"
255 p[0] = p[1]
256
257 def p_empty(self, p):
258 "empty :"
259
260 def p_decls(self, p):
261 "decls : declsx"
262 p[0] = ast.DeclListAST(self, p[1])
263
264 def p_declsx__list(self, p):
265 "declsx : decl declsx"
266 p[0] = [ p[1] ] + p[2]
267
268 def p_declsx__none(self, p):
269 "declsx : empty"
270 p[0] = []
271
272 def p_decl__machine(self, p):
273 "decl : MACHINE '(' ident pairs ')' ':' params '{' decls '}'"
274 p[0] = ast.MachineAST(self, p[3], p[4], p[7], p[9])
275
276 def p_decl__action(self, p):
277 "decl : ACTION '(' ident pairs ')' statements"
278 p[0] = ast.ActionDeclAST(self, p[3], p[4], p[6])
279
280 def p_decl__in_port(self, p):
281 "decl : IN_PORT '(' ident ',' type ',' var pairs ')' statements"
282 p[0] = ast.InPortDeclAST(self, p[3], p[5], p[7], p[8], p[10])
283
284 def p_decl__out_port(self, p):
285 "decl : OUT_PORT '(' ident ',' type ',' var pairs ')' SEMI"
286 p[0] = ast.OutPortDeclAST(self, p[3], p[5], p[7], p[8])
287
288 def p_decl__trans0(self, p):
289 "decl : TRANS '(' idents ',' idents ',' ident pairs ')' idents"
290 p[0] = ast.TransitionDeclAST(self, p[3], p[5], p[7], p[8], p[10])
291
292 def p_decl__trans1(self, p):
293 "decl : TRANS '(' idents ',' idents pairs ')' idents"
294 p[0] = ast.TransitionDeclAST(self, p[3], p[5], None, p[6], p[8])
295
296 def p_decl__extern0(self, p):
297 "decl : EXTERN_TYPE '(' type pairs ')' SEMI"
298 p[4]["external"] = "yes"
299 p[0] = ast.TypeDeclAST(self, p[3], p[4], [])
300
301 def p_decl__extern1(self, p):
302 "decl : EXTERN_TYPE '(' type pairs ')' '{' type_methods '}'"
303 p[4]["external"] = "yes"
304 p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7])
305
306 def p_decl__global(self, p):
307 "decl : GLOBAL '(' type pairs ')' '{' type_members '}'"
308 p[4]["global"] = "yes"
309 p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7])
310
311 def p_decl__struct(self, p):
312 "decl : STRUCT '(' type pairs ')' '{' type_members '}'"
313 p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7])
314
315 def p_decl__enum(self, p):
316 "decl : ENUM '(' type pairs ')' '{' type_enums '}'"
317 p[4]["enumeration"] = "yes"
318 p[0] = ast.EnumDeclAST(self, p[3], p[4], p[7])
319
320 def p_decl__object(self, p):
321 "decl : type ident pairs SEMI"
322 p[0] = ast.ObjDeclAST(self, p[1], p[2], p[3])
323
324 def p_decl__func_decl(self, p):
325 """decl : void ident '(' params ')' pairs SEMI
326 | type ident '(' params ')' pairs SEMI"""
327 p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], None)
328
329 def p_decl__func_def(self, p):
330 """decl : void ident '(' params ')' pairs statements
331 | type ident '(' params ')' pairs statements"""
332 p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], p[7])
333
334 # Type fields
335 def p_type_members__list(self, p):
336 "type_members : type_member type_members"
337 p[0] = [ p[1] ] + p[2]
338
339 def p_type_members__empty(self, p):
340 "type_members : empty"
341 p[0] = []
342
343 def p_type_member__1(self, p):
344 "type_member : type ident pairs SEMI"
345 p[0] = ast.TypeFieldMemberAST(self, p[1], p[2], p[3], None)
346
347 def p_type_member__2(self, p):
348 "type_member : type ident ASSIGN expr SEMI"
349 p[0] = ast.TypeFieldMemberAST(self, p[1], p[2],
350 ast.PairListAST(self), p[4])
351
352 # Methods
353 def p_type_methods__list(self, p):
354 "type_methods : type_method type_methods"
355 p[0] = [ p[1] ] + p[2]
356
357 def p_type_methods(self, p):
358 "type_methods : empty"
359 p[0] = []
360
361 def p_type_method(self, p):
362 "type_method : type_or_void ident '(' types ')' pairs SEMI"
363 p[0] = ast.TypeFieldMethodAST(self, p[1], p[2], p[4], p[6])
364
365 # Enum fields
366 def p_type_enums__list(self, p):
367 "type_enums : type_enum type_enums"
368 p[0] = [ p[1] ] + p[2]
369
370 def p_type_enums__empty(self, p):
371 "type_enums : empty"
372 p[0] = []
373
374 def p_type_enum(self, p):
375 "type_enum : ident pairs SEMI"
376 p[0] = ast.TypeFieldEnumAST(self, p[1], p[2])
377
378 # Type
379 def p_types__multiple(self, p):
380 "types : type ',' types"
381 p[0] = [ p[1] ] + p[3]
382
383 def p_types__one(self, p):
384 "types : type"
385 p[0] = [ p[1] ]
386
387 def p_types__empty(self, p):
388 "types : empty"
389 p[0] = []
390
391 def p_type(self, p):
392 "type : ident"
393 p[0] = ast.TypeAST(self, p[1])
394
395 def p_void(self, p):
396 "void : VOID"
397 p[0] = ast.TypeAST(self, p[1])
398
399 def p_type_or_void(self, p):
400 """type_or_void : type
401 | void"""
402 p[0] = p[1]
403
404 # Formal Param
405 def p_params__many(self, p):
406 "params : param ',' params"
407 p[0] = [ p[1] ] + p[3]
408
409 def p_params__one(self, p):
410 "params : param"
411 p[0] = [ p[1] ]
412
413 def p_params__none(self, p):
414 "params : empty"
415 p[0] = []
416
417 def p_param(self, p):
418 "param : type ident"
419 p[0] = ast.FormalParamAST(self, p[1], p[2])
420
118 'MachineType.cc',
119 'MachineType.hh',
120 'Types.hh' ])
121
122 for decl_list in self.decl_list_vec:
123 f |= decl_list.files()
124
125 return f
126
127 t_ignore = '\t '
128
129 # C or C++ comment (ignore)
130 def t_c_comment(self, t):
131 r'/\*(.|\n)*?\*/'
132 t.lexer.lineno += t.value.count('\n')
133
134 def t_cpp_comment(self, t):
135 r'//.*'
136
137 # Define a rule so we can track line numbers
138 def t_newline(self, t):
139 r'\n+'
140 t.lexer.lineno += len(t.value)
141
142 reserved = {
143 'global' : 'GLOBAL',
144 'machine' : 'MACHINE',
145 'in_port' : 'IN_PORT',
146 'out_port' : 'OUT_PORT',
147 'action' : 'ACTION',
148 'transition' : 'TRANS',
149 'structure' : 'STRUCT',
150 'external_type' : 'EXTERN_TYPE',
151 'enumeration' : 'ENUM',
152 'peek' : 'PEEK',
153 'enqueue' : 'ENQUEUE',
154 'copy_head' : 'COPY_HEAD',
155 'check_allocate' : 'CHECK_ALLOCATE',
156 'check_stop_slots' : 'CHECK_STOP_SLOTS',
157 'if' : 'IF',
158 'else' : 'ELSE',
159 'return' : 'RETURN',
160 'THIS' : 'THIS',
161 'CHIP' : 'CHIP',
162 'void' : 'VOID',
163 'new' : 'NEW',
164 }
165
166 literals = ':[]{}(),='
167
168 tokens = [ 'EQ', 'NE', 'LT', 'GT', 'LE', 'GE',
169 'LEFTSHIFT', 'RIGHTSHIFT',
170 'NOT', 'AND', 'OR',
171 'PLUS', 'DASH', 'STAR', 'SLASH',
172 'DOUBLE_COLON', 'SEMI',
173 'ASSIGN', 'DOT',
174 'IDENT', 'LIT_BOOL', 'FLOATNUMBER', 'NUMBER', 'STRING' ]
175 tokens += reserved.values()
176
177 t_EQ = r'=='
178 t_NE = r'!='
179 t_LT = r'<'
180 t_GT = r'>'
181 t_LE = r'<='
182 t_GE = r'>='
183 t_LEFTSHIFT = r'<<'
184 t_RIGHTSHIFT = r'>>'
185 t_NOT = r'!'
186 t_AND = r'&&'
187 t_OR = r'\|\|'
188 t_PLUS = r'\+'
189 t_DASH = r'-'
190 t_STAR = r'\*'
191 t_SLASH = r'/'
192 t_DOUBLE_COLON = r'::'
193 t_SEMI = r';'
194 t_ASSIGN = r':='
195 t_DOT = r'\.'
196
197 precedence = (
198 ('left', 'AND', 'OR'),
199 ('left', 'EQ', 'NE'),
200 ('left', 'LT', 'GT', 'LE', 'GE'),
201 ('left', 'RIGHTSHIFT', 'LEFTSHIFT'),
202 ('left', 'PLUS', 'DASH'),
203 ('left', 'STAR', 'SLASH'),
204 ('right', 'NOT', 'UMINUS'),
205 )
206
207 def t_IDENT(self, t):
208 r'[a-zA-Z_][a-zA-Z_0-9]*'
209 if t.value == 'true':
210 t.type = 'LIT_BOOL'
211 t.value = True
212 return t
213
214 if t.value == 'false':
215 t.type = 'LIT_BOOL'
216 t.value = False
217 return t
218
219 # Check for reserved words
220 t.type = self.reserved.get(t.value, 'IDENT')
221 return t
222
223 def t_FLOATNUMBER(self, t):
224 '[0-9]+[.][0-9]+'
225 try:
226 t.value = float(t.value)
227 except ValueError:
228 raise TokenError("Illegal float", t)
229 return t
230
231 def t_NUMBER(self, t):
232 r'[0-9]+'
233 try:
234 t.value = int(t.value)
235 except ValueError:
236 raise TokenError("Illegal number", t)
237 return t
238
239 def t_STRING1(self, t):
240 r'\"[^"\n]*\"'
241 t.type = 'STRING'
242 t.value = t.value[1:-1]
243 return t
244
245 def t_STRING2(self, t):
246 r"\'[^'\n]*\'"
247 t.type = 'STRING'
248 t.value = t.value[1:-1]
249 return t
250
251 def p_file(self, p):
252 "file : decls"
253 p[0] = p[1]
254
255 def p_empty(self, p):
256 "empty :"
257
258 def p_decls(self, p):
259 "decls : declsx"
260 p[0] = ast.DeclListAST(self, p[1])
261
262 def p_declsx__list(self, p):
263 "declsx : decl declsx"
264 p[0] = [ p[1] ] + p[2]
265
266 def p_declsx__none(self, p):
267 "declsx : empty"
268 p[0] = []
269
270 def p_decl__machine(self, p):
271 "decl : MACHINE '(' ident pairs ')' ':' params '{' decls '}'"
272 p[0] = ast.MachineAST(self, p[3], p[4], p[7], p[9])
273
274 def p_decl__action(self, p):
275 "decl : ACTION '(' ident pairs ')' statements"
276 p[0] = ast.ActionDeclAST(self, p[3], p[4], p[6])
277
278 def p_decl__in_port(self, p):
279 "decl : IN_PORT '(' ident ',' type ',' var pairs ')' statements"
280 p[0] = ast.InPortDeclAST(self, p[3], p[5], p[7], p[8], p[10])
281
282 def p_decl__out_port(self, p):
283 "decl : OUT_PORT '(' ident ',' type ',' var pairs ')' SEMI"
284 p[0] = ast.OutPortDeclAST(self, p[3], p[5], p[7], p[8])
285
286 def p_decl__trans0(self, p):
287 "decl : TRANS '(' idents ',' idents ',' ident pairs ')' idents"
288 p[0] = ast.TransitionDeclAST(self, p[3], p[5], p[7], p[8], p[10])
289
290 def p_decl__trans1(self, p):
291 "decl : TRANS '(' idents ',' idents pairs ')' idents"
292 p[0] = ast.TransitionDeclAST(self, p[3], p[5], None, p[6], p[8])
293
294 def p_decl__extern0(self, p):
295 "decl : EXTERN_TYPE '(' type pairs ')' SEMI"
296 p[4]["external"] = "yes"
297 p[0] = ast.TypeDeclAST(self, p[3], p[4], [])
298
299 def p_decl__extern1(self, p):
300 "decl : EXTERN_TYPE '(' type pairs ')' '{' type_methods '}'"
301 p[4]["external"] = "yes"
302 p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7])
303
304 def p_decl__global(self, p):
305 "decl : GLOBAL '(' type pairs ')' '{' type_members '}'"
306 p[4]["global"] = "yes"
307 p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7])
308
309 def p_decl__struct(self, p):
310 "decl : STRUCT '(' type pairs ')' '{' type_members '}'"
311 p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7])
312
313 def p_decl__enum(self, p):
314 "decl : ENUM '(' type pairs ')' '{' type_enums '}'"
315 p[4]["enumeration"] = "yes"
316 p[0] = ast.EnumDeclAST(self, p[3], p[4], p[7])
317
318 def p_decl__object(self, p):
319 "decl : type ident pairs SEMI"
320 p[0] = ast.ObjDeclAST(self, p[1], p[2], p[3])
321
322 def p_decl__func_decl(self, p):
323 """decl : void ident '(' params ')' pairs SEMI
324 | type ident '(' params ')' pairs SEMI"""
325 p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], None)
326
327 def p_decl__func_def(self, p):
328 """decl : void ident '(' params ')' pairs statements
329 | type ident '(' params ')' pairs statements"""
330 p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], p[7])
331
332 # Type fields
333 def p_type_members__list(self, p):
334 "type_members : type_member type_members"
335 p[0] = [ p[1] ] + p[2]
336
337 def p_type_members__empty(self, p):
338 "type_members : empty"
339 p[0] = []
340
341 def p_type_member__1(self, p):
342 "type_member : type ident pairs SEMI"
343 p[0] = ast.TypeFieldMemberAST(self, p[1], p[2], p[3], None)
344
345 def p_type_member__2(self, p):
346 "type_member : type ident ASSIGN expr SEMI"
347 p[0] = ast.TypeFieldMemberAST(self, p[1], p[2],
348 ast.PairListAST(self), p[4])
349
350 # Methods
351 def p_type_methods__list(self, p):
352 "type_methods : type_method type_methods"
353 p[0] = [ p[1] ] + p[2]
354
355 def p_type_methods(self, p):
356 "type_methods : empty"
357 p[0] = []
358
359 def p_type_method(self, p):
360 "type_method : type_or_void ident '(' types ')' pairs SEMI"
361 p[0] = ast.TypeFieldMethodAST(self, p[1], p[2], p[4], p[6])
362
363 # Enum fields
364 def p_type_enums__list(self, p):
365 "type_enums : type_enum type_enums"
366 p[0] = [ p[1] ] + p[2]
367
368 def p_type_enums__empty(self, p):
369 "type_enums : empty"
370 p[0] = []
371
372 def p_type_enum(self, p):
373 "type_enum : ident pairs SEMI"
374 p[0] = ast.TypeFieldEnumAST(self, p[1], p[2])
375
376 # Type
377 def p_types__multiple(self, p):
378 "types : type ',' types"
379 p[0] = [ p[1] ] + p[3]
380
381 def p_types__one(self, p):
382 "types : type"
383 p[0] = [ p[1] ]
384
385 def p_types__empty(self, p):
386 "types : empty"
387 p[0] = []
388
389 def p_type(self, p):
390 "type : ident"
391 p[0] = ast.TypeAST(self, p[1])
392
393 def p_void(self, p):
394 "void : VOID"
395 p[0] = ast.TypeAST(self, p[1])
396
397 def p_type_or_void(self, p):
398 """type_or_void : type
399 | void"""
400 p[0] = p[1]
401
402 # Formal Param
403 def p_params__many(self, p):
404 "params : param ',' params"
405 p[0] = [ p[1] ] + p[3]
406
407 def p_params__one(self, p):
408 "params : param"
409 p[0] = [ p[1] ]
410
411 def p_params__none(self, p):
412 "params : empty"
413 p[0] = []
414
415 def p_param(self, p):
416 "param : type ident"
417 p[0] = ast.FormalParamAST(self, p[1], p[2])
418
419 def p_param__default(self, p):
420 "param : type ident '=' NUMBER"
421 p[0] = ast.FormalParamAST(self, p[1], p[2], p[4])
422
421 # Idents and lists
422 def p_idents__braced(self, p):
423 "idents : '{' identx '}'"
424 p[0] = p[2]
425
426 def p_idents__bare(self, p):
427 "idents : ident"
428 p[0] = [ p[1] ]
429
430 def p_identx__multiple_1(self, p):
431 """identx : ident SEMI identx
432 | ident ',' identx"""
433 p[0] = [ p[1] ] + p[3]
434
435 def p_identx__multiple_2(self, p):
436 "identx : ident identx"
437 p[0] = [ p[1] ] + p[2]
438
439 def p_identx__single(self, p):
440 "identx : empty"
441 p[0] = [ ]
442
443 def p_ident(self, p):
444 "ident : IDENT"
445 p[0] = p[1]
446
447 # Pair and pair lists
448 def p_pairs__list(self, p):
449 "pairs : ',' pairsx"
450 p[0] = p[2]
451
452 def p_pairs__empty(self, p):
453 "pairs : empty"
454 p[0] = ast.PairListAST(self)
455
456 def p_pairsx__many(self, p):
457 "pairsx : pair ',' pairsx"
458 p[0] = p[3]
459 p[0].addPair(p[1])
460
461 def p_pairsx__one(self, p):
462 "pairsx : pair"
463 p[0] = ast.PairListAST(self)
464 p[0].addPair(p[1])
465
466 def p_pair__assign(self, p):
467 """pair : ident '=' STRING
468 | ident '=' ident"""
469 p[0] = ast.PairAST(self, p[1], p[3])
470
471 def p_pair__literal(self, p):
472 "pair : STRING"
473 p[0] = ast.PairAST(self, "short", p[1])
474
475 # Below are the rules for action descriptions
476 def p_statements__inner(self, p):
477 "statements : '{' statements_inner '}'"
478 p[0] = ast.StatementListAST(self, p[2])
479
480 def p_statements__none(self, p):
481 "statements : '{' '}'"
482 p[0] = ast.StatementListAST(self, [])
483
484 def p_statements_inner__many(self, p):
485 "statements_inner : statement statements_inner"
486 p[0] = [ p[1] ] + p[2]
487
488 def p_statements_inner__one(self, p):
489 "statements_inner : statement"
490 p[0] = [ p[1] ]
491
492 def p_exprs__multiple(self, p):
493 "exprs : expr ',' exprs"
494 p[0] = [ p[1] ] + p[3]
495
496 def p_exprs__one(self, p):
497 "exprs : expr"
498 p[0] = [ p[1] ]
499
500 def p_exprs__empty(self, p):
501 "exprs : empty"""
502 p[0] = []
503
504 def p_statement__expression(self, p):
505 "statement : expr SEMI"
506 p[0] = ast.ExprStatementAST(self, p[1])
507
508 def p_statement__assign(self, p):
509 "statement : expr ASSIGN expr SEMI"
510 p[0] = ast.AssignStatementAST(self, p[1], p[3])
511
512 def p_statement__enqueue(self, p):
513 "statement : ENQUEUE '(' var ',' type pairs ')' statements"
514 p[0] = ast.EnqueueStatementAST(self, p[3], p[5], p[6], p[8])
515
516 def p_statement__peek(self, p):
517 "statement : PEEK '(' var ',' type pairs ')' statements"
518 p[0] = ast.PeekStatementAST(self, p[3], p[5], p[6], p[8], "peek")
519
520 def p_statement__copy_head(self, p):
521 "statement : COPY_HEAD '(' var ',' var pairs ')' SEMI"
522 p[0] = ast.CopyHeadStatementAST(self, p[3], p[5], p[6])
523
524 def p_statement__check_allocate(self, p):
525 "statement : CHECK_ALLOCATE '(' var ')' SEMI"
526 p[0] = ast.CheckAllocateStatementAST(self, p[3])
527
528 def p_statement__check_stop(self, p):
529 "statement : CHECK_STOP_SLOTS '(' var ',' STRING ',' STRING ')' SEMI"
530 p[0] = ast.CheckStopStatementAST(self, p[3], p[5], p[7])
531
532 def p_statement__return(self, p):
533 "statement : RETURN expr SEMI"
534 p[0] = ast.ReturnStatementAST(self, p[2])
535
536 def p_statement__if(self, p):
537 "statement : if_statement"
538 p[0] = p[1]
539
540 def p_if_statement__if(self, p):
541 "if_statement : IF '(' expr ')' statements"
542 p[0] = ast.IfStatementAST(self, p[3], p[5], None)
543
544 def p_if_statement__if_else(self, p):
545 "if_statement : IF '(' expr ')' statements ELSE statements"
546 p[0] = ast.IfStatementAST(self, p[3], p[5], p[7])
547
548 def p_statement__if_else_if(self, p):
549 "if_statement : IF '(' expr ')' statements ELSE if_statement"
550 p[0] = ast.IfStatementAST(self, p[3], p[5],
551 ast.StatementListAST(self, p[7]))
552
553 def p_expr__var(self, p):
554 "aexpr : var"
555 p[0] = p[1]
556
557 def p_expr__literal(self, p):
558 "aexpr : literal"
559 p[0] = p[1]
560
561 def p_expr__enumeration(self, p):
562 "aexpr : enumeration"
563 p[0] = p[1]
564
565 def p_expr__func_call(self, p):
566 "aexpr : ident '(' exprs ')'"
567 p[0] = ast.FuncCallExprAST(self, p[1], p[3])
568
569 def p_expr__new(self, p):
570 "aexpr : NEW type"
571 p[0] = ast.NewExprAST(self, p[2])
572
573 # globally access a local chip component and call a method
574 def p_expr__local_chip_method(self, p):
575 "aexpr : THIS DOT var '[' expr ']' DOT var DOT ident '(' exprs ')'"
576 p[0] = ast.LocalChipMethodAST(self, p[3], p[5], p[8], p[10], p[12])
577
578 # globally access a local chip component and access a data member
579 def p_expr__local_chip_member(self, p):
580 "aexpr : THIS DOT var '[' expr ']' DOT var DOT field"
581 p[0] = ast.LocalChipMemberAST(self, p[3], p[5], p[8], p[10])
582
583 # globally access a specified chip component and call a method
584 def p_expr__specified_chip_method(self, p):
585 "aexpr : CHIP '[' expr ']' DOT var '[' expr ']' DOT var DOT ident '(' exprs ')'"
586 p[0] = ast.SpecifiedChipMethodAST(self, p[3], p[6], p[8], p[11], p[13],
587 p[15])
588
589 # globally access a specified chip component and access a data member
590 def p_expr__specified_chip_member(self, p):
591 "aexpr : CHIP '[' expr ']' DOT var '[' expr ']' DOT var DOT field"
592 p[0] = ast.SpecifiedChipMemberAST(self, p[3], p[6], p[8], p[11], p[13])
593
594 def p_expr__member(self, p):
595 "aexpr : aexpr DOT ident"
596 p[0] = ast.MemberExprAST(self, p[1], p[3])
597
598 def p_expr__member_method_call(self, p):
599 "aexpr : aexpr DOT ident '(' exprs ')'"
600 p[0] = ast.MemberMethodCallExprAST(self, p[1], p[3], p[5])
601
602 def p_expr__member_method_call_lookup(self, p):
603 "aexpr : aexpr '[' exprs ']'"
604 p[0] = ast.MemberMethodCallExprAST(self, p[1], "lookup", p[3])
605
606 def p_expr__class_method_call(self, p):
607 "aexpr : type DOUBLE_COLON ident '(' exprs ')'"
608 p[0] = ast.ClassMethodCallExprAST(self, p[1], p[3], p[5])
609
610 def p_expr__aexpr(self, p):
611 "expr : aexpr"
612 p[0] = p[1]
613
614 def p_expr__binary_op(self, p):
615 """expr : expr STAR expr
616 | expr SLASH expr
617 | expr PLUS expr
618 | expr DASH expr
619 | expr LT expr
620 | expr GT expr
621 | expr LE expr
622 | expr GE expr
623 | expr EQ expr
624 | expr NE expr
625 | expr AND expr
626 | expr OR expr
627 | expr RIGHTSHIFT expr
628 | expr LEFTSHIFT expr"""
629 p[0] = ast.InfixOperatorExprAST(self, p[1], p[2], p[3])
630
631 # FIXME - unary not
632 def p_expr__unary_op(self, p):
633 """expr : NOT expr
634 | DASH expr %prec UMINUS"""
635 p[0] = PrefixOperatorExpr(p[1], p[2])
636
637 def p_expr__parens(self, p):
638 "aexpr : '(' expr ')'"
639 p[0] = p[2]
640
641 def p_literal__string(self, p):
642 "literal : STRING"
643 p[0] = ast.LiteralExprAST(self, p[1], "string")
644
645 def p_literal__number(self, p):
646 "literal : NUMBER"
647 p[0] = ast.LiteralExprAST(self, p[1], "int")
648
649 def p_literal__float(self, p):
650 "literal : FLOATNUMBER"
651 p[0] = ast.LiteralExprAST(self, p[1], "int")
652
653 def p_literal__bool(self, p):
654 "literal : LIT_BOOL"
655 p[0] = ast.LiteralExprAST(self, p[1], "bool")
656
657 def p_enumeration(self, p):
658 "enumeration : ident ':' ident"
659 p[0] = ast.EnumExprAST(self, ast.TypeAST(self, p[1]), p[3])
660
661 def p_var(self, p):
662 "var : ident"
663 p[0] = ast.VarExprAST(self, p[1])
664
665 def p_field(self, p):
666 "field : ident"
667 p[0] = p[1]
423 # Idents and lists
424 def p_idents__braced(self, p):
425 "idents : '{' identx '}'"
426 p[0] = p[2]
427
428 def p_idents__bare(self, p):
429 "idents : ident"
430 p[0] = [ p[1] ]
431
432 def p_identx__multiple_1(self, p):
433 """identx : ident SEMI identx
434 | ident ',' identx"""
435 p[0] = [ p[1] ] + p[3]
436
437 def p_identx__multiple_2(self, p):
438 "identx : ident identx"
439 p[0] = [ p[1] ] + p[2]
440
441 def p_identx__single(self, p):
442 "identx : empty"
443 p[0] = [ ]
444
445 def p_ident(self, p):
446 "ident : IDENT"
447 p[0] = p[1]
448
449 # Pair and pair lists
450 def p_pairs__list(self, p):
451 "pairs : ',' pairsx"
452 p[0] = p[2]
453
454 def p_pairs__empty(self, p):
455 "pairs : empty"
456 p[0] = ast.PairListAST(self)
457
458 def p_pairsx__many(self, p):
459 "pairsx : pair ',' pairsx"
460 p[0] = p[3]
461 p[0].addPair(p[1])
462
463 def p_pairsx__one(self, p):
464 "pairsx : pair"
465 p[0] = ast.PairListAST(self)
466 p[0].addPair(p[1])
467
468 def p_pair__assign(self, p):
469 """pair : ident '=' STRING
470 | ident '=' ident"""
471 p[0] = ast.PairAST(self, p[1], p[3])
472
473 def p_pair__literal(self, p):
474 "pair : STRING"
475 p[0] = ast.PairAST(self, "short", p[1])
476
477 # Below are the rules for action descriptions
478 def p_statements__inner(self, p):
479 "statements : '{' statements_inner '}'"
480 p[0] = ast.StatementListAST(self, p[2])
481
482 def p_statements__none(self, p):
483 "statements : '{' '}'"
484 p[0] = ast.StatementListAST(self, [])
485
486 def p_statements_inner__many(self, p):
487 "statements_inner : statement statements_inner"
488 p[0] = [ p[1] ] + p[2]
489
490 def p_statements_inner__one(self, p):
491 "statements_inner : statement"
492 p[0] = [ p[1] ]
493
494 def p_exprs__multiple(self, p):
495 "exprs : expr ',' exprs"
496 p[0] = [ p[1] ] + p[3]
497
498 def p_exprs__one(self, p):
499 "exprs : expr"
500 p[0] = [ p[1] ]
501
502 def p_exprs__empty(self, p):
503 "exprs : empty"""
504 p[0] = []
505
506 def p_statement__expression(self, p):
507 "statement : expr SEMI"
508 p[0] = ast.ExprStatementAST(self, p[1])
509
510 def p_statement__assign(self, p):
511 "statement : expr ASSIGN expr SEMI"
512 p[0] = ast.AssignStatementAST(self, p[1], p[3])
513
514 def p_statement__enqueue(self, p):
515 "statement : ENQUEUE '(' var ',' type pairs ')' statements"
516 p[0] = ast.EnqueueStatementAST(self, p[3], p[5], p[6], p[8])
517
518 def p_statement__peek(self, p):
519 "statement : PEEK '(' var ',' type pairs ')' statements"
520 p[0] = ast.PeekStatementAST(self, p[3], p[5], p[6], p[8], "peek")
521
522 def p_statement__copy_head(self, p):
523 "statement : COPY_HEAD '(' var ',' var pairs ')' SEMI"
524 p[0] = ast.CopyHeadStatementAST(self, p[3], p[5], p[6])
525
526 def p_statement__check_allocate(self, p):
527 "statement : CHECK_ALLOCATE '(' var ')' SEMI"
528 p[0] = ast.CheckAllocateStatementAST(self, p[3])
529
530 def p_statement__check_stop(self, p):
531 "statement : CHECK_STOP_SLOTS '(' var ',' STRING ',' STRING ')' SEMI"
532 p[0] = ast.CheckStopStatementAST(self, p[3], p[5], p[7])
533
534 def p_statement__return(self, p):
535 "statement : RETURN expr SEMI"
536 p[0] = ast.ReturnStatementAST(self, p[2])
537
538 def p_statement__if(self, p):
539 "statement : if_statement"
540 p[0] = p[1]
541
542 def p_if_statement__if(self, p):
543 "if_statement : IF '(' expr ')' statements"
544 p[0] = ast.IfStatementAST(self, p[3], p[5], None)
545
546 def p_if_statement__if_else(self, p):
547 "if_statement : IF '(' expr ')' statements ELSE statements"
548 p[0] = ast.IfStatementAST(self, p[3], p[5], p[7])
549
550 def p_statement__if_else_if(self, p):
551 "if_statement : IF '(' expr ')' statements ELSE if_statement"
552 p[0] = ast.IfStatementAST(self, p[3], p[5],
553 ast.StatementListAST(self, p[7]))
554
555 def p_expr__var(self, p):
556 "aexpr : var"
557 p[0] = p[1]
558
559 def p_expr__literal(self, p):
560 "aexpr : literal"
561 p[0] = p[1]
562
563 def p_expr__enumeration(self, p):
564 "aexpr : enumeration"
565 p[0] = p[1]
566
567 def p_expr__func_call(self, p):
568 "aexpr : ident '(' exprs ')'"
569 p[0] = ast.FuncCallExprAST(self, p[1], p[3])
570
571 def p_expr__new(self, p):
572 "aexpr : NEW type"
573 p[0] = ast.NewExprAST(self, p[2])
574
575 # globally access a local chip component and call a method
576 def p_expr__local_chip_method(self, p):
577 "aexpr : THIS DOT var '[' expr ']' DOT var DOT ident '(' exprs ')'"
578 p[0] = ast.LocalChipMethodAST(self, p[3], p[5], p[8], p[10], p[12])
579
580 # globally access a local chip component and access a data member
581 def p_expr__local_chip_member(self, p):
582 "aexpr : THIS DOT var '[' expr ']' DOT var DOT field"
583 p[0] = ast.LocalChipMemberAST(self, p[3], p[5], p[8], p[10])
584
585 # globally access a specified chip component and call a method
586 def p_expr__specified_chip_method(self, p):
587 "aexpr : CHIP '[' expr ']' DOT var '[' expr ']' DOT var DOT ident '(' exprs ')'"
588 p[0] = ast.SpecifiedChipMethodAST(self, p[3], p[6], p[8], p[11], p[13],
589 p[15])
590
591 # globally access a specified chip component and access a data member
592 def p_expr__specified_chip_member(self, p):
593 "aexpr : CHIP '[' expr ']' DOT var '[' expr ']' DOT var DOT field"
594 p[0] = ast.SpecifiedChipMemberAST(self, p[3], p[6], p[8], p[11], p[13])
595
596 def p_expr__member(self, p):
597 "aexpr : aexpr DOT ident"
598 p[0] = ast.MemberExprAST(self, p[1], p[3])
599
600 def p_expr__member_method_call(self, p):
601 "aexpr : aexpr DOT ident '(' exprs ')'"
602 p[0] = ast.MemberMethodCallExprAST(self, p[1], p[3], p[5])
603
604 def p_expr__member_method_call_lookup(self, p):
605 "aexpr : aexpr '[' exprs ']'"
606 p[0] = ast.MemberMethodCallExprAST(self, p[1], "lookup", p[3])
607
608 def p_expr__class_method_call(self, p):
609 "aexpr : type DOUBLE_COLON ident '(' exprs ')'"
610 p[0] = ast.ClassMethodCallExprAST(self, p[1], p[3], p[5])
611
612 def p_expr__aexpr(self, p):
613 "expr : aexpr"
614 p[0] = p[1]
615
616 def p_expr__binary_op(self, p):
617 """expr : expr STAR expr
618 | expr SLASH expr
619 | expr PLUS expr
620 | expr DASH expr
621 | expr LT expr
622 | expr GT expr
623 | expr LE expr
624 | expr GE expr
625 | expr EQ expr
626 | expr NE expr
627 | expr AND expr
628 | expr OR expr
629 | expr RIGHTSHIFT expr
630 | expr LEFTSHIFT expr"""
631 p[0] = ast.InfixOperatorExprAST(self, p[1], p[2], p[3])
632
633 # FIXME - unary not
634 def p_expr__unary_op(self, p):
635 """expr : NOT expr
636 | DASH expr %prec UMINUS"""
637 p[0] = PrefixOperatorExpr(p[1], p[2])
638
639 def p_expr__parens(self, p):
640 "aexpr : '(' expr ')'"
641 p[0] = p[2]
642
643 def p_literal__string(self, p):
644 "literal : STRING"
645 p[0] = ast.LiteralExprAST(self, p[1], "string")
646
647 def p_literal__number(self, p):
648 "literal : NUMBER"
649 p[0] = ast.LiteralExprAST(self, p[1], "int")
650
651 def p_literal__float(self, p):
652 "literal : FLOATNUMBER"
653 p[0] = ast.LiteralExprAST(self, p[1], "int")
654
655 def p_literal__bool(self, p):
656 "literal : LIT_BOOL"
657 p[0] = ast.LiteralExprAST(self, p[1], "bool")
658
659 def p_enumeration(self, p):
660 "enumeration : ident ':' ident"
661 p[0] = ast.EnumExprAST(self, ast.TypeAST(self, p[1]), p[3])
662
663 def p_var(self, p):
664 "var : ident"
665 p[0] = ast.VarExprAST(self, p[1])
666
667 def p_field(self, p):
668 "field : ident"
669 p[0] = p[1]