Esempio n. 1
0
        private PhraseTypeEnum _lastOpForError; //可能引起错误的那个运算符

        public SemanticAnalyzer(ref PhraseStorage ps)
        {
            _optr = new Stack();
            _opnd = new Stack();
            _op   = new Stack();
            _ps   = ps;
        }
Esempio n. 2
0
 public PhraseAnalyzer(string sentence, ref PhraseStorage ps)
 {
     //清除前一次的词法分析结果
     _ps = ps;
     _ps.ClearResult();
     //保存句子
     _sentence = sentence;
     _succeed  = true;
     //小写化句子中的所有字母
     _chArray = sentence.ToLower().ToCharArray();
     if (Analyze() == false)     //出错
     {
         _ps.AddPhraseResult("Error", PhraseTypeEnum.unknown);
     }
 }
Esempio n. 3
0
        public static PhraseStorage SplitFormula(this string formula)
        {
            PhraseStorage  ps = new PhraseStorage();
            PhraseAnalyzer pa = new PhraseAnalyzer(formula + "#=", ref ps);

            if (pa.Succeed == false)
            {
                throw new Exception(string.Format("公式“{0}”错误", formula));;
            }
            SemanticAnalyzer sa = new SemanticAnalyzer(ref ps);

            if (sa.Check() == false)
            {
                throw new Exception(string.Format("公式“{0}”有误,{1}", formula, sa.ErrorTip));;
            }

            return(ps);
        }
Esempio n. 4
0
        /// <summary>
        /// 将A/B/C拆分成(A/B)/C 后处理除号成DECODE函数
        /// </summary>
        /// <param name="ps"></param>
        /// <param name="phraseTypeList"></param>
        /// <param name="ifZero"></param>
        /// <returns></returns>
        public static string PhraseStorageDecode(PhraseStorage ps, List <PhraseTypeEnum> phraseTypeList, decimal ifZero)
        {
            StringCollection psCopy = new StringCollection();

            foreach (string sCopy in ps.PhraseResult)
            {
                psCopy.Add(sCopy);
            }
            List <PhraseTypeEnum> phraseList = new List <PhraseTypeEnum>(ps.PhraseTypeResult);

            for (int i = 0; i < phraseList.Count; i++)
            {
                if (phraseList[i] == PhraseTypeEnum.divide)
                {
                    int tmp = 0;
                    int n   = i - 1;
                    for (; n > 0; n--)
                    {
                        if (phraseList[n] == PhraseTypeEnum.rightbracket)
                        {
                            tmp++;
                        }
                        else if (phraseList[n] == PhraseTypeEnum.leftbracket)
                        {
                            tmp--;
                            if (tmp == 0)
                            {
                                break;
                            }
                        }
                        else if (phraseList[n] == PhraseTypeEnum.number)
                        {
                            if (tmp == 0)
                            {
                                break;
                            }
                        }
                        else
                        {
                            if (tmp == 0)
                            {
                                n = n + 1;
                                break;
                            }
                        }
                    }
                    tmp = 0;
                    int k = i + 1;
                    for (; k < phraseList.Count; k++)
                    {
                        if (phraseList[k] == PhraseTypeEnum.rightbracket)
                        {
                            tmp--;
                            if (tmp == 0)
                            {
                                break;
                            }
                        }
                        else if (phraseList[k] == PhraseTypeEnum.leftbracket)
                        {
                            tmp++;
                        }
                        else if (phraseList[k] == PhraseTypeEnum.number)
                        {
                            if (tmp == 0)
                            {
                                k = k + 1;
                                break;
                            }
                        }
                        else
                        {
                            if (tmp == 0)
                            {
                                break;
                            }
                        }
                    }
                    if (k == psCopy.Count)
                    {
                        psCopy.Add(")");
                        phraseList.Add(PhraseTypeEnum.rightbracket);
                    }
                    else
                    {
                        psCopy.Insert(k, ")");
                        phraseList.Insert(k, PhraseTypeEnum.rightbracket);
                    }
                    psCopy.Insert(n, "(");
                    phraseList.Insert(n, PhraseTypeEnum.leftbracket);
                    i = k + 1;
                }
            }


            return(Formula2Decode(psCopy, phraseList, 0, psCopy.Count, ifZero));
        }