Ejemplo n.º 1
0
def makeTree( orderedTokens):
    
    root = recurse(orderedTokens)

    runner = root
    stack = Stack()
    while runner:
        rep = runner.value
        if runner.left:
            rep+=" l: "+runner.left.value
            stack.push(runner.left)
        if runner.right:
            rep+=" r: "+runner.right.value
            stack.push(runner.right)
        runner = stack.pop()
    return root
Ejemplo n.º 2
0
def ll_parsing(parse_table, grammar, input_string):

    print "\nOutput: "
    input_symbols = input_string.split(",")
    stack = Stack()
    stack.push('$')
    stack.push(grammar.non_terminals[0])
    input_token = input_symbols[0]

    i = 0
    errors = []

    x = stack.peek()
    error = False
    while x is not '$':
        if x == input_token:
            print "MATCH " + input_token
            stack.pop()
            input_symbols.pop(0)
            input_token = str(input_symbols[0])
            i += 1
        elif x in grammar.terminals:
            print "Error in " + input_string + "\t\t fatal"
            errors.append(i)
            error = True
            break
        else:
            parse_table_entry = look_parse_table(parse_table, x, input_token,grammar)
            if parse_table_entry is ERROR:
                print "Error in " + input_token + "\t\t fatal"
                errors.append(i)
                error = True
                break
            elif parse_table_entry is SYNC:
                print "Error in " + input_token + "\t\t recuperable"
                errors.append(i)
                error = True
                stack.pop()
            else:
                production = grammar.productions[parse_table_entry]
                print production.get_object_as_string()
                stack.pop()
                if '@' not in production.body:
                    for item in reversed(production.body):
                        stack.push(item)
        x = stack.peek()


    if not error:
        print "\nSuccess"
    else:
        print "\nError"