From 2c293489331ee2c2846cffdc4e72c83bc01f1a89 Mon Sep 17 00:00:00 2001 From: tra0115 <dac.phu.cuong.tran.st@vsb.cz> Date: Wed, 6 Mar 2024 12:58:07 +0000 Subject: [PATCH] Upload New File --- lab2.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 lab2.py diff --git a/lab2.py b/lab2.py new file mode 100644 index 0000000..a21abaa --- /dev/null +++ b/lab2.py @@ -0,0 +1,41 @@ +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 -- GitLab