├── .gitignore
├── .travis.yml
├── CMakeLists.txt
├── LICENSE
├── Makefile
├── README.md
├── main.cpp
├── mc_driver.cpp
├── mc_driver.hpp
├── mc_lexer.l
├── mc_parser.yy
├── mc_scanner.hpp
└── test
├── test0.pl
├── wordlist.txt
└── wordlist_out.txt
/.gitignore:
--------------------------------------------------------------------------------
1 | *.swp
2 | *.o
3 | location.hh
4 | position.hh
5 | stack.hh
6 | mc_lexer.yy.cc
7 | mc_parser.output
8 | mc_parser.tab.cc
9 | mc_parser.tab.hh
10 | my_wc
11 | build*/
12 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | ## container build, no sudo
2 | sudo: false
3 |
4 | language: cpp
5 |
6 | compiler:
7 | - gcc
8 | - clang
9 |
10 | os:
11 | - linux
12 |
13 | cache:
14 | apt:
15 | ccache:
16 | directories:
17 |
18 | env:
19 | global:
20 | - GCC_VERSION=5
21 | - CXXFLAGS=-std=c++11
22 | - CFLAGS=-std=c99
23 |
24 | branches:
25 | only:
26 | - master
27 |
28 | before_install:
29 | - if [ ${CC} != "clang" ]; then
30 | export CC=/usr/bin/gcc-${GCC_VERSION} CXX=/usr/bin/g++-${GCC_VERSION};
31 | C=gcc;
32 | fi
33 |
34 |
35 | addons:
36 | apt:
37 | sources:
38 | - ubuntu-toolchain-r-test
39 | packages:
40 | # ensure these match GCC_VERSION
41 | - gcc-5
42 | - g++-5
43 | - binutils
44 | - gawk
45 | - build-essential
46 | - wget
47 | - libtool
48 | - flex
49 |
50 |
51 | before_script:
52 | - echo $LANG
53 | - echo $LC_ALL
54 | - wget --no-check-certificate http://ftp.gnu.org/gnu/bison/bison-3.5.tar.gz
55 | - mkdir $HOME/bison
56 | - tar -xf bison-3.5.tar.gz; cd bison-3.5; ./configure --prefix=$HOME/bison; make -j 4 install; cd $PWD/..;
57 | - export PATH=$HOME/bison/bin:$PATH
58 | - wget --no-check-certificate http://llvm.org/releases/3.7.1/clang+llvm-3.7.1-x86_64-linux-gnu-ubuntu-14.04.tar.xz;
59 | - tar -xf clang+llvm-3.7.1-x86_64-linux-gnu-ubuntu-14.04.tar.xz
60 | - export PATH=$PWD/clang+llvm-3.7.1-x86_64-linux-gnu-ubuntu-14.04/bin:$PATH
61 | script:
62 | - ${CC} --version ; ${CXX} --version;
63 | - bison --version
64 | - pwd
65 | - ls -la $HOME
66 | - env
67 | - make all && make test
68 |
--------------------------------------------------------------------------------
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-deprecated-register")
2 |
3 | project(simple_wc_example CXX)
4 |
5 | cmake_minimum_required(VERSION 3.1)
6 |
7 | find_package(BISON REQUIRED)
8 | find_package(FLEX REQUIRED)
9 |
10 | BISON_TARGET(mc_parser
11 | mc_parser.yy
12 | ${CMAKE_CURRENT_BINARY_DIR}/mc_parser.tab.cc)
13 | FLEX_TARGET(mc_lexer
14 | mc_lexer.l
15 | ${CMAKE_CURRENT_BINARY_DIR}/mc_lexer.yy.cc)
16 | ADD_FLEX_BISON_DEPENDENCY(mc_lexer mc_parser)
17 |
18 | add_executable(my_wc
19 | main.cpp
20 | mc_driver.cpp
21 |
22 | ${FLEX_mc_lexer_OUTPUTS}
23 | ${BISON_mc_parser_OUTPUTS}
24 | )
25 | target_include_directories(my_wc
26 | PRIVATE
27 | ${CMAKE_CURRENT_SOURCE_DIR}
28 | ${FLEX_INCLUDE_DIRS}
29 | ${CMAKE_CURRENT_BINARY_DIR})
30 | set_property(TARGET my_wc
31 | PROPERTY CXX_STANDARD 14)
32 |
33 | enable_testing()
34 | add_test(NAME basic
35 | COMMAND ${CMAKE_SOURCE_DIR}/test/test0.pl
36 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
37 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | CC ?= clang
2 | CXX ?= clang++
3 |
4 | EXE = my_wc
5 |
6 | CDEBUG = -g -Wall
7 |
8 | CXXDEBUG = -g -Wall
9 |
10 | CSTD = -std=c99
11 | CXXSTD = -std=c++14
12 |
13 | CFLAGS = -Wno-deprecated-register -O0 $(CDEBUG) $(CSTD)
14 | CXXFLAGS = -Wno-deprecated-register -O0 $(CXXDEBUG) $(CXXSTD)
15 |
16 |
17 | CPPOBJ = main mc_driver
18 | SOBJ = parser lexer
19 |
20 | FILES = $(addsuffix .cpp, $(CPPOBJ))
21 |
22 | OBJS = $(addsuffix .o, $(CPPOBJ))
23 |
24 | CLEANLIST = $(addsuffix .o, $(OBJ)) $(OBJS) \
25 | mc_parser.tab.cc mc_parser.tab.hh \
26 | location.hh position.hh \
27 | stack.hh mc_parser.output parser.o \
28 | lexer.o mc_lexer.yy.cc $(EXE)\
29 |
30 | .PHONY: all
31 | all: wc
32 |
33 | wc: $(FILES)
34 | $(MAKE) $(SOBJ)
35 | $(MAKE) $(OBJS)
36 | $(CXX) $(CXXFLAGS) -o $(EXE) $(OBJS) parser.o lexer.o $(LIBS)
37 |
38 |
39 | parser: mc_parser.yy
40 | bison -d -v mc_parser.yy
41 | $(CXX) $(CXXFLAGS) -c -o parser.o mc_parser.tab.cc
42 |
43 | lexer: mc_lexer.l
44 | flex --outfile=mc_lexer.yy.cc $<
45 | $(CXX) $(CXXFLAGS) -c mc_lexer.yy.cc -o lexer.o
46 |
47 | .PHONY: test
48 | test:
49 | test/test0.pl
50 |
51 | .PHONY: clean
52 | clean:
53 | rm -rf $(CLEANLIST)
54 |
55 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ### Simple Word Count Parser Example
2 | =================
3 |
4 | ### Build Status
5 | Tested on linux and OS X
6 |
7 | [](https://travis-ci.org/jonathan-beard/simple_wc_example)
8 |
9 | ### About
10 |
11 | So this is a super simple parser example. The tutorial is located here:
12 | link
13 |
14 | Between the tutorial and the code you should be able to get started with more
15 | advanced projects using C++ and Flex/Bison. Admittedly the example itself is a
16 | bit contrived, however it's mean to be simple and get the point across. If
17 | there are changes in Flex/Bison that prevent the example from compiling or if
18 | you just have improvements, feel free to generate a pull request or just shoot
19 | me an e-mail.
20 |
21 | To compile just download and run make, there's a super simple test harness to
22 | make sure it runs in the test dir.
23 |
24 | Or you use CMake.
25 |
26 | mkdir build/
27 | cmake ..
28 | make
29 |
30 | or with clang
31 |
32 | mkdir build/
33 | CC=clang CXX=clang++ cmake ..
34 | make
35 |
36 | or with clang with optimisations
37 |
38 | mkdir build/
39 | CC=clang CXX=clang++ cmake .. -DCMAKE_BUILD_TYPE=Release
40 | make
41 |
42 | To run the simple test after make call
43 |
44 | ctest
45 |
46 | ## NOTES
47 | * **OS X Users** I've heard of some issues compiling if you've installed
48 | command line tools, then XCode.app afterwards on Catalina (with bash). When compiling with
49 | ```clang++```, the C++ header files aren't found. If you have this issue, exporting an environment
50 | variable, such as this,
51 | ```export CXX=/usr/bin/clang++```, seems to be a workaround. If anybody has a better
52 | solution (aside from wiping out all build tools and reinstalling) please let
53 | me know.
54 | * **OS X Users** If you have this message when compiling on OS X (likely Catalina and later),
55 | ```bash
56 | mc_lexer.yy.cc:675:8: error: member reference type 'std::istream *' (aka 'basic_istream *') is a
57 | pointer; did you mean to use '->'?
58 | ```
59 | Then this means that you've a broken cpp path. To see why this is, go to the command line, type
60 | ```bash
61 | cpp -xc++ -v < /dev/null
62 | ```
63 | to see your path included by clang. The key problem is the lack of a modern FlexLexer.h in the
64 | command line tools of OS X, which is compouned when you install a new verson of Flex and the
65 | header isn't in the path. So, your binary will be the right one, the headers the wrong ones.
66 | As an example, the header file we want on my install of OS X (fresh VM install), is in
67 | ```
68 | /System/Volumes/Data/usr/local/Cellar/flex/2.6.4_1/include/FlexLexer.h
69 | ```
70 | which is installed by _home brew_. Unfortunately, the default include path pulls in the system
71 | default version at
72 | ```
73 | /System/Volumes/Data/Library/Developer/CommandLineTools/usr/include/FlexLexer.h
74 | ```
75 | **HOW TO FIX**
76 |
77 | 1. Interestingly there's yet another version in ```/Library/Developer```. So how do you fix?
78 | The simplest way if you have sudo access, do:
79 | ```
80 | sudo -s
81 | ln -s /FlexLexer.h
82 | ```
83 | As an example, here's my path:
84 | ```
85 | ln -s /System/Volumes/Data/usr/local/Cellar/flex/2.6.4_1/include/FlexLexer.h
86 | ```
87 | 2. Second, **best** way for non-root is to set the _CPLUS\_INCLUDE\_PATH_ like this
88 | ```
89 | CPLUS_INCLUDE_PATH=/System/Volumes/Data/usr/local/Cellar/flex/2.6.4_1/include/:$CPLUS_INCLUDE_PATH
90 | ```
91 | using my path above.
92 |
93 | * **Bison Version** - updated the latest head to test with Bison vs. 3.5, there were a few minor
94 | changes in syntax, now reflected in the example code. Will no longer work with Bison vs. < 3.3,
95 | see notes from [link](https://lwn.net/Articles/777594/).
96 |
97 |
98 | Thanks!
99 |
--------------------------------------------------------------------------------
/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | #include "mc_driver.hpp"
6 |
7 | int
8 | main( const int argc, const char **argv )
9 | {
10 | /** check for the right # of arguments **/
11 | if( argc != 2 )
12 | {
13 | /** exit with failure condition **/
14 | return ( EXIT_FAILURE );
15 | }
16 |
17 | if( std::strncmp( argv[ 1 ], "-h", 2 ) == 0 )
18 | {
19 | /** simple help menu **/
20 | std::cout << "use -o for pipe to std::cin\n";
21 | std::cout << "just give a filename to count from a file\n";
22 | std::cout << "use -h to get this menu\n";
23 | return( EXIT_SUCCESS );
24 | }
25 |
26 | MC::MC_Driver driver;
27 | /** example for piping input from terminal, i.e., using cat **/
28 | if( std::strncmp( argv[ 1 ], "-o", 2 ) == 0 )
29 | {
30 | driver.parse( std::cin );
31 | }
32 | /** example reading input from a file **/
33 | else
34 | {
35 | /** assume file, prod code, use stat to check **/
36 | driver.parse( argv[1] );
37 | }
38 | driver.print( std::cout ) << "\n";
39 |
40 | return( EXIT_SUCCESS );
41 | }
42 |
--------------------------------------------------------------------------------
/mc_driver.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | #include "mc_driver.hpp"
6 |
7 | MC::MC_Driver::~MC_Driver()
8 | {
9 | delete(scanner);
10 | scanner = nullptr;
11 | delete(parser);
12 | parser = nullptr;
13 | }
14 |
15 | void
16 | MC::MC_Driver::parse( const char * const filename )
17 | {
18 | assert( filename != nullptr );
19 | std::ifstream in_file( filename );
20 | if( ! in_file.good() )
21 | {
22 | exit( EXIT_FAILURE );
23 | }
24 | parse_helper( in_file );
25 | return;
26 | }
27 |
28 | void
29 | MC::MC_Driver::parse( std::istream &stream )
30 | {
31 | if( ! stream.good() && stream.eof() )
32 | {
33 | return;
34 | }
35 | //else
36 | parse_helper( stream );
37 | return;
38 | }
39 |
40 |
41 | void
42 | MC::MC_Driver::parse_helper( std::istream &stream )
43 | {
44 |
45 | delete(scanner);
46 | try
47 | {
48 | scanner = new MC::MC_Scanner( &stream );
49 | }
50 | catch( std::bad_alloc &ba )
51 | {
52 | std::cerr << "Failed to allocate scanner: (" <<
53 | ba.what() << "), exiting!!\n";
54 | exit( EXIT_FAILURE );
55 | }
56 |
57 | delete(parser);
58 | try
59 | {
60 | parser = new MC::MC_Parser( (*scanner) /* scanner */,
61 | (*this) /* driver */ );
62 | }
63 | catch( std::bad_alloc &ba )
64 | {
65 | std::cerr << "Failed to allocate parser: (" <<
66 | ba.what() << "), exiting!!\n";
67 | exit( EXIT_FAILURE );
68 | }
69 | const int accept( 0 );
70 | if( parser->parse() != accept )
71 | {
72 | std::cerr << "Parse failed!!\n";
73 | }
74 | return;
75 | }
76 |
77 | void
78 | MC::MC_Driver::add_upper()
79 | {
80 | uppercase++;
81 | chars++;
82 | words++;
83 | }
84 |
85 | void
86 | MC::MC_Driver::add_lower()
87 | {
88 | lowercase++;
89 | chars++;
90 | words++;
91 | }
92 |
93 | void
94 | MC::MC_Driver::add_word( const std::string &word )
95 | {
96 | words++;
97 | chars += word.length();
98 | for(const char &c : word ){
99 | if( islower( c ) )
100 | {
101 | lowercase++;
102 | }
103 | else if ( isupper( c ) )
104 | {
105 | uppercase++;
106 | }
107 | }
108 | }
109 |
110 | void
111 | MC::MC_Driver::add_newline()
112 | {
113 | lines++;
114 | chars++;
115 | }
116 |
117 | void
118 | MC::MC_Driver::add_char()
119 | {
120 | chars++;
121 | }
122 |
123 |
124 | std::ostream&
125 | MC::MC_Driver::print( std::ostream &stream )
126 | {
127 | stream << red << "Results: " << norm << "\n";
128 | stream << blue << "Uppercase: " << norm << uppercase << "\n";
129 | stream << blue << "Lowercase: " << norm << lowercase << "\n";
130 | stream << blue << "Lines: " << norm << lines << "\n";
131 | stream << blue << "Words: " << norm << words << "\n";
132 | stream << blue << "Characters: " << norm << chars << "\n";
133 | return(stream);
134 | }
135 |
--------------------------------------------------------------------------------
/mc_driver.hpp:
--------------------------------------------------------------------------------
1 | #ifndef __MCDRIVER_HPP__
2 | #define __MCDRIVER_HPP__ 1
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | #include "mc_scanner.hpp"
9 | #include "mc_parser.tab.hh"
10 |
11 | namespace MC{
12 |
13 | class MC_Driver{
14 | public:
15 | MC_Driver() = default;
16 |
17 | virtual ~MC_Driver();
18 |
19 | /**
20 | * parse - parse from a file
21 | * @param filename - valid string with input file
22 | */
23 | void parse( const char * const filename );
24 | /**
25 | * parse - parse from a c++ input stream
26 | * @param is - std::istream&, valid input stream
27 | */
28 | void parse( std::istream &iss );
29 |
30 | void add_upper();
31 | void add_lower();
32 | void add_word( const std::string &word );
33 | void add_newline();
34 | void add_char();
35 |
36 | std::ostream& print(std::ostream &stream);
37 | private:
38 |
39 | void parse_helper( std::istream &stream );
40 |
41 | std::size_t chars = 0;
42 | std::size_t words = 0;
43 | std::size_t lines = 0;
44 | std::size_t uppercase = 0;
45 | std::size_t lowercase = 0;
46 | MC::MC_Parser *parser = nullptr;
47 | MC::MC_Scanner *scanner = nullptr;
48 |
49 | const std::string red = "\033[1;31m";
50 | const std::string blue = "\033[1;36m";
51 | const std::string norm = "\033[0m";
52 | };
53 |
54 | } /* end namespace MC */
55 | #endif /* END __MCDRIVER_HPP__ */
56 |
--------------------------------------------------------------------------------
/mc_lexer.l:
--------------------------------------------------------------------------------
1 | %{
2 | /* C++ string header, for string ops below */
3 | #include
4 |
5 | /* Implementation of yyFlexScanner */
6 | #include "mc_scanner.hpp"
7 | #undef YY_DECL
8 | #define YY_DECL int MC::MC_Scanner::yylex( MC::MC_Parser::semantic_type * const lval, MC::MC_Parser::location_type *loc )
9 |
10 | /* typedef to make the returns for the tokens shorter */
11 | using token = MC::MC_Parser::token;
12 |
13 | /* define yyterminate as this instead of NULL */
14 | #define yyterminate() return( token::END )
15 |
16 | /* msvc2010 requires that we exclude this header file. */
17 | #define YY_NO_UNISTD_H
18 |
19 | /* update location on matching */
20 | #define YY_USER_ACTION loc->step(); loc->columns(yyleng);
21 |
22 | %}
23 |
24 | %option debug
25 | %option nodefault
26 | %option yyclass="MC::MC_Scanner"
27 | %option noyywrap
28 | %option c++
29 |
30 | %%
31 | %{ /** Code executed at the beginning of yylex **/
32 | yylval = lval;
33 | %}
34 |
35 | [a-z] {
36 | return( token::LOWER );
37 | }
38 |
39 | [A-Z] {
40 | return( token::UPPER );
41 | }
42 |
43 | [a-zA-Z]+ {
44 | /**
45 | * Section 10.1.5.1 of the 3.0.2 Bison Manual says the
46 | * following should work:
47 | * yylval.build( yytext );
48 | * but it doesn't.
49 | * ref: http://goo.gl/KLn0w2
50 | */
51 | yylval->build< std::string >( yytext );
52 | return( token::WORD );
53 | }
54 |
55 | \n {
56 | // Update line number
57 | loc->lines();
58 | return( token::NEWLINE );
59 | }
60 |
61 | . {
62 | return( token::CHAR );
63 | }
64 | %%
65 |
66 |
67 |
--------------------------------------------------------------------------------
/mc_parser.yy:
--------------------------------------------------------------------------------
1 | %skeleton "lalr1.cc"
2 | %require "3.0"
3 | %debug
4 | %defines
5 | %define api.namespace {MC}
6 | /**
7 | * bison 3.3.2 change
8 | * %define parser_class_name to this, updated
9 | * should work for previous bison versions as
10 | * well. -jcb 24 Jan 2020
11 | */
12 | %define api.parser.class {MC_Parser}
13 |
14 | %code requires{
15 | namespace MC {
16 | class MC_Driver;
17 | class MC_Scanner;
18 | }
19 |
20 | // The following definitions is missing when %locations isn't used
21 | # ifndef YY_NULLPTR
22 | # if defined __cplusplus && 201103L <= __cplusplus
23 | # define YY_NULLPTR nullptr
24 | # else
25 | # define YY_NULLPTR 0
26 | # endif
27 | # endif
28 |
29 | }
30 |
31 | %parse-param { MC_Scanner &scanner }
32 | %parse-param { MC_Driver &driver }
33 |
34 | %code{
35 | #include
36 | #include
37 | #include
38 |
39 | /* include for all driver functions */
40 | #include "mc_driver.hpp"
41 |
42 | #undef yylex
43 | #define yylex scanner.yylex
44 | }
45 |
46 | %define api.value.type variant
47 | %define parse.assert
48 |
49 | %token END 0 "end of file"
50 | %token UPPER
51 | %token LOWER
52 | %token WORD
53 | %token NEWLINE
54 | %token CHAR
55 |
56 | %locations
57 |
58 | %%
59 |
60 | list_option : END | list END;
61 |
62 | list
63 | : item
64 | | list item
65 | ;
66 |
67 | item
68 | : UPPER { driver.add_upper(); }
69 | | LOWER { driver.add_lower(); }
70 | | WORD { driver.add_word( $1 ); }
71 | | NEWLINE { driver.add_newline(); }
72 | | CHAR { driver.add_char(); }
73 | ;
74 |
75 | %%
76 |
77 |
78 | void
79 | MC::MC_Parser::error( const location_type &l, const std::string &err_message )
80 | {
81 | std::cerr << "Error: " << err_message << " at " << l << "\n";
82 | }
83 |
--------------------------------------------------------------------------------
/mc_scanner.hpp:
--------------------------------------------------------------------------------
1 | #ifndef __MCSCANNER_HPP__
2 | #define __MCSCANNER_HPP__ 1
3 |
4 | #if ! defined(yyFlexLexerOnce)
5 | #include
6 | #endif
7 |
8 | #include "mc_parser.tab.hh"
9 | #include "location.hh"
10 |
11 | namespace MC{
12 |
13 | class MC_Scanner : public yyFlexLexer{
14 | public:
15 |
16 | MC_Scanner(std::istream *in) : yyFlexLexer(in)
17 | {
18 | };
19 | virtual ~MC_Scanner() {
20 | };
21 |
22 | //get rid of override virtual function warning
23 | using FlexLexer::yylex;
24 |
25 | virtual
26 | int yylex( MC::MC_Parser::semantic_type * const lval,
27 | MC::MC_Parser::location_type *location );
28 | // YY_DECL defined in mc_lexer.l
29 | // Method body created by flex in mc_lexer.yy.cc
30 |
31 |
32 | private:
33 | /* yyval ptr */
34 | MC::MC_Parser::semantic_type *yylval = nullptr;
35 | };
36 |
37 | } /* end namespace MC */
38 |
39 | #endif /* END __MCSCANNER_HPP__ */
40 |
--------------------------------------------------------------------------------
/test/test0.pl:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env perl
2 | use strict;
3 | use warnings;
4 |
5 | use File::Basename;
6 | my $test_path = dirname(__FILE__);
7 |
8 | system( "./my_wc $test_path/wordlist.txt > temp.txt" );
9 | my $diff = `diff temp.txt $test_path/wordlist_out.txt`;
10 | chomp( $diff );
11 | if( $diff )
12 | {
13 | print STDERR $diff;
14 | unlink( "temp.txt" );
15 | exit( -1 );
16 | }
17 | print STDERR "***PASSED***\n";
18 | unlink( "temp.txt" );
19 | exit( 0 );
20 |
--------------------------------------------------------------------------------
/test/wordlist_out.txt:
--------------------------------------------------------------------------------
1 | [1;31mResults: [0m
2 | [1;36mUppercase: [0m0
3 | [1;36mLowercase: [0m543271
4 | [1;36mLines: [0m64130
5 | [1;36mWords: [0m64154
6 | [1;36mCharacters: [0m607454
7 |
8 |
--------------------------------------------------------------------------------