Answered step by step
Verified Expert Solution
Question
1 Approved Answer
from tkinter import * import re class MyFirstGUI: def __init__(self, master): self.master = master master.title(Lexical Analyzer for TinyPie) self.counter = 1 self.label1 = Label(master, text=Lexical
from tkinter import * import re class MyFirstGUI: def __init__(self, master): self.master = master master.title("Lexical Analyzer for TinyPie") self.counter = 1 self.label1 = Label(master, text="Lexical Analyzer for TinyPie", bg="skyblue", fg="white").grid(row=0, column=2, padx=5, pady=5) self.label2 = Label(master, text="Source Code Input:").grid(row=1, column=1, padx=5) self.label3 = Label(master, text="Lexical Analyzed Result:").grid(row=1, column=3, padx=5) self.label4 = Label(master, text="Current Processing Line: ").grid(row=3, column=1, padx=5, pady=5) self.label5 = Label(master, text=self.counter).grid(row=3, column=2, padx=5, pady=5) self.text = Text(master, width=20, height=10, selectborderwidth=10, wrap=WORD) self.text.grid(row=2, column=1, padx=5, pady=5) self.text_2 = Text(master, width=20, height=10, selectborderwidth=10, wrap=WORD) self.text_2.grid(row=2, column=3, padx=5, pady=5, sticky=W) self.nextLine = Button(master, bg="green", fg="white", width=9, text="Next Line",command=self.getline).grid(row=4, column=1, padx=5, pady=5, sticky=E) self.quit = Button(master, bg="green", fg="white", width=9, text="Quit", command=master.destroy).grid(row=4, column=3, padx=5, pady=5, sticky=E) def getline(self): tmp = self.counter tmp += .0 tmp2 = tmp + .99 code = self.text.get(str(tmp), str(tmp2)) self.text_2.insert(END, code+" ") self.cutOneLineTokens(code) self.updateCounter() def updateCounter(self): self.counter += 1 self.label5 = Label(text=self.counter).grid(row=3, column=2, padx=5, pady=5) def cutOneLineTokens(self, st): print(st) keywords = re.findall('\b(int)\b|\b(if)\b|\b(else)\b', st) operators = re.findall('(\+)|(\=)|(\>)', st) separators = re.findall('(\()|(\))|(\:)|(\)|(\)', st) identifiers = re.findall('(?=[A-Za-z])[^(int)^(else)^if]\w+', st) t = re.findall('/\b(int)\b|\b(if)\b|\b(else)\b/', "int A1=5") print("TEST ", t) root = Tk() my_gui = MyFirstGUI(root) root.mainloop()
OUTPUT:
TEST []
My question is why does python not capture "int" when reading:
t = re.findall('/\b(int)\b|\b(if)\b|\b(else)\b/', "int A1=5")
print("TEST ", t)
I used a regex checker and it captures it there but not in python.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started