def show_parser_table(input_list, todos, inputs, action):

    headers = ["Todo", "Input", "Action"]
    table = zip(todos, inputs, action)
    print_blue("*." * 20)
    print_yellow(f"input is : {input_list}")
    print(tabulate(table, headers=headers))
Example #2
0
    def show_tree(self):

        nodes_list = [self]
        current_node = self

        
        while nodes_list:
            current_node = nodes_list.pop(0)
            print("---"*current_node.depth, end='-' )
            print_dark_cyan(f"Name: {current_node.name}, Id: {current_node.id}")
            

            
            for n in current_node.children:
                nodes_list.append(n)
                print("---"*n.depth, end='-' )
                print_yellow(f"Child Name: {n.name}, Id: {n.id}")
Example #3
0
    def simplify_tree(self):

        # for each leaf that is epsilon 𝛆, remove it, and its parent if it is the only child
        unwanted = []
        x = []
        for leaf in self.leaves:
            print_yellow(leaf.name)
            if leaf.name == '𝛆':
                #print_red("it is eps")
                parent = leaf.parent
                #print_green(parent.children)
                parent.children = parent.children.remove(leaf)
                #self.leaves.remove(leaf)
                x.append(leaf)
                
                

                if not isinstance(parent.children, list):
                    parent.children = []

                del leaf
        for i in x:
            self.leaves.remove(i)       
Example #4
0
def print_node(n, option=0):
    block_list = ["METHOD_BODY", "WHILE", "IF","DECLARATION", "STATEMENT_LIST_2","STATEMENT_LIST"]
    if not option:
        if n.name in block_list:
            print_yellow(f"Node Name: {n.name}")
        elif n.name == '"relop"':
            print_dark_cyan(f"Node Name: {n.name}, Value: {n.value}, lex: {n.lexeme}")
        else:
            print_dark_cyan(f"Node Name: {n.name}, Value: {n.value}, lex: {n.lexeme}, type:{n.type}")
    else:
        if n.name == "METHOD_BODY":
             print_yellow(f"Node Name: {n.name}, code: {n.code}")
        elif n.name in block_list:
            print_yellow(f"Node Name: {n.name}")
        elif n.name == '"relop"':
            print_dark_cyan(f"Node Name: {n.name}, Value: {n.value}, lex: {n.lexeme} ")
        else:
            print_dark_cyan(f"Node Name: {n.name}, Value: {n.value}, lex: {n.lexeme}, type:{n.type}")
Example #5
0
def main():

    ## set default file for args
    actions_file = get_arg(1, "inputs/actions.txt")
    lexemes_file = get_arg(2, "inputs/lexemes.txt")

    
    cd = get_current_directory()
    # actions = ["METHOD_BODY   ⟶   ['STATEMENT_LIST']", "STATEMENT_LIST   ⟶   ['STATEMENT', 'STATEMENT_LIST_2']", "STATEMENT   ⟶   ['DECLARATION']", 'DECLARATION   ⟶   [\'PRIMITIVE_TYPE\', "\'id\'", "\';\'"]', 'PRIMITIVE_TYPE   ⟶   ["\'int\'"]', "Match : 'int'", "Match : 'id'", "Match : ';'", "STATEMENT_LIST_2   ⟶   ['STATEMENT', 'STATEMENT_LIST_2']", "STATEMENT   ⟶   ['ASSIGNMENT']", 'ASSIGNMENT   ⟶   ["\'id\'", "\'assign\'", \'EXPRESSION\', "\';\'"]', "Match : 'id'", "Match : 'assign'", "EXPRESSION   ⟶   ['SIMPLE_EXPRESSION', 'EXPRESSION_2']", "SIMPLE_EXPRESSION   ⟶   ['TERM', 'SIMPLE_EXPRESSION_2']", "TERM   ⟶   ['FACTOR', 'TERM_2']", 'FACTOR   ⟶   ["\'num\'"]', "Match : 'num'", 'TERM_2   ⟶   not found', 'SIMPLE_EXPRESSION_2   ⟶   not found', 'EXPRESSION_2   ⟶   not found', "Match : ';'", "STATEMENT_LIST_2   ⟶   ['STATEMENT', 'STATEMENT_LIST_2']", "STATEMENT   ⟶   ['DECLARATION']", 'DECLARATION   ⟶   [\'PRIMITIVE_TYPE\', "\'id\'", "\';\'"]', 'PRIMITIVE_TYPE   ⟶   ["\'int\'"]', "Match : 'int'", "Match : 'id'", "Match : ';'", "STATEMENT_LIST_2   ⟶   ['STATEMENT', 'STATEMENT_LIST_2']", "STATEMENT   ⟶   ['ASSIGNMENT']", 'ASSIGNMENT   ⟶   ["\'id\'", "\'assign\'", \'EXPRESSION\', "\';\'"]', "Match : 'id'", "Match : 'assign'", "EXPRESSION   ⟶   ['SIMPLE_EXPRESSION', 'EXPRESSION_2']", "SIMPLE_EXPRESSION   ⟶   ['TERM', 'SIMPLE_EXPRESSION_2']", "TERM   ⟶   ['FACTOR', 'TERM_2']", 'FACTOR   ⟶   ["\'num\'"]', "Match : 'num'", 'TERM_2   ⟶   not found', 'SIMPLE_EXPRESSION_2   ⟶   not found', 'EXPRESSION_2   ⟶   not found', "Match : ';'", "STATEMENT_LIST_2   ⟶   ['STATEMENT', 'STATEMENT_LIST_2']", "STATEMENT   ⟶   ['DECLARATION']", 'DECLARATION   ⟶   [\'PRIMITIVE_TYPE\', "\'id\'", "\';\'"]', 'PRIMITIVE_TYPE   ⟶   ["\'int\'"]', "Match : 'int'", "Match : 'id'", "Match : ';'", "STATEMENT_LIST_2   ⟶   ['STATEMENT', 'STATEMENT_LIST_2']", "STATEMENT   ⟶   ['ASSIGNMENT']", 'ASSIGNMENT   ⟶   ["\'id\'", "\'assign\'", \'EXPRESSION\', "\';\'"]', "Match : 'id'", "Match : 'assign'", "EXPRESSION   ⟶   ['SIMPLE_EXPRESSION', 'EXPRESSION_2']", "SIMPLE_EXPRESSION   ⟶   ['TERM', 'SIMPLE_EXPRESSION_2']", "TERM   ⟶   ['FACTOR', 'TERM_2']", 'FACTOR   ⟶   ["\'num\'"]', "Match : 'num'", 'TERM_2   ⟶   not found', 'SIMPLE_EXPRESSION_2   ⟶   not found', 'EXPRESSION_2   ⟶   not found', "Match : ';'", "STATEMENT_LIST_2   ⟶   ['STATEMENT', 'STATEMENT_LIST_2']", "STATEMENT   ⟶   ['ASSIGNMENT']", 'ASSIGNMENT   ⟶   ["\'id\'", "\'assign\'", \'EXPRESSION\', "\';\'"]', "Match : 'id'", "Match : 'assign'", "EXPRESSION   ⟶   ['SIMPLE_EXPRESSION', 'EXPRESSION_2']", "SIMPLE_EXPRESSION   ⟶   ['TERM', 'SIMPLE_EXPRESSION_2']", "TERM   ⟶   ['FACTOR', 'TERM_2']", 'FACTOR   ⟶   ["\'id\'"]', "Match : 'id'", 'TERM_2   ⟶   not found', 'SIMPLE_EXPRESSION_2   ⟶   ["\'addop\'", \'TERM\', \'SIMPLE_EXPRESSION_2\']', "Match : 'addop'", "TERM   ⟶   ['FACTOR', 'TERM_2']", 'FACTOR   ⟶   ["\'id\'"]', "Match : 'id'", 'TERM_2   ⟶   ["\'mulop\'", \'FACTOR\', \'TERM_2\']', "Match : 'mulop'", 'FACTOR   ⟶   ["\'num\'"]', "Match : 'num'", 'TERM_2   ⟶   not found', 'SIMPLE_EXPRESSION_2   ⟶   not found', 'EXPRESSION_2   ⟶   not found', "Match : ';'", "STATEMENT_LIST_2   ⟶   ['STATEMENT', 'STATEMENT_LIST_2']", "STATEMENT   ⟶   ['IF']", 'IF   ⟶   ["\'if\'", "\'(\'", \'EXPRESSION\', "\')\'", "\'{\'", \'STATEMENT\', "\'}\'", "\'else\'", "\'{\'", \'STATEMENT\', "\'}\'"]', "Match : 'if'", "Match : '('", "EXPRESSION   ⟶   ['SIMPLE_EXPRESSION', 'EXPRESSION_2']", "SIMPLE_EXPRESSION   ⟶   ['TERM', 'SIMPLE_EXPRESSION_2']", "TERM   ⟶   ['FACTOR', 'TERM_2']", 'FACTOR   ⟶   ["\'id\'"]', "Match : 'id'", 'TERM_2   ⟶   not found', 'SIMPLE_EXPRESSION_2   ⟶   not found', 'EXPRESSION_2   ⟶   ["\'relop\'", \'SIMPLE_EXPRESSION\']', "Match : 'relop'", "SIMPLE_EXPRESSION   ⟶   ['TERM', 'SIMPLE_EXPRESSION_2']", "TERM   ⟶   ['FACTOR', 'TERM_2']", 'FACTOR   ⟶   ["\'num\'"]', "Match : 'num'", 'TERM_2   ⟶   not found', 'SIMPLE_EXPRESSION_2   ⟶   not found', "Match : ')'", "Match : '{'", "STATEMENT   ⟶   ['ASSIGNMENT']", 'ASSIGNMENT   ⟶   ["\'id\'", "\'assign\'", \'EXPRESSION\', "\';\'"]', "Match : 'id'", "Match : 'assign'", "EXPRESSION   ⟶   ['SIMPLE_EXPRESSION', 'EXPRESSION_2']", "SIMPLE_EXPRESSION   ⟶   ['TERM', 'SIMPLE_EXPRESSION_2']", "TERM   ⟶   ['FACTOR', 'TERM_2']", 'FACTOR   ⟶   ["\'num\'"]', "Match : 'num'", 'TERM_2   ⟶   not found', 'SIMPLE_EXPRESSION_2   ⟶   not found', 'EXPRESSION_2   ⟶   not found', "Match : ';'", "Match : '}'", "Match : 'else'", "Match : '{'", "STATEMENT   ⟶   ['ASSIGNMENT']", 'ASSIGNMENT   ⟶   ["\'id\'", "\'assign\'", \'EXPRESSION\', "\';\'"]', "Match : 'id'", "Match : 'assign'", "EXPRESSION   ⟶   ['SIMPLE_EXPRESSION', 'EXPRESSION_2']", "SIMPLE_EXPRESSION   ⟶   ['TERM', 'SIMPLE_EXPRESSION_2']", "TERM   ⟶   ['FACTOR', 'TERM_2']", 'FACTOR   ⟶   ["\'num\'"]', "Match : 'num'", 'TERM_2   ⟶   not found', 'SIMPLE_EXPRESSION_2   ⟶   not found', 'EXPRESSION_2   ⟶   not found', "Match : ';'", "Match : '}'", "STATEMENT_LIST_2   ⟶   ['STATEMENT', 'STATEMENT_LIST_2']", "STATEMENT   ⟶   ['WHILE']", 'WHILE   ⟶   ["\'while\'", "\'(\'", \'EXPRESSION\', "\')\'", "\'{\'", \'STATEMENT\', "\'}\'"]', "Match : 'while'", "Match : '('", "EXPRESSION   ⟶   ['SIMPLE_EXPRESSION', 'EXPRESSION_2']", "SIMPLE_EXPRESSION   ⟶   ['TERM', 'SIMPLE_EXPRESSION_2']", "TERM   ⟶   ['FACTOR', 'TERM_2']", 'FACTOR   ⟶   ["\'id\'"]', "Match : 'id'", 'TERM_2   ⟶   not found', 'SIMPLE_EXPRESSION_2   ⟶   not found', 'EXPRESSION_2   ⟶   ["\'relop\'", \'SIMPLE_EXPRESSION\']', "Match : 'relop'", "SIMPLE_EXPRESSION   ⟶   ['TERM', 'SIMPLE_EXPRESSION_2']", "TERM   ⟶   ['FACTOR', 'TERM_2']", 'FACTOR   ⟶   ["\'num\'"]', "Match : 'num'", 'TERM_2   ⟶   not found', 'SIMPLE_EXPRESSION_2   ⟶   not found', "Match : ')'", "Match : '{'", "STATEMENT   ⟶   ['ASSIGNMENT']", 'ASSIGNMENT   ⟶   ["\'id\'", "\'assign\'", \'EXPRESSION\', "\';\'"]', "Match : 'id'", "Match : 'assign'", "EXPRESSION   ⟶   ['SIMPLE_EXPRESSION', 'EXPRESSION_2']", "SIMPLE_EXPRESSION   ⟶   ['TERM', 'SIMPLE_EXPRESSION_2']", "TERM   ⟶   ['FACTOR', 'TERM_2']", 'FACTOR   ⟶   ["\'id\'"]', "Match : 'id'", 'TERM_2   ⟶   not found', 'SIMPLE_EXPRESSION_2   ⟶   ["\'addop\'", \'TERM\', \'SIMPLE_EXPRESSION_2\']', "Match : 'addop'", "TERM   ⟶   ['FACTOR', 'TERM_2']", 'FACTOR   ⟶   ["\'num\'"]', "Match : 'num'", 'TERM_2   ⟶   not found', 'SIMPLE_EXPRESSION_2   ⟶   not found', 'EXPRESSION_2   ⟶   not found', "Match : ';'", "Match : '}'", 'STATEMENT_LIST_2   ⟶   not found', 'Match : $', 'Success']
    
    action_path = cd + '/' + actions_file

    actions = read_file_list(action_path)

    ## modify the parsing table to adapt with this phase
    act = modify_actions(actions)
    act = post_modify_actions(act)
    act = post_modify_actions_2(act)

    
    ## make sure the actions list is not empty
    root_name = act[0][0][0]

    ## assign the root node
    root = Node(root_name, None)
    root.build_tree(act)


    ## remove epsillon nodes
    root.simplify_it(['𝛆'])

    lex_path = cd + '/' + lexemes_file
    lex = read_file_list(lex_path)
    
    ## add lexemes to the tree
    root.add_lexemes(lex)

    ## mind that additional "" is added
    ## simplify tree >> APT

    root.simplify_it(['";"', '"{"','"}"', '"("', '")"','"if"','"else"','"while"'])

    root.update_leaves()
    
    #for i in root.leaves:
    #    print_green(f"leaf update: {i.name}")


    ## build the AST
    root.eliminate_exp({'"addop"', '"mulop"', '"relop"', '"assign"'})
    reduce_tree(root)

    ## build symbol table
    symtab = SymTable()

    ## eval values for each node
    get_val_2(root,symtab)

    #root.show_tree_2()

    ## eval values for conditional nodes
    get_val_virtual(root,symtab)
    
    ## generate the byte code
    generate_code(root, symtab)
    #root.show_tree_2(1)
    code = root.code
    code = root.get_code()

    #print_blue("code is")
    #print_yellow(code)

    code_list = code.split('\n')
    #print_green(code_list)
    #for i in code_list:
    #    print_green(i)

    root.backpatch()
    #print_purple(root.jasmin_in("coode"))
    

    code_list = root.get_code()
    print_green(code_list)

    print_yellow(symtab.coolTable)

    # output j file for jasmin
    code_path = cd + '/' + 'outputs' + '/' + 'code.j'
    write_in_file(code_path, root.jasmin_in("code"))
Example #6
0
def parse_input(parsing_table, input_list, start_symbol, non_terminal_list, terminal_list, delay=0):
    '''
    http://www.cs.ecu.edu/karl/5220/spr16/Notes/Top-down/LL1.html

    stack todo = [$]
    stack matched = []
    t = get_next_input()

    while todo is not empty:
        if TOS(todo) is non terminal:
            x = lookup_table(TOS(todo), t)
            if x:
                todo.push(x)
            else:
                raise_error(panic)
        else if TOS(todo) is terminal:
            if t == TOS(todo):
                t= get_next_input()
            else:
                raise_error(panic)
    
    '''
    input_list.append('$')
    terminal_list.add('𝛆')
    terminal_list.add('$')

    print_blue(f"the input: {input_list}")
    t= input_list.pop(0)
    print_yellow(f"current token: {t}")
    todo = ['$',start_symbol]
    match = []
    action = []
    s = []
    todos = [todo.copy()]
    count = 0

    #temp state
    first_state = ['$',start_symbol]

    # todos
    A = []
    #inputs
    B = []
    #actions
    C = []

    # returns todos , actions, inputs
    inputs = [t]
    if count == 0:
        todos.append(todo.copy())
    while todo:
        c = "Consume 𝛆"

        time.sleep(delay)
        # todos
        todos.append(todo[-1])

        if todo[-1] in non_terminal_list:
            print_yellow(f'todo.TOS : {todo[-1]}, terminal : {t}')
            x = lookup_table(parsing_table, todo[-1], t)

        
            #tos = todo[-1]

            tos =todo.pop(-1)
            #todos.append(tos.copy())

            s.append(tos)

           
            if x:
                action.append({tos:x.copy()})
                c = {tos:x.copy()}

                print_green(f"found in table {x}")
                # x is a list needs some pops
                x.reverse()
                #replace the current terminal with the def
                #todo.pop(-1)
                while(x):
                    todo.append(x.pop(0))
            
            else:
                #print_red("Not found in table")
                action.append({tos:"not found"})

                c = {tos:"not found"}

                pass

        elif todo[-1] in terminal_list:
            # if equal remove it in both sides
            if t == todo[-1]:
                print_green(f"*.*.* Match : {t} *.*.*")
                action.append(f"Match : {t}")

                match.append(t)
                c = f"Match : {t}"

                if input_list:
                    t = input_list.pop(0)
                    inputs.append(t)

                tos = todo.pop(-1)
                s.append(tos)
                
            elif todo[-1] == '𝛆':
                tos = todo.pop(-1)
                s.append(tos)
                action.append({tos:"epsi"})
                
            else:
                print_yellow("Sytanx Error")
                todo.pop(-1)
                

        print_dark_cyan(f"Todo Stack: {todo}")
        print_blue(f"Current Token: {t}")
        print_yellow(f"Matches Stack: {match}")
        A.append(todo.copy())
        B.append(t)
        C.append(pretty_rule(c))
        count +=1

    
    A.insert(0,first_state)

    if not input_list:
        print_green("*.*.* Success! *.*.*")
        B.append(" ")
        C.append("Success")
        return A,B,C
        return True
    else:
        print_purple("Sytanx Error")
        return False