Skip to content
Snippets Groups Projects
Commit 2c293489 authored by tra0115's avatar tra0115
Browse files

Upload New File

parent d2b7d223
Branches main
No related merge requests found
lab2.py 0 → 100644
def analyzer(line):
dict = {
"OP": ["+","-","*"],
"DIV": "div",
"MOD": "mod",
"LPAR": "(",
"RPAR": ")",
"SEMICOLON":";",
}
for i in line:
if i == "//":
break
elif i.isnumeric():
print("NUM:"+i)
elif i not in ["+","-","*","div","mod","(",")",";"] and isinstance(i,str):
print("ID: "+i)
else:
for key,val in dict.items():
if i in val:
if i in ["div","mod","(",")",";"]:
print(key)
else:
print(key+":"+i)
def valid(line):
st = line.replace("(","( ")
st = st.replace(")"," ) ")
st = st.replace("-","- ")
check = st.split(" ")
check = [item for item in check if item != '']
return check
file = open('file.txt', 'r')
Lines = file.readlines()
for l in Lines:
line = valid(l)
analyzer(line)
file.close()
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment