From b1d3873ec52692b0442666718da4175379697bb2 Mon Sep 17 00:00:00 2001 From: Nilay Vaish Date: Mon, 1 Sep 2014 16:55:44 -0500 Subject: 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. --- src/mem/slicc/parser.py | 132 +++++++++++++++++++++++++----------------------- 1 file changed, 69 insertions(+), 63 deletions(-) (limited to 'src/mem/slicc/parser.py') 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 '}'" -- cgit v1.2.3