From 80455fcaefeb52fcf9ff1f655e123b4004b23552 Mon Sep 17 00:00:00 2001
From: Nicholas Robinson <nwrobins@edu.uwaterloo.ca>
Date: Sun, 9 Feb 2020 01:55:59 -0500
Subject: [PATCH] fixed parsing & rm parsetests dir

- fixed some error handling in parsing
- needed to reset global vars after each test
- added some more logic for testing Parsing in Test.py to match Scanning
---
 ParseTests/add2num  | 13 -------------
 ParseTests/comments |  6 ------
 ParseTests/empty    |  0
 ParseTests/literals | 13 -------------
 ParseTests/test     |  3 ---
 Parsing.py          | 16 +++++++++++-----
 Test.py             | 17 ++++++++++++++++-
 7 files changed, 27 insertions(+), 41 deletions(-)
 delete mode 100644 ParseTests/add2num
 delete mode 100644 ParseTests/comments
 delete mode 100644 ParseTests/empty
 delete mode 100644 ParseTests/literals
 delete mode 100644 ParseTests/test

diff --git a/ParseTests/add2num b/ParseTests/add2num
deleted file mode 100644
index a8251d3..0000000
--- a/ParseTests/add2num
+++ /dev/null
@@ -1,13 +0,0 @@
-PUBLIC public CLASS class ID AddTwoIntegers LBRACK { 
-PUBLIC public STATIC static VOID void ID main LPAREN ( ID String LSQRBRACK [ RSQRBRACK ] ID args RPAREN ) LBRACK { 
-
-INT int ID one ASSIGN = NUM 10 SEMICO ; 
-INT int ID two ASSIGN = NUM 20 SEMICO ; 
-INT int ID he234slk ASSIGN = NUM 30 SEMICO ; 
-INT int ID i COMMA , ID j SEMICO ; 
-INT int ID sum ASSIGN = ID one ADD + ID two SEMICO ; 
-IF if LPAREN ( ID two GT > NUM 45 RPAREN ) LBRACK { 
-RETURN return ID two SEMICO ; 
-RBRACK } 
-RBRACK } 
-RBRACK } 
diff --git a/ParseTests/comments b/ParseTests/comments
deleted file mode 100644
index b225c81..0000000
--- a/ParseTests/comments
+++ /dev/null
@@ -1,6 +0,0 @@
-PUBLIC public CLASS class ID Comments LBRACK { 
-PUBLIC public STATIC static VOID void ID main LPAREN ( ID String LSQRBRACK [ RSQRBRACK ] ID args RPAREN ) LBRACK { 
-INT int ID four ASSIGN = NUM 4 SEMICO ; 
-
-RBRACK } 
-RBRACK } 
diff --git a/ParseTests/empty b/ParseTests/empty
deleted file mode 100644
index e69de29..0000000
diff --git a/ParseTests/literals b/ParseTests/literals
deleted file mode 100644
index fad30d5..0000000
--- a/ParseTests/literals
+++ /dev/null
@@ -1,13 +0,0 @@
-PUBLIC public CLASS class ID Literals LBRACK { 
-PUBLIC public STATIC static VOID void ID main LPAREN ( ID String LSQRBRACK [ RSQRBRACK ] ID args RPAREN ) LBRACK { 
-CHAR char ID i ASSIGN = LITERALCHAR 'h' SEMICO ; 
-CHAR char ID x ASSIGN = LITERALCHAR '\'' SEMICO ; 
-ID String ID s ASSIGN = LITERALSTRING "hello" SEMICO ; 
-ID String ID s2 ASSIGN = LITERALSTRING "hello \' and \"" SEMICO ; 
-BOOLEAN boolean ID b ASSIGN = LITERALBOOL true SEMICO ; 
-BOOLEAN boolean ID a ASSIGN = LITERALBOOL false SEMICO ; 
-INT int ID x ASSIGN = NULL null SEMICO ; 
-BOOLEAN boolean ID pp ASSIGN = LITERALBOOL true EQUAL == LITERALBOOL false SEMICO ; 
-BOOLEAN boolean ID fd ASSIGN = NUM 100 LE <= NUM 1000 SEMICO ; 
-RBRACK } 
-RBRACK } 
diff --git a/ParseTests/test b/ParseTests/test
deleted file mode 100644
index 4a40241..0000000
--- a/ParseTests/test
+++ /dev/null
@@ -1,3 +0,0 @@
-CLASS class ID A LBRACK {
-INT int ID x ASSIGN = NUM 0 SEMICO ;
-RBRACK }
\ No newline at end of file
diff --git a/Parsing.py b/Parsing.py
index 06b5d7b..a284122 100644
--- a/Parsing.py
+++ b/Parsing.py
@@ -50,7 +50,7 @@ class State():
                 break
 
         if not ruleToUse:
-            raise ValueError('ERROR: No rule found') 
+            raise ValueError('No rule found') 
 
         # what type of rule is it? shift or reduce?
         if (ruleToUse[2] == 'shift'):
@@ -77,7 +77,7 @@ class State():
             theStack.append(newNode)
             return False
         else:
-            raise ValueError('ERROR: rule neither shift nor reduce.') 
+            raise ValueError('rule neither shift nor reduce.')
 
     def output(self):
         print('accepting: ['),
@@ -148,14 +148,20 @@ def checkSequence(tokens):
         if theRule != '':
             rulesToOutput.append(' '.join(theRule))
         else:
-            print('ERROR')
+            print('last rule not found')
 
     # printNodeStack(theStack)
     return rulesToOutput
 
 # set up grammar from files and set up states
 def setUpGrammar():
-    global cfgrules, lr1trans
+    global cfgrules, lr1trans, theStack, states
+
+    # reset global vars (TODO: there has to be a better way???)
+    cfgrules = []
+    lr1trans = []
+    theStack = []
+    states   = [State() for _ in range(15)]
 
     # TODO: is there an easier way to convert this?
     #  '0 BOF shift 1\n1 procedures shift 2'
@@ -190,6 +196,6 @@ def parse(tokens):
     try:
         result = checkSequence(tokens)
     except ValueError as err:
-        return (None, err)
+        return (None, err.args[0])
 
     return (result, "success")
diff --git a/Test.py b/Test.py
index e747b55..5016c6b 100644
--- a/Test.py
+++ b/Test.py
@@ -17,6 +17,7 @@ def main():
     print("**********************************************************")
 
     for f in testFiles:
+        print(f)
 
         # Scanning 
         content = open(f, "r").read()
@@ -35,7 +36,21 @@ def main():
     
         # Parsing
         print("Parsing starts")
-        parse(tokens)
+
+        try:
+            (steps, errorString) = parse(tokens)
+        except:
+            print("Exception in Parsing")
+
+        # Error in Parsing
+        if steps is None:
+            print("ERROR in Parsing: " + errorString)
+            print("**********************************************************")
+            continue
+        
+        print("All Steps:")
+        print(steps)
+
         print("**********************************************************")
 
 main()
\ No newline at end of file
-- 
GitLab