예제 #1
0
def parse_object(tokens):
    s = Stack()
    tokens_count = len(tokens)
    n = 0
    while n < tokens_count:
        elem = tokens[n]
        n += 1
        if elem == ']':
            l = []
            while s.top() != '[':
                top = s.pop()
                l.insert(0, top)
            s.pop()
            s.push(l)
            continue

        if elem == '}':
            l = []
            while s.top() != '{':
                top = s.pop()
                l.insert(0, top)
            s.pop()
            if len(l) > 0 and not len(l) % 3 == 0 and not l.count(':') == len(l) / 3:
                raise ValueError
            d = {l[i]: l[i + 2] for i in range(0, len(l), 3)}
            s.push(d)
            continue

        s.push(elem)

    return s.top()
예제 #2
0
    def analyze(self, word):
        word = Stack(*reversed(word))
        qi = self.q0
        stack = Stack(self.Z0)

        memory = [(qi, word, stack)]

        depth = 0
        new_memory = []

        for (qi, word, stack) in memory:
            possibleRules = list(
                filter(
                    lambda rule: rule[0] == qi and rule[1] in
                    (word.top(), 'ε') and rule[2] in
                    (stack[:len(rule[1])], 'ε'), self.ô))

            print(qi, word.reverse(), stack.reverse())
            print(possibleRules)