Skip to content

Commit 12c7877

Browse files
committed
Minor bug fix interpreting parse() return value
1) Instead of testing for == FAIL below,we test for != ACCEPT (in which ACCEPT has a value of zero).When compiling on a MSVC 2010 platform using cygwin's Flex and Bison,we note that the return value from parse() is not -1 but rather +1.It is more likely that on different platforms the return value of zerowill be used consistantly to indicate an accept state. Whereas, thevalue of the abort or fail state will be more likely not to be a specificvalue but rather a value that is not equal to zero. 2) The expression:   if (p != nullptr) delete p; can be rewritten more concisely   delete p; since delete is well behaved when passed a nullptr. 3) One should release the pointer to the old scanner and parser before allocating a new one. Otherwise, you will have memory leaks if MC_Driver::parse() is call more than once per instance.
1 parent 96dc7ff commit 12c7877

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

mc_driver.cpp

+19-5
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,40 @@
44

55
#include "mc_driver.hpp"
66

7-
#ifndef FAIL
8-
#define FAIL -1
7+
// wamckee 2013 APR 2 - Instead of testing for == FAIL below,
8+
// we test for != ACCEPT (in which ACCEPT has a value of zero).
9+
// When compiling on a MSVC 2010 platform using cygwin's Flex and Bison,
10+
// we note that the return value from parse() is not -1 but rather +1.
11+
// It is more likely that on different platforms the return value of zero
12+
// will be used consistantly to indicate an accept state. Whereas, the
13+
// value of the abort or fail state will be more likely not to be a specific
14+
// value but rather a value that is not equal to zero.
15+
16+
#ifndef ACCEPT
17+
#define ACCEPT 0
918
#endif
1019

1120
MC::MC_Driver::~MC_Driver(){
12-
if( scanner != nullptr ) delete(scanner);
13-
if( parser != nullptr ) delete(parser);
21+
delete(scanner); // wamckee 2013 APR 2
22+
delete(parser); // wamckee 2013 APR 2
1423
}
1524

1625
void MC::MC_Driver::parse( const char *filename ){
1726
assert( filename != nullptr );
1827
std::ifstream in_file( filename );
1928
if( ! in_file.good() ) exit( EXIT_FAILURE );
29+
30+
delete(scanner); // wamckee 2013 APR 2
2031
scanner = new MC::MC_Scanner( &in_file );
2132
/* check to see if its initialized */
2233
assert( scanner != nullptr );
34+
35+
delete(parser); // wamckee 2013 APR 2
2336
parser = new MC::MC_Parser( (*scanner) /* scanner */,
2437
(*this) /* driver */ );
2538
assert( parser != nullptr );
26-
if(parser->parse() == FAIL)
39+
40+
if(parser->parse() != ACCEPT) // wamckee 2013 APR 2
2741
{
2842
std::cerr << "Parse failed!!\n";
2943
}

0 commit comments

Comments
 (0)