diff --git a/ParseTests/add2num b/ParseTests/add2num deleted file mode 100644 index a8251d320499028ad58add0f37011254b4799982..0000000000000000000000000000000000000000 --- 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 b225c812412eba41690d78f691dcfcf2b811a946..0000000000000000000000000000000000000000 --- 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 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/ParseTests/literals b/ParseTests/literals deleted file mode 100644 index fad30d5eff388e323d9a2d15987ea01e49e22e59..0000000000000000000000000000000000000000 --- 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 4a40241d28cb3a49740fa5e92dc0e4f4f4a7f5dd..0000000000000000000000000000000000000000 --- 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 06b5d7bad836eb86453b77666466a66dd0e499bf..a2841226826688478b7a1bc256dd81ea1e8b9a42 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 e747b55b6dc754137d2aceee66a053818f6af53e..5016c6b5cc89b58d973827e761bd304de4f87dfa 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