Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. # Alfredo Juarez
  2. # Homework 2
  3. # CS351
  4. # 09/20/19
  5.  
  6.  
  7. import tkinter
  8. from tkinter import *
  9. from time import strftime
  10. import re
  11.  
  12. numLines = 0
  13. count = 1
  14. window = Tk()
  15. window.geometry("650x350")
  16. window.resizable(True, True)
  17. window.title("HW2: Lexical Analyzer for TinyPie")
  18.  
  19.  
  20. # Count the number of lines being entered in the input
  21. def countEntries():
  22. number_Entries = inputinTxtBox.get("1.0",
  23. "end").splitlines() # I got help from classmates on this splitlines(), but also googled it how to use it.
  24. totalLines = len(number_Entries)
  25. return totalLines
  26.  
  27.  
  28. # Prints out the input into the output lines
  29. def getLines():
  30. number_Entries = countEntries()
  31. global count
  32. if (count <= number_Entries):
  33. inputLine = inputinTxtBox.get(str(count) + ".0", str(count) + ".end")
  34. outInTextBx.insert(END, inputLine)
  35. outInTextBx.insert(END, "\n")
  36. count += 1
  37. CurrentLine.config(text=count - 1)
  38. else:
  39. outInTextBx.insert('end', "No more lines to process: Exit" + "\n")
  40.  
  41.  
  42. InputLabel = Label(window, text="Source Code Input: ", fg="dark green")
  43. inputinTxtBox = Text(window, borderwidth=2, relief=RAISED)
  44. OutputLabel = Label(window, text="Lexical Analyzed Result: ", fg="red")
  45. outInTextBx = Text(window, borderwidth=2, relief=RAISED)
  46. CurrentLineLabel = Label(window, text="Current Processing Line: ", fg="dark green")
  47. CurrentLine = Label(window, text="0", fg="dark green")
  48. NextLineButton = Button(window, text='Next Line', command=getLines, fg="dark green")
  49. QuitButton = Button(window, text='Exit', command=window.destroy, fg="red")
  50.  
  51. InputLabel.place(x=10, y=10)
  52. inputinTxtBox.place(x=10, y=30, width=310, height=210)
  53. OutputLabel.place(x=350, y=10)
  54. outInTextBx.place(x=330, y=30, width=310, height=210)
  55. CurrentLineLabel.place(x=20, y=280)
  56. CurrentLine.place(x=150, y=280)
  57. NextLineButton.place(x=245, y=300)
  58. QuitButton.place(x=590, y=300)
  59.  
  60. window.mainloop() # Alfredo Juarez
  61. # Homework 2
  62. # CS351
  63. # 09/20/19
  64.  
  65.  
  66. import tkinter
  67. from tkinter import *
  68. from time import strftime
  69. import re
  70.  
  71. numLines = 0
  72. count = 1
  73. window = Tk()
  74. window.geometry("650x350")
  75. window.resizable(True, True)
  76. window.title("HW2: Lexical Analyzer for TinyPie")
  77.  
  78.  
  79. # Count the number of lines being entered in the input
  80. def countEntries():
  81. number_Entries = inputinTxtBox.get("1.0", "end").splitlines()
  82. # I got help from classmates on this splitlines(), but also googled it how to use it.
  83. totalLines = len(number_Entries)
  84. return totalLines
  85.  
  86.  
  87. # Prints out the input into the output lines
  88. def getLines():
  89. number_Entries = countEntries()
  90. global count
  91. if (count <= number_Entries):
  92. inputLine = inputinTxtBox.get(str(count) + ".0", str(count) + ".end")
  93. parsedInputLine = get_keyword_list(inputLine)
  94. outInTextBx.insert(END, parsedInputLine)
  95. outInTextBx.insert(END, "\n")
  96. count += 1
  97. CurrentLine.config(text=count - 1)
  98. else:
  99. outInTextBx.insert('end', "No more lines to process: Exit" + "\n")
  100.  
  101. def get_keyword_list(self, string):
  102. string = string.replace("\n", "")
  103. keywordList = []
  104.  
  105. # Find Keywords
  106. tokens = re.findall(r'(if|else|int)', string)
  107. for token in tokens:
  108. keywordList.append("<Keyword, " + token + " >")
  109. # Find Operators
  110. tokens = re.findall(r'[=\+>]', string)
  111. for token in tokens:
  112. keywordList.append("<Operator, " + token + " >")
  113. # Find Separators
  114. tokens = re.findall(r'(\(|\)|:|")', string)
  115. for token in tokens:
  116. keywordList.append("<Separators, " + token + " >")
  117. # Find Identifiers
  118. tokens = re.findall(r'[a-zA-Z]+\d*', string)
  119. for token in tokens:
  120. if token != 'int' and token != 'if' and token:
  121. keywordList.append("<Identifiers, " + token + " >")
  122.  
  123. outputString = "".join(keywordList)
  124.  
  125. return outputString
  126.  
  127. InputLabel = Label(window, text="Source Code Input: ", fg="dark green")
  128. inputinTxtBox = Text(window, borderwidth=2, relief=RAISED)
  129. OutputLabel = Label(window, text="Lexical Analyzed Result: ", fg="red")
  130. outInTextBx = Text(window, borderwidth=2, relief=RAISED)
  131. CurrentLineLabel = Label(window, text="Current Processing Line: ", fg="dark green")
  132. CurrentLine = Label(window, text="0", fg="dark green")
  133. NextLineButton = Button(window, text='Next Line', command=getLines, fg="dark green")
  134. QuitButton = Button(window, text='Exit', command=window.destroy, fg="red")
  135.  
  136. InputLabel.place(x=10, y=10)
  137. inputinTxtBox.place(x=10, y=30, width=310, height=210)
  138. OutputLabel.place(x=350, y=10)
  139. outInTextBx.place(x=330, y=30, width=310, height=210)
  140. CurrentLineLabel.place(x=20, y=280)
  141. CurrentLine.place(x=150, y=280)
  142. NextLineButton.place(x=245, y=300)
  143. QuitButton.place(x=590, y=300)
  144.  
  145. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement