Beispiel #1
0
        /// <summary>
        /// constructorul clasei sintactic, creaza un analizor sintactic
        /// </summary>
        /// <param name="lista">codul pascal inpartit pe linii</param>
        /// <param name="text">obiect de tip textbox in care se va afisa codul C care se produce in urma compilarii</param>
        public Sintactic(string[] lista, TextBox text)
        {
            this.lex = new Lexical(lista);
            this.text = text;
            this.text.Text = "";
            this.codPascal = lista;
            this.lex.Analizare();
            urmator_atom = lex.UrmatorAtom();

            lista_variabile.Add("integer", "tipvariabila");
            lista_variabile.Add("char", "tipvariabila");
            lista_variabile.Add("real", "tipvariabila");
            lista_variabile.Add("longint", "tipvariabila");
            lista_variabile.Add("record", "tipvariabila");
        }
Beispiel #2
0
 /// <summary>
 /// Functia cere urmatorul atom de la analizorul lexical
 /// </summary>
 private void NextAtom()
 {
     anterior_atom.indice = curent_atom.indice;
     anterior_atom.valoare = curent_atom.valoare;
     anterior_atom.linie = curent_atom.linie;
     curent_atom.linie = urmator_atom.linie;
     curent_atom.indice = urmator_atom.indice;
     curent_atom.valoare = urmator_atom.valoare;
     urmator_atom = lex.UrmatorAtom();
 }
Beispiel #3
0
        /// <summary>
        /// tratarea operatiei de asignare
        /// </summary>
        /// <returns>false in cazul unei erori sau a cazului in care nu este vorba de o asignare</returns>
        private bool Asignare()
        {
            if ((int)curent_atom.indice == (int)define.IDENTIFIER)
            {

                if (lista_variabile.Contains(curent_atom.valoare) == true)
                {
                    pr = "secund";
                    AdaugareInCod(TranslatareAtom(curent_atom));
                    tip_variabila = (string)lista_variabile[curent_atom.valoare];
                    NextAtom();
                    if ((int)curent_atom.indice == (int)define.ASSIGN)
                    {
                        AdaugareInCod(TranslatareAtom(curent_atom));
                        NextAtom();
                        Expresie_repetare();
                    }
                    else
                    {
                        eroare = true;
                        Errors("Eroare: Lipseste operatorul de atribuire:, \":=\"!");
                        AtomSincronizare();
                        return true;
                    }
                    return true;
                }
                else if ((int)urmator_atom.indice == (int)define.PARDL)
                    {
                        Atom curent = new Atom(curent_atom.indice, curent_atom.valoare, curent_atom.linie);
                        NextAtom();
                        if ((int)curent_atom.indice == (int)define.PARDL)
                        {
                            NextAtom();
                            if ((int)curent_atom.indice == (int)define.NRINTEGER)
                            {
                                int valoare = System.Convert.ToInt32(curent_atom.valoare);
                                NextAtom();
                                if ((int)curent_atom.indice == (int)define.PARDR)
                                {
                                    NextAtom();
                                    AdaugareInCod(TranslatareAtom(curent) + "[" + valoare.ToString() + "]");
                                    if ((int)curent_atom.indice == (int)define.ASSIGN)
                                    {
                                        AdaugareInCod(TranslatareAtom(curent_atom));
                                        NextAtom();
                                        Expresie_repetare();
                                    }
                                    else
                                    {
                                        eroare = true;
                                        Errors("Eroare: Lipseste operatorul de atribuire:, \":=\"!");
                                        AtomSincronizare();
                                        return true;
                                    }
                                    return true;
                                }
                                else
                                {
                                    eroare = true;
                                    Errors("Eroare: Nu ati introdus operatorul \"]\"!");
                                    AtomSincronizare();
                                    return true;
                                }
                            }
                            else
                            {
                                eroare = true;
                                Errors("Eroare: Identificatorul nu este un numar de tip intreg!");
                                AtomSincronizare();
                                return true;
                            }
                        }
                        else
                        {
                            eroare = true;
                            Errors("Eroare: Variabila inexistenta!");
                            AtomSincronizare();
                            return true;
                        }
                    }
                else
                {
                    eroare = true;
                    Errors("Eroare: Variabila nedefinita: " + curent_atom.valoare);
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
Beispiel #4
0
 /// <summary>
 /// functie pentru translatarea atomilor din pascal in C
 /// </summary>
 /// <param name="atom">atomul de translatat</param>
 /// <returns>atomul rezultat in urma translatarii</returns>
 private string TranslatareAtom(Atom atom)
 {
     if (atom.valoare.CompareTo("begin") == 0)
     {
         tabulator++;
         return "{\r\n";
     }
     else if (atom.valoare.CompareTo("end") == 0)
     {
         return "}\r\n";
     }
     else if (atom.valoare.CompareTo("uses") == 0) return "#include";
     else if (atom.valoare.CompareTo("if") == 0) return "if";
     else if (atom.valoare.CompareTo("then") == 0) return " ";
     else if (atom.valoare.CompareTo("else") == 0) return "else";
     else if (atom.valoare.CompareTo("while") == 0) return "while";
     else if (atom.valoare.CompareTo("do") == 0) return " ";
     else if (atom.valoare.CompareTo("repeat") == 0) return "do \r\n";
     else if (atom.valoare.CompareTo("until") == 0) return "}; while!";
     else if (atom.valoare.CompareTo("procedure") == 0) return "void ";
     else if (atom.valoare.CompareTo("function") == 0) return " ";
     else if (atom.valoare.CompareTo("boolean") == 0) return "int";
     else if (atom.valoare.CompareTo("integer") == 0) return "int";
     else if (atom.valoare.CompareTo("longint") == 0) return "long int";
     else if (atom.valoare.CompareTo("real") == 0) return "float";
     else if (atom.valoare.CompareTo("word") == 0) return "unsigned int";
     else if (atom.valoare.CompareTo("byte") == 0) return "unsigned char";
     else if (atom.valoare.CompareTo("false") == 0) return "0";
     else if (atom.valoare.CompareTo("true") == 0) return "1";
     else if (atom.valoare.CompareTo("and") == 0) return " && ";
     else if (atom.valoare.CompareTo("or") == 0) return " || ";
     else if (atom.valoare.CompareTo("not") == 0) return "!";
     else if (atom.valoare.CompareTo(",") == 0) return ", ";
     else if (atom.valoare.CompareTo(".") == 0) return "";
     else if (atom.valoare.CompareTo(";") == 0) return ";\r\n";
     else if (atom.valoare.CompareTo(":=") == 0) return " = ";
     else if (atom.valoare.CompareTo(":") == 0) return " ";
     else if (atom.valoare.CompareTo("+") == 0) return " + ";
     else if (atom.valoare.CompareTo("-") == 0) return " - ";
     else if (atom.valoare.CompareTo("*") == 0) return " * ";
     else if (atom.valoare.CompareTo("/") == 0) return " / ";
     else if (atom.valoare.CompareTo("=") == 0) return " == ";
     else if (atom.valoare.CompareTo("<>") == 0) return " != ";
     else if (atom.valoare.CompareTo("><") == 0) return " != ";
     else if (atom.valoare.CompareTo("{") == 0) return "/* \r\n ";
     else if (atom.valoare.CompareTo("}") == 0) return "*/ \r\n ";
     else if (atom.valoare.CompareTo("(") == 0) return "(";
     else if (atom.valoare.CompareTo(")") == 0) return ") ";
     else if (atom.valoare.CompareTo("nil") == 0) return "NULL";
     else if (atom.valoare.CompareTo("case") == 0) return "switch (";
     else if (atom.valoare.CompareTo("of") == 0) return ") {";
     else if (atom.valoare.CompareTo("record") == 0) return "struct {";
     else if (atom.valoare.CompareTo("pointer") == 0) return "void *";
     else if (atom.valoare.CompareTo("private") == 0) return "private:";
     else if (atom.valoare.CompareTo("program") == 0)
     {
         return "\r\n//";
     }
     else if (atom.valoare.CompareTo("var") == 0) return "\r\n";
     else if (atom.valoare.CompareTo("write") == 0) return "printf";
     else if (atom.valoare.CompareTo("scan") == 0) return "scanf";
     else if (atom.valoare.CompareTo("mod") == 0) return "%";
     else if (atom.valoare.CompareTo("div") == 0) return "/";
     else if (atom.valoare.CompareTo("const") == 0) return "const";
     else return atom.valoare;
 }
Beispiel #5
0
        /// <summary>
        /// identificarea termenilor care fac parte din expresie
        /// </summary>
        /// <returns>false in cazul unei erori</returns>
        private bool Operatii()
        {
            if ((int)curent_atom.indice == (int)define.IDENTIFIER)
            {

                if (lista_variabile.Contains(curent_atom.valoare) == true)
                {
                    if (lista_variabile.Contains(curent_atom.valoare) == true)
                    {
                        if (pr.CompareTo("prim") != 0)
                        {
                            string tip_va = "";
                            if (((string)(lista_variabile[curent_atom.valoare])).CompareTo("integer") == 0)
                            {
                                tip_va = "integer";
                            }
                            else if (((string)(lista_variabile[curent_atom.valoare])).CompareTo("real") == 0)
                            {
                                tip_va = "real";
                            }
                            else if (((string)(lista_variabile[curent_atom.valoare])).CompareTo("long int") == 0)
                            {
                                tip_va = "long int";
                            }
                            else if (((string)(lista_variabile[curent_atom.valoare])).CompareTo("char") == 0)
                            {
                                tip_va = "char";
                            }
                            if (tip_variabila.CompareTo(tip_va) == 0)
                            {
                                AdaugareInCod(TranslatareAtom(curent_atom));
                                NextAtom();
                                return true;
                            }
                            else
                            {
                                eroare = true;
                                Errors("Eroare: Variabila de tip: " + tip_variabila + ", nu se poate converta in: " + tip_va);
                                AtomSincronizare();
                                return true;
                            }
                        }
                        else
                        {
                            AdaugareInCod(TranslatareAtom(curent_atom));
                            tip_variabila = (string)lista_variabile[curent_atom.valoare];
                            NextAtom();
                            return true;
                        }
                    }
                }
                else
                {
                    Atom curent = new Atom(curent_atom.indice, curent_atom.valoare, curent_atom.linie);
                    NextAtom();
                    if ((int)curent_atom.indice == (int)define.PARDL)
                    {
                        NextAtom();
                        if ((int)curent_atom.indice == (int)define.NRINTEGER)
                        {
                            int valoare = System.Convert.ToInt32(curent_atom.valoare);
                            NextAtom();
                            if ((int)curent_atom.indice == (int)define.PARDR)
                            {
                                NextAtom();
                                string temp2 = TranslatareAtom(curent) + "[" + valoare.ToString() + "]";
                                if (lista_variabile.Contains(temp2) == true)
                                {
                                    AdaugareInCod(temp2);
                                    return true;
                                }
                                else
                                {
                                    eroare = true;
                                    Errors("Eroare: Variabila \"" + temp2 + "\" nu a fost declarata!");
                                    AtomSincronizare();
                                    return true;
                                }
                            }
                            else
                            {
                                eroare = true;
                                Errors("Eroare: Nu ati introdus operatorul \"]\"!");
                                AtomSincronizare();
                                return true;
                            }
                        }
                        else
                        {
                            eroare = true;
                            Errors("Eroare: Identificatorul nu este un numar de tip intreg!");
                            AtomSincronizare();
                            return true;
                        }
                    }
                    else
                    {
                        eroare = true;
                        Errors("Eroare: Variabila sau constanta nu exista!");
                        AtomSincronizare();
                        return true;
                    }
                }
            }
            else if (Constante() == true)
            {
                return true;
            }
            else if ((int)curent_atom.indice == (int)define.PARL)
            {
                AdaugareInCod(TranslatareAtom(curent_atom));
                NextAtom();
                if (Expresie_repetare() == true)
                {
                    if ((int)curent_atom.indice == (int)define.PARR)
                    {
                        AdaugareInCod(TranslatareAtom(curent_atom));
                        NextAtom();
                    }
                    else
                    {
                        eroare = true;
                        Errors("Eroare: Nu ati introdus paranteza \")\"!");
                        return false;
                    }
                }
                else
                {
                    return false;
                }
                return true;
            }
            else
            {
                return false;
            }
            return true;
        }
Beispiel #6
0
        /// <summary>
        /// functie de identificare a caracterelor
        /// </summary>
        /// <param name="j">numarul cuvantului</param>
        /// <param name="t">numarul caracterului din cuvant</param>
        /// <param name="i">numarul liniei</param>
        /// <param name="atom_i">sirul de caractere</param>
        /// <returns></returns>
        private int AnalizareCaractere(int j, int t,int i, string[] atom_i)
        {
            if (atom_i[j][t] == '{')
            {
                tip_sir = "comentariu";
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.ACOLL, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";

            }
            else if (atom_i[j][t] == '}')
            {
                tip_sir = "identificator";
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.ACOLR, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";

            }
            else if (atom_i[j][t] == ':')
            {
                atom += atom_i[j][t];
                if (atom_i[j].Length > t + 1)
                {
                    if (atom_i[j][t + 1] == '=')
                    {
                        atom += atom_i[j][t + 1];
                        Atom atom_curent = new Atom((int)define.ASSIGN, atom,i);
                        tabel_atomi.Add(count, atom_curent);
                        count++;
                        if (atom_i[j].Length > t + 1)
                        {
                            stare = Identificator.idntf(atom_i[j][t + 2]);
                        }
                        atom = "";
                        t++;
                    }
                    else
                    {
                        Atom atom_curent = new Atom((int)define.DOUAOUNCTE, atom,i);
                        tabel_atomi.Add(count, atom_curent);
                        count++;
                        if (atom_i[j].Length > t + 1)
                        {
                            stare = Identificator.idntf(atom_i[j][t + 1]);
                        }
                        atom = "";
                    }
                }
                else
                {
                    Atom atom_curent = new Atom((int)define.DOUAOUNCTE, atom, i);
                    tabel_atomi.Add(count, atom_curent);
                    count++;
                    if (atom_i[j].Length > t + 1)
                    {
                        stare = Identificator.idntf(atom_i[j][t + 1]);
                    }
                    atom = "";
                }
            }
            else if (atom_i[j][t] == ';')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.PUNCTSIVIRGULA, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";

            }
            else if (atom_i[j][t] == '.')
            {
                atom += atom_i[j][t];
                if (atom_i[j].Length > t + 1)
                {
                    if (atom_i[j][t + 1] == '.')
                    {
                        atom += atom_i[j][t + 1];
                        Atom atom_curent = new Atom((int)define.DOMENIU, atom,i);
                        tabel_atomi.Add(count, atom_curent);
                        count++;
                        if (atom_i[j].Length > t + 1)
                        {
                            stare = Identificator.idntf(atom_i[j][t + 2]);
                        }
                        atom = "";
                        t++;
                        return t;
                    }
                }
                else
                {
                    Atom atom_curent = new Atom((int)define.PUNCT, atom,i);
                    tabel_atomi.Add(count, atom_curent);
                    count++;
                    if (atom_i[j].Length > t + 1)
                    {
                        stare = Identificator.idntf(atom_i[j][t + 1]);
                    }
                    atom = "";
                }
            }
            else if (atom_i[j][t] == '(')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.PARL, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";

            }
            else if (atom_i[j][t] == ')')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.PARR, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == '\'')
            {
                if (tip_sir.CompareTo("caracter") != 0)
                {
                    tip_sir = "caracter";
                }
                else
                {
                    tip_sir = "identificator";
                }
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.GHILIMELESIMPLE, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == '\"')
            {
                if (tip_sir.CompareTo("string") != 0)
                {
                    tip_sir = "string";
                }
                else
                {
                    tip_sir = "identificator";
                }
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.GHILIMELE, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == '=')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.EQUAL, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == '>')
            {
                atom += atom_i[j][t];
                if (atom_i[j].Length > t + 1)
                {
                    if (atom_i[j][t + 1] == '=')
                    {
                        atom += atom_i[j][t + 1];
                        Atom atom_curent = new Atom((int)define.MAIMARESAUEGAL, atom,i);
                        tabel_atomi.Add(count, atom_curent);
                        count++;
                        if (atom_i[j].Length > t + 1)
                        {
                            stare = Identificator.idntf(atom_i[j][t + 2]);
                        }
                        atom = "";
                        t++;
                    }
                    else
                    {
                        Atom atom_curent = new Atom((int)define.MAIMARE, atom,i);
                        tabel_atomi.Add(count, atom_curent);
                        count++;
                        if (atom_i[j].Length > t + 1)
                        {
                            stare = Identificator.idntf(atom_i[j][t + 1]);
                        }
                        atom = "";
                    }
                }
                else
                {
                    Atom atom_curent = new Atom((int)define.MAIMARE, atom, i);
                    tabel_atomi.Add(count, atom_curent);
                    count++;
                    if (atom_i[j].Length > t + 1)
                    {
                        stare = Identificator.idntf(atom_i[j][t + 1]);
                    }
                    atom = "";
                }
            }
            else if (atom_i[j][t] == '<')
            {
                atom += atom_i[j][t];
                if (atom_i[j].Length > t + 1)
                {
                    if (atom_i[j][t + 1] == '=')
                    {
                        atom += atom_i[j][t + 1];
                        Atom atom_curent = new Atom((int)define.MAIMICSAUEGAL, atom,i);
                        tabel_atomi.Add(count, atom_curent);
                        count++;
                        if (atom_i[j].Length > t + 1)
                        {
                            stare = Identificator.idntf(atom_i[j][t + 2]);
                        }
                        atom = "";
                        t++;
                    }
                    else
                    {
                        Atom atom_curent = new Atom((int)define.MAIMIC, atom,i);
                        tabel_atomi.Add(count, atom_curent);
                        count++;
                        if (atom_i[j].Length > t + 1)
                        {
                            stare = Identificator.idntf(atom_i[j][t + 1]);
                        }
                        atom = "";
                    }
                }
                else
                {
                    Atom atom_curent = new Atom((int)define.MAIMIC, atom, i);
                    tabel_atomi.Add(count, atom_curent);
                    count++;
                    if (atom_i[j].Length > t + 1)
                    {
                        stare = Identificator.idntf(atom_i[j][t + 1]);
                    }
                    atom = "";
                }
            }
            else if (atom_i[j][t] == '+')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.PLUS, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == '-')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.MINUS, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == '/')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.DIVIZARE, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == '*')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.MUL, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == '[')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.PARDL, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == ']')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.PARDR, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == '@')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.AT, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == '^')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.POINTER, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == ',')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.VIRGULA, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == '$')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.DOLLAR, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == '!')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.EXCLAM, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == '#')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.DIEZ, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == '%')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.PROCENT, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == '?')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.INTREBARE, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == '\\')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.BACKSLASH, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (atom_i[j][t] == '|')
            {
                atom += atom_i[j][t];
                Atom atom_curent = new Atom((int)define.SAU, atom,i);
                tabel_atomi.Add(count, atom_curent);
                count++;
                if (atom_i[j].Length > t + 1)
                {
                    stare = Identificator.idntf(atom_i[j][t + 1]);
                }
                atom = "";
            }
            else if (((int)atom_i[j][t]) < 32 || ((int)atom_i[j][t]) > 125)
            {
                t++;
            }
            return t;
        }
Beispiel #7
0
        /// <summary>
        /// functie de determinare a atomilor din cod pascal
        /// </summary>
        public void Analizare()
        {
            for (int i = 0; i < lista.Length; i++)
            {
                string linie = lista[i];
                linie = linie.ToLower();
                string[] atom_i = linie.Split(' ');
                for (int j = 0; j < atom_i.Length; j++)
                {
                    atom_i[j] = atom_i[j].Replace(" ", "");
                    atom_i[j] = atom_i[j].Replace("\t", "");
                    atom_i[j] = atom_i[j].Replace("\n", "");
                    atom_i[j] = atom_i[j].Replace("\r", "");

                    if (atom_i[j].Length > 0)
                    {
                        stare = Identificator.idntf(atom_i[j][0]);
                        for (int t = 0; t < atom_i[j].Length; t++)
                        {
                            if (stare.CompareTo("caracter") != 0)
                            {
                                if (Identificator.idntf(atom_i[j][t]).CompareTo(stare) == 0)
                                {
                                    if (stare.CompareTo("alfabetic") == 0)
                                    {
                                        atom += atom_i[j][t];
                                    }
                                    else
                                    {
                                        atom += atom_i[j][t];
                                        if (atom_i[j].Length > t + 1)
                                        {
                                            if (atom_i[j][t + 1] == '.')
                                            {
                                                if (atom_i[j].Length > t + 2)
                                                {
                                                    if (atom_i[j][t + 2] == '.')
                                                    {
                                                        Atom atom_curent = new Atom((int)define.NRINTEGER, atom, i);
                                                        tabel_atomi.Add(count, atom_curent);
                                                        count++;
                                                        stare = Identificator.idntf(atom_i[j][t]);
                                                        atom = "";
                                                    }
                                                    else
                                                    {
                                                        t++;
                                                        atom += atom_i[j][t];
                                                        tip_nr_real = true;
                                                        if (atom_i[j].Length > t + 1)
                                                        {
                                                            if (atom_i[j][t + 1] == 'E' || atom_i[j][t + 1] == 'e')
                                                            {
                                                                t++;
                                                                atom += 'E';
                                                            }
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    t++;
                                                    atom += atom_i[j][t];
                                                    tip_nr_real = true;
                                                    if (atom_i[j].Length > t + 1)
                                                    {
                                                        if (atom_i[j][t + 1] == 'E' || atom_i[j][t + 1] == 'e')
                                                        {
                                                            t++;
                                                            atom += 'E';
                                                        }
                                                    }
                                                }
                                            }
                                            if (atom_i[j].Length > t + 1)
                                            {
                                                if (atom_i[j][t + 1] == 'E' || atom_i[j][t + 1] == 'e')
                                                {
                                                    t++;
                                                    atom += 'E';
                                                    tip_nr_real = true;
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {

                                    if (tabel_expresii.ContainsKey(atom) == true && tip_sir.CompareTo("identificator") == 0)
                                    {
                                        Atom atom_curent = new Atom((int)tabel_expresii[atom], atom, i);
                                        tabel_atomi.Add(count, atom_curent);
                                        stare = Identificator.idntf(atom_i[j][t]);
                                        count++;
                                        atom = "";
                                        t--;
                                    }
                                    else if (stare.CompareTo("alfabetic") == 0)
                                    {
                                        Atom atom_curent = new Atom(0, null, 0);
                                        if (tip_sir.CompareTo("identificator") == 0)
                                        {
                                            atom_curent = new Atom((int)define.IDENTIFIER, atom, i);
                                        }
                                        else if (tip_sir.CompareTo("comentariu") == 0)
                                        {
                                            atom_curent = new Atom((int)define.COMENTARIU, atom, i);
                                        }
                                        else if (tip_sir.CompareTo("caracter") == 0)
                                        {
                                            atom_curent = new Atom((int)define.CHARACTER, atom, i);
                                        }
                                        else if (tip_sir.CompareTo("string") == 0)
                                        {
                                            atom_curent = new Atom((int)define.SIRCARACTERE, atom, i);
                                        }

                                        if (atom_curent.valoare.Length > 0)
                                        {
                                            tabel_atomi.Add(count, atom_curent);
                                              count++;
                                        }
                                        stare = Identificator.idntf(atom_i[j][t]);
                                        atom = "";
                                        t--;
                                    }
                                    else if (stare.CompareTo("numar") == 0)
                                    {
                                        Atom atom_curent = new Atom(0, null, 0);
                                        if (tip_nr_real == true)
                                        {
                                            atom_curent = new Atom((int)define.NRREAL, atom, i);
                                            tip_nr_real = false;
                                        }
                                        else
                                        {
                                            atom_curent = new Atom((int)define.NRINTEGER, atom, i);
                                        }
                                        if (atom_curent.valoare.Length > 0)
                                        {
                                            tabel_atomi.Add(count, atom_curent);
                                            count++;
                                        }
                                        stare = Identificator.idntf(atom_i[j][t]);
                                        atom = "";
                                        t--;
                                    }
                                }
                            }
                            else
                            {
                                t = AnalizareCaractere(j, t, i, atom_i);
                            }
                        }
                        if (stare.CompareTo("alfabetic") == 0)
                        {
                            if (tabel_expresii.ContainsKey(atom) == true && tip_sir.CompareTo("identificator") == 0)
                            {
                                Atom atom_curent = new Atom((int)tabel_expresii[atom], atom, i);
                                if (atom_curent.valoare.Length > 0)
                                {
                                    tabel_atomi.Add(count, atom_curent);
                                    count++;
                                }
                                atom = "";
                            }
                            else
                            {
                                Atom atom_curent = new Atom(0, null, 0);
                                if (tip_sir.CompareTo("identificator") == 0)
                                {
                                    atom_curent = new Atom((int)define.IDENTIFIER, atom, i);
                                }
                                else if (tip_sir.CompareTo("comentariu") == 0)
                                {
                                    atom_curent = new Atom((int)define.COMENTARIU, atom, i);
                                }
                                else if (tip_sir.CompareTo("caracter") == 0)
                                {
                                    atom_curent = new Atom((int)define.CHARACTER, atom, i);
                                }
                                else if (tip_sir.CompareTo("string") == 0)
                                {
                                    atom_curent = new Atom((int)define.SIRCARACTERE, atom, i);
                                }
                                if (atom_curent.valoare.Length > 0)
                                {
                                    tabel_atomi.Add(count, atom_curent);
                                    count++;
                                }
                                atom = "";
                            }
                        }
                        else if (stare.CompareTo("numar") == 0 && atom.CompareTo("") != 0)
                        {
                            Atom atom_curent = new Atom(0, null, 0);
                            if (tip_nr_real == true)
                            {
                                atom_curent = new Atom((int)define.NRREAL, atom, i);
                                tip_nr_real = false;
                            }
                            else
                            {
                                atom_curent = new Atom((int)define.NRINTEGER, atom, i);
                            }
                            if (atom_curent.valoare.Length > 0)
                            {
                                tabel_atomi.Add(count, atom_curent);
                                count++;
                            }
                            atom = "";
                        }
                        else if (stare.CompareTo("caracter") == 0 && atom.CompareTo(".") == 0)
                        {
                            Atom atom_curent = new Atom((int)define.PUNCT, atom, i);
                            if (atom_curent.valoare.Length > 0)
                            {
                                tabel_atomi.Add(count, atom_curent);
                                count++;
                            }
                            atom = "";
                        }
                    }
                }
            }
            tabel_atomi.Add(count, new Atom((int)define.END, "sfarsit", lista.Length+1));
            contor = tabel_atomi.Count;
        }