Changeset 18171


Ignore:
Timestamp:
12/27/21 10:57:02 (6 months ago)
Author:
gkronber
Message:

#2938: fixed a bug in the parser

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs

    r18169 r18171  
    377377
    378378    private void FoldLeftRecursive(ISymbolicExpressionTreeNode parent) {
    379       if (parent.SubtreeCount > 0) {
     379      if (parent.SubtreeCount > 1) {
    380380        var child = parent.GetSubtree(0);
    381381        FoldLeftRecursive(child);
    382         if(parent.Symbol == child.Symbol) {
     382        if (parent.Symbol == child.Symbol && IsAssociative(parent.Symbol)) {
    383383          parent.RemoveSubtree(0);
    384           for(int i=0;i<child.SubtreeCount; i++) {
     384          for (int i = 0; i < child.SubtreeCount; i++) {
    385385            parent.InsertSubtree(i, child.GetSubtree(i));
    386386          }
     
    443443        // '<' 'num' [ '=' ['+'|'-'] number ] '>'
    444444        return ParseNumber(tokens);
    445       } else if(next.TokenType == TokenType.Operator && (next.strVal == "-" || next.strVal == "+")) {
     445      } else if (next.TokenType == TokenType.Operator && (next.strVal == "-" || next.strVal == "+")) {
    446446        // ['+' | '-' ] SimpleFact
    447447        if (tokens.Dequeue().strVal == "-") {
     
    499499        if (next.strVal == "+" || next.strVal == "-") {
    500500          if (tokens.Dequeue().strVal == "-") sign = -1.0;
    501         } 
    502         if(tokens.Peek().TokenType != TokenType.Number) {
     501        }
     502        if (tokens.Peek().TokenType != TokenType.Number) {
    503503          throw new ArgumentException("Expected number.");
    504504        }
     
    632632      return exprList.ToArray();
    633633    }
     634
     635    private bool IsAssociative(ISymbol sy) {
     636      return sy == GetSymbol("+") || sy == GetSymbol("-") ||
     637             sy == GetSymbol("*") || sy == GetSymbol("/") ||
     638             sy == GetSymbol("AND") || sy == GetSymbol("OR") || sy == GetSymbol("XOR");
     639    }
    634640  }
    635641}
  • trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/InfixExpressionParserTest.cs

    r18170 r18171  
    145145      // numeric parameter with sign
    146146      Assert.AreEqual("1", formatter.Format(parser.Parse("-<num=-1.0>")));
     147
     148      // nested functions
     149      Assert.AreEqual("SIN(SIN(SIN('X1')))", formatter.Format(parser.Parse("SIN(SIN(SIN(X1)))")));
    147150    }
    148151  }
Note: See TracChangeset for help on using the changeset viewer.