summaryrefslogtreecommitdiff
path: root/src/mem/slicc/parser.py
diff options
context:
space:
mode:
authorNilay Vaish <nilay@cs.wisc.edu>2014-09-01 16:55:44 -0500
committerNilay Vaish <nilay@cs.wisc.edu>2014-09-01 16:55:44 -0500
commitb1d3873ec52692b0442666718da4175379697bb2 (patch)
treecff299c0befcef4f843272e1e9f633060fa8c815 /src/mem/slicc/parser.py
parent3202ec98e713d3f997e6b47f06451236c565f706 (diff)
downloadgem5-b1d3873ec52692b0442666718da4175379697bb2.tar.xz
ruby: slicc: improve the grammar
This patch changes the grammar for SLICC so as to remove some of the redundant / duplicate rules. In particular rules for object/variable declaration and class member declaration have been unified. Similarly, the rules for a general function and a class method have been unified. One more change is in the priority of two rules. The first rule is on declaring a function with all the params typed and named. The second rule is on declaring a function with all the params only typed. Earlier the second rule had a higher priority. Now the first rule has a higher priority.
Diffstat (limited to 'src/mem/slicc/parser.py')
-rw-r--r--src/mem/slicc/parser.py132
1 files changed, 69 insertions, 63 deletions
diff --git a/src/mem/slicc/parser.py b/src/mem/slicc/parser.py
index 1a8fbd937..04ba4640c 100644
--- a/src/mem/slicc/parser.py
+++ b/src/mem/slicc/parser.py
@@ -318,9 +318,41 @@ class SLICC(Grammar):
p[4]["state_decl"] = "yes"
p[0] = ast.StateDeclAST(self, p[3], p[4], p[7])
- def p_decl__object(self, p):
- "decl : type ident pairs SEMI"
- p[0] = ast.ObjDeclAST(self, p[1], p[2], p[3])
+ # Type fields
+ def p_type_members__list(self, p):
+ "type_members : type_member type_members"
+ p[0] = [ p[1] ] + p[2]
+
+ def p_type_members__empty(self, p):
+ "type_members : empty"
+ p[0] = []
+
+ def p_type_member__0(self, p):
+ """type_member : obj_decl
+ | func_decl
+ | func_def"""
+ p[0] = p[1]
+
+ # Member / Variable declarations
+ def p_decl__obj_decl(self, p):
+ "decl : obj_decl"
+ p[0] = p[1]
+
+ def p_obj_decl__0(self, p):
+ "obj_decl : type ident pairs SEMI"
+ p[0] = ast.ObjDeclAST(self, p[1], p[2], p[3], None)
+
+ def p_obj_decl__1(self, p):
+ "obj_decl : type STAR ident pairs SEMI"
+ p[0] = ast.ObjDeclAST(self, p[1], p[3], p[4], None)
+
+ def p_obj_decl__2(self, p):
+ "obj_decl : type ident ASSIGN expr SEMI"
+ p[0] = ast.ObjDeclAST(self, p[1], p[2], ast.PairListAST(self), p[4])
+
+ def p_obj_decl__3(self, p):
+ "obj_decl : type STAR ident ASSIGN expr SEMI"
+ p[0] = ast.ObjDeclAST(self, p[1], p[3], ast.PairListAST(self), p[5])
# Function definition and declaration
def p_decl__func_decl(self, p):
@@ -332,6 +364,11 @@ class SLICC(Grammar):
| type ident '(' params ')' pairs SEMI"""
p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], None)
+ def p_func_decl__1(self, p):
+ """func_decl : void ident '(' types ')' pairs SEMI
+ | type ident '(' types ')' pairs SEMI"""
+ p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], None)
+
def p_decl__func_def(self, p):
"decl : func_def"
p[0] = p[1]
@@ -341,32 +378,6 @@ class SLICC(Grammar):
| type ident '(' params ')' pairs statements"""
p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], p[7])
- # Type fields
- def p_type_members__list(self, p):
- "type_members : type_member type_members"
- p[0] = [ p[1] ] + p[2]
-
- def p_type_members__empty(self, p):
- "type_members : empty"
- p[0] = []
-
- def p_type_method__0(self, p):
- "type_member : type_or_void ident '(' types ')' pairs SEMI"
- p[0] = ast.TypeFieldMethodAST(self, p[1], p[2], p[4], p[6])
-
- def p_type_method__1(self, p):
- "type_member : type_or_void ident '(' params ')' pairs statements"
- p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], p[7])
-
- def p_type_member__1(self, p):
- "type_member : type_or_void ident pairs SEMI"
- p[0] = ast.TypeFieldMemberAST(self, p[1], p[2], p[3], None)
-
- def p_type_member__2(self, p):
- "type_member : type_or_void ident ASSIGN expr SEMI"
- p[0] = ast.TypeFieldMemberAST(self, p[1], p[2],
- ast.PairListAST(self), p[4])
-
# Enum fields
def p_type_enums__list(self, p):
"type_enums : type_enum type_enums"
@@ -393,40 +404,6 @@ class SLICC(Grammar):
"type_state : ident ',' enumeration pairs SEMI"
p[0] = ast.TypeFieldStateAST(self, p[1], p[3], p[4])
- # Type
- def p_types__multiple(self, p):
- "types : type ',' types"
- p[0] = [ p[1] ] + p[3]
-
- def p_types__one(self, p):
- "types : type"
- p[0] = [ p[1] ]
-
- def p_types__empty(self, p):
- "types : empty"
- p[0] = []
-
- def p_typestr__multi(self, p):
- "typestr : typestr DOUBLE_COLON ident"
- p[0] = '%s::%s' % (p[1], p[3])
-
- def p_typestr__single(self, p):
- "typestr : ident"
- p[0] = p[1]
-
- def p_type__one(self, p):
- "type : typestr"
- p[0] = ast.TypeAST(self, p[1])
-
- def p_void(self, p):
- "void : VOID"
- p[0] = ast.TypeAST(self, p[1])
-
- def p_type_or_void(self, p):
- """type_or_void : type
- | void"""
- p[0] = p[1]
-
# Formal Param
def p_params__many(self, p):
"params : param ',' params"
@@ -464,6 +441,35 @@ class SLICC(Grammar):
"param : type ident '=' STRING"
p[0] = ast.FormalParamAST(self, p[1], p[2], p[4])
+ # Type
+ def p_types__multiple(self, p):
+ "types : type ',' types"
+ p[0] = [ p[1] ] + p[3]
+
+ def p_types__one(self, p):
+ "types : type"
+ p[0] = [ p[1] ]
+
+ def p_types__empty(self, p):
+ "types : empty"
+ p[0] = []
+
+ def p_typestr__multi(self, p):
+ "typestr : typestr DOUBLE_COLON ident"
+ p[0] = '%s::%s' % (p[1], p[3])
+
+ def p_typestr__single(self, p):
+ "typestr : ident"
+ p[0] = p[1]
+
+ def p_type__one(self, p):
+ "type : typestr"
+ p[0] = ast.TypeAST(self, p[1])
+
+ def p_void(self, p):
+ "void : VOID"
+ p[0] = ast.TypeAST(self, p[1])
+
# Idents and lists
def p_idents__braced(self, p):
"idents : '{' identx '}'"