diff --git a/recursiveDescentParsers/cplusplus/interpreter/Makefile b/recursiveDescentParsers/cplusplus/interpreter/Makefile index 1413587551f21fa452621abbe4fc5e957fbab02b..bd7ef7e88e247a88d55389f2b540acddb4d21cce 100644 --- a/recursiveDescentParsers/cplusplus/interpreter/Makefile +++ b/recursiveDescentParsers/cplusplus/interpreter/Makefile @@ -6,7 +6,8 @@ PROGS := interpreter -TESTFILES := testInput.txt testOutput.txt +TESTINPUT := goodTest.input badTest.input + # Uncomment only one of the next two lines (choose your c++ compiler) # CC=g++ CC := clang++ @@ -36,8 +37,14 @@ clean: test: $(PROGS) $(TESTFILES) # Running interpreter in test mode - -./interpreter testInput.txt 2>&1 >tmp.txt + -./interpreter goodTest.input >tmp.txt 2>&1 + # Checking output -- no news is good news! + @diff -qs goodTest.reference tmp.txt + -$(RM) tmp.txt + # Running interpreter in test mode + -./interpreter badTest.input 2>&1 >tmp.txt # Checking output -- no news is good news! - diff testOutput.txt tmp.txt - -rm tmp.txt + @diff -qs badTest.reference tmp.txt + -$(RM) tmp.txt + diff --git a/recursiveDescentParsers/cplusplus/interpreter/badTest.input b/recursiveDescentParsers/cplusplus/interpreter/badTest.input new file mode 100644 index 0000000000000000000000000000000000000000..99faffe929d271a664703888821ac01a3bf91828 --- /dev/null +++ b/recursiveDescentParsers/cplusplus/interpreter/badTest.input @@ -0,0 +1,8 @@ +42? +42^2 +(42)) +((42 +(42 / 0) +(42 / (1-1)) +42 +/- 3.14159 + diff --git a/recursiveDescentParsers/cplusplus/interpreter/badTest.reference b/recursiveDescentParsers/cplusplus/interpreter/badTest.reference new file mode 100644 index 0000000000000000000000000000000000000000..c2830f438a0a2c2b578318af9ab27b7a2795083b --- /dev/null +++ b/recursiveDescentParsers/cplusplus/interpreter/badTest.reference @@ -0,0 +1,33 @@ +0:42? +INTERPRETER: 42 +ERROR on line 0, column 3 : Bad Character(s) found. +42?--^ +1:42^2 +INTERPRETER: 42 +ERROR on line 1, column 3 : Bad Character(s) found. +42^2--^ +1:42^2 +INTERPRETER: 2 +2:(42)) +INTERPRETER: 42 +2:(42)) +ERROR on line 2, column 5 : Expected Left Parenthesis or number +(42))----^ +INTERPRETER: 0 +3:((42 +ERROR on line 4, column 1 : Expected Right Parenthesis +(42 / 0)^ +ERROR on line 4, column 3 : Expected Right Parenthesis +(42 / 0)--^ +ERROR on line 4, column 8 : Division by zero! +(42 / 0)-------^ +INTERPRETER: 0 +5:(42 / (1-1)) +ERROR on line 5, column 12 : Division by zero! +(42 / (1-1))-----------^ +ERROR on line 6, column 2 : Expected Right Parenthesis +42 +/- 3.14159-^ +ERROR on line 6, column 5 : Expected Left Parenthesis or number +42 +/- 3.14159----^ +INTERPRETER: -3.14159 +End Of File! diff --git a/recursiveDescentParsers/cplusplus/interpreter/testInput.txt b/recursiveDescentParsers/cplusplus/interpreter/goodTest.input similarity index 100% rename from recursiveDescentParsers/cplusplus/interpreter/testInput.txt rename to recursiveDescentParsers/cplusplus/interpreter/goodTest.input diff --git a/recursiveDescentParsers/cplusplus/interpreter/testOutput.txt b/recursiveDescentParsers/cplusplus/interpreter/goodTest.reference similarity index 100% rename from recursiveDescentParsers/cplusplus/interpreter/testOutput.txt rename to recursiveDescentParsers/cplusplus/interpreter/goodTest.reference diff --git a/recursiveDescentParsers/cplusplus/interpreter/interpreter.cpp b/recursiveDescentParsers/cplusplus/interpreter/interpreter.cpp index 2963c1adec56107e5a709792d232721940b298c4..6753b12a6161b16e6e25a9c10ff184d657a8545c 100644 --- a/recursiveDescentParsers/cplusplus/interpreter/interpreter.cpp +++ b/recursiveDescentParsers/cplusplus/interpreter/interpreter.cpp @@ -47,17 +47,6 @@ static int currentColumnNumber = 0; static Token next_token; // again with the global variables... static numberType currentNumber; // = zero.... -static void printErrorMsg( const std::string Error ) -{ - std::cout << "ERROR on line " << currentLineNumber - << ", column " << currentColumnNumber << " : " - << Error << std::endl; - std::cout << currentLine; - for ( int col = 0; col < currentColumnNumber-1; col++ ) - std::cout << '-'; - std::cout << '^' << std::endl; -} // end printErrorMsg - // The Lexer // ========== static bool skippedWhiteSpace( ) { // return true if not at EOF, i.e. if skipped @@ -120,6 +109,18 @@ static Token gettok( ) { return bad_tok; } // end gettok +static void printErrorMsg( const std::string Error ) +{ + std::cout << "ERROR on line " << currentLineNumber + << ", column " << currentColumnNumber << " : " + << Error << std::endl; + std::cout << currentLine; + for ( int col = 0; col < currentColumnNumber-1; col++ ) + std::cout << '-'; + std::cout << '^' << std::endl; + next_token = gettok( ); // skip over badness -- move on! +} // end printErrorMsg + // PARSING!!! // =========== // @@ -223,10 +224,16 @@ int main( int argc, char **argv ) { // else if 1 == argc .... std::string fileName( argv[1] ); if ( "-" != fileName ) { - static std::ifstream ifs( fileName ); + static std::ifstream ifs( fileName, std::ifstream::in ); + if ( ! ifs.good( ) ) { + std::cerr << "Could not open file " << fileName + << '\n'; + return( -2 ); // return NOT OK status + }; // else if ifs.good() ... input = &ifs; } + // Prime the pump! next_token = gettok( ); @@ -236,6 +243,8 @@ int main( int argc, char **argv ) { << currentLine << std::endl; numberType value = E( ); std::cout << "INTERPRETER: " << value << std::endl; + if ( bad_tok == next_token ) + printErrorMsg( "Bad Character(s) found." ); }; std::cout << "End Of File!" << std::endl;