Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
CompilerConstruction
CodeSamples
Commits
b41e1b3a
Commit
b41e1b3a
authored
May 01, 2020
by
Ronald Charles Moore
Browse files
added better error checking
parent
411d0123
Changes
6
Hide whitespace changes
Inline
Side-by-side
recursiveDescentParsers/cplusplus/interpreter/Makefile
View file @
b41e1b3a
...
...
@@ -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
recursiveDescentParsers/cplusplus/interpreter/badTest.input
0 → 100644
View file @
b41e1b3a
42?
42^2
(42))
((42
(42 / 0)
(42 / (1-1))
42 +/- 3.14159
recursiveDescentParsers/cplusplus/interpreter/badTest.reference
0 → 100644
View file @
b41e1b3a
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!
recursiveDescentParsers/cplusplus/interpreter/
t
est
I
nput
.txt
→
recursiveDescentParsers/cplusplus/interpreter/
goodT
est
.i
nput
View file @
b41e1b3a
File moved
recursiveDescentParsers/cplusplus/interpreter/
testOutput.txt
→
recursiveDescentParsers/cplusplus/interpreter/
goodTest.reference
View file @
b41e1b3a
File moved
recursiveDescentParsers/cplusplus/interpreter/interpreter.cpp
View file @
b41e1b3a
...
...
@@ -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
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment