Skip to content

Commit fe04642

Browse files
updates
1 parent 7aebfcf commit fe04642

File tree

5 files changed

+60
-50
lines changed

5 files changed

+60
-50
lines changed

Makefile

-5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ CXXFLAGS = -O0 $(CXXDEBUG) $(CXXSTD)
1717
CPPOBJ = main mc_driver
1818
SOBJ = parser lexer
1919

20-
LIBS = -lfl
21-
2220
FILES = $(addsuffix .cpp, $(CPPOBJ))
2321

2422
OBJS = $(addsuffix .o, $(CPPOBJ))
@@ -36,7 +34,6 @@ wc: $(FILES)
3634
$(MAKE) $(SOBJ)
3735
$(MAKE) $(OBJS)
3836
$(CXX) $(CXXFLAGS) -o $(EXE) $(OBJS) parser.o lexer.o $(LIBS)
39-
rm -rf *.o
4037

4138

4239
parser: mc_parser.yy
@@ -47,8 +44,6 @@ lexer: mc_lexer.l
4744
flex --outfile=mc_lexer.yy.cc $<
4845
$(CXX) $(CXXFLAGS) -c mc_lexer.yy.cc -o lexer.o
4946

50-
%.o: %.cpp
51-
$(CXX) -c $(CXXFLAGS) $(CXXSTD) -o $@ $<
5247

5348
.PHONY: clean
5449
clean:

mc_driver.cpp

+50-27
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,87 @@
44

55
#include "mc_driver.hpp"
66

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
18-
#endif
19-
207
MC::MC_Driver::~MC_Driver(){
21-
delete(scanner); // wamckee 2013 APR 2
22-
delete(parser); // wamckee 2013 APR 2
8+
delete(scanner);
9+
delete(parser);
2310
}
2411

25-
void MC::MC_Driver::parse( const char *filename ){
12+
void
13+
MC::MC_Driver::parse( const char *filename )
14+
{
2615
assert( filename != nullptr );
2716
std::ifstream in_file( filename );
2817
if( ! in_file.good() ) exit( EXIT_FAILURE );
2918

30-
delete(scanner); // wamckee 2013 APR 2
19+
delete(scanner);
20+
scanner = nullptr;
3121
scanner = new MC::MC_Scanner( &in_file );
3222
/* check to see if its initialized */
3323
assert( scanner != nullptr );
3424

35-
delete(parser); // wamckee 2013 APR 2
25+
delete(parser);
26+
parser = nullptr;
3627
parser = new MC::MC_Parser( (*scanner) /* scanner */,
3728
(*this) /* driver */ );
3829
assert( parser != nullptr );
3930

40-
if(parser->parse() != ACCEPT) // wamckee 2013 APR 2
31+
const int accept( 0 );
32+
if( parser->parse() != accept )
4133
{
4234
std::cerr << "Parse failed!!\n";
4335
}
4436
}
4537

46-
void MC::MC_Driver::add_upper(){ uppercase++; chars++; words++; }
38+
void
39+
MC::MC_Driver::add_upper()
40+
{
41+
uppercase++;
42+
chars++;
43+
words++;
44+
}
4745

48-
void MC::MC_Driver::add_lower(){ lowercase++; chars++; words++; }
46+
void
47+
MC::MC_Driver::add_lower()
48+
{
49+
lowercase++;
50+
chars++;
51+
words++;
52+
}
4953

50-
void MC::MC_Driver::add_word( const std::string &c ){
54+
void
55+
MC::MC_Driver::add_word( const std::string &c )
56+
{
5157
words++;
5258
chars += c.length();
5359
for(auto it(c.begin()); it != c.end(); ++it){
54-
if( islower( (*it) ) ) { lowercase++; }
55-
else if ( isupper( (*it) ) ) { uppercase++; }
60+
if( islower( (*it) ) )
61+
{
62+
lowercase++;
63+
}
64+
else if ( isupper( (*it) ) )
65+
{
66+
uppercase++;
67+
}
5668
}
5769
}
5870

59-
void MC::MC_Driver::add_newline(){ lines++; chars++; }
71+
void
72+
MC::MC_Driver::add_newline()
73+
{
74+
lines++;
75+
chars++;
76+
}
6077

61-
void MC::MC_Driver::add_char(){ chars++; }
78+
void
79+
MC::MC_Driver::add_char()
80+
{
81+
chars++;
82+
}
6283

6384

64-
std::ostream& MC::MC_Driver::print(std::ostream &stream){
85+
std::ostream&
86+
MC::MC_Driver::print( std::ostream &stream )
87+
{
6588
stream << "Uppercase: " << uppercase << "\n";
6689
stream << "Lowercase: " << lowercase << "\n";
6790
stream << "Lines: " << lines << "\n";

mc_driver.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <string>
55
#include "mc_scanner.hpp"
6-
#include "mc_Parser.tab.hh"
6+
#include "mc_parser.tab.hh"
77

88
namespace MC{
99

mc_lexer.l

-4
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,19 @@ typedef MC::MC_Parser::token token;
2626

2727
%%
2828
[a-z] {
29-
yylval->sval = STOKEN( yytext );
3029
return(token::LOWER);
3130
}
3231
[A-Z] {
33-
yylval->sval = STOKEN( yytext );
3432
return(token::UPPER);
3533
}
3634
[a-zA-Z]+ {
3735
yylval->sval = STOKEN( yytext );
3836
return(token::WORD);
3937
}
4038
\n {
41-
yylval->sval = STOKEN( yytext );
4239
return(token::NEWLINE);
4340
}
4441
. {
45-
yylval->sval = STOKEN( yytext );
4642
return(token::CHAR);
4743
}
4844
%%

mc_parser.yy

+9-13
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,26 @@
3838
std::string *sval;
3939
}
4040

41-
%token END 0 "end of file"
42-
%token <sval> UPPER
43-
%token <sval> LOWER
44-
%token <sval> WORD
45-
%token <sval> NEWLINE
46-
%token <sval> CHAR
41+
%token END 0 "end of file"
42+
%token UPPER
43+
%token LOWER
44+
%token <sval> WORD
45+
%token NEWLINE
46+
%token CHAR
4747

48-
%type <sval> object
4948

50-
%destructor { delete( $$ ); } UPPER LOWER WORD NEWLINE CHAR
49+
%destructor { delete( $$ ); } WORD
5150

5251

5352
%%
5453

5554
list_option : END | list END;
5655

5756
list
58-
: object
59-
| list object
57+
: item
58+
| list item
6059
;
6160

62-
object : item { delete( $$ ); }
63-
6461
item
6562
: UPPER { driver.add_upper(); }
6663
| LOWER { driver.add_lower(); }
@@ -79,7 +76,6 @@ void MC::MC_Parser::error( const MC::MC_Parser::location_type &l,
7976
}
8077

8178

82-
8379
/* include for access to scanner.yylex */
8480
#include "mc_scanner.hpp"
8581
static int yylex(MC::MC_Parser::semantic_type *yylval,

0 commit comments

Comments
 (0)