https://github.com/csb6/ginevra
A very basic C++ preprocessor supporting only #define directives, based on the ginevra program in Arthur Pyster's book, Compiler Design and Construction
https://github.com/csb6/ginevra
cpp17 preprocessor
Last synced: about 1 year ago
JSON representation
A very basic C++ preprocessor supporting only #define directives, based on the ginevra program in Arthur Pyster's book, Compiler Design and Construction
- Host: GitHub
- URL: https://github.com/csb6/ginevra
- Owner: csb6
- Created: 2019-11-23T05:40:15.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-12-17T23:11:30.000Z (over 6 years ago)
- Last Synced: 2025-02-17T00:21:09.536Z (over 1 year ago)
- Topics: cpp17, preprocessor
- Language: C++
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Ginevra++
This is a collection of two implementations of a very basic C-like preprocessor that operates on any text file with a `.cpp` or `.h` extension. It only implements the `#define` preprocessor directive, but it tokenizes the content of the file, replaces all tokens that match symbols
defined in the `#define` directives, and prints out the result to the console.
Basic error handling is included: it checks that the given file exists and has the right file extension, and the program will print errors if a token ends unexpectedly.
Both programs are based on the ginevra preprocessor implemented in Arthur Pyster's book
*Compiler Design and Construction*, but both take advantage of C++'s standard library to
simplify the implementation immensely. Also, since the book's implementation of the symbol
table used a linked list instead of a hash map, this implementation may in fact be
faster than the C version found in the book for programs with large numbers of `#define`s!
This project was more challenging than I expected, but it helped me gain experience
in C++'s fstreams, as well as when object-oriented programming does/doesn't work well
### ginevra++.cpp
This is the more object-oriented version of the program, although it is quite a bit longer
than `better.cpp` and much more complicated. However, there is slightly more error checking,
and if this were to be expanded into a full compiler, it would probably be more maintainable
and extensible.
### better.cpp
After writing `ginevra++.cpp` and feeling dissatisfied with its complexity, I rewrote
the program in a more procedural form, removing the separate constants representing tokens
and greatly simplifying the conditional logic of the parser while still preserving nearly
all of the features. The code is not the prettiest, but I think that it is much more easy to
reason about than the more OOP version in `ginevra++.cpp`.
## Usage
Building `ginevra++.cpp` is built with C++17, although it should also be able to compile.
under C++14. Run `./build-ginevra++.sh`, then run `./ginevra++ [some .cpp or .h file]`
Building `better.cpp` requires at least C++17. Run `./build.sh`, then run
`./better [some .cpp or .h file]`