├── Binary.py ├── Generator.py ├── README.md ├── SayehInstruction.py ├── Semantic.py ├── StateMachine.py ├── Syntax.py ├── Token.py ├── check.py ├── compiler.py └── test.txt /Binary.py: -------------------------------------------------------------------------------- 1 | def decimal_to_binary(dec_num='', bin_len=16,low=False,high=False): 2 | number = abs(int(dec_num)) 3 | binary = '' 4 | while number >= 1: 5 | binary = str(number % 2)+str(binary) 6 | number = int(number/2) 7 | len_digit = len(binary) 8 | for i in range(bin_len-len_digit): 9 | binary = '0'+binary 10 | if low: 11 | return binary[8:] 12 | if high: 13 | return binary[:8] 14 | return binary 15 | 16 | 17 | def binary_to_decimal(bin_num): 18 | binary = str(bin_num) 19 | power = len(bin_num) - 1 20 | decimal = 0 21 | for digit in binary: 22 | decimal += (2**power) * int(digit) 23 | power -= 1 24 | return str(decimal) 25 | 26 | def complement(bin_num,sec_num): 27 | bin_num= str(bin_num) 28 | bin_len = len(bin_num) - 1 29 | return 2**bin_len-int(binary_to_decimal(bin_num))+int(sec_num) -------------------------------------------------------------------------------- /Generator.py: -------------------------------------------------------------------------------- 1 | from StateMachine import * 2 | from Token import * 3 | from Binary import * 4 | from SayehInstruction import * 5 | from Semantic import operation_priority, main_operations, rel_operations 6 | 7 | symbol_table = [] 8 | identifier_table = [] 9 | keyword_table = [] 10 | regmem_table = [] 11 | number_table = [] 12 | operator_table = [] 13 | punctuation_table = [] 14 | 15 | keywords=['true','false','int','bool','char','else','while','if'] 16 | operators=['=', '+', '-', '/', '*', '||', '&&', '++', '--', '==', '!=', '>', '<', '>=', '<=', '!', '+=', '-=', '*=', '/=', '%='] 17 | punctuations=['(', ')', '{', '}', ',', ';'] 18 | 19 | logical_priority = {} 20 | 21 | current_wp = 0 22 | last_available_reg = 0 23 | memory_capacity = 1023 24 | last_available_mem = memory_capacity + 0 25 | last_available_number = 0 26 | 27 | operation_stack = [] 28 | operand_stack = [] 29 | 30 | Registers_situation = [0, 0, 0, 0] 31 | 32 | 33 | def set_address_division(address_number): 34 | address_result = [] 35 | while address_number >= 255: 36 | address_result.append(255) 37 | address_number -= 255 38 | if address_result is not 0: 39 | address_result.append(address_number) 40 | return address_result 41 | 42 | 43 | def get_next_wp(): 44 | next_wp_result = [] 45 | global current_wp, Registers_situation, last_available_mem 46 | if current_wp == 60: 47 | for x in operand_stack: 48 | if x[0] is not 'identifier' and x[2] in [0, 1, 2, 3]: 49 | next_wp_result.append(move_immd_low(decimal_to_binary(3, 2), decimal_to_binary(dec_num=last_available_mem, low=True))) 50 | next_wp_result.append(move_immd_high(decimal_to_binary(3, 2), decimal_to_binary(dec_num=last_available_mem, high=True))) 51 | next_wp_result.append(store_address(decimal_to_binary(3, 2), decimal_to_binary(x[2], 2))) 52 | x[1] = 'mem' 53 | x[2] = last_available_mem 54 | last_available_mem -= 1 55 | Registers_situation = [0, 0, 0, 0] 56 | next_wp_result.append(clr_wp()) 57 | current_wp = 0 58 | else: 59 | for x in operand_stack: 60 | if x[0] is not 'identifier' and x[2] in [0, 1]: 61 | next_wp_result.append(move_immd_low(decimal_to_binary(3, 2), decimal_to_binary(dec_num=last_available_mem, low=True))) 62 | next_wp_result.append(move_immd_high(decimal_to_binary(3, 2), decimal_to_binary(dec_num=last_available_mem, high=True))) 63 | next_wp_result.append(store_address(decimal_to_binary(3, bin_len=2), decimal_to_binary(x[2], bin_len=2))) 64 | x[1] = 'mem' 65 | x[2] = last_available_mem 66 | last_available_mem -= 1 67 | if x[0] is not 'identifier' and x[2] in [2, 3]: 68 | x[2] -= 2 69 | Registers_situation = Registers_situation[2:]+[0, 0] 70 | next_wp_result.append(add_win_pointer(decimal_to_binary(2, 8))) 71 | current_wp += 2 72 | return next_wp_result 73 | 74 | 75 | def find_empty_register(number_of_desired_registers): 76 | if number_of_desired_registers == 1: 77 | for value in range(len(Registers_situation)): 78 | if Registers_situation[value] == 0: 79 | return [True, value] 80 | return [False] 81 | elif number_of_desired_registers == 2: 82 | result = [] 83 | for value in range(len(Registers_situation)): 84 | if Registers_situation[value] == 0: 85 | result += [value] 86 | if len(result) == 2: 87 | return [True] + result 88 | return [False] 89 | return [False] 90 | 91 | 92 | def calculate(relational_operation=False): 93 | global operand_stack 94 | calculate_res = [] 95 | first_var = operand_stack[-1] 96 | looping_var = [] 97 | reg_add = [0, 0] 98 | not_bool = False 99 | if operation_stack[-1] is not '!': 100 | second_var = operand_stack[-2] 101 | looping_var.append(second_var) 102 | else: 103 | not_bool = True 104 | looping_var.append(first_var) 105 | for tmp_var in looping_var: 106 | if tmp_var[0] == 'identifier': 107 | reg_res = find_empty_register(2) 108 | if not reg_res[0]: 109 | calculate_res += get_next_wp() 110 | if tmp_var == looping_var[1]: 111 | reg_add[0] -= 2 112 | reg_res = find_empty_register(2) 113 | if operation_stack[-1] is '!': 114 | reg_add[1] = reg_res[2] 115 | calculate_res.append(move_immd_low(decimal_to_binary(reg_res[2], 2), decimal_to_binary(dec_num=tmp_var[1][-1], low=True))) 116 | calculate_res.append(move_immd_high(decimal_to_binary(reg_res[2], 2), decimal_to_binary(dec_num=tmp_var[1][-1], high=True))) 117 | calculate_res.append(load_address(decimal_to_binary(reg_res[1], 2), decimal_to_binary(reg_res[2], 2))) 118 | if tmp_var == first_var: 119 | reg_add[0] = reg_res[1] 120 | else: 121 | reg_add[1] = reg_res[1] 122 | Registers_situation[reg_res[1]] = 1 123 | else: 124 | if tmp_var[1] == 'reg': 125 | if tmp_var == first_var: 126 | reg_add[0] = tmp_var[2] 127 | else: 128 | reg_add[1] = tmp_var[2] 129 | reg_res = find_empty_register(1) 130 | if not reg_res[0]: 131 | calculate_res += get_next_wp() 132 | if tmp_var == looping_var[1]: 133 | reg_add[0] -= 2 134 | reg_res = find_empty_register(1) 135 | if operation_stack[-1] is '!': 136 | reg_add[1] = reg_res[1] 137 | elif tmp_var[1] == 'mem': 138 | reg_res = find_empty_register(2) 139 | if not reg_res[0]: 140 | calculate_res += get_next_wp() 141 | if tmp_var == looping_var[1]: 142 | reg_add[0] -= 2 143 | reg_res = find_empty_register(2) 144 | if operation_stack[-1] is '!': 145 | reg_add[1] = reg_res[2] 146 | calculate_res.append(move_immd_low(decimal_to_binary(reg_res[2], 2), decimal_to_binary(dec_num=tmp_var[2], low=True))) 147 | calculate_res.append(move_immd_high(decimal_to_binary(reg_res[2], 2), decimal_to_binary(dec_num=tmp_var[2], high=True))) 148 | calculate_res.append(load_address(decimal_to_binary(reg_res[1], 2), decimal_to_binary(reg_res[1], 2))) 149 | if tmp_var == first_var: 150 | reg_add[0] = reg_res[1] 151 | else: 152 | reg_add[1] = reg_res[1] 153 | Registers_situation[reg_res[1]] = 1 154 | else: 155 | reg_res = find_empty_register(2) 156 | if not reg_res[0]: 157 | calculate_res += get_next_wp() 158 | if tmp_var == looping_var[1]: 159 | reg_add[0] -= 2 160 | reg_res = find_empty_register(2) 161 | if operation_stack[-1] is '!': 162 | reg_add[1] = reg_res[2] 163 | calculate_res.append(move_immd_low(decimal_to_binary(reg_res[1], 2), decimal_to_binary(dec_num=tmp_var[-1], low=True))) 164 | calculate_res.append(move_immd_high(decimal_to_binary(reg_res[1], 2), decimal_to_binary(dec_num=tmp_var[-1], high=True))) 165 | tmp_var[1] = 'reg' 166 | tmp_var[2] = reg_res[1] 167 | if tmp_var == first_var: 168 | reg_add[0] = reg_res[1] 169 | else: 170 | reg_add[1] = reg_res[1] 171 | Registers_situation[reg_res[1]] = 1 172 | operand_stack.pop() 173 | if operation_stack[-1] is not '!': 174 | operand_stack.pop() 175 | Registers_situation[min(reg_add)] = 1 176 | Registers_situation[max(reg_add)] = 0 177 | if relational_operation: 178 | operation = operation_stack.pop() 179 | calculate_res.append(clr_c()) 180 | calculate_res.append(clr_z()) 181 | if operation == '>': 182 | calculate_res.append(compare_registers(decimal_to_binary(reg_add[1], 2), decimal_to_binary(reg_add[0], 2))) 183 | calculate_res.append(branch_if_c(decimal_to_binary(dec_num=4,bin_len=8))) 184 | calculate_res.append(move_immd_low(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=0, low=True))) 185 | calculate_res.append(move_immd_high(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=0, high=True))) 186 | calculate_res.append(jump_relative(decimal_to_binary(dec_num=3,bin_len=8))) 187 | calculate_res.append(move_immd_low(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=1, low=True))) 188 | calculate_res.append(move_immd_high(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=1, high=True))) 189 | if operation == '>=': 190 | calculate_res.append(compare_registers(decimal_to_binary(reg_add[0], 2), decimal_to_binary(reg_add[1], 2))) 191 | calculate_res.append(branch_if_c(decimal_to_binary(dec_num=4, bin_len=8))) 192 | calculate_res.append(move_immd_low(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=1, low=True))) 193 | calculate_res.append(move_immd_high(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=1, high=True))) 194 | calculate_res.append(jump_relative(decimal_to_binary(dec_num=3, bin_len=8))) 195 | calculate_res.append(move_immd_low(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=0, low=True))) 196 | calculate_res.append(move_immd_high(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=0, high=True))) 197 | if operation == '<': 198 | calculate_res.append(compare_registers(decimal_to_binary(reg_add[0], 2), decimal_to_binary(reg_add[1], 2))) 199 | calculate_res.append(branch_if_c(decimal_to_binary(dec_num=4, bin_len=8))) 200 | calculate_res.append(move_immd_low(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=0, low=True))) 201 | calculate_res.append(move_immd_high(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=0, high=True))) 202 | calculate_res.append(jump_relative(decimal_to_binary(dec_num=3, bin_len=8))) 203 | calculate_res.append(move_immd_low(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=1, low=True))) 204 | calculate_res.append(move_immd_high(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=1, high=True))) 205 | if operation == '<=': 206 | calculate_res.append(compare_registers(decimal_to_binary(reg_add[1], 2), decimal_to_binary(reg_add[0], 2))) 207 | calculate_res.append(branch_if_c(decimal_to_binary(dec_num=4, bin_len=8))) 208 | calculate_res.append(move_immd_low(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=1, low=True))) 209 | calculate_res.append(move_immd_high(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=1, high=True))) 210 | calculate_res.append(jump_relative(decimal_to_binary(dec_num=3, bin_len=8))) 211 | calculate_res.append(move_immd_low(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=0, low=True))) 212 | calculate_res.append(move_immd_high(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=0, high=True))) 213 | if operation == '==': 214 | calculate_res.append(compare_registers(decimal_to_binary(reg_add[0], 2), decimal_to_binary(reg_add[1], 2))) 215 | calculate_res.append(branch_if_z(decimal_to_binary(dec_num=3, bin_len=8))) 216 | calculate_res.append(move_immd_low(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=0, low=True))) 217 | calculate_res.append(move_immd_high(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=0, high=True))) 218 | calculate_res.append(jump_relative(decimal_to_binary(dec_num=3, bin_len=8))) 219 | calculate_res.append(move_immd_low(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=1, low=True))) 220 | calculate_res.append(move_immd_high(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=1, high=True))) 221 | r = Registers_situation +[] 222 | calculate_res.append(r) 223 | if operation == '!=': 224 | calculate_res.append(compare_registers(decimal_to_binary(reg_add[0], 2), decimal_to_binary(reg_add[1], 2))) 225 | calculate_res.append(branch_if_z(decimal_to_binary(dec_num=3, bin_len=8))) 226 | calculate_res.append(move_immd_low(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=1, low=True))) 227 | calculate_res.append(move_immd_high(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=1, high=True))) 228 | calculate_res.append(jump_relative(decimal_to_binary(dec_num=3, bin_len=8))) 229 | calculate_res.append(move_immd_low(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=0, low=True))) 230 | calculate_res.append(move_immd_high(decimal_to_binary(min(reg_add), 2), decimal_to_binary(dec_num=0, high=True))) 231 | operand_stack.append(['not_identifier', 'reg', min(reg_add)]) 232 | 233 | else: 234 | operation = operation_stack.pop() 235 | if operation == '&&': 236 | calculate_res.append(and_registers(decimal_to_binary(dec_num=min(reg_add), bin_len=2), decimal_to_binary(dec_num=max(reg_add), bin_len=2))) 237 | if operation == '||': 238 | calculate_res.append(or_registers(decimal_to_binary(dec_num=min(reg_add), bin_len=2), decimal_to_binary(dec_num=max(reg_add), bin_len=2))) 239 | if operation == '!': 240 | calculate_res.append(not_register(decimal_to_binary(dec_num=reg_add[1], bin_len=2), decimal_to_binary(dec_num=reg_add[0], bin_len=2))) 241 | Registers_situation[min(reg_add)] = 0 242 | Registers_situation[max(reg_add)] = 0 243 | if operation in ['+','+=']: 244 | calculate_res.append(add_registers(decimal_to_binary(min(reg_add[0],reg_add[1]), 2),decimal_to_binary(max(reg_add[0],reg_add[1]), 2))) 245 | if operation in ['-','-=']: 246 | calculate_res.append(subtract_registers(decimal_to_binary(min(reg_add[0], reg_add[1]), 2), decimal_to_binary(max(reg_add[0], reg_add[1]), 2))) 247 | if operation in ['*','*=']: 248 | calculate_res.append(multiply_registers(decimal_to_binary(min(reg_add[0], reg_add[1]), 2), decimal_to_binary(max(reg_add[0], reg_add[1]), 2))) 249 | if operation in ['/','/=']: 250 | calculate_res.append(division_registers(decimal_to_binary(min(reg_add[0], reg_add[1]), 2), decimal_to_binary(max(reg_add[0], reg_add[1]), 2))) 251 | if not not_bool: 252 | operand_stack.append(['not_identifier', 'reg', min(reg_add[0], reg_add[1])]) 253 | else: 254 | operand_stack.append(['not_identifier', 'reg', reg_add[1]]) 255 | return calculate_res 256 | 257 | 258 | def registers_handle(): 259 | var = operand_stack[-2:] 260 | needed = 0 261 | for variable in var: 262 | if variable[0][0] == 'identifier' or (variable[0][1] is not 'reg'): 263 | needed += 1 264 | if needed == 0: 265 | return 266 | elif needed == 1: 267 | if find_empty_register(1)[0]: 268 | return 269 | return 270 | elif needed == 2: 271 | if find_empty_register(2)[0]: 272 | return 273 | return 274 | 275 | 276 | def register_file_memory(): 277 | return 0 278 | 279 | 280 | def find_var(searching_identifier): 281 | for variable in identifier_table: 282 | if variable[0] == searching_identifier: 283 | return variable 284 | 285 | 286 | def set_to_symbol_table(token): 287 | if token in keywords: 288 | x = -1 289 | for z in range(len(keyword_table)): 290 | if token == keyword_table[z]: 291 | x = z 292 | symbol_table.append(['keyword', z]) 293 | if x == -1: 294 | keyword_table.append(token) 295 | symbol_table.append(['keyword', len(keyword_table)-1]) 296 | elif token in operators: 297 | x = -1 298 | for z in range(len(operator_table)): 299 | if token == operator_table[z]: 300 | x = z 301 | symbol_table.append(['operator', z]) 302 | if x == -1: 303 | operator_table.append(token) 304 | symbol_table.append(['operator', len(operator_table) - 1]) 305 | elif token in punctuations: 306 | x = -1 307 | for z in range(len(punctuation_table)): 308 | if token == punctuation_table[z]: 309 | x = z 310 | symbol_table.append(['punctutation', z]) 311 | if x == -1: 312 | punctuation_table.append(token) 313 | symbol_table.append(['punctutation', len(punctuation_table) - 1]) 314 | else: 315 | try: 316 | int(token) 317 | number_table.append(['s'+str(len(number_table)),token,None,None]) 318 | symbol_table.append(['number',len(number_table) -1]) 319 | except: 320 | x = -1 321 | for z in range(len(identifier_table)): 322 | if token == identifier_table[z][0]: 323 | x = z 324 | symbol_table.append(['identifier', z]) 325 | if x == -1: 326 | identifier_table.append([token,None,None]) 327 | symbol_table.append(['identifier', len(identifier_table) - 1]) 328 | 329 | 330 | def find_end(tokens, start_token, char): 331 | token_enum = start_token + 0 332 | if char == ')': 333 | parentheses_count = 0 334 | finded = False 335 | while not finded: 336 | if tokens[token_enum] == '(': 337 | parentheses_count += 1 338 | elif tokens[token_enum] == ')': 339 | parentheses_count -= 1 340 | if parentheses_count == 0: 341 | return token_enum 342 | token_enum += 1 343 | else: 344 | if char == '}': 345 | brace_count = 0 346 | finded = False 347 | while not finded: 348 | if tokens[token_enum] == '{': 349 | brace_count += 1 350 | elif tokens[token_enum] == '}': 351 | brace_count -= 1 352 | if brace_count == 0: 353 | return token_enum 354 | token_enum += 1 355 | elif char == ';': 356 | finded = False 357 | while not finded and token_enum 0: 440 | expression_res += calculate() 441 | if current_state == 0 and tmp_state in [1, 3]: 442 | operation_stack.append(tmp_token) 443 | if current_state == 9 and tmp_token is not ')': 444 | operand_stack.append(['identifier', find_var(tmp_token)]) 445 | if token_enum == end -1 : 446 | while len(operation_stack) > 0 and operation_stack[-1] in rel_operations: 447 | expression_res += calculate(relational_operation=True) 448 | if current_state == 3 and tmp_token is not ')': 449 | operand_stack.append(['identifier', find_var(tmp_token)]) 450 | if current_state in [4, 7]: 451 | if tmp_token is not '(': 452 | while len(operation_stack) > 0 and operation_priority[tmp_token] <= operation_priority[operation_stack[-1]]: 453 | expression_res += calculate() 454 | operation_stack.append(tmp_token) 455 | if current_state in [2, 6]: 456 | if tmp_token == ')': 457 | while operation_stack[-1] is not '(': 458 | if operation_stack[-1] in rel_operations: 459 | expression_res += calculate(relational_operation=True) 460 | else: 461 | expression_res += calculate() 462 | operation_stack.pop() 463 | else: 464 | if isVariable(tmp_token): 465 | operand_stack.append(['identifier', find_var(tmp_token)]) 466 | else: 467 | operand_stack.append(['not_identifier', 'None', '', tmp_token]) 468 | if current_state == 5: 469 | while len(operation_stack) > 0 and operation_stack[-1] in main_operations: 470 | expression_res += calculate() 471 | operation_stack.append(tmp_token) 472 | if current_state == 0 and tmp_state == 0: 473 | operation_stack.append(tmp_token) 474 | if (current_state == 6 and token_enum == end-1) or (current_state == 0 and tmp_state == 6): 475 | while len(operation_stack) > 0 and operation_stack[-1] in main_operations: 476 | expression_res += calculate() 477 | while len(operation_stack) > 0 and operation_stack[-1] in rel_operations: 478 | expression_res += calculate(relational_operation=True) 479 | if current_state == 6 and token_enum == end-1: 480 | while len(operation_stack) > 0: 481 | expression_res += calculate() 482 | if current_state == 0 and tmp_state == 6: 483 | if len(operation_stack) > 0 and operation_stack[-1] in main_operations and operation_priority[tmp_token] < operation_priority[operation_stack[-1]]: 484 | while operation_priority[tmp_token] < operation_priority[operation_stack[-1]] and operation_stack[-1] in main_operations: 485 | expression_res += calculate() 486 | if len(operation_stack) > 0 and operation_stack[-1] in rel_operations: 487 | expression_res += calculate(relational_operation=True) 488 | operation_stack.append(tmp_token) 489 | token_enum += 1 490 | return expression_res 491 | 492 | 493 | def check_statement(tokens, start=0, end=0): 494 | statement_res = [] 495 | global last_available_mem , last_available_number, operand_stack 496 | current_state = 0 497 | token_enum = start 498 | token_end = end 499 | while token_enum < token_end: 500 | tmp_token = tokens[token_enum].strip() 501 | tmp_state = current_state 502 | current_state = statement_automata[current_state][token_statement_num(tmp_token)] 503 | if current_state in [2, 6, 9, 12]: 504 | last_var = tmp_token 505 | variable = find_var(tmp_token) 506 | # if variable is not defined set a memory address to its table 507 | if current_state is not 12: 508 | variable[2] = last_available_mem 509 | last_available_mem -= 1 510 | if current_state == 14 and tmp_state == 12: 511 | start_tmp = token_enum+1 512 | # handling character setting variable situation 513 | if current_state == 4: 514 | operand_stack.append(['not_identifier', 'None', '', ord(list(tmp_token)[1])]) 515 | statement_res += store_variable(last_var) 516 | 517 | # handling bool defined scope 518 | if current_state == 7: 519 | if tmp_state in [14, 15]: 520 | tmp_state = 7 521 | exp_end = find_end(tokens,start_tmp,';') 522 | statement_res += check_expression(tokens,start=start_tmp,end=exp_end-1) 523 | current_state = 0 524 | token_enum = exp_end-1 525 | else: 526 | exp_end = find_end(tokens, token_enum+1, ';') 527 | statement_res += check_expression(tokens, start=token_enum+1, end=exp_end-1) 528 | current_state = 0 529 | token_enum = exp_end-1 530 | statement_res += store_variable(last_var) 531 | 532 | # handling_if_scope 533 | if current_state == 0 and tmp_token == 'if': 534 | result, ass_res = check_if(tokens,start=token_enum+1) 535 | token_enum = result[0] 536 | statement_res += ass_res 537 | 538 | # handling_while_scope 539 | elif current_state == 0 and tmp_token == 'while': 540 | result, ass_res = check_while(tokens, start=token_enum+1) 541 | token_enum = result[0] 542 | statement_res += ass_res 543 | 544 | # operation computing 545 | if current_state == 15 and tmp_state == 14 and isVariable(tmp_token): 546 | not_known_var = tmp_token 547 | if tmp_state == 15 and current_state in [0, 10]: 548 | operand_stack.append(['identifier', find_var(not_known_var)]) 549 | statement_res += store_variable(last_var) 550 | if current_state == 10 and tmp_token is not '=': 551 | if tmp_state == 12: 552 | operand_stack.append(['identifier', find_var(last_var)]) 553 | if tmp_token is not '(': 554 | while len(operation_stack) > 0 and operation_priority[tmp_token] <= operation_priority[operation_stack[-1]]: 555 | statement_res += calculate() 556 | operation_stack.append(tmp_token) 557 | if current_state == 13: 558 | operand_stack.append(['identifier', find_var(last_var)]) 559 | operand_stack.append(['not_identifier', 'None', '', '1']) 560 | if tmp_token == '++': 561 | operation_stack.append('+') 562 | else: 563 | operation_stack.append('-') 564 | statement_res += calculate() 565 | statement_res += store_variable(last_var) 566 | if current_state == 11: 567 | if tmp_token == ')': 568 | while operation_stack[-1] is not '(': 569 | statement_res += calculate() 570 | operation_stack.pop() 571 | else: 572 | if isVariable(tmp_token): 573 | operand_stack.append(['identifier',find_var(tmp_token)]) 574 | else: 575 | operand_stack.append(['not_identifier', 'None', '', tmp_token]) 576 | if current_state == 0 and tmp_state == 11: 577 | while len(operation_stack) > 0: 578 | statement_res += calculate() 579 | statement_res += store_variable(last_var) 580 | token_enum += 1 581 | return statement_res 582 | 583 | 584 | def check_if(tokens, start): 585 | global Registers_situation, operand_stack 586 | if_res = [] 587 | token_enum = start + 0 588 | start_statement = find_end(tokens, token_enum, char=')') 589 | if_res += check_expression(tokens, start=token_enum + 1, end=start_statement) 590 | tmp_r = Registers_situation + [] 591 | tmp_operand_stack = operand_stack + [] 592 | operand_stack = [] 593 | if tokens[start_statement + 1] == '{': 594 | end_statement = find_end(tokens, start_statement + 1, char='}') 595 | if_res_tmp = check_statement(tokens=tokens, start=start_statement + 2, end=end_statement) 596 | else: 597 | end_statement = find_end(tokens, start_statement + 1, char=';') 598 | if_res_tmp = check_statement(tokens=tokens, start=start_statement + 1, end=end_statement) 599 | end_statement -= 1 600 | len_statements_if = len(if_res_tmp) 601 | Registers_situation = tmp_r 602 | if len(operand_stack) > 0: 603 | if operand_stack[-1][0] == 'identifier' and operand_stack[-1][1][1] is None: 604 | reg_res = find_empty_register(2) 605 | if not reg_res[0]: 606 | if_res += get_next_wp() 607 | reg_res = find_empty_register(2) 608 | tmp_var = operand_stack.pop()[1][-1] 609 | if_res.append(move_immd_low(decimal_to_binary(reg_res[2], 2), decimal_to_binary(dec_num=tmp_var, low=True))) 610 | if_res.append(move_immd_high(decimal_to_binary(reg_res[2], 2), decimal_to_binary(dec_num=tmp_var, high=True))) 611 | if_res.append(load_address(decimal_to_binary(dec_num=reg_res[1],bin_len=2), decimal_to_binary(dec_num=reg_res[2],bin_len=2))) 612 | Registers_situation[reg_res[1]] = 1 613 | elif operand_stack[-1][0] == 'not_identifier' and operand_stack[-1][1] is 'None': 614 | reg_res = find_empty_register(2) 615 | if not reg_res[0]: 616 | if_res += get_next_wp() 617 | reg_res = find_empty_register(2) 618 | tmp_var = operand_stack.pop()[-1] 619 | if_res.append(move_immd_low(decimal_to_binary(reg_res[1], 2), decimal_to_binary(dec_num=tmp_var, low=True))) 620 | if_res.append(move_immd_high(decimal_to_binary(reg_res[1], 2), decimal_to_binary(dec_num=tmp_var, high=True))) 621 | Registers_situation[reg_res[1]] = 1 622 | Registers_situation = tmp_r 623 | operand_stack = tmp_operand_stack 624 | if len(operand_stack) > 0: 625 | if operand_stack[-1][0] == 'identifier' and operand_stack[-1][1][1] is None: 626 | reg_res = find_empty_register(2) 627 | if not reg_res[0]: 628 | if_res += get_next_wp() 629 | reg_res = find_empty_register(2) 630 | tmp_var = operand_stack.pop()[1][-1] 631 | if_res.append(move_immd_low(decimal_to_binary(reg_res[2], 2), decimal_to_binary(dec_num=tmp_var, low=True))) 632 | if_res.append(move_immd_high(decimal_to_binary(reg_res[2], 2), decimal_to_binary(dec_num=tmp_var, high=True))) 633 | if_res.append(load_address(decimal_to_binary(dec_num=reg_res[1], bin_len=2),decimal_to_binary(dec_num=reg_res[2], bin_len=2))) 634 | Registers_situation[reg_res[1]] = 1 635 | elif operand_stack[-1][0] == 'not_identifier' and operand_stack[-1][1] is 'None': 636 | reg_res = find_empty_register(2) 637 | if not reg_res[0]: 638 | if_res += get_next_wp() 639 | reg_res = find_empty_register(2) 640 | tmp_var = operand_stack.pop()[-1] 641 | if_res.append(move_immd_low(decimal_to_binary(reg_res[1], 2), decimal_to_binary(dec_num=tmp_var, low=True))) 642 | if_res.append(move_immd_high(decimal_to_binary(reg_res[1], 2), decimal_to_binary(dec_num=tmp_var, high=True))) 643 | Registers_situation[reg_res[1]] = 1 644 | reg_res = find_empty_register(2) 645 | if not reg_res[0]: 646 | if_res += get_next_wp() 647 | reg_res = find_empty_register(2) 648 | if_res.append(move_immd_low(decimal_to_binary(reg_res[2], 2), decimal_to_binary(dec_num=memory_capacity, low=True))) 649 | if_res.append(move_immd_high(decimal_to_binary(reg_res[2], 2), decimal_to_binary(dec_num=memory_capacity, high=True))) 650 | if_res.append(load_address(decimal_to_binary(reg_res[1], 2), decimal_to_binary(reg_res[2], 2))) 651 | if_res.append(compare_registers(decimal_to_binary(reg_res[1] - 1, 2), decimal_to_binary(reg_res[1], 2))) 652 | if_res.append(branch_if_c(decimal_to_binary(dec_num=2, bin_len=8))) 653 | if_res.append(jump_relative(decimal_to_binary(dec_num=len_statements_if+1,bin_len=8))) 654 | if_res += if_res_tmp 655 | if len(tokens) > end_statement+1 and tokens[end_statement+1] == 'else': 656 | if tokens[end_statement + 2] == '{': 657 | end_statement_tmp = find_end(tokens, end_statement + 2, char='}') 658 | if_res += check_statement(tokens=tokens, start=end_statement + 3, end=end_statement_tmp) 659 | else: 660 | end_statement_tmp = find_end(tokens, end_statement + 2, char=';') 661 | if_res += check_statement(tokens=tokens, start=end_statement + 2, end=end_statement_tmp) 662 | return [end_statement_tmp, True], if_res 663 | else: 664 | return [end_statement, True], if_res 665 | 666 | 667 | def check_while(tokens, start): 668 | global Registers_situation, operand_stack 669 | while_res = [] 670 | token_enum = start + 0 671 | start_statement = find_end(tokens, token_enum, char=')') 672 | while_res_tmp_exp = check_expression(tokens, start=token_enum + 1, end=start_statement) 673 | while_res += while_res_tmp_exp 674 | tmp_r = Registers_situation + [] 675 | tmp_operand_stack = operand_stack + [] 676 | operand_stack = [] 677 | if tokens[start_statement+1] == '{': 678 | end_statement = find_end(tokens, start_statement + 1, char='}') 679 | while_res_tmp = check_statement(tokens=tokens, start=start_statement + 2, end=end_statement) 680 | else: 681 | end_statement = find_end(tokens, start_statement + 1, char=';') 682 | while_res_tmp = check_statement(tokens=tokens, start=start_statement + 1, end=end_statement) 683 | Registers_situation = tmp_r 684 | operand_stack = tmp_operand_stack 685 | if len(operand_stack) > 0: 686 | if operand_stack[-1][0] == 'identifier' and operand_stack[-1][1][1] is None: 687 | reg_res = find_empty_register(2) 688 | if not reg_res[0]: 689 | while_res += get_next_wp() 690 | reg_res = find_empty_register(2) 691 | tmp_var = operand_stack.pop()[1][-1] 692 | while_res.append(move_immd_low(decimal_to_binary(reg_res[2], 2), decimal_to_binary(dec_num=tmp_var, low=True))) 693 | while_res.append(move_immd_high(decimal_to_binary(reg_res[2], 2), decimal_to_binary(dec_num=tmp_var, high=True))) 694 | while_res.append(load_address(decimal_to_binary(dec_num=reg_res[1], bin_len=2),decimal_to_binary(dec_num=reg_res[2], bin_len=2))) 695 | Registers_situation[reg_res[1]] = 1 696 | elif operand_stack[-1][0] == 'not_identifier' and operand_stack[-1][1] is 'None': 697 | reg_res = find_empty_register(2) 698 | if not reg_res[0]: 699 | while_res += get_next_wp() 700 | reg_res = find_empty_register(2) 701 | tmp_var = operand_stack.pop()[-1] 702 | while_res.append(move_immd_low(decimal_to_binary(reg_res[1], 2), decimal_to_binary(dec_num=tmp_var, low=True))) 703 | while_res.append(move_immd_high(decimal_to_binary(reg_res[1], 2), decimal_to_binary(dec_num=tmp_var, high=True))) 704 | Registers_situation[reg_res[1]] = 1 705 | reg_res = find_empty_register(2) 706 | if not reg_res[0]: 707 | while_res += get_next_wp() 708 | reg_res = find_empty_register(2) 709 | len_statements_while = len(while_res_tmp) 710 | while_res.append(move_immd_low(decimal_to_binary(reg_res[2], 2), decimal_to_binary(dec_num=memory_capacity, low=True))) 711 | while_res.append(move_immd_high(decimal_to_binary(reg_res[2], 2), decimal_to_binary(dec_num=memory_capacity, high=True))) 712 | while_res.append(load_address(decimal_to_binary(reg_res[1], 2), decimal_to_binary(reg_res[2], 2))) 713 | while_res.append(compare_registers(decimal_to_binary(reg_res[1] - 1, 2), decimal_to_binary(reg_res[1], 2))) 714 | while_res.append(branch_if_c(decimal_to_binary(dec_num=2, bin_len=8))) 715 | while_res.append(jump_relative(decimal_to_binary(dec_num=len_statements_while + 1, bin_len=8))) 716 | while_res += while_res_tmp 717 | reg_res = find_empty_register(2) 718 | if not reg_res[0]: 719 | while_res += get_next_wp() 720 | reg_res = find_empty_register(2) 721 | address_moving = set_address_division(int((memory_capacity-len(while_res_tmp_exp)-len(while_res_tmp)))) 722 | while_res.append(save_pc(decimal_to_binary(reg_res[1], 2), decimal_to_binary(dec_num=address_moving[0], bin_len=8))) 723 | for i in address_moving[1:-1]: 724 | while_res.append(move_immd_low(decimal_to_binary(reg_res[2], 2), decimal_to_binary(dec_num=i, low=True))) 725 | while_res.append(move_immd_high(decimal_to_binary(reg_res[2], 2), decimal_to_binary(dec_num=i, high=True))) 726 | while_res.append(add_registers(decimal_to_binary(dec_num=reg_res[1],bin_len=2),decimal_to_binary(dec_num=reg_res[2],bin_len=2))) 727 | while_res.append(jump_address(decimal_to_binary(reg_res[1], 2), decimal_to_binary(dec_num=address_moving[-1],bin_len=8))) 728 | return [end_statement, True], while_res 729 | 730 | 731 | def generate_binary_code(tokens): 732 | resulted_assembly_code = [] 733 | global last_available_mem 734 | resulted_assembly_code.append(move_immd_low(decimal_to_binary(0, 2), decimal_to_binary(dec_num=1, low=True))) 735 | resulted_assembly_code.append(move_immd_high(decimal_to_binary(0, 2), decimal_to_binary(dec_num=1, high=True))) 736 | resulted_assembly_code.append(move_immd_low(decimal_to_binary(1, 2), decimal_to_binary(dec_num=last_available_mem, low=True))) 737 | resulted_assembly_code.append(move_immd_high(decimal_to_binary(1, 2), decimal_to_binary(dec_num=last_available_mem, high=True))) 738 | resulted_assembly_code.append(store_address(decimal_to_binary(dec_num=0, bin_len=2), decimal_to_binary(dec_num=1, bin_len=2))) 739 | last_available_mem -= 1 740 | for token in tokens: 741 | set_to_symbol_table(token) 742 | resulted_assembly_code += check_statement(tokens, end=len(tokens)) 743 | z = 0 744 | for i in resulted_assembly_code: 745 | print(z,end=' ') 746 | z += 1 747 | print(i) 748 | 749 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # compiler 2 | A Simple Compiler for C Language by Python 3 | #This report will be completed as soon as possible 4 | -------------------------------------------------------------------------------- /SayehInstruction.py: -------------------------------------------------------------------------------- 1 | def no_operation(): 2 | return '0000000000000000' 3 | 4 | 5 | def halt(): 6 | return '0000000100000000' 7 | 8 | 9 | def set_z(): 10 | return '0000001000000000' 11 | 12 | 13 | def clr_z(): 14 | return '0000001100000000' + 'clrz' 15 | 16 | 17 | def set_c(): 18 | return '0000010000000000' 19 | 20 | 21 | def clr_c(): 22 | return '0000010100000000' + 'clrc' 23 | 24 | 25 | def clr_wp(): 26 | return '0000011000000000' 27 | 28 | 29 | def move_register(Rd, Rs): 30 | return '0001'+str(Rd)+str(Rs)+'00000000' 31 | 32 | 33 | def load_address(Rd, Rs): 34 | return '0010'+str(Rd)+str(Rs)+'00000000' + 'load address' 35 | 36 | 37 | def store_address(Rd, Rs): 38 | return '0011'+str(Rd)+str(Rs)+'00000000' + 'store address' 39 | 40 | 41 | def and_registers(Rd, Rs): 42 | return '0110'+str(Rd)+str(Rs)+'00000000' + 'and' 43 | 44 | 45 | def or_registers(Rd, Rs): 46 | return '0111'+str(Rd)+str(Rs)+'00000000' + 'or' 47 | 48 | 49 | def not_register(Rd, Rs): 50 | return '1000'+str(Rd)+str(Rs)+'00000000' + 'not' 51 | 52 | 53 | def shift_left(Rd, Rs): 54 | return '1001'+str(Rd)+str(Rs)+'00000000' 55 | 56 | 57 | def shift_right(Rd, Rs): 58 | return '1010'+str(Rd)+str(Rs)+'00000000' 59 | 60 | 61 | def add_registers(Rd, Rs): 62 | return '1011'+str(Rd)+str(Rs)+'00000000' + 'add' 63 | 64 | 65 | def subtract_registers(Rd, Rs): 66 | return '1100'+str(Rd)+str(Rs)+'00000000' + 'sub' 67 | 68 | 69 | def multiply_registers(Rd, Rs): 70 | return '1101'+str(Rd)+str(Rs)+'00000000' + 'multiply' 71 | 72 | 73 | def division_registers(Rd, Rs): 74 | #It is not complete 75 | return '1101' + str(Rd) + str(Rs) + '00000000' 76 | 77 | 78 | def compare_registers(Rd, Rs): 79 | return '1110'+str(Rd)+str(Rs)+'00000000' + 'compare' 80 | 81 | 82 | def move_immd_low(Rd, I): 83 | return '1111'+str(Rd)+'00'+str(I) + 'mil' 84 | 85 | 86 | def move_immd_high(Rd, I): 87 | return '1111'+str(Rd)+'01'+str(I) + 'mih' 88 | 89 | 90 | def save_pc(Rd, I): 91 | return '1111'+str(Rd)+'10'+str(I) 92 | 93 | 94 | def jump_address(Rd, I): 95 | return '1111'+str(Rd)+'11'+str(I) + 'jpa' 96 | 97 | 98 | def jump_relative(I): 99 | return '00000111'+str(I) + 'jra' 100 | 101 | 102 | def branch_if_z(I): 103 | return '00001000'+str(I) + 'biz' 104 | 105 | 106 | def branch_if_c(I): 107 | return '00001001'+str(I) + 'bic' 108 | 109 | 110 | def add_win_pointer(I): 111 | return '00001010'+str(I) + 'add_win' -------------------------------------------------------------------------------- /Semantic.py: -------------------------------------------------------------------------------- 1 | from Token import * 2 | 3 | from StateMachine import * 4 | 5 | operation_priority = {'!': 0, '&&': -1, '||': -2, '(': -3, '+': 2, '+=': 2, '-': 2, '-=': 2, '*': 3, '*=': 3, '/': 3, '/=': 3} 6 | main_operations = ['+', '+=', '-', '-=', '*', '*=', '/', '/='] 7 | rel_operations = ['<', '<=', '==', '>', '>='] 8 | 9 | operation_stack = [] 10 | variable_stack = [] 11 | 12 | defined_var = dict() 13 | 14 | 15 | def get_value(var): 16 | try: 17 | res = int(var) 18 | return [[],res,False] 19 | except: 20 | return defined_var[var]+[True] 21 | 22 | 23 | def is_var(var): 24 | try: 25 | res = int(var) 26 | return False 27 | except: 28 | return True 29 | 30 | def calculate(): 31 | tmp_operation = operation_stack.pop() 32 | if tmp_operation in ['-', '-=']: 33 | first_num = get_value(variable_stack.pop()) 34 | second_num = get_value(variable_stack.pop()) 35 | if first_num is None or second_num is None : 36 | return ['Variable is Undefined',False] 37 | if first_num[2] == True : 38 | if first_num[0] == 'int': 39 | if second_num[2] == True : 40 | if second_num[0] == 'int': 41 | return [second_num[1] - first_num[1], True] 42 | return [second_num[:-1], False] 43 | return [second_num[1] - first_num[1], True] 44 | return [first_num[:-1],False] 45 | if second_num[2] == True: 46 | if second_num[0] == 'int': 47 | return [second_num[1] - first_num[1], True] 48 | return [second_num[:-1], False] 49 | return [second_num[1] - first_num[1], True] 50 | if tmp_operation in ['+', '+=']: 51 | first_num = get_value(variable_stack.pop()) 52 | second_num = get_value(variable_stack.pop()) 53 | if first_num is None or second_num is None : 54 | return ['Variable is Undefined',False] 55 | if first_num[2] == True: 56 | if first_num[0] == 'int': 57 | if second_num[2] == True: 58 | if second_num[0] == 'int': 59 | return [second_num[1] + first_num[1], True] 60 | return [second_num[:-1], False] 61 | return [second_num[1] + first_num[1], True] 62 | return [first_num[:-1], False] 63 | if second_num[2] == True: 64 | if second_num[0] == 'int': 65 | return [second_num[1] + first_num[1], True] 66 | return [second_num[:-1], False] 67 | return [second_num[1] + first_num[1], True] 68 | if tmp_operation in ['*', '*=']: 69 | first_num = get_value(variable_stack.pop()) 70 | second_num = get_value(variable_stack.pop()) 71 | if first_num is None or second_num is None : 72 | return ['Variable is Undefined',False] 73 | if first_num[2] == True: 74 | if first_num[0] == 'int': 75 | if second_num[2] == True: 76 | if second_num[0] == 'int': 77 | return [second_num[1] * first_num[1], True] 78 | return [second_num[:-1], False] 79 | return [second_num[1] * first_num[1], True] 80 | return [first_num[:-1], False] 81 | if second_num[2] == True: 82 | if second_num[0] == 'int': 83 | return [second_num[1] * first_num[1], True] 84 | return [second_num[:-1], False] 85 | return [second_num[1] * first_num[1], True] 86 | if tmp_operation in ['/', '/=']: 87 | first_num = get_value(variable_stack.pop()) 88 | second_num = get_value(variable_stack.pop()) 89 | if first_num is None or second_num is None : 90 | return ['Variable is Undefined',False] 91 | if first_num[2] == True: 92 | if first_num[0] == 'int': 93 | if second_num[2] == True: 94 | if second_num[0] == 'int': 95 | if first_num[1] == 0: 96 | return ['Division by Zero', False] 97 | return [second_num[1] / first_num[1], True] 98 | return [second_num[:-1], False] 99 | if first_num[1] == 0: 100 | return ['Division by Zero', False] 101 | return [second_num[1] / first_num[1], True] 102 | return [first_num[:-1], False] 103 | if second_num[2] == True: 104 | if second_num[0] == 'int': 105 | if first_num[1] == 0: 106 | return ['Division by Zero', False] 107 | return [second_num[1] / first_num[1], True] 108 | return [second_num[:-1], False] 109 | if first_num[1] == 0: 110 | return ['Division by Zero', False] 111 | return [second_num[1] / first_num[1], True] 112 | 113 | 114 | def find_end(tokens,start_token,char): 115 | token_enum = start_token + 0 116 | if char == ')': 117 | parentheses_count = 0 118 | finded = False 119 | while not finded: 120 | if tokens[token_enum] == '(': 121 | parentheses_count += 1 122 | elif tokens[token_enum] == ')': 123 | parentheses_count -= 1 124 | if parentheses_count == 0: 125 | return token_enum 126 | token_enum += 1 127 | return -1 128 | else: 129 | if char == '}': 130 | brace_count = 0 131 | finded = False 132 | while not finded: 133 | if tokens[token_enum] == '{': 134 | brace_count += 1 135 | elif tokens[token_enum] == '}': 136 | brace_count -= 1 137 | if brace_count == 0: 138 | return token_enum 139 | token_enum += 1 140 | return -1 141 | elif char == ';': 142 | finded = False 143 | while not finded and token_enum= 0: 154 | tmp_token = tokens[token_enum].strip() 155 | tmp_state = current_state + 0 156 | current_state = expression_automata[current_state][token_expression_num(tokens[token_enum].strip())] 157 | if current_state == 3: 158 | if tmp_token in defined_var: 159 | variable_stack.append(tmp_token) 160 | else: 161 | return ['Variable is not Defined',False,token_enum] 162 | if current_state == 4 or current_state == 7: 163 | if len(operation_stack) > 0 and operation_priority[operation_stack[-1]] > operation_priority[tmp_token] and tmp_token is not '(': 164 | while len(operation_stack) > 0 and operation_priority[operation_stack[-1]] > operation_priority[tmp_token] and tmp_token is not '(': 165 | cal_res = calculate() 166 | if cal_res[1]: 167 | variable_stack.append(cal_res[0]) 168 | else: 169 | return cal_res+[token_enum] 170 | elif len(operation_stack) > 0 and tmp_token != '(' and operation_stack[-1] != '(' and operation_priority[ 171 | operation_stack[-1]] == operation_priority[tmp_token]: 172 | cal_res = calculate() 173 | if cal_res[1]: 174 | variable_stack.append(cal_res[0]) 175 | else: 176 | return cal_res+[token_enum] 177 | operation_stack.append(tmp_token) 178 | if current_state == 2 or current_state == 6: 179 | if tmp_token == ')': 180 | while operation_stack[-1] is not '(': 181 | cal_res = calculate() 182 | if cal_res[1]: 183 | variable_stack.append(cal_res[0]) 184 | else: 185 | return cal_res+[token_enum] 186 | operation_stack.pop() 187 | else: 188 | variable_stack.append(tmp_token) 189 | if (tmp_state == 2 and current_state == 5) or (tmp_state == 6 and current_state == 0) or token_enum == end-1: 190 | while len(operation_stack) > 0: 191 | cal_res = calculate() 192 | if cal_res[1]: 193 | variable_stack.append(cal_res[0]) 194 | else: 195 | return cal_res+[token_enum] 196 | token_enum += 1 197 | return [current_state,True,token_enum] 198 | 199 | 200 | def sem_check(tokens,start=0,end=0): 201 | return check_statement(tokens=tokens,start=start,end=end) 202 | 203 | 204 | def check_statement(tokens,start=0,end=0): 205 | current_state = 0 206 | token_enum = start 207 | token_end = end 208 | while token_enum < token_end: 209 | tmp_token = tokens[token_enum].strip() 210 | tmp_state = current_state + 0 211 | current_state = statement_automata[current_state][token_statement_num(tmp_token)] 212 | if current_state == 13: 213 | if defined_var[last_variable][0] != 'int': 214 | return ['Variable Type is not Int',False,token_enum] 215 | if tmp_token=='++': 216 | defined_var[last_variable][1] += 1 217 | else: 218 | defined_var[last_variable][1] -= 1 219 | if current_state == 12: 220 | if tmp_token in defined_var: 221 | last_variable = tmp_token 222 | else: 223 | return ['Variable is not Defined',False,token_enum] 224 | if current_state == 9: 225 | if tmp_token not in defined_var: 226 | defined_var[tmp_token] = ['int',None] 227 | last_variable = tmp_token 228 | else: 229 | return ['Variable is Defined Before',False,token_enum] 230 | elif current_state == 2 : 231 | if tmp_token not in defined_var: 232 | defined_var[tmp_token] = ['char', None] 233 | last_variable = tmp_token 234 | else: 235 | return ['Variable is Defined Before',False, token_enum] 236 | elif current_state == 6: 237 | if tmp_token not in defined_var: 238 | defined_var[tmp_token] = ['bool', None] 239 | last_variable = tmp_token 240 | else: 241 | return ['Variable is Defined Before',False, token_enum] 242 | if current_state == 10 and tmp_token is not '=': 243 | if tmp_token in ['+=', '-=', '*=', '/=']: 244 | variable_stack.append(last_variable) 245 | if len(operation_stack) > 0 and operation_priority[operation_stack[-1]] > operation_priority[tmp_token] and tmp_token is not '(': 246 | while len(operation_stack) > 0 and operation_priority[operation_stack[-1]] > operation_priority[tmp_token] and tmp_token is not '(': 247 | cal_res = calculate() 248 | if cal_res[1]: 249 | variable_stack.append(cal_res[0]) 250 | else: 251 | return cal_res + [token_enum] 252 | elif len(operation_stack)>0 and tmp_token!='(' and operation_stack[-1]!='(' and operation_priority[operation_stack[-1]] == operation_priority[tmp_token]: 253 | cal_res = calculate() 254 | if cal_res[1]: 255 | variable_stack.append(cal_res[0]) 256 | else: 257 | return cal_res + [token_enum] 258 | operation_stack.append(tmp_token) 259 | if current_state == 11: 260 | if tmp_token == ')': 261 | while operation_stack[-1] is not '(': 262 | cal_res = calculate() 263 | if cal_res[1]: 264 | variable_stack.append(cal_res[0]) 265 | else: 266 | return cal_res + [token_enum] 267 | operation_stack.pop() 268 | else: 269 | variable_stack.append(tmp_token) 270 | if tmp_state == 11 and current_state == 0: 271 | while len(operation_stack)> 0: 272 | cal_res = calculate() 273 | if cal_res[1]: 274 | variable_stack.append(cal_res[0]) 275 | else: 276 | return cal_res + [token_enum] 277 | defined_var[last_variable][1] = int(variable_stack.pop()) 278 | print(defined_var[last_variable]) 279 | if current_state == 0 and tmp_token == 'if': 280 | result = check_if(tokens,start=token_enum+1) 281 | if result[1]: 282 | token_enum = result[0] + 1 283 | else: 284 | return result 285 | elif current_state == 0 and tmp_token == 'while': 286 | result = check_while(tokens,start=token_enum+1) 287 | if result[1]: 288 | token_enum = result[0] + 1 289 | else: 290 | return result 291 | token_enum += 1 292 | return [current_state , True , token_enum] 293 | 294 | 295 | def check_if(tokens,start): 296 | token_enum = start + 0 297 | if tokens[token_enum] == '(': 298 | start_statement = find_end(tokens, token_enum, char=')') 299 | result = check_expression(tokens, start=token_enum + 1, end=start_statement) 300 | if not result[1]: 301 | return result 302 | else: 303 | if tokens[start_statement + 1] == '{': 304 | end_statement = find_end(tokens, start_statement + 1, char='}') 305 | statement_bool = check_statement(tokens=tokens, start=start_statement + 2, end=end_statement) 306 | if not statement_bool[1]: 307 | return [statement_bool[0] , False , statement_bool[2]] 308 | else: 309 | end_statement = find_end(tokens, start_statement + 1, char=';') 310 | statement_bool = check_statement(tokens=tokens, start=start_statement + 1, end=end_statement) 311 | if not statement_bool[1]: 312 | return [statement_bool[0] , False , statement_bool[2]] 313 | if len(tokens) > end_statement+1 and tokens[end_statement+1] == 'else': 314 | if tokens[end_statement + 2] == '{': 315 | end_statement_tmp = find_end(tokens, end_statement + 2, char='}') 316 | statement_bool = check_statement(tokens=tokens, start=end_statement + 3, end=end_statement_tmp) 317 | if not statement_bool[1]: 318 | return [statement_bool[0] , False , statement_bool[2]] 319 | else: 320 | end_statement_tmp = find_end(tokens, end_statement + 2, char=';') 321 | statement_bool = check_statement(tokens=tokens, start=end_statement + 2, end=end_statement_tmp) 322 | if not statement_bool[1]: 323 | return [statement_bool[0] , False , statement_bool[2]] 324 | return [end_statement_tmp , True] 325 | else: 326 | return [end_statement , True] 327 | return -1 328 | 329 | 330 | def check_while(tokens,start): 331 | token_enum = start + 0 332 | if tokens[token_enum] == '(': 333 | start_statement = find_end(tokens, token_enum, char=')') 334 | result = check_expression(tokens, start=token_enum + 1, end=start_statement) 335 | if not result[1]: 336 | return result 337 | else: 338 | if tokens[start_statement+1] == '{': 339 | end_statement = find_end(tokens, start_statement + 1, char='}') 340 | result = check_statement(tokens=tokens, start=start_statement + 2, end=end_statement) 341 | if not result[1]: 342 | return result 343 | else: 344 | end_statement = find_end(tokens, start_statement + 1, char=';') 345 | result = check_statement(tokens=tokens, start=start_statement + 1, end=end_statement) 346 | if not result[1]: 347 | return result 348 | return [end_statement , True] 349 | return -1 -------------------------------------------------------------------------------- /StateMachine.py: -------------------------------------------------------------------------------- 1 | expression_automata = [ 2 | [0,-1,-1,-1,0,-1,1,10,3,2,-1],#0 3 | [-2,0,-2,-2,-2,1,-2,-2,-2,-2,-2], 4 | [-3,-3,5,4,-3,2,-3,-3,-3,-3,-3], 5 | [-4,0,8,4,-4,3,-4,-4,-4,-4,-4], 6 | [-5,-5,-5,-5,4,-5,-5,-5,2,2,-5], 7 | [5,-6,-6,-6,5,-6,-6,-6,6,6,-6],#5 8 | [-7,0,-7,7,-7,6,-7,-7,-7,-7,-7], 9 | [-8,-8,-8,-8,7,-8,-8,-8,6,6,-8], 10 | [8,-9,-9,-9,8,-9,13,12,9,6,-9], 11 | [-10,0,-10,7,-10,9,-10,-10,-10,-10,-10], 12 | [-11,-11,11,-11,-11,10,-11,-11,-11,-11,-11],#10 13 | [-12,-12,-12,-12,11,-12,-12,-12,12,12,-12], 14 | [-13,0,-13,-13,-13,12,-13,-13,-13,-13,-13] 15 | ] 16 | 17 | 18 | statement_automata = [ 19 | [-1, -1, -1, 5, 8, 1, 0, -1, -1, 0, 0, -1, -1, 12, -1, -1, -1, -1, -1], # 0 20 | [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, -1, -1, -1, -1, -1], 21 | [-2, -2, -2, -2, -2, -2, 0, 1, 3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2], 22 | [-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, 3, -3, 4, 4, -3, -3, -3, -3], 23 | [-4, -4, -4, -4, -4, -4, 0, -4, -4, -4, -4, -4, 4, 6, -4, -4, -4, -4, -4], 24 | [-5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, 6, -5, -5, -5, -5, -5], # 5 25 | [-6, -6, -6, -6, -6, -6, 0, 5, 7, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6], 26 | [-7, -7, -7, -7, -7, -7, 0, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7], 27 | [-8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, 9, -8, -8, -8, -8, -8], 28 | [-9, -9, -9, -9, -9, -9, 0, 8, 10, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9], 29 | [-10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, 10, -10, 11, -10, 11, -10, -10, -10], # 10 30 | [-11, 10, -11, -11, -11, -11, 0, -11, -11, -11, -11, -11, 11, -11, -11, -11, -11, -11, -11], 31 | [10, -12, 13, -12, -12, -12, -12, -12, 14, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12], 32 | [-13, -13, -13, -13, -13, -13, 0, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13], 33 | [-14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, 14, -14, 15, 4, 15, -14, -14, 7], 34 | [10, 10, 13, -15, -15, -15, 0, -15, -15, -15, -15, -15, 15, -15, -15, -15, -15, 7, -15] # 15 35 | ] -------------------------------------------------------------------------------- /Syntax.py: -------------------------------------------------------------------------------- 1 | # automata = [ 2 | # [-1,-2,-3,-4,-24,16,10,26,0,-5,-6,1,1,2,-7,-13,-10,14,-38,-11,-8],#0 3 | # [-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,2,-9,-9,-9,-39,-9,-9], 4 | # [-12,-12,-12,-12,-12,-12,-12,-12,-12,-12,-12,-12,-12,-12,2,-12,3,3,-40,4,-12], 5 | # [-13,2,6,5,-13,-13,-13,-13,-13,-13,-13,-13,-13,-13,2,0,-13,-13,-41,-13,-13], 6 | # [-14,-14,6,5,-14,-14,-14,-14,-14,-14,-14,-14,-14,-14,-14,-14,-14,-14,-42,-14,-14], 7 | # [-15,-15,-15,-15,-15,-15,-15,-15,-15,-15,-15,-15,-15,-15,-15,-15,-15,4,-43,4,-15],#5 8 | # [-16,-16,-16,-16,-16,-16,-16,-16,-16,-16,-16,-16,-16,-16,6,-16,7,7,-44,8,-16], 9 | # [-17,2,-17,9,-17,-17,-17,-17,-17,-17,-17,-17,-17,-17,-17,0,-17,-17,-45,-17,-17], 10 | # [-18,2,-18,9,-18,-18,-18,-18,-18,-18,-18,-18,-18,-18,-18,0,-18,-18,-46,-18,-18], 11 | # [-19,-19,-19,-19,-19,-19,-19,-19,-19,-19,-19,-19,-19,-19,9,-19,-19,8,-47,8,-19], 12 | # [-20,-20,-20,-20,-20,-20,-20,-20,-20,-20,-20,-20,-20,-20,-20,-20,-20,11,-48,-20,-20],#10 13 | # [-21,-21,-21,-21,-21,-21,-21,-21,0,10,12,-21,-21,-21,-21,-21,-21,-21,-49,-21,-21], 14 | # [-22,-22,-22,-22,-22,-22,-22,-22,-22,-22,-22,-22,-22,12,12,-22,-22,13,-50,13,-22], 15 | # [-23,-23,-23,12,-23,-23,-23,-23,0,-23,-23,-23,-23,-23,-23,13,-23,-23,-51,-23,-23], 16 | # [-24,-24,-24,-24,15,-24,-24,-24,-24,-24,12,-24,-24,-24,-24,-24,-24,-24,-52,-24,-24], 17 | # [-25,-25,-25,-25,-25,-25,-25,-25,0,-25,-25,-25,-25,-25,-25,-25,-25,-25,-53,-25,-25],#15 18 | # [-26,-26,-26,-26,-26,-26,-26,-26,-26,-26,-26,-26,-26,-26,-26,-26,-26,17,-54,-26,-26], 19 | # [-27,-27,-27,-27,-27,-27,-27,-27,0,16,18,-27,-27,-27,-27,-27,-27,-27,-55,-27,-27], 20 | # [-28,-28,-28,-28,-28,-28,-28,-28,-28,-28,-28,-28,-28,-28,18,-28,-28,19,-56,20,-28], 21 | # [-29,-29,22,21,-29,-29,-29,-29,0,-29,-29,-29,-29,-29,-29,-29,-29,-29,-57,-29,-29], 22 | # [-30,-30,22,21,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,20,-30,-30,-58,-30,-30],#20 23 | # [-31,-31,-31,-31,-31,-31,-31,-31,-31,-31,-31,-31,-31,-31,21,-31,-31,20,-59,20,-31], 24 | # [-32,-32,-32,-32,-32,-32,-32,-32,-32,-32,-32,-32,-32,-32,22,-32,-32,23,-60,24,-32], 25 | # [-33,18,-33,25,-33,-33,-33,-33,0,-33,-33,-33,-33,-33,-33,23,-33,-33,-61,-33,-33], 26 | # [-34,18,-34,25,-34,-34,-34,-34,0,-34,-34,-34,-34,-34,-34,24,-34,-34,-62,-34,-34], 27 | # [-67,-67,-67,-67,-67,-67,-67,-67,-67,-67,-67,-67,-67,-67,25,-67,-67,24,-67,24,-67],#25 28 | # [-35,-35,-35,-35,-35,-35,-35,-35,-35,-35,-35,-35,-35,-35,-35,-35,-35,27,-63,-35,-35], 29 | # [-36,-36,-36,-36,-36,-36,-36,-36,-36,26,29,-36,-36,-36,-36,-36,-36,-36,-64,-36,-36], 30 | # [-37,-37,-37,-37,-37,-37,-37,-37,0,-37,-37,-37,-37,-37,-37,-37,-37,-37,-65,-37,-37], 31 | # [-66,-66,-66,-66,-66,-66,-66,-66,-66,-66,-66,-66,-66,-66,-66,-66,-66,-66,28,-66,-66] 32 | # # [p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p], 33 | # ] 34 | 35 | from StateMachine import * 36 | from Token import token_expression_num , token_statement_num 37 | 38 | tokens_lines = [] 39 | 40 | 41 | def get_line(token_lines,token_number): 42 | for index,line_num in enumerate(token_lines): 43 | if line_num > token_number: 44 | final_result = index+1 45 | break 46 | return final_result 47 | 48 | 49 | def find_end(tokens,start_token,char): 50 | token_enum = start_token + 0 51 | if char == ')': 52 | parentheses_count = 0 53 | finded = False 54 | while not finded: 55 | if tokens[token_enum] == '(': 56 | parentheses_count += 1 57 | elif tokens[token_enum] == ')': 58 | parentheses_count -= 1 59 | if parentheses_count == 0: 60 | return token_enum 61 | token_enum += 1 62 | return -1 63 | else: 64 | if char == '}': 65 | brace_count = 0 66 | finded = False 67 | while not finded: 68 | if tokens[token_enum] == '{': 69 | brace_count += 1 70 | elif tokens[token_enum] == '}': 71 | brace_count -= 1 72 | if brace_count == 0: 73 | return token_enum 74 | token_enum += 1 75 | return -1 76 | elif char == ';': 77 | finded = False 78 | while not finded and token_enum= 0: 89 | current_state = expression_automata[current_state][token_expression_num(tokens[token_enum].strip())] 90 | if current_state >= 0 : 91 | token_enum += 1 92 | if current_state < 0 : 93 | return [current_state,False,token_enum] 94 | return [current_state,True,token_enum] 95 | 96 | 97 | def check_statement(tokens,start=0,end=0): 98 | current_state = 0 99 | token_enum = start 100 | token_end = end 101 | while token_enum < token_end and current_state >= 0: 102 | tmp_token = tokens[token_enum].strip() 103 | tmp_state = current_state 104 | current_state = statement_automata[current_state][token_statement_num(tmp_token)] 105 | if current_state == 14 and tmp_state == 12: 106 | start_tmp = token_enum+1 107 | if current_state == 7: 108 | if tmp_state == 15: 109 | exp_end = find_end(tokens,start_tmp,';') 110 | if exp_end == -1: 111 | return [-1000, False, start_tmp+1] 112 | result = check_expression(tokens,start=start_tmp,end=exp_end-1) 113 | if not result[1]: 114 | return result 115 | current_state = 0 116 | token_enum = exp_end-1 117 | else: 118 | exp_end = find_end(tokens, token_enum+1, ';') 119 | if exp_end == -1: 120 | return [-1000, False, token_enum + 1] 121 | result = check_expression(tokens, start=token_enum+1, end=exp_end-1) 122 | if not result[1]: 123 | return result 124 | current_state = 0 125 | token_enum = exp_end-1 126 | if current_state == 0 and tmp_token == 'if': 127 | result = check_if(tokens,start=token_enum+1) 128 | if result[1]: 129 | token_enum = result[0] + 1 130 | else: 131 | return [result[0], False , result[2]] 132 | elif current_state == 0 and tmp_token == 'while': 133 | result = check_while(tokens,start=token_enum+1) 134 | if result[1]: 135 | token_enum = result[0] + 1 136 | else: 137 | return result 138 | elif current_state >= 0: 139 | token_enum += 1 140 | if current_state != 0: 141 | return [current_state,False , token_enum] 142 | return [current_state , True , token_enum] 143 | 144 | 145 | def check_if(tokens,start): 146 | token_enum = start + 0 147 | if tokens[token_enum] == '(': 148 | start_statement = find_end(tokens, token_enum, char=')') 149 | result = check_expression(tokens, start=token_enum + 1, end=start_statement) 150 | if not result[1]: 151 | return result 152 | else: 153 | if tokens[start_statement + 1] == '{': 154 | end_statement = find_end(tokens, start_statement + 1, char='}') 155 | statement_bool = check_statement(tokens=tokens, start=start_statement + 2, end=end_statement) 156 | if not statement_bool[1]: 157 | return [statement_bool[0] , False , statement_bool[2]] 158 | else: 159 | end_statement = find_end(tokens, start_statement + 1, char=';') 160 | if end_statement == -1 : 161 | return [-1000, False , start_statement+1] 162 | statement_bool = check_statement(tokens=tokens, start=start_statement + 1, end=end_statement) 163 | if not statement_bool[1]: 164 | return [statement_bool[0] , False , statement_bool[2]] 165 | if len(tokens) > end_statement+1 and tokens[end_statement+1] == 'else': 166 | if tokens[end_statement + 2] == '{': 167 | end_statement_tmp = find_end(tokens, end_statement + 2, char='}') 168 | statement_bool = check_statement(tokens=tokens, start=end_statement + 3, end=end_statement_tmp) 169 | if not statement_bool[1]: 170 | return [statement_bool[0] , False , statement_bool[2]] 171 | else: 172 | end_statement_tmp = find_end(tokens, end_statement + 2, char=';') 173 | if end_statement_tmp == -1: 174 | return [start_statement + 1, False, -1000] 175 | statement_bool = check_statement(tokens=tokens, start=end_statement + 2, end=end_statement_tmp) 176 | if not statement_bool[1]: 177 | return [statement_bool[0] , False , statement_bool[2]] 178 | return [end_statement_tmp , True] 179 | else: 180 | return [end_statement , True] 181 | return [-2000, False, token_enum] 182 | 183 | 184 | def check_while(tokens,start): 185 | token_enum = start + 0 186 | if tokens[token_enum] == '(': 187 | start_statement = find_end(tokens, token_enum, char=')') 188 | result = check_expression(tokens, start=token_enum + 1, end=start_statement) 189 | if not result[1]: 190 | return result 191 | else: 192 | if tokens[start_statement+1] == '{': 193 | end_statement = find_end(tokens, start_statement + 1, char='}') 194 | result = check_statement(tokens=tokens, start=start_statement + 2, end=end_statement) 195 | if not result[1]: 196 | return [result[0],False , result[2]] 197 | else: 198 | end_statement = find_end(tokens, start_statement + 1, char=';') 199 | if end_statement == -1: 200 | return [-1000, False, start+1] 201 | result = check_statement(tokens=tokens, start=start_statement + 1, end=end_statement) 202 | if not result[1]: 203 | return [result[0], False , result[2]] 204 | return [end_statement , True] 205 | return [-2000, False, token_enum] -------------------------------------------------------------------------------- /Token.py: -------------------------------------------------------------------------------- 1 | assignment_operators = ['=', '+=', '-=', '*=', '/=', '%='] 2 | logical_operators = ['&&', '||', '!'] 3 | relational_operators = ['==', '!=', '>', '<', '>=', '<='] 4 | arithmetic_operators = ['+', '-', '*', '/', '++', '--'] 5 | boolean = ['true', 'false'] 6 | 7 | 8 | def isVariable(token): 9 | result = token[0].isalpha() 10 | for i in token[1:]: 11 | result = result and (i.isalpha() or i.isdigit()) 12 | return result 13 | 14 | 15 | def isCharacter(token): 16 | if len(token) == 3 and token[0] == '\'' and token[2] == '\'' and token[1].isalpha(): 17 | return True 18 | 19 | 20 | def token_statement_num(token): 21 | if token in relational_operators+logical_operators: 22 | return 17 23 | if token in boolean: 24 | return 18 25 | elif token in assignment_operators[1:]: 26 | return 0 27 | elif token in arithmetic_operators[:-2]: 28 | return 1 29 | elif token in arithmetic_operators[-2:]: 30 | return 2 31 | elif token == 'bool': 32 | return 3 33 | elif token == 'int': 34 | return 4 35 | elif token == 'char': 36 | return 5 37 | elif token == ';': 38 | return 6 39 | elif token == ',': 40 | return 7 41 | elif token == '=': 42 | return 8 43 | elif token == 'if': 44 | return 9 45 | elif token == 'while': 46 | return 10 47 | elif token == '(': 48 | return 11 49 | elif token == ')': 50 | return 12 51 | elif isVariable(token): 52 | return 13 53 | elif isCharacter(token): 54 | return 14 55 | else: 56 | try: 57 | int(token) 58 | return 15 59 | except: 60 | return 16 61 | 62 | def token_expression_num(token): 63 | if token == '!': 64 | return 0 65 | elif token in logical_operators[:-1]: 66 | return 1 67 | elif token in relational_operators: 68 | return 2 69 | elif token in arithmetic_operators[:-2]: 70 | return 3 71 | elif token == '(': 72 | return 4 73 | elif token == ')': 74 | return 5 75 | elif token in boolean: 76 | return 6 77 | elif isCharacter(token): 78 | return 7 79 | elif isVariable(token): 80 | return 8 81 | else: 82 | try: 83 | int(token) 84 | return 9 85 | except: 86 | return 10 -------------------------------------------------------------------------------- /check.py: -------------------------------------------------------------------------------- 1 | def parenthes_brace_Check(file_input): 2 | # It checks whether the number of open and close parentheses or braces are equal or not 3 | line_number = 1 4 | comma_counter = 0 5 | brace_counter = 0 6 | tmp_token = '' 7 | for line in file_input: 8 | for token in line.replace('\t',' ').split(' '): 9 | if token.strip() == '(': 10 | comma_counter +=1 11 | elif token.strip() == ')': 12 | comma_counter -= 1 13 | if comma_counter<0: 14 | return False,str(line_number),str(tmp_token),'parenthese' 15 | elif token.strip() == '{': 16 | brace_counter += 1 17 | elif token.strip() == '}': 18 | brace_counter -= 1 19 | if brace_counter <0: 20 | return False,str(line_number),str(tmp_token),'brace' 21 | tmp_token = token 22 | line_number += 1 23 | if comma_counter > 0: 24 | return False,'0','1','parenthes' 25 | elif brace_counter > 0: 26 | return False,'0','1','brace' 27 | return True,[],[],[] -------------------------------------------------------------------------------- /compiler.py: -------------------------------------------------------------------------------- 1 | from check import * 2 | 3 | from Syntax import * 4 | 5 | from Semantic import sem_check 6 | 7 | from Generator import generate_binary_code 8 | 9 | tokens_lines = [] 10 | 11 | with open('test.txt','r') as file: 12 | parenthes_brace_bool, parenthes_brace_Res, parenthes_brace_Token, problem_type = parenthes_brace_Check(file) 13 | if parenthes_brace_bool: 14 | with open('test.txt','r') as tmp_file: 15 | tokens = [] 16 | line_num = 0 17 | for line in tmp_file: 18 | for token in line.replace('\t', ' ').replace('\n', '').split(' '): 19 | if token.strip() != '': 20 | tokens.append(token.strip()) 21 | line_num += 1 22 | tokens_lines.append(line_num) 23 | tokens_number = len(tokens) 24 | result = check_statement(tokens,end=tokens_number) 25 | if result[1]: 26 | print('Successful') 27 | print(sem_check(tokens,start=0,end=tokens_number)) 28 | generate_binary_code(tokens) 29 | else: 30 | print('Line '+ str(get_line(tokens_lines,result[2])) +' : Error ') 31 | else: 32 | if parenthes_brace_Res == '0': 33 | if problem_type == 'parenthes': 34 | print('Error : Parentheses hasn\'t been closed somewhere') 35 | else: 36 | print('Error : Brace hasn\'t been closed somewhere') 37 | else: 38 | if problem_type == 'parenthes': 39 | print('Line '+parenthes_brace_Res+' - Error : Parentheses Not Expected ' + parenthes_brace_Res + ' after \"' + parenthes_brace_Token +'\"') 40 | else: 41 | print('Line '+parenthes_brace_Res+' - Error : Brace Not Expected ' + parenthes_brace_Res + ' after \"' + parenthes_brace_Token +'\"') -------------------------------------------------------------------------------- /test.txt: -------------------------------------------------------------------------------- 1 | int a = 0 ; 2 | if ( 2 > 3 ) 3 | a ++ ; --------------------------------------------------------------------------------