- Timestamp:
- 12/08/18 07:50:29 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2915-AbsoluteSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs
r16239 r16352 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() }, 97 101 { "ABS", new Absolute() }, 98 102 { "EXP", new Exponential()}, … … 171 175 && str[pos] != '*' 172 176 && str[pos] != '/' 177 && str[pos] != '^' 173 178 && str[pos] != ')' 174 179 && str[pos] != ']' 180 && str[pos] != '}' 175 181 && str[pos] != ',') { 176 182 sb.Append(str[pos]); … … 231 237 pos++; 232 238 yield return new Token { TokenType = TokenType.Operator, strVal = "*" }; 239 } else if (str[pos] == '^') { 240 pos++; 241 yield return new Token { TokenType = TokenType.Operator, strVal = "^" }; 233 242 } else if (str[pos] == '(') { 234 243 pos++; … … 243 252 pos++; 244 253 yield return new Token { TokenType = TokenType.RightBracket, strVal = "]" }; 254 } else if (str[pos] == '{') { 255 pos++; 256 yield return new Token { TokenType = TokenType.LeftPar, strVal = "{" }; 257 } else if (str[pos] == '}') { 258 pos++; 259 yield return new Token { TokenType = TokenType.RightPar, strVal = "}" }; 245 260 } else if (str[pos] == '=') { 246 261 pos++; … … 364 379 } 365 380 366 /// Fact = '(' Expr ')' 367 /// | 'LAG' '(' varId ',' ['+' | '-' ] number ')' 368 /// | funcId '(' ArgList ')' 369 /// | VarExpr | number 381 // Fact = SimpleFact ['^' SimpleFact] 382 private ISymbolicExpressionTreeNode ParseFact(Queue<Token> tokens) { 383 var expr = ParseSimpleFact(tokens); 384 var next = tokens.Peek(); 385 if (next.TokenType == TokenType.Operator && next.strVal == "^") { 386 tokens.Dequeue(); // skip; 387 388 var p = GetSymbol("^").CreateTreeNode(); 389 p.AddSubtree(expr); 390 p.AddSubtree(ParseSimpleFact(tokens)); 391 expr = p; 392 } 393 return expr; 394 } 395 396 397 /// SimpleFact = '(' Expr ')' 398 /// | '{' Expr '}' 399 /// | 'LAG' '(' varId ',' ['+' | '-' ] number ')' 400 /// | funcId '(' ArgList ') 401 /// | VarExpr 402 /// | number 370 403 /// ArgList = Expr { ',' Expr } 371 404 /// VarExpr = varId OptFactorPart … … 374 407 /// varVal = ident | ' ident ' | " ident " 375 408 /// ident = '_' | letter { '_' | letter | digit } 376 private ISymbolicExpressionTreeNode Parse Fact(Queue<Token> tokens) {409 private ISymbolicExpressionTreeNode ParseSimpleFact(Queue<Token> tokens) { 377 410 var next = tokens.Peek(); 378 411 if (next.TokenType == TokenType.LeftPar) { 379 tokens.Dequeue();412 var initPar = tokens.Dequeue(); // match par type 380 413 var expr = ParseExpr(tokens); 381 414 var rPar = tokens.Dequeue(); 382 415 if (rPar.TokenType != TokenType.RightPar) 383 throw new ArgumentException("expected )"); 416 throw new ArgumentException("expected closing parenthesis"); 417 if (initPar.strVal == "(" && rPar.strVal == "}") 418 throw new ArgumentException("expected closing )"); 419 if (initPar.strVal == "{" && rPar.strVal == ")") 420 throw new ArgumentException("expected closing }"); 384 421 return expr; 385 422 } else if (next.TokenType == TokenType.Identifier) { … … 428 465 if (rPar.TokenType != TokenType.RightPar) 429 466 throw new ArgumentException("expected )"); 467 430 468 431 469 return funcNode;
Note: See TracChangeset
for help on using the changeset viewer.