Example #1
0
        public Element[] Parse(string exprString, int endIndex, out int lastMatchedIndex)
        {
            parser_error = "";
            //为了防止在词法分析过程中出现字符串数组越界,在末尾补上无效字符
            exprString = exprString.Insert(endIndex, "~~~~~~");
            Element[] Sequence = new Element[maxLength];
            int       seqIndex = 0;
            Element   var      = new Element(Token.Variable);

            lastMatchedIndex = -1;
            for (int i = 0; i < exprString.Length; i++)
            {
                if (exprString[i] == ' ')
                {
                }
                //符号是数字
                else if (char.IsNumber(exprString[i]))
                {
                    int    j     = i;
                    double value = 0;
                    while (j < exprString.Length && ((exprString[j] <= '9' && exprString[j] >= '0') || exprString[j] == '.'))
                    {
                        j++;
                    }
                    try
                    {
                        value = Double.Parse(exprString.Substring(i, j - i));
                    }
                    catch (FormatException fe)
                    {
                        parser_error        += "数字格式错误!\n";
                        Sequence[seqIndex++] = new Element(Token.Null);
                        return(Sequence);
                    }
                    Sequence[seqIndex++] = new Element(Token.Number, value);
                    i = j - 1;
                }
                //符号是保留字
                else if (char.IsLower(exprString[i]))
                {
                    List <string> Candiates = Symbols.LongestMatch(exprString, i, endIndex, out int lastMatchedIndex_1);
                    if (Candiates.Count == 1 && Candiates[0].Length == lastMatchedIndex_1 - lastMatchedIndex)
                    {
                        Sequence[seqIndex++] = new Element(Symbols.SymbolToToken[Candiates[0]]);
                        if (Sequence[seqIndex - 1].token == Token.Variable)
                        {
                            Sequence[seqIndex - 1] = var;
                        }
                        i = lastMatchedIndex_1;
                    }
                    else
                    {
                        parser_error        += "未找到该标识符!\n";
                        Sequence[seqIndex++] = new Element(Token.Null);
                        return(Sequence);
                    }
                }
                //符号是单个字符的情况
                else
                {
                    switch (exprString[i])
                    {
                    case '+': Sequence[seqIndex++] = new Element(Token.Add); break;

                    case '-': Sequence[seqIndex++] = new Element(Token.Sub); break;

                    case '*': Sequence[seqIndex++] = new Element(Token.Mul); break;

                    case '/': Sequence[seqIndex++] = new Element(Token.Div); break;

                    case '^': Sequence[seqIndex++] = new Element(Token.Pow); break;

                    case '(': Sequence[seqIndex++] = new Element(Token.Lparen); break;

                    case ')': Sequence[seqIndex++] = new Element(Token.Rparen); break;

                    case ',': Sequence[seqIndex++] = new Element(Token.Comma); break;

                    case '~': Sequence[seqIndex++] = new Element(Token.Null); break;

                    default: Sequence[seqIndex++] = new Element(Token.Null);
                        parser_error += "错误符号!\n"; return(Sequence);
                    }
                }
                lastMatchedIndex = i;
            }
            Sequence[seqIndex++] = new Element(Token.Null);
            return(Sequence);
        }