Changeset 14251 for branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer
- Timestamp:
- 08/10/16 20:10:25 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs
r14026 r14251 32 32 /// Parses mathematical expressions in infix form. E.g. x1 * (3.0 * x2 + x3) 33 33 /// Identifier format (functions or variables): '_' | letter { '_' | letter | digit } 34 /// Variables names can be set under quotes "" or '' because variable names might contain spaces. 34 /// Variables names and variable values can be set under quotes "" or '' because variable names might contain spaces. 35 /// Variable = ident | " ident " | ' ident ' 35 36 /// It is also possible to use functions e.g. log("x1") or real-valued constants e.g. 3.1415 . 36 37 /// Variable names are case sensitive. Function names are not case sensitive. 38 /// 39 /// 40 /// S = Expr EOF 41 /// Expr = ['-' | '+'] Term { '+' Term | '-' Term } 42 /// Term = Fact { '*' Fact | '/' Fact } 43 /// Fact = '(' Expr ')' | funcId '(' Expr ')' | VarExpr | number 44 /// VarExpr = varId [ '=' varVal] 45 /// varId = ident | ' ident ' | " ident " 46 /// varVal = ident | ' ident ' | " ident " 47 /// ident = '_' | letter { '_' | letter | digit } 37 48 /// </summary> 38 49 public sealed class InfixExpressionParser { 39 private enum TokenType { Operator, Identifier, Number, LeftPar, RightPar, E nd, NA };50 private enum TokenType { Operator, Identifier, Number, LeftPar, RightPar, Eq, End, NA }; 40 51 private class Token { 41 52 internal double doubleVal; … … 64 75 private Constant constant = new Constant(); 65 76 private Variable variable = new Variable(); 77 private BinaryFactorVariable binaryFactorVar = new BinaryFactorVariable(); 66 78 67 79 private ProgramRootSymbol programRootSymbol = new ProgramRootSymbol(); … … 143 155 pos++; 144 156 while (pos < str.Length && !char.IsWhiteSpace(str[pos]) 145 && (str[pos] != '+' || str[pos -1] == 'e' || str[pos-1] == 'E') // continue reading exponents157 && (str[pos] != '+' || str[pos - 1] == 'e' || str[pos - 1] == 'E') // continue reading exponents 146 158 && (str[pos] != '-' || str[pos - 1] == 'e' || str[pos - 1] == 'E') 147 && str[pos] != '*' 159 && str[pos] != '*' 148 160 && str[pos] != '/' 149 161 && str[pos] != ')') { … … 211 223 pos++; 212 224 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 225 } else if (str[pos] == '=') { 226 pos++; 227 yield return new Token { TokenType = TokenType.Eq, strVal = "=" }; 228 } 229 } 230 } 231 221 232 private ISymbolicExpressionTreeNode ParseS(Queue<Token> tokens) { 222 233 var expr = ParseExpr(tokens); … … 326 337 } 327 338 328 // Fact = '(' Expr ')' | funcId '(' Expr ')' | varId | number339 // Fact = '(' Expr ')' | funcId '(' Expr ')' | varId [ = valId ] | number 329 340 private ISymbolicExpressionTreeNode ParseFact(Queue<Token> tokens) { 330 341 var next = tokens.Peek(); … … 355 366 } else { 356 367 // variable 357 var varNode = (VariableTreeNode)variable.CreateTreeNode(); 358 varNode.Weight = 1.0; 359 varNode.VariableName = idTok.strVal; 360 return varNode; 368 if (tokens.Peek().TokenType == TokenType.Eq) { 369 // binary factor 370 tokens.Dequeue(); // skip Eq 371 var valTok = tokens.Dequeue(); 372 if (valTok.TokenType != TokenType.Identifier) throw new ArgumentException("expected identifier"); 373 var binFactorNode = (BinaryFactorVariableTreeNode)binaryFactorVar.CreateTreeNode(); 374 binFactorNode.Weight = 1.0; 375 binFactorNode.VariableName = idTok.strVal; 376 binFactorNode.VariableValue = valTok.strVal; 377 return binFactorNode; 378 } else { 379 // variable 380 var varNode = (VariableTreeNode)variable.CreateTreeNode(); 381 varNode.Weight = 1.0; 382 varNode.VariableName = idTok.strVal; 383 return varNode; 384 } 361 385 } 362 386 } else if (next.TokenType == TokenType.Number) { … … 369 393 } 370 394 } 395 371 396 } 372 397 }
Note: See TracChangeset
for help on using the changeset viewer.