From 971bb7327482f68fad6e8c7a49d7e9353706220e Mon Sep 17 00:00:00 2001 From: gaeltp3 Date: Fri, 22 Nov 2013 09:44:01 +0100 Subject: [PATCH] Wertetabelle --- GDE_3_2008/Cparser.h | 311 ++++++++++++++++++++-- GDE_3_2008/GDE_3.vcxproj | 5 - GDE_3_2008/GDE_3.vcxproj.filters | 15 -- GDE_3_2008/main.cpp | 9 + Hazard/Hazard/CParser.cpp | 10 +- Hazard/Hazard/Cell.h | 2 +- Hazard/Hazard/Hazard.cpp | 23 +- Hazard/Hazard/Hazard.vcxproj | 1 + Hazard/Hazard/Hazard.vcxproj.filters | 3 + Hazard/Hazard/PrimImplikantCollection.cpp | 8 +- Hazard/Hazard/PrimImplikantCollection.h | 4 +- Hazard/Hazard/Wertetabelle.h | 1 - 12 files changed, 329 insertions(+), 63 deletions(-) create mode 100644 GDE_3_2008/main.cpp diff --git a/GDE_3_2008/Cparser.h b/GDE_3_2008/Cparser.h index fbefa86..41bdd79 100644 --- a/GDE_3_2008/Cparser.h +++ b/GDE_3_2008/Cparser.h @@ -1,49 +1,318 @@ -#ifndef CPARSER -#define CPARSER - +#include "stdafx.h" +#pragma warning(disable:4786) #include -#include "PrimImplikanten.h" - +#include using namespace std; +#define Getc(s) getc(s) +#define Ungetc(c) {ungetc(c,IP_Input); ugetflag=1;} -class Cparser{ -private: - PrimImplikant implikant; - +/* + * Lexical analyzer states. + */ +enum lexstate{L_START,L_INT,L_IDENT,L_STRING,L_STRING2, + L_COMMENT,L_TEXT_COMMENT,L_LINE_COMMENT,L_END_TEXT_COMMENT}; + +const int STRING1=3; +const int IDENTIFIER=4; +const int INTEGER1=5; +const int TOKENSTART=300; + +class CParser +{ public: - string yytext; - string yytext1; - int LineNumber; - struct tyylval{ - string s; - int i; + string yytext; //input buffer + struct tyylval{ //value return + string s; //structure + int i; }yylval; + FILE *IP_Input; //Input File + FILE *IP_Error; //Error Output + FILE *IP_List; //List Output + int IP_LineNumber; //Line counter + int ugetflag; //checks ungets + int prflag; //controls printing + map IP_Token_table; //Tokendefinitions + map IP_revToken_table; //reverse Tokendefinitions - FILE* IP_Input; - void pr_tokentable(); - void load_tokenentry(string s ,int x); - int yylex(); - + int CParser::yylex(); //lexial analyser + void CParser::yyerror(char *ers); //error reporter + int CParser::IP_MatchToken(string &tok); //checks the token + void CParser::InitParse(FILE *inp,FILE *err,FILE *lst); + int CParser::yyparse(); //parser + void CParser::pr_tokentable(); //test output for tokens + void CParser::IP_init_token_table(); //loads the tokens + void CParser::Load_tokenentry(string str,int index);//load one token + void CParser::PushString(char c); //Used for dtring assembly + CParser(){IP_LineNumber = 1;ugetflag=0;prflag=0;}; //Constructor }; +//------------------------------------------------------------------------ +// Adds a character to the string value +void CParser::PushString(char c) +{ + yylval.s += c; +} +//------------------------------------------------------------------------ +void CParser::Load_tokenentry(string str,int index) +{ + IP_Token_table[str]=index; + IP_revToken_table[index]=str; +} +void CParser::IP_init_token_table() +{ + Load_tokenentry("STRING1",STRING1); + Load_tokenentry("IDENTIFIER",IDENTIFIER); + Load_tokenentry("INTEGER1",INTEGER1); + int ii=TOKENSTART; + Load_tokenentry("Variables",ii++); + Load_tokenentry("Terms",ii++); +} +//------------------------------------------------------------------------ +void CParser::pr_tokentable() +{ + + typedef map::const_iterator CI; + const char* buf; + printf( "Symbol Table ---------------------------------------------\n"); + for(CI p=IP_Token_table.begin(); p!=IP_Token_table.end(); ++p){ + buf = p->first.c_str(); + printf(" key:%s", buf); + printf(" val:%d\n", p->second);; + } +} +//------------------------------------------------------------------------ +int CParser::yyparse() +{ + int tok; + if(prflag)fprintf(IP_List,"%5d ",(int)IP_LineNumber); + /* + * Go parse things! + */ + while ((tok=yylex())!=0){ + printf("%d ",tok); + if(tok==STRING1) + printf("%s %s ",IP_revToken_table[tok].c_str(),yylval.s.c_str()); + else + if(tok==INTEGER1) + printf("%s %d ",IP_revToken_table[tok].c_str(),yylval.i); + else + if(tok==IDENTIFIER) + printf("%s %s ",IP_revToken_table[tok].c_str(),yylval.s.c_str()); + else + if(tok>=TOKENSTART) + printf("%s ",IP_revToken_table[tok].c_str()); + else + printf("%c ",tok); + if(!prflag)printf("\n"); + } + return 0; +} +//------------------------------------------------------------------------ +/* + * Parse Initfile: + * + * This builds the context tree and then calls the real parser. + * It is passed two file streams, the first is where the input comes + * from; the second is where error messages get printed. + */ +void CParser::InitParse(FILE *inp,FILE *err,FILE *lst) +{ + /* + * Set up the file state to something useful. + */ + IP_Input = inp; + IP_Error = err; + IP_List = lst; + IP_LineNumber = 1; + ugetflag=0; + /* + * Define both the enabled token and keyword strings. + */ + IP_init_token_table(); +} +//------------------------------------------------------------------------ +/* + * yyerror: + * + * Standard error reporter, it prints out the passed string + * preceeded by the current filename and line number. + */ +void CParser::yyerror(char *ers) +{ + fprintf(IP_Error,"line %d: %s\n",IP_LineNumber,ers); +} +//------------------------------------------------------------------------ +int CParser::IP_MatchToken(string &tok) +{ + int retval; + if( IP_Token_table.find(tok) != IP_Token_table.end()){ + retval = (IP_Token_table[tok]); + }else{ + retval = (0); + } + return retval; +} +//------------------------------------------------------------------------ +/* + * yylex: + * + */ +int CParser::yylex() +{ + //Locals + int c; + lexstate s; + /* + * Keep on sucking up characters until we find something which + * explicitly forces us out of this function. + */ + for (s = L_START; 1;){ + c = Getc(IP_Input); + yytext = yytext + (char)c; + if(!ugetflag) { + if(c != EOF)if(prflag)fprintf(IP_List,"%c",c); + }else ugetflag = 0; + switch (s){ + //Starting state, look for something resembling a token. + case L_START: + if (isdigit(c)){ + s = L_INT; + }else if (isalpha(c) || c == '\\' ){ + s = L_IDENT; + }else if (isspace(c)){ + if (c == '\n'){ + IP_LineNumber += 1; + if(prflag) + fprintf(IP_List,"%5d ",(int)IP_LineNumber); + } + yytext = ""; + }else if (c == '/'){ + yytext = ""; + s = L_COMMENT; + }else if (c == '"'){ + s = L_STRING; + }else if (c == EOF){ + return ('\0'); + }else{ + return (c); + } + break; -#endif + case L_COMMENT: + if (c == '/') + s = L_LINE_COMMENT; + else if(c == '*') + s = L_TEXT_COMMENT; + else{ + Ungetc(c); + return('/'); /* its the division operator not a comment */ + } + break; + case L_LINE_COMMENT: + if ( c == '\n'){ + s = L_START; + Ungetc(c); + } + yytext = ""; + break; + case L_TEXT_COMMENT: + if ( c == '\n'){ + IP_LineNumber += 1; + }else if (c == '*') + s = L_END_TEXT_COMMENT; + yytext = ""; + break; + case L_END_TEXT_COMMENT: + if (c == '/'){ + s = L_START; + }else{ + s = L_TEXT_COMMENT; + } + yytext = ""; + break; + + /* + * Suck up the integer digits. + */ + case L_INT: + if (isdigit(c)){ + break; + }else { + Ungetc(c); + yylval.s = yytext.substr(0,yytext.size()-1); + yylval.i = atoi(yylval.s.c_str()); + return (INTEGER1); + } + break; + + /* + * Grab an identifier, see if the current context enables + * it with a specific token value. + */ + + case L_IDENT: + if (isalpha(c) || isdigit(c) || c == '_') + break; + Ungetc(c); + yytext = yytext.substr(0,yytext.size()-1); + yylval.s = yytext; + if (c = IP_MatchToken(yytext)){ + return (c); + }else{ + return (IDENTIFIER); + } + + /* + * Suck up string characters but once resolved they should + * be deposited in the string bucket because they can be + * arbitrarily long. + */ + case L_STRING2: + s = L_STRING; + if(c == '"'){ + PushString((char)c); + }else{ + if(c == '\\'){ + PushString((char)c); + }else{ + PushString((char)'\\'); + PushString((char)c); + } + } + break; + case L_STRING: + if (c == '\n') + IP_LineNumber += 1; + else if (c == '\r') + ; + else if (c == '"' || c == EOF){ + return (STRING1); + } else if(c=='\\'){ + s = L_STRING2; + //PushString((char)c); + }else + PushString((char)c); + break; + default: printf("***Fatal Error*** Wrong case label in yylex\n"); + } + } +} \ No newline at end of file diff --git a/GDE_3_2008/GDE_3.vcxproj b/GDE_3_2008/GDE_3.vcxproj index 2d36a10..0231512 100644 --- a/GDE_3_2008/GDE_3.vcxproj +++ b/GDE_3_2008/GDE_3.vcxproj @@ -111,12 +111,10 @@ - - Create Create @@ -128,13 +126,10 @@ - - - diff --git a/GDE_3_2008/GDE_3.vcxproj.filters b/GDE_3_2008/GDE_3.vcxproj.filters index 608a703..a75e1db 100644 --- a/GDE_3_2008/GDE_3.vcxproj.filters +++ b/GDE_3_2008/GDE_3.vcxproj.filters @@ -51,12 +51,6 @@ Console - - Quelldateien - - - Quelldateien - @@ -101,15 +95,6 @@ Console - - Headerdateien - - - Headerdateien - - - Headerdateien - diff --git a/GDE_3_2008/main.cpp b/GDE_3_2008/main.cpp new file mode 100644 index 0000000..cbf8f66 --- /dev/null +++ b/GDE_3_2008/main.cpp @@ -0,0 +1,9 @@ +#include +#include + +using namespace std; + +void user_main() +{ + cout << "Hello"; +} \ No newline at end of file diff --git a/Hazard/Hazard/CParser.cpp b/Hazard/Hazard/CParser.cpp index 3b42295..0fed8a0 100644 --- a/Hazard/Hazard/CParser.cpp +++ b/Hazard/Hazard/CParser.cpp @@ -8,9 +8,9 @@ using namespace std; #define Getc(s) getc(s) #define Ungetc(c) {ungetc(c,IP_Input); ugetflag=1;} -extern unsigned int dimension; -extern unsigned int numElements; -extern bool KNF; + unsigned int dimension; + unsigned int numElements; + bool KNF; // Adds a character to the string value void CParser::PushString(char c) @@ -108,14 +108,14 @@ int CParser::yyparse(PrimImplikantCollection* &pic, vector* &variables) } break; case P_TERMS_VALUE: - if (tok == INTEGER1) + if (tok == INTEGER1) // hier verstehe ich nicht was. { if (!KNFset) { KNF = (yylval.i == 0); KNFset = true; } - else if ((yylval.i == 0) ^ KNF) + else if ((yylval.i == 0) ^ KNF)// ^= EXOR Verpnü { fprintf(IP_Error, "*** FATAL ERROR *** You can only define either KNF or DNF!\n"); fprintf(IP_Error, "In line %3d: %s>%i\n", (int)IP_LineNumber, pic->back()->name.c_str(), yylval.i); diff --git a/Hazard/Hazard/Cell.h b/Hazard/Hazard/Cell.h index b81eab8..bd642c7 100644 --- a/Hazard/Hazard/Cell.h +++ b/Hazard/Hazard/Cell.h @@ -15,7 +15,7 @@ public: vector GetNeighbors(); // returns numElements Cells vector GetHazards(); // returns the neighbor Cells which are hazardous - Cell(unsigned int index, PrimImplikantCollection* &globalPIC) + Cell(unsigned int index, PrimImplikantCollection* &globalPIC) { this->index = index; this->primImplikanten = globalPIC->primImplikantenAt(index); diff --git a/Hazard/Hazard/Hazard.cpp b/Hazard/Hazard/Hazard.cpp index 5da206e..81a5a0f 100644 --- a/Hazard/Hazard/Hazard.cpp +++ b/Hazard/Hazard/Hazard.cpp @@ -9,12 +9,13 @@ #include "PrimImplikant.h" #include "PrimImplikantCollection.h" #include "Cell.h" +#include "Wertetabelle.h" using namespace std; -unsigned int dimension = 0; // = variables.size() -unsigned int numElements = 0; // = 2 ^ dimension -bool KNF = false; +extern unsigned int dimension = 0; // = variables.size() +extern unsigned int numElements = 0; // = 2 ^ dimension +extern bool KNF = false; int _tmain(int argc, _TCHAR* argv[]) { @@ -43,7 +44,7 @@ int _tmain(int argc, _TCHAR* argv[]) return -1; } - PrimImplikantCollection* globalPIC = new PrimImplikantCollection(); + PrimImplikantCollection* globalPIC = new PrimImplikantCollection();// Objektzeiger vom Typ PIC vector* variables = new vector(); CParser parser; @@ -55,6 +56,8 @@ int _tmain(int argc, _TCHAR* argv[]) system("pause"); return 1; } + + Wertetabelle objekt(globalPIC); system("pause"); /*pic.add(7); @@ -80,7 +83,7 @@ int _tmain(int argc, _TCHAR* argv[]) cout << endl; }*/ - + /* // initialize Cells vector cells; cells.resize(numElements); @@ -93,11 +96,11 @@ int _tmain(int argc, _TCHAR* argv[]) numOnes++; } - + // find hazards if (numOnes > numElements / 2) // we have more 1 than 0 --> checkerboard --> 50% of cells are checked { - for (unsigned int i = 0; i < numElements; i++) + for (unsigned int i = 0; i < numElements; i += 2) { cout << "\nSchachbrettmuster\n"; unsigned int grayI = i ^ (i/2); // transform to gray code @@ -118,7 +121,7 @@ int _tmain(int argc, _TCHAR* argv[]) for (unsigned int i = 0; i < numElements; i++) { cout << "\nÜberspringe Nullen\n"; - if (!cells[i]->value) + if (cells[i]->value == 0) continue; vector hazardousNeighbors = cells[i]->GetHazards(); @@ -131,7 +134,7 @@ int _tmain(int argc, _TCHAR* argv[]) globalPIC->add(i, (*c)->index); // add PI that solves hazard. Not quite smart... } } - } - system("pause"); + } + system("pause");*/ return 0; } \ No newline at end of file diff --git a/Hazard/Hazard/Hazard.vcxproj b/Hazard/Hazard/Hazard.vcxproj index bb18d5a..87116ab 100644 --- a/Hazard/Hazard/Hazard.vcxproj +++ b/Hazard/Hazard/Hazard.vcxproj @@ -85,6 +85,7 @@ + diff --git a/Hazard/Hazard/Hazard.vcxproj.filters b/Hazard/Hazard/Hazard.vcxproj.filters index e1340cd..ac7da1f 100644 --- a/Hazard/Hazard/Hazard.vcxproj.filters +++ b/Hazard/Hazard/Hazard.vcxproj.filters @@ -36,6 +36,9 @@ Headerdateien + + Headerdateien + diff --git a/Hazard/Hazard/PrimImplikantCollection.cpp b/Hazard/Hazard/PrimImplikantCollection.cpp index 0bd57ba..f610458 100644 --- a/Hazard/Hazard/PrimImplikantCollection.cpp +++ b/Hazard/Hazard/PrimImplikantCollection.cpp @@ -1,4 +1,4 @@ -//#include +#include #include "stdafx.h" #include #include @@ -63,12 +63,12 @@ PrimImplikant* PrimImplikantCollection::at(unsigned int const &index) return this->PIVector.at(index); } -/*PrimImplikant* PrimImplikantCollection::operator[](unsigned int const &index){ - if (index <= PIVector.size()){ +PrimImplikant* PrimImplikantCollection::operator[](unsigned int const &index){ + if (index < PIVector.size()){ return this->PIVector.at(index); } cerr << "Fehler!!!! PIVector.size()=" << PIVector.size() << endl; return 0; -}*/ \ No newline at end of file +} \ No newline at end of file diff --git a/Hazard/Hazard/PrimImplikantCollection.h b/Hazard/Hazard/PrimImplikantCollection.h index 0b4c474..b3cbc8c 100644 --- a/Hazard/Hazard/PrimImplikantCollection.h +++ b/Hazard/Hazard/PrimImplikantCollection.h @@ -1,4 +1,4 @@ -//#include +#include #include #include #include "PrimImplikant.h" @@ -24,6 +24,8 @@ public: PrimImplikant* at(unsigned int const &index); PrimImplikant* operator[](unsigned int const &index); const PrimImplikant* operator[](unsigned int const &index) const; + + //friend int Wertetabelle::print(PrimImplikantCollection*&); ~PrimImplikantCollection() // destructor { diff --git a/Hazard/Hazard/Wertetabelle.h b/Hazard/Hazard/Wertetabelle.h index a5b3b7b..75a1d3b 100644 --- a/Hazard/Hazard/Wertetabelle.h +++ b/Hazard/Hazard/Wertetabelle.h @@ -1,7 +1,6 @@ #include #include #include "PrimImplikantCollection.h" -#include "Cparser.h" #include using namespace std;