<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def eval_complex_cor_pattern(self, pattern, hosts, services, hostgroups, servicegroups, running=False):
 # pylint: disable=too-many-branches """Parse and build recursively a tree of DependencyNode from a complex pattern :param pattern: pattern to parse :type pattern: str :param hosts: hosts list, used to find a specific host :type hosts: alignak.objects.host.Host :param services: services list, used to find a specific service :type services: alignak.objects.service.Service :param running: rules are evaluated at run time and parsing. True means runtime :type running: bool :return: root node of parsed tree :rtype: alignak.dependencynode.DependencyNode """

        node = DependencyNode()
        pattern = self.eval_xof_pattern(node, pattern)

        in_par = False
        tmp = ''
        son_is_not = False  # We keep is the next son will be not or not
        stacked_parenthesis = 0
        for char in pattern:
            if char == '(':
                stacked_parenthesis += 1

                in_par = True
                tmp = tmp.strip()
                # Maybe we just start a par, but we got some things in tmp
                # that should not be good in fact !
                if stacked_parenthesis == 1 and tmp != '':
                    # TODO : real error
                    print("ERROR : bad expression near", tmp)
                    continue

                # If we are already in a par, add this (
                # but not if it's the first one so
                if stacked_parenthesis > 1:
                    tmp += char

            elif char == ')':
                stacked_parenthesis -= 1

                if stacked_parenthesis < 0:
                    # TODO : real error
                    print("Error : bad expression near", tmp, "too much ')'")
                    continue

                if stacked_parenthesis == 0:
                    tmp = tmp.strip()
                    son = self.eval_cor_pattern(tmp, hosts, services,
                                                hostgroups, servicegroups, running)
                    # Maybe our son was notted
                    if son_is_not:
                        son.not_value = True
                        son_is_not = False
                    node.sons.append(son)
                    in_par = False
                    # OK now clean the tmp so we start clean
                    tmp = ''
                    continue

                # ok here we are still in a huge par, we just close one sub one
                tmp += char

            # Expressions in par will be parsed in a sub node after. So just
            # stack pattern
            elif in_par:
                tmp += char

            # Until here, we're not in par

            # Manage the NOT for an expression. Only allow ! at the beginning
            # of a host or a host,service expression.
            elif char == '!':
                tmp = tmp.strip()
                if tmp and tmp[0] != '!':
                    print("Error : bad expression near", tmp, "wrong position for '!'")
                    continue
                # Flags next node not state
                son_is_not = True
                # DO NOT keep the c in tmp, we consumed it

            elif char in ['&', '|']:
                # Oh we got a real cut in an expression, if so, cut it
                tmp = tmp.strip()
                # Look at the rule viability
                if node.operand is not None and node.operand != 'of:' and char != node.operand:
                    # Should be logged as a warning / info? :)
                    return None

                if node.operand != 'of:':
                    node.operand = char
                if tmp != '':
                    son = self.eval_cor_pattern(tmp, hosts, services,
                                                hostgroups, servicegroups, running)
                    # Maybe our son was notted
                    if son_is_not:
                        son.not_value = True
                        son_is_not = False
                    node.sons.append(son)
                tmp = ''

            # Maybe it's a classic character or we're in par, if so, continue
            else:
                tmp += char

        # Be sure to manage the trainling part when the line is done
        tmp = tmp.strip()
        if tmp != '':
            son = self.eval_cor_pattern(tmp, hosts, services,
                                        hostgroups, servicegroups, running)
            # Maybe our son was notted
            if son_is_not:
                son.not_value = True
                son_is_not = False
            node.sons.append(son)

        # We got our nodes, so we can update 0 values of of_values
        # with the number of sons
        node.switch_zeros_of_values()

        return node