From 0f3fa0ffee0f6c03b9eeb56ec29f35409d4ca754 Mon Sep 17 00:00:00 2001 From: "Prof. Ronald Moore" <ronald.moore@h-da.de> Date: Thu, 30 Apr 2020 20:07:40 +0200 Subject: [PATCH] Skeleton --- .../cplusplus/interpreter/Makefile | 36 +++++++++++ .../cplusplus/interpreter/README.md | 25 ++++++++ .../cplusplus/interpreter/interpreter | Bin 0 -> 17600 bytes .../cplusplus/interpreter/interpreter.cpp | 60 ++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 recursiveDescentParsers/cplusplus/interpreter/Makefile create mode 100644 recursiveDescentParsers/cplusplus/interpreter/README.md create mode 100755 recursiveDescentParsers/cplusplus/interpreter/interpreter create mode 100644 recursiveDescentParsers/cplusplus/interpreter/interpreter.cpp diff --git a/recursiveDescentParsers/cplusplus/interpreter/Makefile b/recursiveDescentParsers/cplusplus/interpreter/Makefile new file mode 100644 index 0000000..fd54984 --- /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 0000000..018defe --- /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 GIT binary patch literal 17600 zcmb<-^>JfjWMqH=W(GS35O0D2M8p9?F*w*l84L^z4h$9y+zbv3atyKzYzzzxEMPH+ zJX}45%aFkYVZdk(2$z8ws&55U9-Wqfs)NxWH-UseG{`;>8yhZwS^%RNp#A{qV+Cn| z@?qjI`YY5OFd8Nf(g(Ir0i>CMfdP%43sS(qzyPC>^?|}>g)T(j3TXJj=p9h|I6wgb zQUY}sNEoE=094-rs6H4i08+rfzyPD6-UY`2$el-^^60b+$PNYu1{e)e3la)=T9N`{ zb3pxtPQ%p0XpmZvzQCs?DIj-(*u-F{_ZWhp_TdVb2&nsEG&Fb_^m8(k%uMuiQgm}N z^GYjpD=f@(%}n%)^Yx6teivb20H-N;zfgvw>Unm*0vy@yS-5_G-G4Vd!+6b8kh?){ z0GS6;19CGc=%Lnu(>zEHgh6t|Vn0R(1|=+tk1{YY2w@R-WW+8WibK5t4)sYm)Gx*% zE{~LSk)0)u!~7jM%!$RJ9u&6FB!p}{gEbC&8FDg{ic3<GwY5QUXvPp9pPrka7hhbG zSX2@p&k*k!;v1imT9l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IoF_yELc+>@2#<lm3{5|*{{*99^&(6?x_;1n z=(qp>^I`6X^|N3!%>A%>3)T*SndisAzyRvw!}P<(0bq0h)We|eGl&V(59^PIf%``Y z|HJxiFd8<F0J9Hd28<1&<3MZ4k=zgK-@#~f_ru%?qq7+p7(inHFg~mw2%}-+5Fq!1 z#spyQ2iXn6Rp9nH!u_yu2pA0;hd>WMnEyem%0Y4B1TqY1Tmv?~0ka>ZA2jv>vmd6v z58MwxjKhE@ra@*w`!AsM3F5*q$SxQgM0cX;hxJooG<Z%Qq!5as#=@<cf~FtVpM}w( zuz*F=59Kn<LemfH_rhq{cos}Oy8A)v8bE0RZVfEKLupXF!SsS?bp0!!_QUkU`q3~N zls`cFU>IHhHZ=XP{x*zu067k(8QLF3;xZgS(+}&H!)SE>qZ!9=iGhKE51NNS=EC|% zu>JY4_yWm+Ff9GR=z9zd4E#v?Vg39a(DMsm>Oguy7{-Uuub^Xqpu7Q+hm8lo&Od;i za{!A6m_C@hK12Nv(+?XrxB%^t7=R2#!Z7_X`UjeR259@30jkjhNiT>A(+8tLM`wV- z6lOnc9Kr*tAC}KRdO;Yb55xvxc1FnFVwir|ctrwKe*j1k5{4#AxCjrp|H=S!E?gX@ zA6-4jtsq6nG%W3c#9_DrYX1!A04b;(G(<{&Fn@u@M`3P<=>^de79f&=fgdfw!|a8~ Xg8C$&UEVMrf>yVIutm`{pm7-hj|}d8 literal 0 HcmV?d00001 diff --git a/recursiveDescentParsers/cplusplus/interpreter/interpreter.cpp b/recursiveDescentParsers/cplusplus/interpreter/interpreter.cpp new file mode 100644 index 0000000..f954cb5 --- /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!!! +} -- GitLab