parser.py (8453:82fc1267d3bb) | parser.py (8454:fad37c6670a6) |
---|---|
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 --- 23 unchanged lines hidden (view full) --- 32 33from m5.util import code_formatter 34from m5.util.grammar import Grammar, ParseError 35 36import slicc.ast as ast 37import slicc.util as util 38from slicc.symbols import SymbolTable 39 | 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 --- 23 unchanged lines hidden (view full) --- 32 33from m5.util import code_formatter 34from m5.util.grammar import Grammar, ParseError 35 36import slicc.ast as ast 37import slicc.util as util 38from slicc.symbols import SymbolTable 39 |
40def read_slicc(sources): 41 if not isinstance(sources, (list,tuple)): 42 sources = [ sources ] 43 44 for source in sources: 45 for sm_file in file(source, "r"): 46 sm_file = sm_file.strip() 47 if not sm_file: 48 continue 49 if sm_file.startswith("#"): 50 continue 51 yield sm_file 52 | |
53class SLICC(Grammar): | 40class SLICC(Grammar): |
54 def __init__(self, protocol, verbose=False): 55 self.decl_list_vec = [] 56 self.protocol = protocol | 41 def __init__(self, filename, verbose=False, traceback=False, **kwargs): 42 self.protocol = None 43 self.traceback = traceback |
57 self.verbose = verbose 58 self.symtab = SymbolTable(self) 59 | 44 self.verbose = verbose 45 self.symtab = SymbolTable(self) 46 |
47 try: 48 self.decl_list = self.parse_file(filename, **kwargs) 49 except ParseError, e: 50 if not self.traceback: 51 sys.exit(str(e)) 52 raise 53 |
|
60 def currentLocation(self): 61 return util.Location(self.current_source, self.current_line, 62 no_warning=not self.verbose) 63 64 def codeFormatter(self, *args, **kwargs): 65 code = code_formatter(*args, **kwargs) 66 code['protocol'] = self.protocol 67 return code 68 | 54 def currentLocation(self): 55 return util.Location(self.current_source, self.current_line, 56 no_warning=not self.verbose) 57 58 def codeFormatter(self, *args, **kwargs): 59 code = code_formatter(*args, **kwargs) 60 code['protocol'] = self.protocol 61 return code 62 |
69 def parse(self, filename): 70 try: 71 decl_list = self.parse_file(filename) 72 except ParseError, e: 73 sys.exit(str(e)) 74 self.decl_list_vec.append(decl_list) 75 76 def load(self, filenames): 77 filenames = list(filenames) 78 while filenames: 79 f = filenames.pop(0) 80 if isinstance(f, (list, tuple)): 81 filenames[0:0] = list(f) 82 continue 83 84 if f.endswith(".slicc"): 85 dirname,basename = os.path.split(f) 86 filenames[0:0] = [ os.path.join(dirname, x) \ 87 for x in read_slicc(f)] 88 else: 89 assert f.endswith(".sm") 90 self.parse(f) 91 | |
92 def process(self): | 63 def process(self): |
93 for decl_list in self.decl_list_vec: 94 decl_list.findMachines() | 64 self.decl_list.findMachines() 65 self.decl_list.generate() |
95 | 66 |
96 for decl_list in self.decl_list_vec: 97 decl_list.generate() 98 | |
99 def writeCodeFiles(self, code_path): 100 self.symtab.writeCodeFiles(code_path) 101 102 def writeHTMLFiles(self, html_path): 103 self.symtab.writeHTMLFiles(html_path) 104 105 def files(self): 106 f = set([ 107 'MachineType.cc', 108 'MachineType.hh', 109 'Types.hh' ]) 110 | 67 def writeCodeFiles(self, code_path): 68 self.symtab.writeCodeFiles(code_path) 69 70 def writeHTMLFiles(self, html_path): 71 self.symtab.writeHTMLFiles(html_path) 72 73 def files(self): 74 f = set([ 75 'MachineType.cc', 76 'MachineType.hh', 77 'Types.hh' ]) 78 |
111 for decl_list in self.decl_list_vec: 112 f |= decl_list.files() | 79 f |= self.decl_list.files() |
113 114 return f 115 116 t_ignore = '\t ' 117 118 # C or C++ comment (ignore) 119 def t_c_comment(self, t): 120 r'/\*(.|\n)*?\*/' 121 t.lexer.lineno += t.value.count('\n') 122 123 def t_cpp_comment(self, t): 124 r'//.*' 125 126 # Define a rule so we can track line numbers 127 def t_newline(self, t): 128 r'\n+' 129 t.lexer.lineno += len(t.value) 130 131 reserved = { | 80 81 return f 82 83 t_ignore = '\t ' 84 85 # C or C++ comment (ignore) 86 def t_c_comment(self, t): 87 r'/\*(.|\n)*?\*/' 88 t.lexer.lineno += t.value.count('\n') 89 90 def t_cpp_comment(self, t): 91 r'//.*' 92 93 # Define a rule so we can track line numbers 94 def t_newline(self, t): 95 r'\n+' 96 t.lexer.lineno += len(t.value) 97 98 reserved = { |
99 'protocol' : 'PROTOCOL', 100 'include' : 'INCLUDE', |
|
132 'global' : 'GLOBAL', 133 'machine' : 'MACHINE', 134 'in_port' : 'IN_PORT', 135 'out_port' : 'OUT_PORT', 136 'action' : 'ACTION', 137 'transition' : 'TRANS', 138 'structure' : 'STRUCT', 139 'external_type' : 'EXTERN_TYPE', --- 111 unchanged lines hidden (view full) --- 251 "empty :" 252 253 def p_decls(self, p): 254 "decls : declsx" 255 p[0] = ast.DeclListAST(self, p[1]) 256 257 def p_declsx__list(self, p): 258 "declsx : decl declsx" | 101 'global' : 'GLOBAL', 102 'machine' : 'MACHINE', 103 'in_port' : 'IN_PORT', 104 'out_port' : 'OUT_PORT', 105 'action' : 'ACTION', 106 'transition' : 'TRANS', 107 'structure' : 'STRUCT', 108 'external_type' : 'EXTERN_TYPE', --- 111 unchanged lines hidden (view full) --- 220 "empty :" 221 222 def p_decls(self, p): 223 "decls : declsx" 224 p[0] = ast.DeclListAST(self, p[1]) 225 226 def p_declsx__list(self, p): 227 "declsx : decl declsx" |
259 p[0] = [ p[1] ] + p[2] | 228 if isinstance(p[1], ast.DeclListAST): 229 decls = p[1].decls 230 elif p[1] is None: 231 decls = [] 232 else: 233 decls = [ p[1] ] 234 p[0] = decls + p[2] |
260 261 def p_declsx__none(self, p): 262 "declsx : empty" 263 p[0] = [] 264 | 235 236 def p_declsx__none(self, p): 237 "declsx : empty" 238 p[0] = [] 239 |
240 def p_decl__protocol(self, p): 241 "decl : PROTOCOL STRING SEMI" 242 if self.protocol: 243 msg = "Protocol can only be set once! Error at %s:%s\n" % \ 244 (self.current_source, self.current_line) 245 raise ParseError(msg) 246 self.protocol = p[2] 247 p[0] = None 248 249 def p_decl__include(self, p): 250 "decl : INCLUDE STRING SEMI" 251 dirname = os.path.dirname(self.current_source) 252 filename = os.path.join(dirname, p[2]) 253 p[0] = self.parse_file(filename) 254 |
|
265 def p_decl__machine(self, p): 266 "decl : MACHINE '(' ident pairs ')' ':' params '{' decls '}'" 267 p[0] = ast.MachineAST(self, p[3], p[4], p[7], p[9]) 268 269 def p_decl__action(self, p): 270 "decl : ACTION '(' ident pairs ')' statements" 271 p[0] = ast.ActionDeclAST(self, p[3], p[4], p[6]) 272 --- 450 unchanged lines hidden --- | 255 def p_decl__machine(self, p): 256 "decl : MACHINE '(' ident pairs ')' ':' params '{' decls '}'" 257 p[0] = ast.MachineAST(self, p[3], p[4], p[7], p[9]) 258 259 def p_decl__action(self, p): 260 "decl : ACTION '(' ident pairs ')' statements" 261 p[0] = ast.ActionDeclAST(self, p[3], p[4], p[6]) 262 --- 450 unchanged lines hidden --- |