Skip to content

Commit

Permalink
Merge pull request #407 from Chasmiccoder/in-402
Browse files Browse the repository at this point in the history
Fixed issue 402. Now simc programs with unbalanced brackets will raise an error.
  • Loading branch information
frankhart2018 authored Dec 15, 2020
2 parents cbc9738 + 07d1c14 commit 923c81e
Showing 1 changed file with 66 additions and 5 deletions.
71 changes: 66 additions & 5 deletions simc/lexical_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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":

Expand All @@ -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]):
Expand Down Expand Up @@ -397,13 +402,31 @@ 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
got_num_or_var = False

# 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))
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

0 comments on commit 923c81e

Please sign in to comment.