Beispiel #1
0
def program():
    lines = lexical.scanner()
    results = []  # to store the information of expressions of statement
    for line in lines:
        dir = for_statement(line)
        if dir:
            results.append(dir)
            print("<-------------- Structure of FOR Statement -------------->")
            print("*************** Expression Tree of FROM ***************")
            dir["FROM"].traverse()
            print("*************** Expression Tree of TO ***************")
            dir["TO"].traverse()
            print("*************** Expression Tree of STEP ***************")
            dir["STEP"].traverse()
            print("*************** Expression Tree of X ***************")
            dir["X"].traverse()
            print("*************** Expression Tree of Y ***************")
            dir["Y"].traverse()
            continue

        dir = scale_statement(line)
        if dir:
            results.append(dir)
            print(
                "<-------------- Structure of SCALE Statement -------------->")
            print("*************** Expression Tree of X ***************")
            dir["X"].traverse()
            print("*************** Expression Tree of Y ***************")
            dir["Y"].traverse()
            continue

        dir = rot_statement(line)
        if dir:
            results.append(dir)
            print("<-------------- Structure of ROT Statement -------------->")
            print("*************** Expression Tree of ANGLE ***************")
            dir["ANGLE"].traverse()
            continue

        dir = origin_statement(line)
        if dir:
            results.append(dir)
            print(
                "<-------------- Structure of ORIGIN Statement -------------->"
            )
            print("*************** Expression Tree of X ***************")
            dir["X"].traverse()
            print("*************** Expression Tree of Y ***************")
            dir["Y"].traverse()
            continue

        # if the statement is not matched, then error
        raise exceptions.SyntaxNotMatched("Syntax not matched")

    return {"lines": lines, "results": results}
Beispiel #2
0
        ir = node.id if node.id else node.data
        print(node.id)
        while node.data != ";":
            node = node.get_next()
            if node.id:
                ir += " " + node.id
            elif node.data:
                ir += " " + node.data
        return ir


if __name__ == "__main__":
    with open("testfiles/testfile_1.txt", 'r') as test:
        code = test.read()

    scan = scanner(code)
    scan.lexical()
    tokens = scan.tokens
    for token in tokens:
        print(token)
    print()

    parsing = parser(tokens, "grammar2.txt")

    print("LL Grammar")
    for i in parsing.grammar:
        for j in parsing.grammar[i]:
            if j[0] == '':
                print(i, '->', "''")
            else:
                print(i, '->', ' '.join(j))