9 #ifndef FAUDES_PD_PDGENERATOR_H
10 #define FAUDES_PD_PDGENERATOR_H
11 #define FAUDES_DEBUG_GENERATOR
12 #define FAUDES_DEBUG_CONTAINER
36 template <
class GlobalAttr,
class StateAttr,
class EventAttr,
class TransAttr>
377 StackSymbolSet::Iterator
FindStackSymbol(
const std::string& rName)
const;
452 bool SetTransition(
const std::string& rX1,
const std::string& rEv,
453 const std::string& rX2);
485 bool SetTransition(
const Transition& rTrans,
const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush);
522 const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush);
542 const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush);
561 bool SetTransition(
const std::string& rX1,
const std::string& rEv,
const std::string& rX2,
const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush);
580 bool SetTransition(
const std::string& rX1,
const std::string& rEv,
const std::string& rX2,
const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush);
599 const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush);
618 const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush);
634 const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush);
649 const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush);
820 virtual bool Valid(
void);
825 typedef TpdGenerator<AttributePushdownGlobal, AttributePushdownState, AttributeCFlags,AttributePushdownTransition>
832 #define THIS TpdGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
833 #define BASE TcGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
834 #define TEMP template <class GlobalAttr, class StateAttr, class EventAttr, class TransAttr>
839 BASE::pGlobalAttribute->mStackSymbols.Name(
"StackSymbols");
841 FD_DG(
"PushdownGenerator(" <<
this <<
")::PushdownGenerator() with csymtab "
842 << (BASE::pGlobalAttribute->mpStackSymbolTable ));
849 FD_DG(
"PushdownGenerator(" <<
this <<
")::PushdownGenerator(rOtherGen) with csymtab"
850 <<
"(BASE::pGlobalAttribute->mpStackSymbolTable)" );
856 BASE::pGlobalAttribute->mStackSymbols.Name(
"StackSymbols");
858 FD_DG(
"PushdownGenerator(" <<
this <<
")::PushdownGenerator(rOtherGen) with csymtab"
859 <<
"(BASE::pGlobalAttribute->mpStackSymbolTable)" );
863 TEMP THIS::TpdGenerator(
const std::string& rFileName) :
BASE(rFileName) {
864 FD_DG(
"PushdownGenerator(" <<
this <<
")::PushdownGenerator(" << rFileName <<
") with csymtab"
865 <<
"(BASE::pGlobalAttribute->mpStackSymbolTable)" );
875 return BASE::pGlobalAttribute->mpStackSymbolTable;
881 BASE::pGlobalAttribute->mpStackSymbolTable=pSymTab;
889 res->EventSymbolTablep(BASE::mpEventSymbolTable);
890 res->mStateNamesEnabled=BASE::mStateNamesEnabled;
891 res->mReindexOnWrite=BASE::mReindexOnWrite;
893 res->StackSymbolTablep(StackSymbolTablep());
908 THIS res= BASE::NewCGen();
910 res.StackSymbolTablep(StackSymbolTablep());
916 return BASE::pGlobalAttribute->mStackSymbols.Size();
921 return BASE::pGlobalAttribute->mStackSymbols;
926 return &BASE::pGlobalAttribute->mStackSymbols;
931 BASE::pGlobalAttribute->mStackSymbols=newstacksymbols;
932 BASE::pGlobalAttribute->mStackSymbols.
Name(
"StackSymbols");
936 TEMP std::string THIS::StackSymbolName(
Idx index)
const {
937 return BASE::pGlobalAttribute->mStackSymbols.SymbolicName(index);
942 return StackSymbol(BASE::pGlobalAttribute->mStackSymbols.SymbolicName(index));
946 TEMP Idx THIS::StackSymbolIndex(
const std::string& rName)
const {
947 return BASE::pGlobalAttribute->mStackSymbols.Index(rName);
951 TEMP Idx THIS::InsStackSymbol(
const std::string& rName) {
952 return BASE::pGlobalAttribute->mStackSymbols.Insert(rName);
957 return BASE::pGlobalAttribute->mStackSymbols.Insert(rSymbol.
Symbol());
962 BASE::pGlobalAttribute->mStackSymbols.InsertSet(rStackSymbolSet);
966 TEMP Idx THIS::SetStackBottom(
const std::string& rName) {
967 Idx i = BASE::pGlobalAttribute->mStackSymbols.Insert(rName);
968 BASE::pGlobalAttribute->mStackBottom = i;
974 Idx i = BASE::pGlobalAttribute->mStackSymbols.Insert(rSymbol.
Symbol());
975 BASE::pGlobalAttribute->mStackBottom = i;
981 if(!ExistsStackSymbol(idx)) {
982 std::stringstream errstr;
983 errstr <<
"stack symbol with index " << idx <<
" not found in generator. " << std::endl;
984 throw Exception(
"PushdownGenerator::SetStackBottom(idx)", errstr.str(), 200);
986 BASE::pGlobalAttribute->mStackBottom =
StackSymbol(StackSymbolName(idx));
992 return BASE::pGlobalAttribute->mStackBottom;
997 FD_DG(
"PushdownGenerator(" <<
this <<
")::DelStackSymbol(" << index <<
")");
998 return BASE::pGlobalAttribute->mStackSymbols.Erase(index);
1002 TEMP bool THIS::DelStackSymbol(
const std::string& rName) {
1003 Idx index=BASE::pGlobalAttribute->mStackSymbols.Index(rName);
1004 return DelStackSymbol(index);
1009 StackSymbolSet::Iterator it=StackSymbolsBegin();
1010 while(it!=StackSymbolsEnd()){
1011 DelStackSymbol(*(it++));
1016 TEMP bool THIS::ExistsStackSymbol(
Idx index)
const {
1017 return BASE::pGlobalAttribute->mStackSymbols.Exists(index);
1022 const std::string& rName)
const {
1023 return BASE::pGlobalAttribute->mStackSymbols.Exists(rName);
1027 TEMP StackSymbolSet::Iterator THIS::FindStackSymbol(
Idx index)
const {
1028 return BASE::pGlobalAttribute->mStackSymbols.Find(index);
1032 TEMP StackSymbolSet::Iterator THIS::FindStackSymbol(
const std::string& rName)
const {
1033 return BASE::pGlobalAttribute->mStackSymbols.Find(rName);
1037 TEMP StackSymbolSet::Iterator THIS::StackSymbolsBegin(
void)
const {
1038 return BASE::pGlobalAttribute->mStackSymbols.Begin();
1042 TEMP StackSymbolSet::Iterator THIS::StackSymbolsEnd(
void)
const {
1043 return BASE::pGlobalAttribute->mStackSymbols.End();
1046 TEMP std::vector<Idx> THIS::StackSymbolsToIndices(
const std::vector<StackSymbol> symbols)
const{
1047 std::vector<StackSymbol>::const_iterator ssit;
1048 std::vector<Idx> rV;
1049 for(ssit = symbols.begin(); ssit != symbols.end(); ssit++){
1050 rV.push_back(StackSymbolIndex(ssit->Symbol()));
1059 for(transit = BASE::TransRelBegin(); transit != BASE::TransRelEnd(); transit++){
1060 s += PopPush(*transit).size();
1066 TEMP bool THIS::SetTransition(
const std::string& rX1,
const std::string& rEv,
const std::string& rX2) {
1067 return BASE::SetTransition(rX1,rEv,rX2);
1073 return BASE::SetTransition(
Transition(x1,ev,x2));
1077 TEMP bool THIS::SetTransition(
const Transition& rTransition,
const TransAttr& rAttr) {
1078 return BASE::SetTransition(rTransition,rAttr);
1084 const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush) {
1107 return SetTransition(rTrans, StackSymbolsToIndices(rPop), StackSymbolsToIndices(rPush));
1111 const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush) {
1112 FD_DG(
"PushdownGenerator(" <<
this <<
")::SetTransition(" << (BASE::TStr(rTrans)) <<
", " <<
1113 ", PopVector" <<
", " <<
"PushVector" <<
") const");
1114 #ifdef FAUDES_CHECKED
1115 EmptyVectorPopPush(rPop);
1116 EmptyVectorPopPush(rPush);
1118 BASE::SetTransition(rTrans);
1121 if(BASE::TransAttributep(rTrans) != 0){
1122 attr = *BASE::TransAttributep(rTrans);
1124 std::pair<std::vector<Idx>, std::vector<Idx> > popPushPair;
1125 popPushPair.first = rPop;
1126 popPushPair.second = rPush;
1128 attr.mPopPush.insert(popPushPair);
1129 #ifdef FAUDES_CHECKED
1130 ConsistentVectorStackSymbol(rPop);
1131 ConsistentVectorStackSymbol(rPush);
1133 return BASE::SetTransition(rTrans,attr);
1138 const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush) {
1139 return SetTransition(
Transition(x1,ev,x2),rPop,rPush);
1144 const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush) {
1145 return SetTransition(
Transition(x1,ev,x2),rPop,rPush);
1150 const std::string& rX1,
const std::string& rEv,
const std::string& rX2,
1151 const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush) {
1152 FD_DG(
"PushdownGenerator(" <<
this <<
")::SetTransition(" << rX1 <<
" " << rEv <<
" " << rX2 <<
1153 ", PopVector" <<
", " <<
"PushVector" <<
") const");
1155 bool res=BASE::SetTransition(rX1,rEv,rX2);
1157 Transition rTrans = *(BASE::FindTransition(rX1,rEv,rX2));
1158 #ifdef FAUDES_CHECKED
1159 EmptyVectorPopPush(rPop);
1160 EmptyVectorPopPush(rPush);
1164 if(BASE::TransAttributep(rTrans) != 0){
1165 attr = *BASE::TransAttributep(rTrans);
1167 std::pair<std::vector<Idx>, std::vector<Idx> > popPushPair;
1168 popPushPair.first = StackSymbolsToIndices(rPop);
1169 popPushPair.second = StackSymbolsToIndices(rPush);
1171 attr.mPopPush.insert(popPushPair);
1172 #ifdef FAUDES_CHECKED
1173 ConsistentVectorStackSymbol(rPop);
1174 ConsistentVectorStackSymbol(rPush);
1176 BASE::TransAttribute(
Transition(BASE::StateIndex(rX1),BASE::EventIndex(rEv),BASE::StateIndex(rX2)),attr);
1182 const std::string& rX1,
const std::string& rEv,
const std::string& rX2,
1183 const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush) {
1185 Transition(BASE::StateIndex(rX1),BASE::EventIndex(rEv),BASE::StateIndex(rX2));
1186 return SetTransition(
Transition(BASE::StateIndex(rX1),BASE::EventIndex(rEv),BASE::StateIndex(rX2)),rPop,rPush);
1191 const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush){
1192 return ClrTransition(
Transition(x1,ev,x2),rPop,rPush);
1197 const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush){
1198 return ClrTransition(
Transition(x1,ev,x2),rPop,rPush);
1202 TEMP bool THIS::ClrTransition(
const Transition& rTrans,
const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush){
1219 return ClrTransition(rTrans,StackSymbolsToIndices(rPop),StackSymbolsToIndices(rPush));
1223 TEMP bool THIS::ClrTransition(
const Transition& rTrans,
const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush){
1226 if(!BASE::ExistsTransition(rTrans)){
1231 if(!BASE::pTransRel->Attributep(rTrans)->ClrPopPush(rPop, rPush)){
1236 if(PopPush(rTrans).empty()){
1237 BASE::ClrTransition(rTrans);
1244 #ifdef FAUDES_CHECKED
1245 if(!BASE::ExistsTransition(rTrans)) {
1246 std::stringstream errstr;
1247 errstr <<
"transition " << BASE::TStr(rTrans) <<
" not found " << std::endl;
1248 throw Exception(
"PushdownGenerator::PopPush(trans)", errstr.str(), 200);
1251 return BASE::pTransRel->Attribute(rTrans).PopPush();
1256 return BASE::pTransRel->Attribute(rTrans).PopPush().begin();
1261 return BASE::pTransRel->Attribute(rTrans).PopPush().end();
1265 TEMP bool THIS::IsStackSymbolLambda(
Idx index)
const{
1283 FD_DG(
"PushdownGenerator(" <<
this <<
")::ConsistentStackSymbol(rStackSymbol)");
1284 if(!StackSymbols().Exists(rStackSymbol.
Symbol())) {
1285 std::stringstream errstr;
1286 errstr <<
"stack symbol table mismatch (symbol " << rStackSymbol.
mSymbol <<
" does not exist)" << std::endl;
1287 throw Exception(
"PushdownGenerator::ConsistentStackSymbol", errstr.str(), 1001);
1289 FD_DG(
"PushdownGenerator(" <<
this <<
")::ConsistentStackSymbol(rStackSymbol): ok");
1293 TEMP void THIS::ConsistentStackSymbol(
Idx idx)
const {
1294 FD_DG(
"PushdownGenerator(" <<
this <<
")::ConsistentStackSymbol(idx)");
1295 if(!StackSymbols().Exists(idx)) {
1296 std::stringstream errstr;
1297 errstr <<
"stack symbol table mismatch (symbol with idx " << idx <<
" does not exist)" << std::endl;
1298 throw Exception(
"PushdownGenerator::ConsistentStackSymbol", errstr.str(), 1001);
1300 FD_DG(
"PushdownGenerator(" <<
this <<
")::ConsistentStackSymbol(idx): ok");
1304 TEMP void THIS::ConsistentVectorStackSymbol(
const std::vector<StackSymbol>& rVector)
const {
1305 FD_DG(
"PushdownGenerator(" <<
this <<
")::ConsistentVectorStackSymbol(rVector)");
1306 std::vector<StackSymbol>::const_iterator it;
1307 it = rVector.begin();
1308 for ( ; it < rVector.end(); it++){
1309 if(!StackSymbols().Exists(it->Symbol())) {
1310 std::stringstream errstr;
1311 errstr <<
"stack symbol table mismatch (symbol " << it->mSymbol <<
" does not exist)" <<std::endl;
1312 throw Exception(
"PushdownGenerator::ConsistentVectorStackSymbol", errstr.str(), 1001);
1315 FD_DG(
"PushdownGenerator(" <<
this <<
")::ConsistentVectorStackSymbol(rVector): ok");
1319 TEMP void THIS::ConsistentVectorStackSymbol(
const std::vector<Idx>& rVector)
const {
1320 FD_DG(
"PushdownGenerator(" <<
this <<
")::ConsistentVectorStackSymbol(rVector)");
1321 std::vector<Idx>::const_iterator it;
1322 it = rVector.begin();
1323 for ( ; it < rVector.end(); it++){
1324 if(!StackSymbols().Exists(*it)) {
1325 std::stringstream errstr;
1326 errstr <<
"stack symbol table mismatch (symbol with index " << *it <<
" does not exist)" <<std::endl;
1327 throw Exception(
"PushdownGenerator::ConsistentVectorStackSymbol", errstr.str(), 1001);
1330 FD_DG(
"PushdownGenerator(" <<
this <<
")::ConsistentVectorStackSymbol(rVector): ok");
1334 TEMP void THIS::EmptyVectorPopPush(
const std::vector<StackSymbol>& rVector)
const {
1335 FD_DG(
"PushdownGenerator(" <<
this <<
")::EmptyVector(rVector)");
1336 if(rVector.empty()) {
1337 std::stringstream errstr;
1338 errstr <<
"empty vector not allowed in pop or push" <<std::endl;
1339 throw Exception(
"PushdownGenerator::EmptyVector", errstr.str(), 1001);
1341 FD_DG(
"PushdownGenerator(" <<
this <<
")::EmptyVectorPopPush(rVector): ok");
1345 TEMP void THIS::EmptyVectorPopPush(
const std::vector<Idx>& rVector)
const {
1346 FD_DG(
"PushdownGenerator(" <<
this <<
")::EmptyVector(rVector)");
1347 if(rVector.empty()) {
1348 std::stringstream errstr;
1349 errstr <<
"empty vector not allowed in pop or push" <<std::endl;
1350 throw Exception(
"PushdownGenerator::EmptyVector", errstr.str(), 1001);
1352 FD_DG(
"PushdownGenerator(" <<
this <<
")::EmptyVectorPopPush(rVector): ok");
1358 Idx index=BASE::StateIndex(stateName);
1359 BASE::pStates->Attributep(index)->SetMerge(rMerge);
1364 BASE::pStates->Attributep(index)->SetMerge(rMerge);
1369 return BASE::pStates->Attributep(index)->Merge();
1373 TEMP void THIS::SetDfaState(
const std::string& stateName,
Idx dfaIndex){
1374 Idx index=BASE::StateIndex(stateName);
1375 BASE::pStates->Attributep(index)->DfaState(dfaIndex);
1380 BASE::pStates->Attributep(index)->DfaState(dfaIndex);
1385 return BASE::pStates->Attributep(index)->DfaState();
1390 FD_DV(
"PushdownGenerator(" <<
this <<
")::Valid()");
1392 if(!BASE::Valid())
return false;