diff --git a/simc/lexical_analyzer.py b/simc/lexical_analyzer.py index a32ba57..7f897a5 100644 --- a/simc/lexical_analyzer.py +++ b/simc/lexical_analyzer.py @@ -288,6 +288,7 @@ def get_raw_tokens(source_code,i,line_num): i += 1 line_num += 1 + def lexical_analyze(filename, table): """ Generate tokens from source code @@ -338,6 +339,10 @@ def lexical_analyze(filename, table): # This is to presently differentiate between bitwise and # and address of operations. got_num_or_var = False + + # To check if the brackets are balanced: + top = -1 + balanced_brackets_stack = [ ] while source_code[i] != "\0": @@ -346,8 +351,8 @@ def lexical_analyze(filename, table): raw_tokens,i,line_num = get_raw_tokens(source_code,i,line_num) tokens.extend(raw_tokens) raw_c = False - got_num_or_var = False - + got_num_or_var = False + # If a digit appears, call numeric_val function and add the numeric token to list, # if it was correct if is_digit(source_code[i]): @@ -397,6 +402,10 @@ def lexical_analyze(filename, table): # Identifying left paren token elif source_code[i] == "(": + # To check if brackets are balanced: + top += 1 + balanced_brackets_stack.append( '(' ) + parantheses_count += 1 tokens.append(Token("left_paren", "", line_num)) i += 1 @@ -404,6 +413,20 @@ def lexical_analyze(filename, table): # Identifying right paren token elif source_code[i] == ")": + # To check if brackets are balanced: + if top == -1: + # If at any time there is underflow, there are too many closing brackets. + top -= 1 + balanced_brackets_stack = balanced_brackets_stack[ :-1] + error( "Too many closing parentheses", line_num ) + elif balanced_brackets_stack[ top ] != '(': + error( "Unbalanced parentheses error", line_num ) + + + else: + top -= 1 + balanced_brackets_stack = balanced_brackets_stack[ :-1] + if parantheses_count > 0: parantheses_count -= 1 tokens.append(Token("right_paren", "", line_num)) @@ -417,7 +440,7 @@ def lexical_analyze(filename, table): tokens.append(Token("call_end", "", line_num)) else: - error("Parentheses does not match.", line_num); + error("Parentheses does not match", line_num) got_num_or_var = False i += 1 @@ -427,30 +450,64 @@ def lexical_analyze(filename, table): if parantheses_count == 0: tokens.append(Token("newline", "", line_num)) else: - error("Parentheses does not match.", line_num); + error("Parentheses does not match.", line_num) i += 1 line_num += 1 # Identifying left brace token elif source_code[i] == "{": + # To check if brackets are balanced: + top += 1 + balanced_brackets_stack.append( '{' ) + tokens.append(Token("left_brace", "", line_num)) i += 1 got_num_or_var = False # Identifying right brace token elif source_code[i] == "}": + # To check if brackets are balanced: + if top == -1: + # If at any time there is underflow, there are too many closing brackets. + top -= 1 + balanced_brackets_stack = balanced_brackets_stack[ :-1] + error( "Too many closing braces", line_num ) + elif balanced_brackets_stack[ top ] != '{': + error( "Unbalanced braces error", line_num ) + + else: + top -= 1 + balanced_brackets_stack = balanced_brackets_stack[ :-1] + tokens.append(Token("right_brace", "", line_num)) i += 1 got_num_or_var = False # Identifying left bracket token elif source_code[i] == "[": + # To check if brackets are balanced: + top += 1 + balanced_brackets_stack.append( '[' ) + tokens.append(Token("left_bracket", "", line_num)) i += 1 # Identifying right bracket token elif source_code[i] == "]": + # To check if brackets are balanced: + if top == -1: + # If at any time there is underflow, there are too many closing brackets. + top -= 1 + balanced_brackets_stack = balanced_brackets_stack[ :-1] + error( "Too many closing brackets", line_num ) + elif balanced_brackets_stack[ top ] != '[': + error( "Unbalanced brackets error", line_num ) + + else: + top -= 1 + balanced_brackets_stack = balanced_brackets_stack[ :-1] + tokens.append(Token("right_bracket", "", line_num)) i += 1 @@ -632,6 +689,10 @@ def lexical_analyze(filename, table): # Otherwise increment the index else: i += 1 - + + # By the end, if stack is not empty, there are extra opening brackets + if top != -1: + error( "Unbalanced parenthesis error", line_num ) + # Return the generated tokens return tokens, module_source_paths