Compare commits

...

2 commits

Author SHA1 Message Date
gaeltp3 971bb73274 Wertetabelle 2013-11-22 09:44:01 +01:00
gaeltp3 1d9e56c7db Headerdatei Wertetabelle :Ausgabe der Werte 2013-11-21 22:01:36 +01:00
12 changed files with 371 additions and 62 deletions

View file

@ -1,49 +1,318 @@
#ifndef CPARSER
#define CPARSER
#include "stdafx.h"
#pragma warning(disable:4786)
#include <string>
#include "PrimImplikanten.h"
#include <map>
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<string,int> IP_Token_table; //Tokendefinitions
map<int,string> 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<string,int>::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");
}
}
}

View file

@ -111,12 +111,10 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Cparser.cpp" />
<ClCompile Include="GDE_3.cpp" />
<ClCompile Include="GDE_3Doc.cpp" />
<ClCompile Include="GDE_3View.cpp" />
<ClCompile Include="MainFrm.cpp" />
<ClCompile Include="PrimImplikant.cpp" />
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
@ -128,13 +126,10 @@
<ClCompile Include="Console\Console.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Cparser.h" />
<ClInclude Include="GDE_3.h" />
<ClInclude Include="GDE_3Doc.h" />
<ClInclude Include="GDE_3View.h" />
<ClInclude Include="MainFrm.h" />
<ClInclude Include="PrimImplikantCollection.h" />
<ClInclude Include="PrimImplikanten.h" />
<ClInclude Include="Resource.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="User.h" />

View file

@ -51,12 +51,6 @@
<ClCompile Include="Console\Console.cpp">
<Filter>Console</Filter>
</ClCompile>
<ClCompile Include="PrimImplikant.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="Cparser.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="GDE_3.h">
@ -101,15 +95,6 @@
<ClInclude Include="Console\Console.h">
<Filter>Console</Filter>
</ClInclude>
<ClInclude Include="PrimImplikantCollection.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="PrimImplikanten.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="Cparser.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="res\GDE_3.ico">

9
GDE_3_2008/main.cpp Normal file
View file

@ -0,0 +1,9 @@
#include <string>
#include <iostream>
using namespace std;
void user_main()
{
cout << "Hello";
}

View file

@ -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<string>* &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);

View file

@ -15,7 +15,7 @@ public:
vector<Cell*> GetNeighbors(); // returns numElements Cells
vector<Cell*> 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);

View file

@ -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<string>* variables = new vector<string>();
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<Cell*> 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<Cell*> 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;
}

View file

@ -85,6 +85,7 @@
<ClInclude Include="PrimImplikantCollection.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
<ClInclude Include="Wertetabelle.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="CParser.cpp" />

View file

@ -36,6 +36,9 @@
<ClInclude Include="CParser.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="Wertetabelle.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">

View file

@ -1,4 +1,4 @@
//#include <iostream>
#include <iostream>
#include "stdafx.h"
#include <string>
#include <vector>
@ -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;
}*/
}

View file

@ -1,4 +1,4 @@
//#include <iostream>
#include <iostream>
#include <string>
#include <vector>
#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
{

View file

@ -0,0 +1,42 @@
#include <string>
#include <vector>
#include "PrimImplikantCollection.h"
#include <iostream>
using namespace std;
unsigned int numElements ;
bool KNF ;
#ifndef WERTETABELLE
#define WERTETABELLE
class Wertetabelle{
public:
int print(PrimImplikantCollection* &PIC){
cout << "KNF=" << KNF << endl;
cout << "Dezimalwerte\t|\tAusgangWerte(y)" << endl;
for (unsigned int i = 0; i < numElements; i++){
cout << "\t" << i << "\t\t|\t" << PIC->valueAt(i) << endl;
}
}
Wertetabelle(PrimImplikantCollection* &PIC){ // Konstruktor
print(PIC);
}
};
#endif