Changeset 15821 for branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs
- Timestamp:
- 02/28/18 14:44:25 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs
r15817 r15821 45 45 46 46 #region HL Symbols for Parsing ExpressionTrees 47 private TypeCoherentExpressionGrammar symbolicExpressionGrammar;48 47 49 48 private ISymbol constSy; … … 57 56 private ISymbol sinSy; 58 57 private ISymbol cosSy; 59 58 60 59 private ISymbol rootSy; 61 60 private ISymbol startSy; 61 62 private InfixExpressionFormatter infixExpressionFormatter; 62 63 #endregion 63 64 … … 73 74 SinFactor = new NonterminalSymbol("SinFactor"); 74 75 CosFactor = new NonterminalSymbol("CosFactor"); 75 76 76 77 SimpleExpr = new NonterminalSymbol("SimpleExpr"); 77 78 SimpleTerm = new NonterminalSymbol("SimpleTerm"); … … 127 128 128 129 #region Parsing to SymbolicExpressionTree 129 symbolicExpressionGrammar = new TypeCoherentExpressionGrammar();130 var symbolicExpressionGrammar = new TypeCoherentExpressionGrammar(); 130 131 symbolicExpressionGrammar.ConfigureAsDefaultRegressionGrammar(); 131 132 … … 143 144 startSy = symbolicExpressionGrammar.Symbols.OfType<StartSymbol>().First(); 144 145 146 infixExpressionFormatter = new InfixExpressionFormatter(); 145 147 #endregion 146 148 } … … 274 276 275 277 #region Parse to SymbolicExpressionTree 278 279 public string ToInfixString(SymbolString sentence) { 280 Debug.Assert(sentence.Any(), "Trying to evaluate empty sentence!"); 281 Debug.Assert(sentence.All(s => s is TerminalSymbol), "Trying to evaluate symbol sequence with nonterminalsymbols!"); 282 283 return infixExpressionFormatter.Format(ParseSymbolicExpressionTree(sentence)); 284 } 285 276 286 public SymbolicExpressionTree ParseSymbolicExpressionTree(SymbolString sentence) { 277 287 Debug.Assert(sentence.Any(), "Trying to evaluate empty sentence!"); 278 288 Debug.Assert(sentence.All(s => s is TerminalSymbol), "Trying to evaluate symbol sequence with nonterminalsymbols!"); 279 280 symbolicExpressionGrammar.ConfigureAsDefaultRegressionGrammar(); // TODO: not necessary to call this for each sentence281 289 282 290 var rootNode = rootSy.CreateTreeNode(); … … 339 347 } 340 348 #endregion 341 342 #region Parse to Infix string343 344 public SymbolString PostfixToInfixParser(SymbolString phrase) {345 Stack<Symbol> parseStack = new Stack<Symbol>(phrase);346 347 return PostfixToInfixSubtreeParser(parseStack);348 }349 350 private SymbolString PostfixToInfixSubtreeParser(Stack<Symbol> parseStack) {351 Symbol head = parseStack.Pop();352 353 SymbolString result = new SymbolString(parseStack.Count);354 355 if (ReferenceEquals(head, Addition) || ReferenceEquals(head, Multiplication)) {356 // right part357 SymbolString rightPart = PostfixToInfixSubtreeParser(parseStack);358 SymbolString leftPart = PostfixToInfixSubtreeParser(parseStack);359 360 result.AddRange(leftPart);361 result.Add(head);362 result.AddRange(rightPart);363 364 } else if (ReferenceEquals(head, Log) || ReferenceEquals(head, Exp)365 || ReferenceEquals(head, Sin) || ReferenceEquals(head, Cos)366 || ReferenceEquals(head, Inv)) {367 result.Add(head);368 result.Add(OpeningBracket);369 result.AddRange(PostfixToInfixSubtreeParser(parseStack));370 result.Add(ClosingBracket);371 372 } else {373 result.Add(head);374 }375 return result;376 }377 378 #endregion379 349 } 380 350 }
Note: See TracChangeset
for help on using the changeset viewer.