From a83b10a4906239cb85f961ef531981b004f4841f Mon Sep 17 00:00:00 2001 From: Pranshul Date: Thu, 16 Jul 2020 14:05:32 +0530 Subject: [PATCH 1/2] Added functionality to check if identifier name is a keyword in C --- lexical_analyzer.py | 40 +++++++++++++++++++++++++++++++++++++--- test.simc | 13 +------------ 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/lexical_analyzer.py b/lexical_analyzer.py index 7758112..421af27 100644 --- a/lexical_analyzer.py +++ b/lexical_analyzer.py @@ -73,7 +73,7 @@ def numeric_val(source_code, i, table, line_num): if numeric_constant.count(".") > 1: error( "Invalid numeric constant, cannot have more than one decimal point in a" - " number!" + " number!" , line_num ) # Check the length after . to distinguish between float and double @@ -119,7 +119,7 @@ def string_val(source_code, i, table, line_num): # Loop until we get a non-digit character while source_code[i] != '"': if source_code[i] == "\0": - error("Unterminated string!") + error("Unterminated string!", line_num) string_constant += source_code[i] i += 1 @@ -174,6 +174,40 @@ def keyword_identifier(source_code, i, table, line_num): # Check if identifier is in symbol table id = table.get_by_symbol(value) + C_keywords = ['break', + 'else', + 'long', + 'switch', + 'case', + 'enum', + 'register', + 'typedef', + 'char', + 'extern', + 'return', + 'union', + 'const', + 'float', + 'short', + 'unsigned', + 'continue', + 'for', + 'signed', + 'void', + 'default', + 'goto', + 'sizeof', + 'volatile', + 'do', + 'if', + 'static', + 'while' + ] + + #Check if identifier is a keyword in class + if value in C_keywords: + error("A keyword cannot be an identifier", line_num) + # If identifier is not in symbol table then give a placeholder datatype var if id == -1: id = table.entry(value, "var", "variable") @@ -199,7 +233,7 @@ def lexical_analyze(filename, table): # Check if file extension is .simc or not if "." not in filename or filename.split(".")[-1] != "simc": - error("Incorrect file extension") + error("Incorrect file extension", line_num) # Read the entire source code as a string source_code = open(filename, "r").read() diff --git a/test.simc b/test.simc index ad0f6a7..0050f34 100644 --- a/test.simc +++ b/test.simc @@ -1,16 +1,5 @@ MAIN var a = 1 var b = 2 - - switch(a + b) { - case 1: - print("Hello") - break - case 2: - print("World") - break - default: - print("Default") - break - } + var char = 1 END_MAIN From ac350279fb37602691786b047ff5220db11308e0 Mon Sep 17 00:00:00 2001 From: frankhart2018 Date: Thu, 16 Jul 2020 17:23:27 +0530 Subject: [PATCH 2/2] Fix: add id name in keyword error --- lexical_analyzer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lexical_analyzer.py b/lexical_analyzer.py index 421af27..f79bd33 100644 --- a/lexical_analyzer.py +++ b/lexical_analyzer.py @@ -206,7 +206,7 @@ def keyword_identifier(source_code, i, table, line_num): #Check if identifier is a keyword in class if value in C_keywords: - error("A keyword cannot be an identifier", line_num) + error("A keyword cannot be an identifier - %s" % value, line_num) # If identifier is not in symbol table then give a placeholder datatype var if id == -1: