Changeset 17434 for branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs
- Timestamp:
- 02/11/20 13:36:02 (5 years ago)
- Location:
- branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
-
branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs
r16130 r17434 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-2018Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 42 42 /// Expr = ['-' | '+'] Term { '+' Term | '-' Term } 43 43 /// Term = Fact { '*' Fact | '/' Fact } 44 /// Fact = '(' Expr ')' 45 /// | 'LAG' '(' varId ',' ['+' | '-' ] number ')' 46 /// | funcId '(' ArgList ')' 47 /// | VarExpr | number 44 /// Fact = SimpleFact [ '^' SimpleFact ] 45 /// SimpleFact = '(' Expr ')' 46 /// | '{' Expr '}' 47 /// | 'LAG' '(' varId ',' ['+' | '-' ] number ') 48 /// | funcId '(' ArgList ')' 49 /// | VarExpr 50 /// | number 48 51 /// ArgList = Expr { ',' Expr } 49 52 /// VarExpr = varId OptFactorPart … … 95 98 { "*", new Multiplication()}, 96 99 { "-", new Subtraction()}, 100 { "^", new Power() }, 101 { "ABS", new Absolute() }, 97 102 { "EXP", new Exponential()}, 98 103 { "LOG", new Logarithm()}, 99 { "POW", new Power() },104 { "POW", new Power() }, 100 105 { "ROOT", new Root()}, 101 106 { "SQR", new Square() }, 102 107 { "SQRT", new SquareRoot() }, 108 { "CUBE", new Cube() }, 109 { "CUBEROOT", new CubeRoot() }, 103 110 { "SIN",new Sine()}, 104 111 { "COS", new Cosine()}, 105 112 { "TAN", new Tangent()}, 113 { "TANH", new HyperbolicTangent()}, 106 114 { "AIRYA", new AiryA()}, 107 115 { "AIRYB", new AiryB()}, … … 119 127 { "DAWSON", new Dawson()}, 120 128 { "EXPINT", new ExponentialIntegralEi()}, 129 { "AQ", new AnalyticQuotient() }, 121 130 { "MEAN", new Average()}, 122 131 { "IF", new IfThenElse()}, … … 167 176 && str[pos] != '*' 168 177 && str[pos] != '/' 178 && str[pos] != '^' 169 179 && str[pos] != ')' 170 180 && str[pos] != ']' 181 && str[pos] != '}' 171 182 && str[pos] != ',') { 172 183 sb.Append(str[pos]); … … 227 238 pos++; 228 239 yield return new Token { TokenType = TokenType.Operator, strVal = "*" }; 240 } else if (str[pos] == '^') { 241 pos++; 242 yield return new Token { TokenType = TokenType.Operator, strVal = "^" }; 229 243 } else if (str[pos] == '(') { 230 244 pos++; … … 239 253 pos++; 240 254 yield return new Token { TokenType = TokenType.RightBracket, strVal = "]" }; 255 } else if (str[pos] == '{') { 256 pos++; 257 yield return new Token { TokenType = TokenType.LeftPar, strVal = "{" }; 258 } else if (str[pos] == '}') { 259 pos++; 260 yield return new Token { TokenType = TokenType.RightPar, strVal = "}" }; 241 261 } else if (str[pos] == '=') { 242 262 pos++; … … 360 380 } 361 381 362 /// Fact = '(' Expr ')' 363 /// | 'LAG' '(' varId ',' ['+' | '-' ] number ')' 364 /// | funcId '(' ArgList ')' 365 /// | VarExpr | number 382 // Fact = SimpleFact ['^' SimpleFact] 383 private ISymbolicExpressionTreeNode ParseFact(Queue<Token> tokens) { 384 var expr = ParseSimpleFact(tokens); 385 var next = tokens.Peek(); 386 if (next.TokenType == TokenType.Operator && next.strVal == "^") { 387 tokens.Dequeue(); // skip; 388 389 var p = GetSymbol("^").CreateTreeNode(); 390 p.AddSubtree(expr); 391 p.AddSubtree(ParseSimpleFact(tokens)); 392 expr = p; 393 } 394 return expr; 395 } 396 397 398 /// SimpleFact = '(' Expr ')' 399 /// | '{' Expr '}' 400 /// | 'LAG' '(' varId ',' ['+' | '-' ] number ')' 401 /// | funcId '(' ArgList ') 402 /// | VarExpr 403 /// | number 366 404 /// ArgList = Expr { ',' Expr } 367 405 /// VarExpr = varId OptFactorPart … … 370 408 /// varVal = ident | ' ident ' | " ident " 371 409 /// ident = '_' | letter { '_' | letter | digit } 372 private ISymbolicExpressionTreeNode Parse Fact(Queue<Token> tokens) {410 private ISymbolicExpressionTreeNode ParseSimpleFact(Queue<Token> tokens) { 373 411 var next = tokens.Peek(); 374 412 if (next.TokenType == TokenType.LeftPar) { 375 tokens.Dequeue();413 var initPar = tokens.Dequeue(); // match par type 376 414 var expr = ParseExpr(tokens); 377 415 var rPar = tokens.Dequeue(); 378 416 if (rPar.TokenType != TokenType.RightPar) 379 throw new ArgumentException("expected )"); 417 throw new ArgumentException("expected closing parenthesis"); 418 if (initPar.strVal == "(" && rPar.strVal == "}") 419 throw new ArgumentException("expected closing )"); 420 if (initPar.strVal == "{" && rPar.strVal == ")") 421 throw new ArgumentException("expected closing }"); 380 422 return expr; 381 423 } else if (next.TokenType == TokenType.Identifier) { … … 424 466 if (rPar.TokenType != TokenType.RightPar) 425 467 throw new ArgumentException("expected )"); 468 426 469 427 470 return funcNode;
Note: See TracChangeset
for help on using the changeset viewer.