Changeset 16315 for branches/2929_PrioritizedGrammarEnumeration/HeuristicLab.Problems.DataAnalysis.Symbolic
- Timestamp:
- 11/21/18 18:17:18 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2929_PrioritizedGrammarEnumeration/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs
r15583 r16315 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 { "EXP", new Exponential()}, 98 102 { "LOG", new Logarithm()}, … … 167 171 && str[pos] != '*' 168 172 && str[pos] != '/' 173 && str[pos] != '^' 169 174 && str[pos] != ')' 170 175 && str[pos] != ']' 176 && str[pos] != '}' 171 177 && str[pos] != ',') { 172 178 sb.Append(str[pos]); … … 227 233 pos++; 228 234 yield return new Token { TokenType = TokenType.Operator, strVal = "*" }; 235 } else if (str[pos] == '^') { 236 pos++; 237 yield return new Token { TokenType = TokenType.Operator, strVal = "^" }; 229 238 } else if (str[pos] == '(') { 230 239 pos++; … … 239 248 pos++; 240 249 yield return new Token { TokenType = TokenType.RightBracket, strVal = "]" }; 250 } else if (str[pos] == '{') { 251 pos++; 252 yield return new Token { TokenType = TokenType.LeftPar, strVal = "{" }; 253 } else if (str[pos] == '}') { 254 pos++; 255 yield return new Token { TokenType = TokenType.RightPar, strVal = "}" }; 241 256 } else if (str[pos] == '=') { 242 257 pos++; … … 360 375 } 361 376 362 /// Fact = '(' Expr ')' 363 /// | 'LAG' '(' varId ',' ['+' | '-' ] number ')' 364 /// | funcId '(' ArgList ')' 365 /// | VarExpr | number 377 // Fact = SimpleFact ['^' SimpleFact] 378 private ISymbolicExpressionTreeNode ParseFact(Queue<Token> tokens) { 379 var expr = ParseSimpleFact(tokens); 380 var next = tokens.Peek(); 381 if (next.TokenType == TokenType.Operator && next.strVal == "^") { 382 tokens.Dequeue(); // skip; 383 384 var p = GetSymbol("^").CreateTreeNode(); 385 p.AddSubtree(expr); 386 p.AddSubtree(ParseSimpleFact(tokens)); 387 expr = p; 388 } 389 return expr; 390 } 391 392 393 /// SimpleFact = '(' Expr ')' 394 /// | '{' Expr '}' 395 /// | 'LAG' '(' varId ',' ['+' | '-' ] number ')' 396 /// | funcId '(' ArgList ') 397 /// | VarExpr 398 /// | number 366 399 /// ArgList = Expr { ',' Expr } 367 400 /// VarExpr = varId OptFactorPart … … 370 403 /// varVal = ident | ' ident ' | " ident " 371 404 /// ident = '_' | letter { '_' | letter | digit } 372 private ISymbolicExpressionTreeNode Parse Fact(Queue<Token> tokens) {405 private ISymbolicExpressionTreeNode ParseSimpleFact(Queue<Token> tokens) { 373 406 var next = tokens.Peek(); 374 407 if (next.TokenType == TokenType.LeftPar) { 375 tokens.Dequeue();408 var initPar = tokens.Dequeue(); // match par type 376 409 var expr = ParseExpr(tokens); 377 410 var rPar = tokens.Dequeue(); 378 411 if (rPar.TokenType != TokenType.RightPar) 379 throw new ArgumentException("expected )"); 412 throw new ArgumentException("expected closing parenthesis"); 413 if (initPar.strVal == "(" && rPar.strVal == "}") 414 throw new ArgumentException("expected closing )"); 415 if (initPar.strVal == "{" && rPar.strVal == ")") 416 throw new ArgumentException("expected closing }"); 380 417 return expr; 381 418 } else if (next.TokenType == TokenType.Identifier) { … … 424 461 if (rPar.TokenType != TokenType.RightPar) 425 462 throw new ArgumentException("expected )"); 463 426 464 427 465 return funcNode;
Note: See TracChangeset
for help on using the changeset viewer.