GrammarPair grammarPairWithLexemList(int startIdx, int endIdx) { GrammarPair result = null; int length = endIdx - startIdx + 1; try { foreach (GrammarPair pair in grammar.Gramatic) { if (length == pair.PartLexems.Count) { bool exist = true; for (int i = 0; i < pair.PartLexems.Count; i++) { if (processValue(stack[i + startIdx]) != pair.PartLexems[i]) { exist = false; } } if (exist) { result = pair; return(result); } } } }catch (Exception) { ; }; return(result); }
void replace(int idx, GrammarPair grammarpair) { stack.RemoveRange(idx, grammarpair.PartLexems.Count); stack.Insert(idx, grammarpair.RootLexem); string original = ""; foreach (string str in grammarpair.PartLexems) { original += str + " "; if (PolizAnalyzer.sharedAnalyzer.operationList.isOperation(str)) { poliz.Add(str); } } Out.Log(Out.State.LogDebug, "Replace " + original + " with " + grammarpair.RootLexem); }
// public void RecursiveSetup(string prevLevelPrevTerm, GrammarPair grammarPair, string prevLevelNextTerm) // { // // string currentTerm = grammarPair.PartLexems[0]; // string nextTerm = grammarPair.PartLexems.Count > 1 ? grammarPair.PartLexems[1] : prevLevelNextTerm; // //SetConnotialBetweenTerminals(Connotial.LessConnotial,prevLevelNextTerm,currentTerm); // // if (this.grammar.GrammarPairWithRootLexem(currentTerm) != null) // { // List<GrammarPair> pairs = this.grammar.GrammarPairWithRootLexem(currentTerm); // foreach (GrammarPair pair in pairs) // { // string firstPartLexem = pair.PartLexems[0]; // if (ConnotialBetweenTerminals(prevLevelPrevTerm,firstPartLexem) == Connotial.NoConnotial) // { // SetConnotialBetweenTerminals(Connotial.LessConnotial,prevLevelPrevTerm,firstPartLexem); // RecursiveSetup(prevLevelPrevTerm,pair,nextTerm); // } // } // } // // for (int i=1;i<grammarPair.PartLexems.Count; i++) // { // currentTerm = grammarPair.PartLexems[i-1]; // nextTerm = grammarPair.PartLexems[i]; // // if (ConnotialBetweenTerminals(currentTerm,nextTerm) == Connotial.NoConnotial) // { // if (this.grammar.GrammarPairWithRootLexem(nextTerm) == null) // { // SetConnotialBetweenTerminals(Connotial.EqualConnotial,currentTerm,nextTerm); // } // else // { // if (i < grammarPair.PartLexems.Count-1) // { // SetConnotialBetweenTerminals(Connotial.EqualConnotial,currentTerm,nextTerm); // string nextNextTerm = grammarPair.PartLexems[i+1]; // List<GrammarPair> pairs = this.grammar.GrammarPairWithRootLexem(nextTerm); // foreach (GrammarPair pair in pairs) // { // SetConnotialBetweenTerminals(Connotial.LessConnotial,currentTerm,pair.PartLexems[0]); // RecursiveSetup(currentTerm,pair,nextNextTerm); // } // } // else // { // SetConnotialBetweenTerminals(Connotial.EqualConnotial,currentTerm,nextTerm); // List<GrammarPair> pairs = this.grammar.GrammarPairWithRootLexem(nextTerm); // foreach (GrammarPair pair in pairs) // { // SetConnotialBetweenTerminals(Connotial.LessConnotial,currentTerm,pair.PartLexems[0]); // RecursiveSetup(currentTerm,pair,prevLevelNextTerm); // } // } // } // } // } // string lastTerm = grammarPair.PartLexems[grammarPair.PartLexems.Count-1]; // SetConnotialBetweenTerminals(Connotial.GreaterConnotial,lastTerm,prevLevelNextTerm); // // if (this.grammar.GrammarPairWithRootLexem(lastTerm) != null) // { // List<GrammarPair> pairs = this.grammar.GrammarPairWithRootLexem(lastTerm); // foreach (GrammarPair pair in pairs) // { // string lastPartLexem = pair.PartLexems[pair.PartLexems.Count-1]; // if (ConnotialBetweenTerminals(lastTerm,lastPartLexem) == Connotial.NoConnotial) // { // SetConnotialBetweenTerminals(Connotial.GreaterConnotial,lastPartLexem,prevLevelNextTerm); // RecursiveSetup(currentTerm,pair,nextTerm); // } // } // } // } public void RecursiveSetup(string prevLevelPrevTerm, GrammarPair grammarPair, string prevLevelNextTerm) { List <string> grammarPairs = new List <string>(); grammarPairs.Add(prevLevelPrevTerm); foreach (string pair in grammarPair.PartLexems) { grammarPairs.Add(pair); } grammarPairs.Add(prevLevelNextTerm); for (int i = 1; i < grammarPairs.Count - 1; i++) { string currentTerm = grammarPairs[i - 1]; string nextTerm = grammarPairs[i]; if (ConnotialBetweenTerminals(currentTerm, nextTerm) == Connotial.NoConnotial) { if (i == 1) { SetConnotialBetweenTerminals(Connotial.LessConnotial, currentTerm, nextTerm); } else { SetConnotialBetweenTerminals(Connotial.EqualConnotial, currentTerm, nextTerm); } if (this.grammar.GrammarPairWithRootLexem(nextTerm) != null) { string nextNextTerm = grammarPairs[i + 1]; List <GrammarPair> pairs = this.grammar.GrammarPairWithRootLexem(nextTerm); foreach (GrammarPair pair in pairs) { RecursiveSetup(currentTerm, pair, nextNextTerm); string pairLastTerm = pair.PartLexems[pair.PartLexems.Count - 1]; SetConnotialBetweenTerminals(Connotial.GreaterConnotial, pairLastTerm, nextNextTerm); } } } } }
private void Analyze() { /* So-so code */ int failedProcessCount = 0; int LastLexemsCount = lexems.Count; stack.Clear(); poliz.Clear(); PutToStack(); htmlTable = new HTMLTable("Stack", "Connotial", "Source Code"); LogToTable(stack, Translators.BottomUpTable.Connotial.NoConnotial, lexems); bool successFinish = false; do { Translators.BottomUpTable.Connotial connotial = table.ConnotialBetweenTerminals(processValue(stack[stack.Count - 1]), processValue(lexems[0])); if (connotial == BottomUpTable.Connotial.EqualConnotial || connotial == BottomUpTable.Connotial.LessConnotial || connotial == BottomUpTable.Connotial.NoConnotial) { PutToStack(); } if (connotial == BottomUpTable.Connotial.GreaterConnotial) { int openScobeIdx = int.MaxValue; for (int i = stack.Count - 2; i >= 0 && openScobeIdx == int.MaxValue; i--) { if (table.ConnotialBetweenTerminals(processValue(stack[i]), processValue(stack[i + 1])) == BottomUpTable.Connotial.LessConnotial) { openScobeIdx = i + 1; } } GrammarPair pair = grammarPairWithLexemList(openScobeIdx, stack.Count - 1); if (pair != null) { if (lowPriorityItems.Contains(pair.RootLexem) && false == allowLowPriorityForItemAtIndex(stack.Count - 1)) { PutToStack(); Out.Log(Out.State.LogDebug, "It's not time for low-level pair"); } else { if (pair.PartLexems.Count > 1 || readyToReplace(pair.PartLexems[0], openScobeIdx)) { replace(openScobeIdx, pair); failedProcessCount = 0; } else { PutToStack(); } } } else { Out.Log(Out.State.LogInfo, "Invalid pair"); return; } } LogToTable(stack, connotial, lexems); if (LastLexemsCount == lexems.Count) { failedProcessCount++; } else { failedProcessCount = 0; } LastLexemsCount = lexems.Count; successFinish = stack[1] == "<app>" && stack.Count == 2; } while (!successFinish && failedProcessCount != 20); htmlTable.WriteToFile(Constants.HTMLTablePath); }