Beispiel #1
0
        public Boolean GetReservedSymbols(string txt)
        {
            LexicalConstants td = new LexicalConstants();

            LexicalConstants.ReservedSymbols       rs = new LexicalConstants.ReservedSymbols();
            LexicalConstants.ReservedSymbolsDelims rsd = new LexicalConstants.ReservedSymbolsDelims();
            Boolean found = false, hastoken = false, exitfor = false;
            Tokens  t = new Tokens();

            rsd = td.AddRange(rsd);
            List <String> words;
            List <char>   delims;
            List <String> temp;
            int           tempctr = 0, limit = 0, sctr = 0;

            if (txt.Length != 1)
            {
                while ((txt.Length - 1) > tempctr && !isEnd(txt[tempctr + 1], rsd))
                {
                    tempctr++;
                }
                if ((txt.Length - 1) > tempctr)
                {
                    if (txt.ElementAt(tempctr) != '+' && txt.ElementAt(tempctr) != '-')
                    {
                        tempctr++;
                    }
                }
                if ((txt.Length - 1) > tempctr)
                {
                    if ((txt.ElementAt(tempctr) == '+' || txt.ElementAt(tempctr) == '-') && tempctr == 0)
                    {
                        tempctr++;
                    }
                }
                if ((txt.Length - 1) > tempctr)
                {
                    if ((txt.ElementAt(tempctr) == '+' || txt.ElementAt(tempctr) == '-') && tempctr == 1 && (txt.ElementAt(tempctr - 1) == '+' || txt.ElementAt(tempctr - 1) == '-'))
                    {
                        tempctr++;
                    }
                }
                if (txt.Length > tempctr)
                {
                    if ((txt.ElementAt(0) == ')') && (txt.ElementAt(1) == '&' || txt.ElementAt(1) == '|'))
                    {
                        tempctr = 1;
                    }
                }
            }

            for (int i = 0; i < 13; i++)
            {
                sctr   = 0;
                words  = new List <String>();
                delims = new List <char>();
                found  = true;
                switch (i)
                {
                case 0:
                    words  = rs.rs_whitespace;
                    delims = rsd.whitespace;
                    break;

                case 1:
                    words  = rs.rs_caplet;
                    delims = rsd.delim_caplet;
                    break;

                case 2:
                    words  = rs.rs_concat;
                    delims = rsd.concat;
                    break;

                case 3:
                    words  = rs.rs_delim4;
                    delims = rsd.delim4;
                    break;

                case 4:
                    words  = rs.rs_delim5;
                    delims = rsd.delim5;
                    break;

                case 5:
                    words  = rs.rs_delim6;
                    delims = rsd.delim6;
                    break;

                case 6:
                    words  = rs.rs_delim7;
                    delims = rsd.delim7;
                    break;

                case 7:
                    words  = rs.rs_delim8;
                    delims = rsd.delim8;
                    break;

                case 8:
                    words  = rs.rs_condop;
                    delims = rsd.condop;
                    break;

                case 9:
                    words  = rs.rs_relop;
                    delims = rsd.relop;
                    break;

                case 10:
                    words  = rs.rs_rel0p;
                    delims = rsd.rel0p;
                    break;

                case 11:
                    words  = rs.rs_math;
                    delims = rsd.math;
                    break;

                case 12:
                    words = rs.rs_done;
                    break;
                }
                //Check Reserved Symbols
                if (i != 12)
                {
                    foreach (char c in txt)
                    {
                        limit = words.Count - 1;
                        temp  = new List <string>();
                        found = false;
                        foreach (string w in words)
                        {
                            //IF NOT OUT OF RANGE
                            if ((w.Length - 1) >= sctr)
                            {
                                if (c == w.ElementAt(sctr))
                                {
                                    found = true;
                                    //CHECK SIZE OF WORD AND INPUT
                                    if (w.Length == tempctr)
                                    {
                                        //CHECK DELIMITER
                                        if ((tempctr - 1) == sctr)
                                        {
                                            foreach (char delim in delims)
                                            {
                                                //IF NOT OUT OF RANGE
                                                if ((txt.Length - 1) > sctr)
                                                {
                                                    //IF FOUND DELIMITER
                                                    if (txt[sctr + 1] == delim)
                                                    {
                                                        found    = true;
                                                        hastoken = true;
                                                        t.setTokens(w);
                                                        t.setLexemes(w);
                                                        t.setAttributes(w);
                                                        token.Add(t);
                                                        valid++;
                                                        break;
                                                    }
                                                }
                                                else if (w == words[limit] && hastoken == false)
                                                {
                                                    found = false;
                                                }
                                            }

                                            if (hastoken)
                                            {
                                                break;
                                            }
                                        }
                                        else
                                        {
                                            temp.Add(w);
                                        }
                                    }
                                }
                            }
                        }
                        sctr++;
                        words = temp;
                        if (found == false)
                        {
                            break;
                        }
                        if (hastoken)
                        {
                            exitfor = true;
                            break;
                        }
                    }
                }
                else
                {
                    if (txt.ElementAt(sctr) == words[0].ElementAt(0))
                    {
                        sctr = txt.Length;
                        t.setTokens("#");
                        t.setLexemes("#");
                        t.setAttributes("Program End");
                        token.Add(t);
                        hastoken = true;
                    }
                }
                if (exitfor)
                {
                    exitfor = false;
                    break;
                }
            }
            if (hastoken)
            {
                ctr = sctr;
            }
            return(hastoken);
        }
Beispiel #2
0
        public Boolean ReservedSymbols(string txt)
        {
            LexicalConstants.ReservedWordsDelim    resWordDelim = new LexicalConstants.ReservedWordsDelim();
            LexicalConstants.ReservedSymbols       resSym       = new LexicalConstants.ReservedSymbols();
            LexicalConstants.ReservedSymbolsDelims resSymDelim  = new LexicalConstants.ReservedSymbolsDelims();
            Tokens token = new Tokens();

            List <string> sym    = new List <string>();
            List <char>   delims = new List <char>();
            List <string> temp   = new List <string>();

            Boolean hasReservedSymbol, hasToken = false, exit = false;

            int flag = -1, limit, ctr = 0;

            for (int a = 0; a < txt.Length; a++)
            {
                foreach (char end in resSymDelim.rs_ender)
                {
                    if (end == txt.ElementAt(a))
                    {
                        hasToken = true;
                    }
                }
                if (hasToken)
                {
                    break;
                }
                flag++;
            }

            if (flag < 0)
            {
                foreach (char one in resSymDelim.rs_one)
                {
                    if (txt.ElementAt(0) == one)
                    {
                        flag++;
                        break;
                    }
                }
            }
            if (txt.Length > 1)
            {
                if (flag == 0)
                {
                    if (txt.ElementAt(1) == '=')
                    {
                        foreach (char repeat in resSymDelim.rs_repeat)
                        {
                            if (txt.ElementAt(0) == repeat)
                            {
                                flag++;
                                break;
                            }
                        }
                    }
                    else if (txt.ElementAt(1) == '+')
                    {
                        if (txt.ElementAt(0) == '+')
                        {
                            flag++;
                        }
                    }
                    if (txt.ElementAt(1) == '-')
                    {
                        if (txt.ElementAt(0) == '-')
                        {
                            flag++;
                        }
                    }
                }
            }

            hasToken = false;

            for (int i = 0; i < 13; i++)
            {
                ctr = 0;
                switch (i)
                {
                case 0:
                    sym    = resSym.rs_2;
                    delims = resWordDelim.delim_2;
                    break;

                case 1:
                    sym    = resSym.rs_5;
                    delims = resSymDelim.delim_5;
                    break;

                case 2:
                    sym    = resSym.rs_7;
                    delims = resSymDelim.delim_7;
                    break;

                case 3:
                    sym    = resSym.rs_8;
                    delims = resSymDelim.delim_8;
                    break;

                case 4:
                    sym    = resSym.rs_9;
                    delims = resSymDelim.delim_9;
                    break;

                case 5:
                    sym    = resSym.rs_whitespace;
                    delims = resWordDelim.whitespace;
                    break;

                case 6:
                    sym    = resSym.rs_arithop;
                    delims = resSymDelim.delim_arithop;
                    break;

                case 7:
                    sym    = resSym.rs_incop;
                    delims = resSymDelim.delim_incop;
                    break;

                case 8:
                    sym    = resSym.rs_lower;
                    delims = resSymDelim.delim_lower;
                    break;

                case 9:
                    sym    = resSym.rs_11;
                    delims = resSymDelim.delim_11;
                    break;

                case 10:
                    sym    = resSym.rs_12;
                    delims = resSymDelim.delim_12;
                    break;

                case 11:
                    sym    = resSym.rs_13;
                    delims = resSymDelim.delim_13;
                    break;

                case 12:
                    sym    = resSym.rs_14;
                    delims = resSymDelim.delim_14;
                    break;

                default:
                    break;
                }

                foreach (char t in txt)
                {
                    hasReservedSymbol = false;
                    limit             = sym.Count - 1;
                    foreach (string symbol  in sym.ToList())
                    {
                        if ((symbol.Length - 1) >= ctr)
                        {
                            if (t == symbol.ElementAt(ctr))
                            {
                                hasReservedSymbol = true;
                                if (symbol.Length == (flag + 1))
                                {
                                    if (flag == ctr)
                                    {
                                        foreach (char delim in delims)
                                        {
                                            if ((txt.Length - 1) > ctr)
                                            {
                                                if (txt[flag + 1] == delim)
                                                {
                                                    hasToken = true;
                                                    token.setTokens(symbol);
                                                    token.setLexemes(symbol);
                                                    token.setDescription(symbol);
                                                    tokens.Add(token);
                                                }
                                            }
                                            else if (symbol == sym[limit] && hasToken == false)
                                            {
                                                hasReservedSymbol = false;
                                            }
                                            else if (hasToken)
                                            {
                                                break;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        temp.Add(symbol);
                                    }
                                }
                            }
                        }
                    }
                    ctr++;
                    sym = temp;
                    if (!hasReservedSymbol)
                    {
                        break;
                    }
                    if (hasToken)
                    {
                        exit = true;
                        break;
                    }
                }
                if (exit)
                {
                    break;
                }
            }
            ctra = ctr;
            return(hasToken);
        }