def find_index(token_list, want, start=0): for i in range(start, len(token_list)): token = token_list[i].string if token == want: return i raise exceptions.SyntaxNotMatched()
def check_for(line, line_number): # define the syntax of FOR-LOOP statement should be # FOR T FROM CONST_ID TO CONST_ID STEP CONST_ID DRAW (T,T) check_list = [] check_list.append(lexical.TokenType.FOR) check_list.append(lexical.TokenType.T) check_list.append(lexical.TokenType.FROM) check_list.append(lexical.TokenType.CONST_ID) check_list.append(lexical.TokenType.TO) check_list.append(lexical.TokenType.CONST_ID) check_list.append(lexical.TokenType.STEP) check_list.append(lexical.TokenType.CONST_ID) check_list.append(lexical.TokenType.DRAW) check_list.append(lexical.TokenType.L_BRACKET) check_list.append(lexical.TokenType.T) check_list.append(lexical.TokenType.COMMA) check_list.append(lexical.TokenType.T) check_list.append(lexical.TokenType.R_BRACKET) length = len(check_list) idx = 0 for token in line: if token.token_type == check_list[idx]: idx += 1 if idx == length: break if idx != length: raise exceptions.SyntaxNotMatched( "Syntax not matched exception in line", line_number)
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}
def check_rot(line, line_number): # define the syntax of ROT-IS statement should be # ROT IS CONST_ID check_list = [] check_list.append(lexical.TokenType.ROT) check_list.append(lexical.TokenType.IS) check_list.append(lexical.TokenType.CONST_ID) length = len(check_list) idx = 0 for token in line: if token.token_type == check_list[idx]: idx += 1 if idx == length: break if idx != length: raise exceptions.SyntaxNotMatched( "Syntax not matched exception in line", line_number)
def check_scale(line, line_number): # define the syntax of SCALE-IS statement should be # SCALE IS (CONST_ID,CONST_ID) check_list = [] check_list.append(lexical.TokenType.SCALE) check_list.append(lexical.TokenType.IS) check_list.append(lexical.TokenType.L_BRACKET) check_list.append(lexical.TokenType.CONST_ID) check_list.append(lexical.TokenType.COMMA) check_list.append(lexical.TokenType.CONST_ID) check_list.append(lexical.TokenType.R_BRACKET) length = len(check_list) idx = 0 for token in line: if token.token_type == check_list[idx]: idx += 1 if idx == length: break if idx != length: raise exceptions.SyntaxNotMatched( "Syntax not matched exception in line", line_number)
def match_token(token, want): # statement to debug # print(token, want) if token != want: raise exceptions.SyntaxNotMatched()