This is my work so far. As messy as it looks, it works nicely. But I can't work out how to identify the keywords. I assume I should use regex? I need some help with this.
in_file = open("capp.cpp","r")
out_file = open("out.txt","w")
out_file.write("\n")
keywords = ["auto","double","int","long",\
"break","else","switch","char"\
"case","enum","register","typedef",\
"extern","return","union","const",\
"float","short","unsigned","continue",\
"for","signed","void","default","goto",\
"sizeof","volatile","do","if","static","while"]
alpha = ['a','b','c','d','e','f','g','h',\
'i','j','k','l','m','n','o','p',\
'q','r','s','t','u','v','w','x','y','z']
line = in_file.readline()
is_mcomment = 0
is_string = 0
is_comment = 0
while (line):
c = 0
is_scomment = 0
is_prepro = 0
is_number = 0
while c < len(line):
### MULTI LINE COMMENTS ###
if line[c] == "/" and line[c+1] == "*":
if is_prepro:
out_file.write("")
is_prepro = 0
out_file.write("")
is_mcomment = 1
is_comment = 1
elif line[c] == "/" and line[c+1] == "/":
if is_prepro:
out_file.write("")
is_prepro = 0
out_file.write("")
is_scomment = 1
is_comment = 1
if line[c] == "#" and not is_comment:
out_file.write("")
is_prepro = 1
if line[c] == "\"" and not is_comment:
if not is_string:
out_file.write("")
is_string = 1
else:
is_string = 2
if line[c].isdigit() and not is_prepro and not is_string and not is_comment:
if not line[c-1] in alpha and not line[c+1] in alpha:
out_file.write("")
is_number = 1
out_file.write(line[c])
### ENDING STRINGS, DIGITS AND MULTI LINE COMMENTS ###
if line[c] == "*" and line[c+1] == "/":
c = c + 1 # No idea why this needs to be done
out_file.write("")
is_mcomment = 0
if is_mcomment == 0 and is_scomment == 0:
is_comment == 0
if is_string == 2:
out_file.write("")
is_string = 0
if is_number == 1:
out_file.write("")
is_number = 0
c = c + 1
### ENDING SINGLE LINE COMMENTS AND PREPROCESSOR ###
if is_scomment:
out_file.write("")
is_scomment = 0
if is_mcomment == 0 and is_scomment == 0:
is_comment == 0
if is_prepro:
out_file.write("")
is_prepro = 0
line = in_file.readline()
out_file.write("\n")
out_file.close()
in_file.close()