Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Alfredo Juarez
- # Homework 2
- # CS351
- # 09/20/19
- import tkinter
- from tkinter import *
- from time import strftime
- import re
- numLines = 0
- count = 1
- window = Tk()
- window.geometry("650x350")
- window.resizable(True, True)
- window.title("HW2: Lexical Analyzer for TinyPie")
- # Count the number of lines being entered in the input
- def countEntries():
- number_Entries = inputinTxtBox.get("1.0",
- "end").splitlines() # I got help from classmates on this splitlines(), but also googled it how to use it.
- totalLines = len(number_Entries)
- return totalLines
- # Prints out the input into the output lines
- def getLines():
- number_Entries = countEntries()
- global count
- if (count <= number_Entries):
- inputLine = inputinTxtBox.get(str(count) + ".0", str(count) + ".end")
- outInTextBx.insert(END, inputLine)
- outInTextBx.insert(END, "\n")
- count += 1
- CurrentLine.config(text=count - 1)
- else:
- outInTextBx.insert('end', "No more lines to process: Exit" + "\n")
- InputLabel = Label(window, text="Source Code Input: ", fg="dark green")
- inputinTxtBox = Text(window, borderwidth=2, relief=RAISED)
- OutputLabel = Label(window, text="Lexical Analyzed Result: ", fg="red")
- outInTextBx = Text(window, borderwidth=2, relief=RAISED)
- CurrentLineLabel = Label(window, text="Current Processing Line: ", fg="dark green")
- CurrentLine = Label(window, text="0", fg="dark green")
- NextLineButton = Button(window, text='Next Line', command=getLines, fg="dark green")
- QuitButton = Button(window, text='Exit', command=window.destroy, fg="red")
- InputLabel.place(x=10, y=10)
- inputinTxtBox.place(x=10, y=30, width=310, height=210)
- OutputLabel.place(x=350, y=10)
- outInTextBx.place(x=330, y=30, width=310, height=210)
- CurrentLineLabel.place(x=20, y=280)
- CurrentLine.place(x=150, y=280)
- NextLineButton.place(x=245, y=300)
- QuitButton.place(x=590, y=300)
- window.mainloop() # Alfredo Juarez
- # Homework 2
- # CS351
- # 09/20/19
- import tkinter
- from tkinter import *
- from time import strftime
- import re
- numLines = 0
- count = 1
- window = Tk()
- window.geometry("650x350")
- window.resizable(True, True)
- window.title("HW2: Lexical Analyzer for TinyPie")
- # Count the number of lines being entered in the input
- def countEntries():
- number_Entries = inputinTxtBox.get("1.0", "end").splitlines()
- # I got help from classmates on this splitlines(), but also googled it how to use it.
- totalLines = len(number_Entries)
- return totalLines
- # Prints out the input into the output lines
- def getLines():
- number_Entries = countEntries()
- global count
- if (count <= number_Entries):
- inputLine = inputinTxtBox.get(str(count) + ".0", str(count) + ".end")
- parsedInputLine = get_keyword_list(inputLine)
- outInTextBx.insert(END, parsedInputLine)
- outInTextBx.insert(END, "\n")
- count += 1
- CurrentLine.config(text=count - 1)
- else:
- outInTextBx.insert('end', "No more lines to process: Exit" + "\n")
- def get_keyword_list(self, string):
- string = string.replace("\n", "")
- keywordList = []
- # Find Keywords
- tokens = re.findall(r'(if|else|int)', string)
- for token in tokens:
- keywordList.append("<Keyword, " + token + " >")
- # Find Operators
- tokens = re.findall(r'[=\+>]', string)
- for token in tokens:
- keywordList.append("<Operator, " + token + " >")
- # Find Separators
- tokens = re.findall(r'(\(|\)|:|")', string)
- for token in tokens:
- keywordList.append("<Separators, " + token + " >")
- # Find Identifiers
- tokens = re.findall(r'[a-zA-Z]+\d*', string)
- for token in tokens:
- if token != 'int' and token != 'if' and token:
- keywordList.append("<Identifiers, " + token + " >")
- outputString = "".join(keywordList)
- return outputString
- InputLabel = Label(window, text="Source Code Input: ", fg="dark green")
- inputinTxtBox = Text(window, borderwidth=2, relief=RAISED)
- OutputLabel = Label(window, text="Lexical Analyzed Result: ", fg="red")
- outInTextBx = Text(window, borderwidth=2, relief=RAISED)
- CurrentLineLabel = Label(window, text="Current Processing Line: ", fg="dark green")
- CurrentLine = Label(window, text="0", fg="dark green")
- NextLineButton = Button(window, text='Next Line', command=getLines, fg="dark green")
- QuitButton = Button(window, text='Exit', command=window.destroy, fg="red")
- InputLabel.place(x=10, y=10)
- inputinTxtBox.place(x=10, y=30, width=310, height=210)
- OutputLabel.place(x=350, y=10)
- outInTextBx.place(x=330, y=30, width=310, height=210)
- CurrentLineLabel.place(x=20, y=280)
- CurrentLine.place(x=150, y=280)
- NextLineButton.place(x=245, y=300)
- QuitButton.place(x=590, y=300)
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement