Skip to content

Commit

Permalink
debug. final version
Browse files Browse the repository at this point in the history
  • Loading branch information
shahabhm committed May 5, 2022
1 parent 4a3f2dc commit 3c9be94
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions parser_compiler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# from fcntl import F_SEAL_SEAL
from scanner import Scanner
from anytree import Node, RenderTree

Expand Down Expand Up @@ -789,20 +788,20 @@
class Parser:

def start(self):
root = self.parse_program()
root, has_syntax_error = self.parse_program()
parse_tree_output = open("parse_tree.txt", "w", encoding="utf-8")
buffer = ''
for pre, fill, node in RenderTree(root):
buffer = buffer + "{:s}{:s}".format(pre, node.name) + "\n"
parse_tree_output.write(buffer)
parse_tree_output.close()
if not has_syntax_error:
self.syntax_error_output.write("There is no syntax error.")
self.syntax_error_output.close()

# syntax_error_output = open("syntax_errors.txt", "w", encoding="utf-8")
# syntax_error_output.write("There is no syntax error.")
# syntax_error_output.close()

def parse_program(self):
syntax_error_output = open("syntax_errors.txt", "w", encoding="utf-8")
self.syntax_error_output = open("syntax_errors.txt", "w", encoding="utf-8")
has_syntax_error = False
terminals = ["break", "continue", "def", "else", "if", "return", "while", "global", "[", "]", "(", ")",
"ID", "=", ";", ",", ":", "==", "<", "+", "-", "*", "**", "NUM", "$"]
Expand All @@ -823,7 +822,7 @@ def parse_program(self):
while True:
if (len(stack) == 0):
Node(name="$", parent=root)
return root
return root, has_syntax_error
current_node = stack.pop()
current_sentential = current_node.name
if current_sentential in terminals:
Expand All @@ -832,23 +831,27 @@ def parse_program(self):
break
else:
has_syntax_error = True
syntax_error_output.write("#{} : syntax error, missing {}\n".format(line_number, current_sentential))
self.syntax_error_output.write("#{} : syntax error, missing {}\n".format(line_number, current_sentential))
current_node.parent = None
continue
else:
next_tokens = parsing_table[current_sentential][effective_token].split(" ")
if "" in next_tokens:
if effective_token == "$":
has_syntax_error = True
stack.append(current_node)
syntax_error_output.write("#{} : syntax error, unexpected EOF\n".format(line_number))
return root
self.syntax_error_output.write("#{} : syntax error, Unexpected EOF\n".format(line_number))
for remaining_node in stack:
remaining_node.parent = None
return root, has_syntax_error
has_syntax_error = True
stack.append(current_node)
syntax_error_output.write("#{} : syntax error, illegal {}\n".format(line_number, effective_token))
self.syntax_error_output.write("#{} : syntax error, illegal {}\n".format(line_number, effective_token))
break
elif "synch" in next_tokens:
has_syntax_error = True
syntax_error_output.write("#{} : syntax error, missing {}\n".format(line_number, current_sentential))
self.syntax_error_output.write("#{} : syntax error, missing {}\n".format(line_number, current_sentential))
current_node.parent = None
continue
new_nodes_list = []
for name in next_tokens:
Expand All @@ -857,9 +860,3 @@ def parse_program(self):
for node in reversed(new_nodes_list):
if node.name != "epsilon":
stack.append(node)
if not has_syntax_error:
syntax_error_output.write("There is no syntax error.")
syntax_error_output.close()



0 comments on commit 3c9be94

Please sign in to comment.