Changed CParser so it imports to PIC

Der CParser bekommt nun einen Verweis auf eine
PrimImplikantenCollection, so dass er die gelesenen Werte gleich da
reinschieben kann.
Außerdem setzt er noch die globalen Variablen.
This commit is contained in:
Jonny007-MKD 2013-11-12 21:59:46 +01:00
parent 6e27485b08
commit 070e2cba9e
9 changed files with 67 additions and 30 deletions

View File

@ -8,7 +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;
// Adds a character to the string value
void CParser::PushString(char c)
@ -48,14 +50,18 @@ void CParser::pr_tokentable()
}
//------------------------------------------------------------------------
int CParser::yyparse()
int CParser::yyparse(PrimImplikantCollection* &pic, vector<string>* &variables)
{
bool KNFset = false;
int tok;
if(prflag)fprintf(IP_List,"%5d ",(int)IP_LineNumber);
/*
* Go parse things!
*/
parsestate pState = P_START;
while ((tok=yylex())!=0){
switch (pState)
{
@ -74,7 +80,8 @@ int CParser::yyparse()
switch(tok)
{
case IDENTIFIER:
printf("Variable %s",yylval.s.c_str());
printf("Variable %s\n", yylval.s.c_str());
variables->push_back(yylval.s.c_str());
break;
case TERMS:
pState = P_TERMS_KEY;
@ -85,10 +92,12 @@ int CParser::yyparse()
switch(tok)
{
case STRING1:
printf("Term Key %s",yylval.s.c_str());
printf("Term Key %s\n", yylval.s.c_str());
pic->add(yylval.s.c_str());
break;
case INTEGER1:
printf("Term Key %d",yylval.i);
printf("Term Key %d\n",yylval.i);
pic->add(yylval.i);
break;
case (int)'>':
pState = P_TERMS_VALUE;
@ -101,13 +110,23 @@ int CParser::yyparse()
case P_TERMS_VALUE:
if (tok == INTEGER1)
{
printf("Term Value %d",yylval.i);
if (!KNFset)
KNF = (yylval.i == 0);
else if ((yylval.i == 0) ^ KNF)
{
printf("*** FATAL ERROR *** You can only define either KNF or DNF!\n");
return 1;
}
printf("Term Value %d\n\n",yylval.i);
pState = P_TERMS_KEY;
}
break;
}
printf("\n");
}
dimension = variables->size();
numElements = (unsigned int)pow(2.0f, (int)dimension);
return 0;
}

View File

@ -5,6 +5,8 @@
#pragma warning(disable:4786)
#include <string>
#include <map>
#include "PrimImplikantCollection.h"
using namespace std;
#define Getc(s) getc(s)
@ -43,11 +45,11 @@ public:
map<int,string> IP_revToken_table; //reverse Tokendefinitions
int CParser::yyparse(PrimImplikantCollection* &pic, vector<string>* &variables); //parser
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

View File

@ -8,27 +8,34 @@
#include "CParser.h"
#include "PrimImplikant.h"
#include "PrimImplikantCollection.h"
#include "Cell.h"
using namespace std;
unsigned int dimension = 0;
unsigned int numElements = 0;
bool KNF = false;
int _tmain(int argc, _TCHAR* argv[])
{
FILE * input;
FILE * error;
FILE * list;
fopen_s(&input, "input.txt", "r");
fopen_s(&input, "..\\res\\input.txt", "r");
if (input == 0)
{
cout << "Fehler Inputdatei";
system("pause");
return -1;
}
fopen_s(&error, "error.txt", "w");
fopen_s(&error, "..\\res\\error.txt", "w");
if (error == 0)
{
cout << "Fehler Fehlerdatei";
system("pause");
return -1;
}
fopen_s(&list, "list.txt", "w");
fopen_s(&list, "..\\res\\list.txt", "w");
if (list == 0)
{
cout << "Fehler Listdatei";
@ -36,15 +43,18 @@ int _tmain(int argc, _TCHAR* argv[])
return -1;
}
PrimImplikantCollection* globalPIC = new PrimImplikantCollection();
vector<string>* variables = new vector<string>();
CParser parser;
parser.IP_init_token_table();
parser.pr_tokentable();
parser.InitParse(input, error, list);
parser.yyparse();
if (parser.yyparse(globalPIC, variables) != 0)
return 1;
system("pause");
PrimImplikantCollection pic;
pic.add(7);
/*pic.add(7);
pic.add("0x1");
pic.add("100");
pic.add("00x");
@ -54,19 +64,26 @@ int _tmain(int argc, _TCHAR* argv[])
PrimImplikant prim13("0x1");
PrimImplikant prim4("100");
PrimImplikant prim4567("1xx");
pic.add(prim4567);
pic.add(prim4567);*/
for (int p = 0; p < 8; p++)
/*for (int p = 0; p < numElements; p++)
{
//printf("Pos %d: prim7=%d, prim13=%d, prim4=%d, prim4567=%d, pic=%d\n", p, prim7.valueAt(p), prim13.valueAt(p), prim4.valueAt(p), prim4567.valueAt(p), pic.valueAt(p));
printf("Pos %d: Matching collections: ", p);
vector<PrimImplikant> matchingPIs = pic.primImplikantenAt(p);
for (vector<PrimImplikant>::iterator i = matchingPIs.begin(); i < matchingPIs.end(); i++)
PrimImplikantCollection matchingPIs = globalPIC->primImplikantenAt(p);
for (int i = 0; i < matchingPIs.size(); i++)
//cout << i->name < ", ";
printf("%s, ", i->name.c_str());
printf("%s, ", matchingPIs[i]->name.c_str());
cout << endl;
}
}*/
vector<Cell*> cells;
cells.resize(numElements);
for (int i = 0; i < numElements; i++)
{
cells[i] = new Cell(i, globalPIC);
printf("Pos %2d: %d\n", i, cells[i]->value);
}
system("pause");
return 0;

View File

@ -79,7 +79,8 @@
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Cparser.h" />
<ClInclude Include="Cell.h" />
<ClInclude Include="CParser.h" />
<ClInclude Include="PrimImplikant.h" />
<ClInclude Include="PrimImplikantCollection.h" />
<ClInclude Include="stdafx.h" />

View File

@ -30,7 +30,10 @@
<ClInclude Include="PrimImplikantCollection.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="Cparser.h">
<ClInclude Include="Cell.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="CParser.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>

View File

@ -1,6 +0,0 @@
Variables: a, b, c, d
Terms:
"0010">1
7>1
1234>1
"0123">1

View File

@ -2,5 +2,6 @@ Variables: a, b, c, d
Terms:
"0010">1
7>1
1234>1
"0123">1
14>1
"01xx">1
"x000">1