Changeset 14564 for stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs
- Timestamp:
- 01/14/17 19:08:39 (8 years ago)
- Location:
- stable
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 14258,14316,14319,14347
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic merged: 14319,14347
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs
r14116 r14564 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]); 143 143 pos++; 144 144 while (pos < str.Length && !char.IsWhiteSpace(str[pos]) 145 && (str[pos] != '+' || str[pos -1] == 'e' || str[pos-1] == 'E') // continue reading exponents145 && (str[pos] != '+' || str[pos - 1] == 'e' || str[pos - 1] == 'E') // continue reading exponents 146 146 && (str[pos] != '-' || str[pos - 1] == 'e' || str[pos - 1] == 'E') 147 && str[pos] != '*' 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 = ")" }; 213 } 214 } 215 } 216 217 // S = Expr EOF 218 // Expr = ['-' | '+'] Term { '+' Term | '-' Term } 219 // Term = Fact { '*' Fact | '/' Fact } 220 // Fact = '(' Expr ')' | funcId '(' Expr ')' | varId | number 214 } else if (str[pos] == ',') { 215 pos++; 216 yield return new Token { TokenType = TokenType.Comma, strVal = "," }; 217 } else { 218 throw new ArgumentException("Invalid character: " + str[pos]); 219 } 220 } 221 } 222 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 } 221 228 private ISymbolicExpressionTreeNode ParseS(Queue<Token> tokens) { 222 229 var expr = ParseExpr(tokens); … … 326 333 } 327 334 328 // Fact = '(' Expr ')' | funcId '(' Expr')' | varId | number335 // Fact = '(' Expr ')' | funcId '(' ArgList ')' | varId | number 329 336 private ISymbolicExpressionTreeNode ParseFact(Queue<Token> tokens) { 330 337 var next = tokens.Peek(); … … 346 353 if (lPar.TokenType != TokenType.LeftPar) 347 354 throw new ArgumentException("expected ("); 348 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 349 363 var rPar = tokens.Dequeue(); 350 364 if (rPar.TokenType != TokenType.RightPar) 351 365 throw new ArgumentException("expected )"); 352 366 353 funcNode.AddSubtree(expr);354 367 return funcNode; 355 368 } else { … … 369 382 } 370 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 } 371 396 } 372 397 }
Note: See TracChangeset
for help on using the changeset viewer.