Пример #1
0
        private bool Constructor_DEC(string AM,string N)
        {
            //FIRST(<Constructor_DEC>)
            if (tokenList[index].CP == "(")
            {
                string PL = "",NPL ="";
                string AL = "", NAL = "";
                if (tokenList[index].CP == "(")
                {
                    index++;
                    semanticAnalyzer.createScope();
                    if (List_Param_Dec(ref AL, PL, ref NAL, NPL))
                    {
                        if (tokenList[index].CP == ")")
                        {
                            index++;
                            string T = AL + "> Null"; 
                            MethodT cons = new MethodT(N, T, AM, "", true);
                            semanticAnalyzer.insertConstructor(cons);
                            if (AL != "")
                            {
                                string[] arr = AL.Split(',');
                                string[] Narr = NAL.Split(',');
                                for (int i = 0; i < arr.Length; i++)
                                {
                                    semanticAnalyzer.insertVariables(Narr[i], arr[i], semanticAnalyzer.getCurrentScope());
                                }

                            }
                            if (tokenList[index].CP == "{")
                            {
                                index++;
                                
                                string T1 = "";
                                if (M_ST(ref T1))
                                {
                                    if (tokenList[index].CP == "}")
                                    {
                                        index++;
                                        semanticAnalyzer.deleteScope();
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }
Пример #2
0
 private bool Object_List(string CN,string AM)
 {
     //FIRST(<Object_List>) = {,, ;}
     if (tokenList[index].CP == "," || tokenList[index].CP == ";")
     {
         //<Object_List>  , ID<Object_Creation_Exp>|;
         if (tokenList[index].CP == ",")
         {
             index++;
             if (tokenList[index].CP == "ID")
             {
                 string N = tokenList[index].VP;
                 MethodT mem = new MethodT(N, "object", AM, "", false);
                 if(semanticAnalyzer.MemberLookUp(mem))
                 {
                     addError("Object Redeclaration");
                 }
                 index++;
                 if (Object_Creation_Exp(CN,N,AM))
                 {
                     return true;
                 }
             }
         }
         else if (tokenList[index].CP == ";")
         {
             index++;
             return true;
         }
     }
     return false;
 }
Пример #3
0
 private bool Method_Call(ref string RT,string N)
 {
     //FIRST(<Method_Call_1>) = { ( }
     if (tokenList[index].CP == "(")
     {
         //<Method_Call_1>  (<Param>) ;
         if (tokenList[index].CP == "(")
         {
             string PL = "";
             string AL = "";
             string AQ = "";
             index++;
             if (Param_List(ref AL, PL, ref AQ))
             {
                 if (tokenList[index].CP == ")")
                 {
                     MethodT newMem = new MethodT(N, AL, "", "", true);
                     RT = semanticAnalyzer.SearchMember(semanticAnalyzer.getCurrentClass(), newMem);
                     if(RT == "invalid")
                     {
                         addError("Undeclared Member");
                     }
                     index++;
                     icg.gen("CALL " + semanticAnalyzer.getCurrentClass() +"_"+ N +"_"+AL+","+AQ);
                     return true;
                 }
             }
         }
     }
     return false;
 }
Пример #4
0
 private bool Array_DEC(string AM,string TM,string RT)
 {
     //FIRST(<Array_DEC>) = {[}
     if (tokenList[index].CP == "[")
     {
         //<Array_DEC>   [] ID <INIT_Array>
         if (tokenList[index].CP == "[")
         {
             index++;
             if (tokenList[index].CP == "]")
             {
                 index++;
                 RT += "[]";
                 if (tokenList[index].CP == "ID")
                 {
                     string N = tokenList[index].VP;
                     if (inMethod)
                     {
                         semanticAnalyzer.insertVariables(N, RT, semanticAnalyzer.getCurrentScope());
                     }
                     else
                     {
                         MethodT mem = new MethodT(N,RT,AM,TM,false);
                         semanticAnalyzer.insertMember(mem);
                     }
                     index++;
                     if (INIT_Array(RT))
                     {
                         return true;
                     }
                 }
             }
         }
     }
     return false;
 }
Пример #5
0
 private bool Object_Creation_Exp(string CN,string N,string AM)
 {
     //FIRST(<Object_Creation_Exp>) = {= , , , ;}
     if (tokenList[index].CP == "AOP" ||
         tokenList[index].CP == "," ||
         tokenList[index].CP == ";")
     {
         //<Object_Creation_Exp>  = new ID  (<List_Const>) <Object_List>  |<Object_List>
         if (tokenList[index].CP == "AOP")
         {
             index++;
             if (tokenList[index].CP == "new")
             {
                 index++;
                 if (tokenList[index].CP == "ID")
                 {
                     string CN1 = tokenList[index].VP;
                     if(!(CN == CN1))
                     {
                         addError("Class Mismatch");
                     }
                     index++;
                     if (tokenList[index].CP == "(")
                     {
                         string PL = "";
                         string AL = "";
                         string AQ = "";
                         index++;
                         if (Param_List(ref AL, PL, ref AQ))
                         {
                             if (tokenList[index].CP == ")")
                             {
                                 index++;
                                 MethodT mem = new MethodT(CN1, AL, AM, "", true);
                                 if(!semanticAnalyzer.LookUpContructor(mem))
                                 {
                                     addError("Constructor not Found");
                                 }
                                 MethodT mem1 = new MethodT(N, "object", AM, "", false);
                                 if(!semanticAnalyzer.MemberLookUp(mem1))
                                 {
                                     addError("Object Redeclaration");
                                 }
                                 else if(inMethod)
                                 {
                                     semanticAnalyzer.insertVariables(N, CN, semanticAnalyzer.getCurrentScope());
                                 }
                                 else
                                 {
                                     semanticAnalyzer.insertMember(mem1);
                                 }
                                 if (Object_List(CN,AM))
                                 {
                                     return true;
                                 }
                             }
                         }
                     }
                 }
             }
         }
         else if (Object_List(CN,AM))
         {
             return true;
         }
     }
     return false;
 }
Пример #6
0
        //Method Declaration
        private bool Method_Link(string AM, string TM, string RT, string N)
        {
            //FIRST(<Method_Link 3>)
            if (tokenList[index].CP == "(")
            {
                string PL = "", NPL = "";
                string AL = "", NAL = "";
                if (tokenList[index].CP == "(")
                {
                    semanticAnalyzer.createScope();
                    index++;
                    if (List_Param_Dec(ref AL, PL, ref NAL, NPL))
                    {
                        if (tokenList[index].CP == ")")
                        {
                            string T = AL + '>' + RT;
                            MethodT newM = new MethodT(N, T, AM, TM, true);
                            semanticAnalyzer.insertMember(newM);
                            if(AL != "")
                            {
                                string[] arr = AL.Split(',');
                                string[] Narr = NAL.Split(',');
                                for(int i =0;i<arr.Length;i++)
                                {
                                    semanticAnalyzer.insertVariables(Narr[i], arr[i], semanticAnalyzer.getCurrentScope());
                                }

                            }
                            index++;
                            inMethod = true;
                            if (tokenList[index].CP == "{")
                            {
                                icg.gen(semanticAnalyzer.getCurrentClass() + "_"+ N +"_"+AL+" Proc");
                                index++;
                                string T1 = "";
                                if (M_ST(ref T1))
                                {
                                    if (tokenList[index].CP == "}")
                                    {
                                        index++;
                                        icg.gen(semanticAnalyzer.getCurrentClass() + "_" + N + "_" + AL + " endP");
                                        inMethod = false;
                                        semanticAnalyzer.deleteScope();
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }
Пример #7
0
 private bool object_array_dec(string CN, string N1, string AM)
 {
     //FIRST(<object_array_dec>) = { = }
     if (tokenList[index].VP == "=")
     {
         //<object_array_dec>  = new ID[<Exp>]<obj_arr_dec1>
         if (tokenList[index].VP == "=")
         {
             index++;
             if (tokenList[index].CP == "new")
             {
                 index++;
                 if (tokenList[index].CP == "ID")
                 {
                     string N2 = tokenList[index].VP;
                     if (!semanticAnalyzer.ClassLookUp(N2))
                     {
                         addError("Undeclared Class");
                     }
                     index++;
                     if (tokenList[index].CP == "[")
                     {
                        index++;
                         string ET = "";
                         string Nexp = "";
                         if (Exp(ref ET,ref Nexp))
                         {
                             if (tokenList[index].CP == "]")
                             {
                                 index++;
                                 if (inMethod)
                                 {
                                     semanticAnalyzer.insertVariables(CN, N1, semanticAnalyzer.getCurrentScope());
                                 }
                                 else
                                 {
                                     MethodT mem = new MethodT(CN, N1, AM, "", false);
                                     semanticAnalyzer.insertMember(mem);
                                 }
                                 if (obj_arr_dec1())
                                 {
                                     return true;
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return false;
 }
Пример #8
0
        public string SearchMember(string classname, MethodT obj)
        {
            // classname.obj
            // a.b;   || a.b(int i, float j);
            for (int i = 0; i < MainSymbolTable.classes.Count; i++)
            {

                if (MainSymbolTable.classes[i].name == classname)
                {
                    ClassT currentClass = MainSymbolTable.classes[i];
                    
                    for (int j = 0; j < currentClass.classdata.Count; j++)
                    {
                        if (currentClass.classdata[j].name == obj.name)
                        {
                            MethodT currentMember = currentClass.classdata[j];
                            if (!obj.methodFlag && !currentMember.methodFlag) // check for variable of a class
                            {
                                obj.type = currentMember.type;
                                return obj.type;
                            }
                            else if (obj.methodFlag && currentMember.methodFlag) // check for method of a class
                            {
                                string paramWithRT = currentMember.type;
                                string[] param = paramWithRT.Split('>');
                                string RT = param[1];
                                string paramAL = param[0];
                                paramWithRT = obj.type;
                                param = paramWithRT.Split('>');
                                string objParam = param[0];
                                if (objParam == paramAL)
                                {
                                    obj.type = currentMember.type;
                                    return obj.type;
                                }
                            }
                            else
                            {
                                return "invalid";
                            }
                        }
                    } // For loop end for finding member of a class
                    return "invalid";
                }
            } // For loop end for finding classes 
            return "invalid";
        }
Пример #9
0
 public bool LookUpContructor(MethodT obj)
 {
     for (int j = 0; j < MainSymbolTable.classes.Count; j++)
     {
         if (MainSymbolTable.classes[j].name == obj.name)
         {
             foreach (MethodT i in MainSymbolTable.classes[j].classdata)
             {
                 if (obj.name == i.name)
                 {
                     if (obj.type == i.type)
                     {
                         return true;
                     }
                 }
             }
             return false;
         }
     }
     return false;
 }
Пример #10
0
        public bool MemberLookUp(MethodT obj)
        {
            //for methods
            bool paramNotMatch = false;
            if (obj.methodFlag)
            {
                for (int i = 0; i < MainSymbolTable.classes.Last().classdata.Count; i++)
                {
                    if (MainSymbolTable.classes.Last().classdata[i].methodFlag &&
                        MainSymbolTable.classes.Last().classdata[i].name == obj.name)
                    {
                        string ParamWithRT = MainSymbolTable.classes.Last().classdata[i].type;
                        string[] Param = ParamWithRT.Split('>');
                        string currentParam = Param[0];
                        ParamWithRT = obj.type;
                        Param = ParamWithRT.Split('>');
                        string objParam = Param[0];
                        if (objParam == currentParam)
                        {
                            paramNotMatch = true;
                        }
                        else
                        {
                            paramNotMatch = false;
                            break;
                        }
                    }
                }

                if (paramNotMatch)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            //for fields
            else
            {
                for (int i = 0; i < MainSymbolTable.classes.Last().classdata.Count; i++)
                {
                    if (!MainSymbolTable.classes.Last().classdata[i].methodFlag &&
                        MainSymbolTable.classes.Last().classdata[i].name == obj.name)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
Пример #11
0
        public bool insertMember(MethodT obj)
        {
            MethodT currentMember = (MethodT)obj.shallowCopy();
            if (MemberLookUp(currentMember))
            {
                errorlist.Add("Method Redeclaration Error " + currentMember.name + " "  + currentMember.type);
                return false;
            }
            else
            {
                MainSymbolTable.classes.Last().classdata.Add(currentMember);
                return true;
            }

        }
Пример #12
0
 public bool insertConstructor(MethodT obj)
 {
     if (obj.name == MainSymbolTable.classes.Last().name)
     {
         return insertMember(obj);
     }
     else
     {
         errorlist.Add("Constructor Redeclaration " + obj.name + " " + obj.type);
         return false;
     }
 }