- Timestamp:
- 10/22/16 10:21:15 (8 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs
r14026 r14347 81 81 } 82 82 strBuilder.Append(")"); 83 } else { 84 // function with multiple arguments 85 strBuilder.Append(token).Append("("); 86 FormatRecursively(node.Subtrees.First(), strBuilder); 87 foreach (var subtree in node.Subtrees.Skip(1)) { 88 strBuilder.Append(", "); 89 FormatRecursively(subtree, strBuilder); 90 } 91 strBuilder.Append(")"); 83 92 } 84 93 } else if (node.SubtreeCount == 1) { … … 94 103 FormatRecursively(node.GetSubtree(0), strBuilder); 95 104 } else { 96 // function 105 // function with only one argument 97 106 strBuilder.Append(token).Append("("); 98 107 FormatRecursively(node.GetSubtree(0), strBuilder); -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs
r14319 r14347 37 37 /// </summary> 38 38 public sealed class InfixExpressionParser { 39 private enum TokenType { Operator, Identifier, Number, LeftPar, RightPar, End, NA };39 private enum TokenType { Operator, Identifier, Number, LeftPar, RightPar, Comma, End, NA }; 40 40 private class Token { 41 41 internal double doubleVal; … … 102 102 { "MEAN", new Average()}, 103 103 { "IF", new IfThenElse()}, 104 { " >", new GreaterThan()},105 { " <", new LessThan()},104 { "GT", new GreaterThan()}, 105 { "LT", new LessThan()}, 106 106 { "AND", new And()}, 107 107 { "OR", new Or()}, … … 138 138 } 139 139 if (char.IsDigit(str[pos])) { 140 // read number (=> read until white space or operator )140 // read number (=> read until white space or operator or comma) 141 141 var sb = new StringBuilder(); 142 142 sb.Append(str[pos]); … … 147 147 && str[pos] != '*' 148 148 && str[pos] != '/' 149 && str[pos] != ')') { 149 && str[pos] != ')' 150 && str[pos] != ',') { 150 151 sb.Append(str[pos]); 151 152 pos++; … … 211 212 pos++; 212 213 yield return new Token { TokenType = TokenType.RightPar, strVal = ")" }; 214 } else if (str[pos] == ',') { 215 pos++; 216 yield return new Token { TokenType = TokenType.Comma, strVal = "," }; 213 217 } else { 214 218 throw new ArgumentException("Invalid character: " + str[pos]); … … 217 221 } 218 222 219 // S = Expr EOF 220 // Expr = ['-' | '+'] Term { '+' Term | '-' Term } 221 // Term = Fact { '*' Fact | '/' Fact } 222 // Fact = '(' Expr ')' | funcId '(' Expr ')' | varId | number 223 // S = Expr EOF 224 // Expr = ['-' | '+'] Term { '+' Term | '-' Term } 225 // Term = Fact { '*' Fact | '/' Fact } 226 // Fact = '(' Expr ')' | funcId '(' ArgList ')' | varId | number 227 // ArgList = Expr { ',' Expr } 223 228 private ISymbolicExpressionTreeNode ParseS(Queue<Token> tokens) { 224 229 var expr = ParseExpr(tokens); … … 328 333 } 329 334 330 // Fact = '(' Expr ')' | funcId '(' Expr')' | varId | number335 // Fact = '(' Expr ')' | funcId '(' ArgList ')' | varId | number 331 336 private ISymbolicExpressionTreeNode ParseFact(Queue<Token> tokens) { 332 337 var next = tokens.Peek(); … … 348 353 if (lPar.TokenType != TokenType.LeftPar) 349 354 throw new ArgumentException("expected ("); 350 var expr = ParseExpr(tokens); 355 var args = ParseArgList(tokens); 356 357 // check semantic constraints 358 if (funcNode.Symbol.MinimumArity > args.Length || funcNode.Symbol.MaximumArity < args.Length) 359 throw new ArgumentException(string.Format("Symbol {0} requires between {1} and {2} arguments.", funcId, 360 funcNode.Symbol.MinimumArity, funcNode.Symbol.MaximumArity)); 361 foreach (var arg in args) funcNode.AddSubtree(arg); 362 351 363 var rPar = tokens.Dequeue(); 352 364 if (rPar.TokenType != TokenType.RightPar) 353 365 throw new ArgumentException("expected )"); 354 366 355 funcNode.AddSubtree(expr);356 367 return funcNode; 357 368 } else { … … 371 382 } 372 383 } 384 385 // ArgList = Expr { ',' Expr } 386 private ISymbolicExpressionTreeNode[] ParseArgList(Queue<Token> tokens) { 387 var exprList = new List<ISymbolicExpressionTreeNode>(); 388 exprList.Add(ParseExpr(tokens)); 389 while (tokens.Peek().TokenType != TokenType.RightPar) { 390 var comma = tokens.Dequeue(); 391 if (comma.TokenType != TokenType.Comma) throw new ArgumentException("expected ',' "); 392 exprList.Add(ParseExpr(tokens)); 393 } 394 return exprList.ToArray(); 395 } 373 396 } 374 397 }
Note: See TracChangeset
for help on using the changeset viewer.