From 3d252f8e5fa2ec3f55730ab6d5d1a4a1b21b2cdf Mon Sep 17 00:00:00 2001 From: Nathan Binkert Date: Tue, 5 Jul 2011 18:30:04 -0700 Subject: grammar: better encapsulation of a grammar and parsing This makes it possible to use the grammar multiple times and use the multiple instances concurrently. This makes implementing an include statement as part of a grammar possible. --- src/mem/slicc/ast/AST.py | 2 +- src/mem/slicc/parser.py | 18 ++++++------------ 2 files changed, 7 insertions(+), 13 deletions(-) (limited to 'src/mem/slicc') diff --git a/src/mem/slicc/ast/AST.py b/src/mem/slicc/ast/AST.py index 8f0d06171..d098c8642 100644 --- a/src/mem/slicc/ast/AST.py +++ b/src/mem/slicc/ast/AST.py @@ -30,7 +30,7 @@ from slicc.util import PairContainer, Location class AST(PairContainer): def __init__(self, slicc, pairs=None): self.slicc = slicc - self.location = Location(slicc.current_file, slicc.lexer.lineno) + self.location = Location(slicc.current_source, slicc.current_line) self.pairs = {} if pairs: self.pairs.update(getattr(pairs, "pairs", pairs)) diff --git a/src/mem/slicc/parser.py b/src/mem/slicc/parser.py index aeda218f7..eb10c2dc4 100644 --- a/src/mem/slicc/parser.py +++ b/src/mem/slicc/parser.py @@ -31,7 +31,7 @@ import re import sys from m5.util import code_formatter -from m5.util.grammar import Grammar, TokenError, ParseError +from m5.util.grammar import Grammar, ParseError import slicc.ast as ast import slicc.util as util @@ -52,9 +52,7 @@ def read_slicc(sources): class SLICC(Grammar): def __init__(self, protocol, **kwargs): - super(SLICC, self).__init__(**kwargs) self.decl_list_vec = [] - self.current_file = None self.protocol = protocol self.symtab = SymbolTable(self) @@ -64,15 +62,11 @@ class SLICC(Grammar): return code def parse(self, filename): - self.current_file = filename - f = file(filename, 'r') - text = f.read() try: - decl_list = super(SLICC, self).parse(text) - except (TokenError, ParseError), e: - sys.exit("%s: %s:%d" % (e, filename, e.token.lineno)) + decl_list = self.parse_file(filename) + except ParseError, e: + sys.exit(str(e)) self.decl_list_vec.append(decl_list) - self.current_file = None def _load(self, *filenames): filenames = list(filenames) @@ -238,7 +232,7 @@ class SLICC(Grammar): try: t.value = float(t.value) except ValueError: - raise TokenError("Illegal float", t) + raise ParseError("Illegal float", t) return t def t_NUMBER(self, t): @@ -246,7 +240,7 @@ class SLICC(Grammar): try: t.value = int(t.value) except ValueError: - raise TokenError("Illegal number", t) + raise ParseError("Illegal number", t) return t def t_STRING1(self, t): -- cgit v1.2.3