diff --git a/recursiveDescentParsers/cplusplus/interpreter/Makefile b/recursiveDescentParsers/cplusplus/interpreter/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..fd54984efa8f29c2ddfa4be5b66f752ceeb96c72 --- /dev/null +++ b/recursiveDescentParsers/cplusplus/interpreter/Makefile @@ -0,0 +1,36 @@ +# This Makefile made available to his students by +# Prof. Ronald Moore +# https://fbi.h-da.de/personen/ronald-moore/ +# mailto:ronald.moore@h-da.de +# with no warranties whatsoever + + +PROGS := interpreter + +# Uncomment only one of the next two lines (choose your c++ compiler) +# CC=g++ +CC := clang++ + +## Add your own CFLAGS if you find them necessary... such as -O3 or so... +CFLAGS := + + +## More preliminaries +# See https://www.gnu.org/software/make/manual/html_node/Special-Targets.html +# In this makefile, we want to keep going even if we find errors +.IGNORE : + +# Tell make that the following "targets" are "phony" +# Cf. https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html#Phony-Targets +.PHONY : all clean + +## Now, the targets -- the things that will get made! + +all: $(PROGS) + +$(PROGS): %: %.cpp + $(CC) -g $< $(CFLAGS) -o $@ + +clean: + $(RM) -fv *~ *.o $(PROGS) + diff --git a/recursiveDescentParsers/cplusplus/interpreter/README.md b/recursiveDescentParsers/cplusplus/interpreter/README.md new file mode 100644 index 0000000000000000000000000000000000000000..018defe32ff5b18eef42ae70368b9ec5826847f1 --- /dev/null +++ b/recursiveDescentParsers/cplusplus/interpreter/README.md @@ -0,0 +1,25 @@ +Contents +======== + +Everything here is taken from the slides for the "Compiler Construction" +course, i.e. it is directly from Prof. Ronald C. Moore. + +Or at least, after all this time, I really don't remember having stolen +this code from somewhere else, but if you find an older copy that looks +similar, please let me know, so I can give credit where credit is due -- +or disavow knowledge of that source (as the case may be). + +You are in the following subdirectory + ** `interpreter` + This code take mathematical expressions and evaluates them, + i.e. it outputs numbers. + +See **Chapter 1 Front End Construction**, Slides 21 and 22 (and please let me know when the inevitable day comes that these slide numbers are no longer correct). + +Ronald Moore +https://fbi.h-da.de/personen/ronald-moore/ +ronald.moore@h-da.de + + +30 April 2020 + diff --git a/recursiveDescentParsers/cplusplus/interpreter/interpreter b/recursiveDescentParsers/cplusplus/interpreter/interpreter new file mode 100755 index 0000000000000000000000000000000000000000..b4110dc560d39b33753aec8ba003afa3bec6f815 Binary files /dev/null and b/recursiveDescentParsers/cplusplus/interpreter/interpreter differ diff --git a/recursiveDescentParsers/cplusplus/interpreter/interpreter.cpp b/recursiveDescentParsers/cplusplus/interpreter/interpreter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f954cb5b38ba6987839b14b031a89dd477bddb67 --- /dev/null +++ b/recursiveDescentParsers/cplusplus/interpreter/interpreter.cpp @@ -0,0 +1,60 @@ +// This code made available to his students by +// Prof. Ronald Moore +// https://fbi.h-da.de/personen/ronald-moore/ +// mailto:ronald.moore@h-da.de +// with no warranties whatsoever +// +// The grammar we are going to parse here is: +// Grammar: +// E → T E´ +// E´ → + T E´ | - T E´ | ε +// T → F T´ +// T´ → * F T´ | / F T´ | ε +// F → ( E ) | num +// where the following are taken to be tokens: +// left and right parenthesis, the plus and minus characters, +// as well as asterisk and forward slash -- and numbers. +// In the script, substraction and division are not supported, +// but it seems like time to add them. +// +// Note that the recursive descent function for (e.g.) E´ +// is nameded "E2ndHalf"- + +#include <cstdio> +#include <cstdlib> +#include <string> +#include <vector> + +// Preliminaries and Utilities +// ============================ + +// global variables -- sue me if you don't like that! +std::string currentLine( "" ); +int currentLineNumber = 0; +int currentColumnNumber = 0; +int currentTokenLength = 0; +// Utility Types +typedef double numberType; // feel fee to change this to something else like int or float or bigint.... + +// the tokens +enum Token { + tok_number, + tok_lparen, + tok_rparen, + tok_plus, + tok_minus, + tok_times, + tok_div +} next_token; // again with the global variables... + +// The Lexer +// ========== + + +// main (!) +// ========= + +int main( int argc, char **argv ) { + + return 0; // Alles klar!!! +}