9 #ifndef FAUDES_PD_PDGENERATOR_H
10 #define FAUDES_PD_PDGENERATOR_H
36 template <
class GlobalAttr,
class StateAttr,
class EventAttr,
class TransAttr>
151 std::string StateStr(
Idx idx)
const;
162 std::string EventStr(
Idx idx)
const;
200 Idx StackSymbolsSize(
void)
const;
233 std::string StackSymbolName(
Idx index)
const;
250 void StackSymbolName(
Idx index,
const std::string& rName)
const;
270 Idx StackSymbolIndex(
const std::string& rName)
const;
281 std::string StackSymbolStr(
Idx idx)
const;
293 Idx InsStackSymbol(
const std::string& rName);
317 Idx SetStackBottom(
const std::string& rName);
341 Idx SetStackBottom(
const Idx idx);
349 Idx StackBottom()
const;
368 bool DelStackSymbol(
Idx index);
378 bool DelStackSymbol(
const std::string& rName);
397 bool ExistsStackSymbol(
Idx index)
const;
408 bool ExistsStackSymbol(
const std::string& rName)
const;
419 StackSymbolSet::Iterator FindStackSymbol(
Idx index)
const;
430 StackSymbolSet::Iterator FindStackSymbol(
const std::string& rName)
const;
441 std::string UniqueStackSymbolName(
const std::string& rName)
const;
448 StackSymbolSet::Iterator StackSymbolsBegin(
void)
const;
455 StackSymbolSet::Iterator StackSymbolsEnd(
void)
const;
465 std::vector<Idx> StackSymbolsToIndices(
const std::vector<StackSymbol> symbols)
const;
475 std::vector<Idx> StackSymbolNamesToIndices(
const std::vector<std::string> symbolnames)
const;
496 Idx TransRelSize()
const;
517 bool SetTransition(
Idx x1,
Idx ev,
Idx x2);
538 bool SetTransition(
const std::string& rX1,
const std::string& rEv,
539 const std::string& rX2);
556 bool SetTransition(
const Transition& rTransition,
const TransAttr& rAttr);
571 bool SetTransition(
const Transition& rTrans,
const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush);
586 bool SetTransition(
const Transition& trans,
const std::vector<std::string>& rPop,
const std::vector<std::string>& rPush);
601 bool SetTransition(
const Transition& rTrans,
const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush);
621 bool SetTransition(
Idx x1,
Idx ev,
Idx x2,
622 const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush);
641 bool SetTransition(
Idx x1,
Idx ev,
Idx x2,
642 const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush);
661 bool SetTransition(
const std::string& rX1,
const std::string& rEv,
const std::string& rX2,
const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush);
682 bool SetTransition(
const std::string& rX1,
const std::string& rEv,
const std::string& rX2,
const std::vector<std::string>& rPop,
const std::vector<std::string>& rPush);
703 bool SetTransition(
Idx x1,
Idx ev,
Idx x2,
const std::vector<std::string>& rPop,
const std::vector<std::string>& rPush);
722 bool SetTransition(
const std::string& rX1,
const std::string& rEv,
const std::string& rX2,
const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush);
748 bool SetTransition(
const std::string& rX1,
const std::string& rEv,
const std::string& rPop,
749 const std::string& rPush,
const std::string& rX2);
773 bool ClrTransition(
const std::string& rX1,
const std::string& rEv,
const std::string& rPop,
774 const std::string& rPush,
const std::string& rX2);
792 bool ClrTransition(
Idx x1,
Idx ev,
Idx x2,
const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush);
810 bool ClrTransition(
Idx x1,
Idx ev,
Idx x2,
const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush);
828 bool ClrTransition(
Idx x1,
Idx ev,
Idx x2,
const std::vector<std::string>& rPop,
const std::vector<std::string>& rPush);
846 bool ClrTransition(std::string x1, std::string ev, std::string x2,
const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush);
864 bool ClrTransition(std::string x1, std::string ev, std::string x2,
const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush);
882 bool ClrTransition(std::string x1, std::string ev, std::string x2,
const std::vector<std::string>& rPop,
const std::vector<std::string>& rPush);
896 bool ClrTransition(
const Transition& rTrans,
const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush);
910 bool ClrTransition(
const Transition& rTrans,
const std::vector<std::string>& rPop,
const std::vector<std::string>& rPush);
924 bool ClrTransition(
const Transition& rTrans,
const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush);
941 bool ExistsTransition(
942 const std::string& rX1,
const std::string& rEv,
const std::string& rX2)
const;
957 bool ExistsTransition(
Idx x1,
Idx ev,
Idx x2)
const;
969 bool ExistsTransition(
const Transition& rTrans)
const;
982 bool ExistsTransition(
Idx x1,
Idx ev)
const;
993 bool ExistsTransition(
Idx x1)
const;
1013 bool ExistsTransition(
Idx x1,
Idx ev,
Idx x2,
const std::vector<Idx>& pop,
const std::vector<Idx>& push)
const;
1033 bool ExistsTransition(
Idx x1,
Idx ev,
Idx x2,
const std::vector<std::string>& pop,
const std::vector<std::string>& push)
const;
1053 bool ExistsTransition(
Idx x1,
Idx ev,
Idx x2,
const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush)
const;
1073 bool ExistsTransition(
const std::string& x1,
const std::string& ev,
const std::string& x2,
const std::vector<StackSymbol>& pop,
const std::vector<StackSymbol>& push)
const;
1093 bool ExistsTransition(
const std::string& x1,
const std::string& ev,
const std::string& x2,
const std::vector<std::string>& pop,
const std::vector<std::string>& push)
const;
1113 bool ExistsTransition(
const std::string& x1,
const std::string& ev,
const std::string& x2,
const std::vector<Idx>& pop,
const std::vector<Idx>& push)
const;
1129 bool ExistsTransition(
const Transition& rTrans,
const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush)
const;
1145 bool ExistsTransition(
const Transition& rTrans,
const std::vector<std::string>& rPop,
const std::vector<std::string>& rPush)
const;
1161 bool ExistsTransition(
const Transition& rTrans,
const std::vector<Idx>& pop,
const std::vector<Idx>& push)
const;
1185 bool ExistsTransition(
const std::string& x1,
const std::string& ev,
const std::string& pop,
const std::string& push,
const std::string& x2)
const;
1207 PopPushSet::const_iterator PopPushBegin(
const Transition& rTrans)
const;
1218 PopPushSet::const_iterator PopPushEnd(
const Transition& rTrans)
const;
1226 Idx InsLambdaStackSymbol();
1236 bool IsStackSymbolLambda(
Idx index)
const;
1246 bool IsEventLambda(
Idx index)
const;
1256 void ConsistentStackSymbol(
const StackSymbol& rStackSymbol)
const;
1265 void ConsistentStackSymbol(
Idx idx)
const;
1274 void ConsistentVectorStackSymbol(
const std::vector<StackSymbol>& rVector)
const;
1283 void ConsistentVectorStackSymbol(
const std::vector<Idx>& rVector)
const;
1291 void EmptyVectorPopPush(
const std::vector<StackSymbol>& rVector)
const;
1299 void EmptyVectorPopPush(
const std::vector<Idx>& rVector)
const;
1309 void SetMerge(
const std::string& stateName,
MergeAbstract& rMerge);
1337 void SetDfaState(
const std::string& stateName,
Idx dfaIndex);
1347 void SetDfaState(
Idx index,
Idx dfaIndex);
1355 Idx DfaState(
Idx index)
const;
1363 virtual bool Valid(
void)
const;
1379 virtual void DotWrite(
const std::string& rFileName)
const;
1402 virtual void DotWrite(
const std::string& rFileName,
bool printInfo,
bool lr)
const;
1421 virtual void DotRead(
const std::string& rFileName);
1431 std::vector<std::string> ParseStackSymbolNames(
const std::string& rStr)
const;
1436 typedef TpdGenerator<AttributePushdownGlobal, AttributePushdownState,
1443 #define THIS TpdGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
1444 #define BASE TcGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
1445 #define TEMP template <class GlobalAttr, class StateAttr, class EventAttr, class TransAttr>
1450 BASE::pGlobalAttribute->mStackSymbols.Name(
"StackSymbols");
1452 FD_DG(
"PushdownGenerator(" <<
this <<
")::PushdownGenerator() with csymtab "
1453 << (BASE::pGlobalAttribute->mpStackSymbolTable ));
1458 FD_DG(
"PushdownGenerator(" <<
this <<
")::PushdownGenerator(rOtherGen) with csymtab"
1459 <<
"(BASE::pGlobalAttribute->mpStackSymbolTable)" );
1465 BASE::pGlobalAttribute->mStackSymbols.Name(
"StackSymbols");
1467 FD_DG(
"PushdownGenerator(" <<
this <<
")::PushdownGenerator(rOtherGen) with csymtab"
1468 <<
"(BASE::pGlobalAttribute->mpStackSymbolTable)" );
1472 TEMP THIS::TpdGenerator(
const std::string& rFileName) {
1473 FD_DG(
"PushdownGenerator(" <<
this <<
")::PushdownGenerator(" << rFileName <<
") with csymtab"
1474 <<
"(BASE::pGlobalAttribute->mpStackSymbolTable)" );
1475 if(rFileName.substr(rFileName.find_last_of(
".") + 1) ==
"dot")
1478 BASE::pGlobalAttribute->mStackSymbols.Name(
"StackSymbols");
1480 THIS::DotRead(rFileName);
1488 FD_DG(
"TpdGenerator(" <<
this <<
")::Assign([type] " << &rSrc <<
")");
1490 if(&rSrc==static_cast<const Type*>(
this))
return *
this;
1498 FD_DG(
"TpdGenerator(" <<
this <<
")::StateStr("<< idx <<
"\")");
1499 #ifdef FAUDES_CHECKED
1500 if (! BASE::ExistsState(idx)) {
1501 std::stringstream errstr;
1502 errstr <<
"state \"" << idx <<
"\" not found in generator \""
1503 << BASE::Name() <<
"\"";
1504 throw Exception(
"TpdGenerator::StateStr(idx)", errstr.str(), 89);
1508 std::string str= BASE::StateName(idx);
1515 FD_DG(
"TpdGenerator(" <<
this <<
")::EventStr("<< idx <<
"\")");
1516 #ifdef FAUDES_CHECKED
1517 if (! BASE::ExistsEvent(idx)) {
1518 std::stringstream errstr;
1519 errstr <<
"event \"" << idx <<
"\" not found in generator \""
1520 << BASE::Name() <<
"\"";
1521 throw Exception(
"TpdGenerator::EventStr(idx)", errstr.str(), 89);
1525 std::string str= BASE::EventName(idx);
1537 return BASE::pGlobalAttribute->mpStackSymbolTable;
1543 BASE::pGlobalAttribute->mpStackSymbolTable=pSymTab;
1551 res->EventSymbolTablep(BASE::mpEventSymbolTable);
1552 res->mStateNamesEnabled=BASE::mStateNamesEnabled;
1553 res->mReindexOnWrite=BASE::mReindexOnWrite;
1555 res->StackSymbolTablep(StackSymbolTablep());
1570 THIS res= BASE::NewCGen();
1572 res.StackSymbolTablep(StackSymbolTablep());
1578 return BASE::pGlobalAttribute->mStackSymbols.Size();
1583 return BASE::pGlobalAttribute->mStackSymbols;
1588 return &BASE::pGlobalAttribute->mStackSymbols;
1593 BASE::pGlobalAttribute->mStackSymbols=newstacksymbols;
1594 BASE::pGlobalAttribute->mStackSymbols.
Name(
"StackSymbols");
1598 TEMP std::string THIS::StackSymbolName(
Idx index)
const {
1599 return BASE::pGlobalAttribute->mStackSymbols.SymbolicName(index);
1603 TEMP void THIS::StackSymbolName(
Idx index,
const std::string& rName)
const {
1604 FD_DG(
"TpdGenerator(" <<
this <<
")::StackSymbolName("
1605 << index <<
",\"" << rName <<
"\")");
1606 #ifdef FAUDES_CHECKED
1607 if (! ExistsStackSymbol(index)) {
1608 std::stringstream errstr;
1609 errstr <<
"stack symbol idex \"" << index <<
"\" not found in generator \""
1610 << BASE::Name() <<
"\"";
1611 throw Exception(
"TpdGenerator::StateName(index, name)", errstr.str(), 90);
1614 BASE::pGlobalAttribute->mStackSymbols.SymbolicName(index, rName);
1619 return StackSymbol(BASE::pGlobalAttribute->mStackSymbols.SymbolicName(index));
1623 TEMP Idx THIS::StackSymbolIndex(
const std::string& rName)
const {
1624 return BASE::pGlobalAttribute->mStackSymbols.Index(rName);
1628 TEMP Idx THIS::InsStackSymbol(
const std::string& rName) {
1629 return BASE::pGlobalAttribute->mStackSymbols.Insert(rName);
1634 return BASE::pGlobalAttribute->mStackSymbols.Insert(rSymbol.
Symbol());
1639 BASE::pGlobalAttribute->mStackSymbols.InsertSet(rStackSymbolSet);
1643 TEMP Idx THIS::SetStackBottom(
const std::string& rName) {
1644 Idx i = BASE::pGlobalAttribute->mStackSymbols.Insert(rName);
1645 BASE::pGlobalAttribute->mStackBottom = i;
1651 Idx i = BASE::pGlobalAttribute->mStackSymbols.Insert(rSymbol.
Symbol());
1652 BASE::pGlobalAttribute->mStackBottom = i;
1658 if(!ExistsStackSymbol(idx)) {
1659 std::stringstream errstr;
1660 errstr <<
"stack symbol with index " << idx <<
" not found in generator. " << std::endl;
1661 throw Exception(
"PushdownGenerator::SetStackBottom(idx)", errstr.str(), 200);
1663 BASE::pGlobalAttribute->mStackBottom =
StackSymbol(StackSymbolName(idx));
1669 return BASE::pGlobalAttribute->mStackBottom;
1674 FD_DG(
"PushdownGenerator(" <<
this <<
")::DelStackSymbol(" << index <<
")");
1675 return BASE::pGlobalAttribute->mStackSymbols.Erase(index);
1679 TEMP bool THIS::DelStackSymbol(
const std::string& rName) {
1680 Idx index=BASE::pGlobalAttribute->mStackSymbols.Index(rName);
1681 return DelStackSymbol(index);
1686 StackSymbolSet::Iterator it=StackSymbolsBegin();
1687 while(it!=StackSymbolsEnd()){
1688 DelStackSymbol(*(it++));
1693 TEMP bool THIS::ExistsStackSymbol(
Idx index)
const {
1694 return BASE::pGlobalAttribute->mStackSymbols.Exists(index);
1699 const std::string& rName)
const {
1700 return BASE::pGlobalAttribute->mStackSymbols.Exists(rName);
1704 TEMP StackSymbolSet::Iterator THIS::FindStackSymbol(
Idx index)
const {
1705 return BASE::pGlobalAttribute->mStackSymbols.Find(index);
1709 TEMP StackSymbolSet::Iterator THIS::FindStackSymbol(
const std::string& rName)
const {
1710 return BASE::pGlobalAttribute->mStackSymbols.Find(rName);
1714 TEMP std::string THIS::StackSymbolStr(
Idx idx)
const{
1715 FD_DG(
"TpdGenerator(" <<
this <<
")::StackSymbolStr("<< idx <<
"\")");
1716 #ifdef FAUDES_CHECKED
1717 if (! ExistsStackSymbol(idx)) {
1718 std::stringstream errstr;
1719 errstr <<
"stack symbol \"" << idx <<
"\" not found in generator \""
1720 << BASE::Name() <<
"\"";
1721 throw Exception(
"TpdGenerator::StackSymbolStr(idx)", errstr.str(), 89);
1724 std::string str= THIS::StackSymbolName(idx);
1730 TEMP std::string THIS::UniqueStackSymbolName(
const std::string& rName)
const {
1731 FD_DG(
"PushdownGenerator(" <<
this <<
")::UniqueStackSymbolName(" << rName <<
")");
1732 std::string name=rName;
1733 if(name==
"") name=
"s";
1734 return BASE::pGlobalAttribute->mpStackSymbolTable->UniqueSymbol(name) ;
1738 TEMP StackSymbolSet::Iterator THIS::StackSymbolsBegin(
void)
const {
1739 return BASE::pGlobalAttribute->mStackSymbols.Begin();
1743 TEMP StackSymbolSet::Iterator THIS::StackSymbolsEnd(
void)
const {
1744 return BASE::pGlobalAttribute->mStackSymbols.End();
1747 TEMP std::vector<Idx> THIS::StackSymbolsToIndices(
const std::vector<StackSymbol> symbols)
const{
1748 std::vector<StackSymbol>::const_iterator ssit;
1749 std::vector<Idx> rV;
1750 for(ssit = symbols.begin(); ssit != symbols.end(); ssit++){
1751 rV.push_back(StackSymbolIndex(ssit->Symbol()));
1757 TEMP std::vector<Idx> THIS::StackSymbolNamesToIndices(
const std::vector<std::string> symbolnames)
const{
1758 std::vector<std::string>::const_iterator ssit;
1759 std::vector<Idx> rV;
1760 for(ssit = symbolnames.begin(); ssit != symbolnames.end(); ssit++){
1761 rV.push_back(StackSymbolIndex(*ssit));
1769 StateSet::Iterator stateit;
1771 PopPushSet::iterator ppit1, ppit2;
1774 for(stateit = BASE::StatesBegin(); stateit != BASE::StatesEnd(); ++stateit){
1776 for(transit1 = BASE::TransRelBegin(*stateit); transit1 != BASE::TransRelEnd(*stateit); ++transit1){
1777 for(transit2 = transit1; transit2 != BASE::TransRelEnd(*stateit); ++transit2){
1779 if((transit1->Ev == transit2->Ev) ||
1780 THIS::IsEventLambda(transit2->Ev) ||
1781 THIS::IsEventLambda(transit1->Ev)){
1786 for(ppit1 = THIS::PopPushBegin(*transit1); ppit1 != THIS::PopPushEnd(*transit1); ++ppit1){
1787 for(ppit2 = THIS::PopPushBegin(*transit2); ppit2 != THIS::PopPushEnd(*transit2); ++ppit2){
1788 if(ppit1->first == ppit2->first)
1791 if(transit1 == transit2){
1810 for(transit = BASE::TransRelBegin(); transit != BASE::TransRelEnd(); transit++){
1811 s += PopPush(*transit).size();
1817 TEMP bool THIS::SetTransition(
const std::string& rX1,
const std::string& rEv,
const std::string& rX2) {
1818 return BASE::SetTransition(rX1,rEv,rX2);
1824 return BASE::SetTransition(
Transition(x1,ev,x2));
1828 TEMP bool THIS::SetTransition(
const Transition& rTransition,
const TransAttr& rAttr) {
1829 return BASE::SetTransition(rTransition,rAttr);
1833 TEMP bool THIS::SetTransition(
const Transition& rTrans,
const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush) {
1834 return SetTransition(rTrans, StackSymbolsToIndices(rPop), StackSymbolsToIndices(rPush));
1837 TEMP bool THIS::SetTransition(
const Transition& rTrans,
const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush) {
1838 FD_DG(
"PushdownGenerator(" <<
this <<
")::SetTransition(" << (BASE::TStr(rTrans)) <<
", " <<
1839 ", PopVector" <<
", " <<
"PushVector" <<
") const");
1840 #ifdef FAUDES_CHECKED
1841 EmptyVectorPopPush(rPop);
1842 EmptyVectorPopPush(rPush);
1844 BASE::SetTransition(rTrans);
1847 if(BASE::TransAttributep(rTrans) != 0){
1848 attr = *BASE::TransAttributep(rTrans);
1850 std::pair<std::vector<Idx>, std::vector<Idx> > popPushPair;
1851 popPushPair.first = rPop;
1852 popPushPair.second = rPush;
1854 attr.mPopPush.insert(popPushPair);
1855 #ifdef FAUDES_CHECKED
1856 ConsistentVectorStackSymbol(rPop);
1857 ConsistentVectorStackSymbol(rPush);
1859 return BASE::SetTransition(rTrans,attr);
1863 TEMP bool THIS::SetTransition(
Idx x1,
Idx ev,
Idx x2,
const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush) {
1864 return SetTransition(
Transition(x1,ev,x2),rPop,rPush);
1868 TEMP bool THIS::SetTransition(
Idx x1,
Idx ev,
Idx x2,
const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush) {
1869 return SetTransition(
Transition(x1,ev,x2),rPop,rPush);
1872 TEMP bool THIS::SetTransition(
const Transition& rTrans,
const std::vector<std::string>& rPop,
const std::vector<std::string>& rPush) {
1873 return SetTransition(rTrans,StackSymbolNamesToIndices(rPop),StackSymbolNamesToIndices(rPush));
1877 TEMP bool THIS::SetTransition(
const std::string& rX1,
const std::string& rEv,
const std::string& rX2,
const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush) {
1878 FD_DG(
"PushdownGenerator(" <<
this <<
")::SetTransition(" << rX1 <<
" " << rEv <<
" " << rX2 <<
1879 ", PopVector" <<
", " <<
"PushVector" <<
") const");
1881 bool res=BASE::SetTransition(rX1,rEv,rX2);
1883 Transition rTrans = *(BASE::FindTransition(rX1,rEv,rX2));
1884 #ifdef FAUDES_CHECKED
1885 EmptyVectorPopPush(rPop);
1886 EmptyVectorPopPush(rPush);
1890 if(BASE::TransAttributep(rTrans) != 0){
1891 attr = *BASE::TransAttributep(rTrans);
1893 std::pair<std::vector<Idx>, std::vector<Idx> > popPushPair;
1894 popPushPair.first = StackSymbolsToIndices(rPop);
1895 popPushPair.second = StackSymbolsToIndices(rPush);
1897 attr.mPopPush.insert(popPushPair);
1898 #ifdef FAUDES_CHECKED
1899 ConsistentVectorStackSymbol(rPop);
1900 ConsistentVectorStackSymbol(rPush);
1902 BASE::TransAttribute(
Transition(BASE::StateIndex(rX1),BASE::EventIndex(rEv),BASE::StateIndex(rX2)),attr);
1907 TEMP bool THIS::SetTransition(
Idx x1,
Idx ev,
Idx x2,
const std::vector<std::string>& rPop,
const std::vector<std::string>& rPush) {
1908 return SetTransition(
Transition(x1,ev,x2),rPop,rPush);
1912 TEMP bool THIS::SetTransition(
const std::string& rX1,
const std::string& rEv,
const std::string& rX2,
const std::vector<std::string>& rPop,
const std::vector<std::string>& rPush) {
1913 return SetTransition(
Transition(BASE::StateIndex(rX1),BASE::EventIndex(rEv),BASE::StateIndex(rX2)),rPop,rPush);
1917 TEMP bool THIS::SetTransition(
const std::string& rX1,
const std::string& rEv,
const std::string& rX2,
const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush) {
1918 return SetTransition(
Transition(BASE::StateIndex(rX1),BASE::EventIndex(rEv),BASE::StateIndex(rX2)),rPop,rPush);
1922 TEMP std::vector<std::string> THIS::ParseStackSymbolNames(
const std::string& rStr)
const{
1923 std::string::size_type end_position,pos= 0;
1924 std::vector<std::string> syms;
1925 std::string s = rStr;
1928 s.erase(std::remove_if(s.begin(), s.end(), ::isspace), s.end());
1932 if (pos != std::string::npos)
1935 end_position = s.find(
"]",++pos);
1936 if (end_position != std::string::npos)
1939 std::string found_text = s.substr(pos, end_position-pos);
1941 if (!found_text.empty()) {
1942 std::string::size_type curr = 0, end = 0;
1948 while ((end = found_text.find(
",", curr)) != std::string::npos) {
1949 sub = found_text.substr(curr, end - curr);
1950 syms.push_back(sub);
1955 if (curr < found_text.size())
1956 syms.push_back(found_text.substr(curr));
1959 #ifdef FAUDES_CHECKED
1961 std::stringstream errstr;
1962 errstr <<
" Missing ']' for pop or push ." << std::endl;
1963 throw Exception(
"TpdGenerator::parsePPString", errstr.str(), 200);
1967 #ifdef FAUDES_CHECKED
1969 std::stringstream errstr;
1970 errstr <<
" Missing '[' for pop or push ." << std::endl;
1971 throw Exception(
"TpdGenerator::parsePPString", errstr.str(), 200);
1983 TEMP bool THIS::SetTransition(
const std::string& rX1,
const std::string& rEv,
const std::string& rPop,
1984 const std::string& rPush,
const std::string& rX2){
1985 return SetTransition(rX1,rEv,rX2,ParseStackSymbolNames(rPop),ParseStackSymbolNames(rPush));
1989 TEMP bool THIS::ClrTransition(
Idx x1,
Idx ev,
Idx x2,
const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush){
1990 return ClrTransition(
Transition(x1,ev,x2),rPop,rPush);
1994 TEMP bool THIS::ClrTransition(
Idx x1,
Idx ev,
Idx x2,
const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush){
1995 return ClrTransition(
Transition(x1,ev,x2),rPop,rPush);
1999 TEMP bool THIS::ClrTransition(
Idx x1,
Idx ev,
Idx x2,
const std::vector<std::string>& rPop,
const std::vector<std::string>& rPush){
2000 return ClrTransition(
Transition(x1,ev,x2),rPop,rPush);
2004 TEMP bool THIS::ClrTransition(std::string x1, std::string ev, std::string x2,
const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush){
2005 return ClrTransition(
Transition(BASE::StateIndex(x1),BASE::EventIndex(ev),BASE::StateIndex(x2)),rPop,rPush);
2009 TEMP bool THIS::ClrTransition(std::string x1, std::string ev, std::string x2,
const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush){
2010 return ClrTransition(
Transition(BASE::StateIndex(x1),BASE::EventIndex(ev),BASE::StateIndex(x2)),rPop,rPush);
2014 TEMP bool THIS::ClrTransition(std::string x1, std::string ev, std::string x2,
const std::vector<std::string>& rPop,
const std::vector<std::string>& rPush){
2015 return ClrTransition(
Transition(BASE::StateIndex(x1),BASE::EventIndex(ev),BASE::StateIndex(x2)),rPop,rPush);
2019 TEMP bool THIS::ClrTransition(
const Transition& rTrans,
const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush){
2020 return ClrTransition(rTrans,StackSymbolsToIndices(rPop),StackSymbolsToIndices(rPush));
2024 TEMP bool THIS::ClrTransition(
const Transition& rTrans,
const std::vector<std::string>& rPop,
const std::vector<std::string>& rPush){
2025 return ClrTransition(rTrans,StackSymbolNamesToIndices(rPop),StackSymbolNamesToIndices(rPush));
2029 TEMP bool THIS::ClrTransition(
const Transition& rTrans,
const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush){
2031 if(!BASE::ExistsTransition(rTrans)){
2036 if(!BASE::pTransRel->Attributep(rTrans)->ClrPopPush(rPop, rPush)){
2041 if(PopPush(rTrans).empty()){
2042 BASE::ClrTransition(rTrans);
2049 TEMP bool THIS::ClrTransition(
const std::string& rX1,
const std::string& rEv,
const std::string& rPop,
const std::string& rPush,
const std::string& rX2){
2050 return ClrTransition(rX1,rEv,rX2,ParseStackSymbolNames(rPop),ParseStackSymbolNames(rPush));
2054 TEMP bool THIS::ExistsTransition(
const std::string& rX1,
const std::string& rEv,
const std::string& rX2)
const{
2055 return BASE::ExistsTransition(rX1,rEv,rX2);
2060 return BASE::ExistsTransition(x1,ev,x2);
2065 return BASE::ExistsTransition(rTrans);
2070 return BASE::ExistsTransition(x1, ev);
2075 return BASE::ExistsTransition(x1);
2079 TEMP bool THIS::ExistsTransition(
Idx x1,
Idx ev,
Idx x2,
const std::vector<Idx>& pop,
const std::vector<Idx>& push)
const{
2080 return THIS::ExistsTransition(
Transition(x1, ev, x2),pop,push);
2084 TEMP bool THIS::ExistsTransition(
Idx x1,
Idx ev,
Idx x2,
const std::vector<std::string>& pop,
const std::vector<std::string>& push)
const{
2085 return THIS::ExistsTransition(
Transition(x1, ev, x2),pop,push);
2089 TEMP bool THIS::ExistsTransition(
Idx x1,
Idx ev,
Idx x2,
const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush)
const{
2090 return ExistsTransition(
Transition(x1,ev,x2),rPop,rPush);
2094 TEMP bool THIS::ExistsTransition(
const std::string& x1,
const std::string& ev,
const std::string& x2,
const std::vector<StackSymbol>& pop,
const std::vector<StackSymbol>& push)
const{
2095 return THIS::ExistsTransition(
Transition(BASE::StateIndex(x1), BASE::EventIndex(ev), BASE::StateIndex(x2)),pop,push);
2099 TEMP bool THIS::ExistsTransition(
const std::string& x1,
const std::string& ev,
const std::string& x2,
const std::vector<std::string>& pop,
const std::vector<std::string>& push)
const{
2100 return THIS::ExistsTransition(
Transition(BASE::StateIndex(x1), BASE::EventIndex(ev), BASE::StateIndex(x2)),pop,push);
2104 TEMP bool THIS::ExistsTransition(
const std::string& x1,
const std::string& ev,
const std::string& x2,
const std::vector<Idx>& pop,
const std::vector<Idx>& push)
const{
2105 return THIS::ExistsTransition(
Transition(BASE::StateIndex(x1), BASE::EventIndex(ev), BASE::StateIndex(x2)),pop,push);
2109 TEMP bool THIS::ExistsTransition(
const Transition& rTrans,
const std::vector<StackSymbol>& rPop,
const std::vector<StackSymbol>& rPush)
const{
2110 return ExistsTransition(rTrans,StackSymbolsToIndices(rPop),StackSymbolsToIndices(rPush));
2114 TEMP bool THIS::ExistsTransition(
const Transition& rTrans,
const std::vector<std::string>& rPop,
const std::vector<std::string>& rPush)
const{
2115 return ExistsTransition(rTrans,StackSymbolNamesToIndices(rPop),StackSymbolNamesToIndices(rPush));
2119 TEMP bool THIS::ExistsTransition(
const Transition& rTrans,
const std::vector<Idx>& rPop,
const std::vector<Idx>& rPush)
const{
2120 if(BASE::ExistsTransition(rTrans)){
2123 std::pair<std::vector<Idx>, std::vector<Idx> > popPushPair;
2124 popPushPair.first = rPop;
2125 popPushPair.second = rPush;
2127 return popPushSet.find(popPushPair) != popPushSet.end();
2134 TEMP bool THIS::ExistsTransition(
const std::string& rX1,
const std::string& rEv,
const std::string& rPop,
2135 const std::string& rPush,
const std::string& rX2)
const{
2137 return ExistsTransition(rX1,rEv,rX2,ParseStackSymbolNames(rPop),ParseStackSymbolNames(rPush));
2142 #ifdef FAUDES_CHECKED
2143 if(!BASE::ExistsTransition(rTrans)) {
2144 std::stringstream errstr;
2145 errstr <<
"transition " << BASE::TStr(rTrans) <<
" not found ";
2146 errstr << std::endl;
2147 throw Exception(
"PushdownGenerator::PopPush(trans)", errstr.str(), 200);
2150 return BASE::pTransRel->Attribute(rTrans).PopPush();
2155 return BASE::pTransRel->Attribute(rTrans).PopPush().begin();
2160 return BASE::pTransRel->Attribute(rTrans).PopPush().end();
2172 TEMP bool THIS::IsStackSymbolLambda(
Idx index)
const{
2190 FD_DG(
"PushdownGenerator(" <<
this <<
")::ConsistentStackSymbol(rStackSymbol)");
2191 if(!StackSymbols().Exists(rStackSymbol.
Symbol())) {
2192 std::stringstream errstr;
2193 errstr <<
"stack symbol table mismatch (symbol " << rStackSymbol.
mSymbol <<
" does not exist)" << std::endl;
2194 throw Exception(
"PushdownGenerator::ConsistentStackSymbol", errstr.str(), 1001);
2196 FD_DG(
"PushdownGenerator(" <<
this <<
")::ConsistentStackSymbol(rStackSymbol): ok");
2200 TEMP void THIS::ConsistentStackSymbol(
Idx idx)
const {
2201 FD_DG(
"PushdownGenerator(" <<
this <<
")::ConsistentStackSymbol(idx)");
2202 if(!StackSymbols().Exists(idx)) {
2203 std::stringstream errstr;
2204 errstr <<
"stack symbol table mismatch (symbol with idx " << idx <<
" does not exist)" << std::endl;
2205 throw Exception(
"PushdownGenerator::ConsistentStackSymbol", errstr.str(), 1001);
2207 FD_DG(
"PushdownGenerator(" <<
this <<
")::ConsistentStackSymbol(idx): ok");
2211 TEMP void THIS::ConsistentVectorStackSymbol(
const std::vector<StackSymbol>& rVector)
const {
2212 FD_DG(
"PushdownGenerator(" <<
this <<
")::ConsistentVectorStackSymbol(rVector)");
2213 std::vector<StackSymbol>::const_iterator it;
2214 it = rVector.begin();
2215 for ( ; it < rVector.end(); it++){
2216 if(!StackSymbols().Exists(it->Symbol())) {
2217 std::stringstream errstr;
2218 errstr <<
"stack symbol table mismatch (symbol " << it->mSymbol <<
" does not exist)" <<std::endl;
2219 throw Exception(
"PushdownGenerator::ConsistentVectorStackSymbol", errstr.str(), 1001);
2222 FD_DG(
"PushdownGenerator(" <<
this <<
")::ConsistentVectorStackSymbol(rVector): ok");
2226 TEMP void THIS::ConsistentVectorStackSymbol(
const std::vector<Idx>& rVector)
const {
2227 FD_DG(
"PushdownGenerator(" <<
this <<
")::ConsistentVectorStackSymbol(rVector)");
2228 std::vector<Idx>::const_iterator it;
2229 it = rVector.begin();
2230 for ( ; it < rVector.end(); it++){
2231 if(!StackSymbols().Exists(*it)) {
2232 std::stringstream errstr;
2233 errstr <<
"stack symbol table mismatch (symbol with index " << *it <<
" does not exist)" <<std::endl;
2234 throw Exception(
"PushdownGenerator::ConsistentVectorStackSymbol", errstr.str(), 1001);
2237 FD_DG(
"PushdownGenerator(" <<
this <<
")::ConsistentVectorStackSymbol(rVector): ok");
2241 TEMP void THIS::EmptyVectorPopPush(
const std::vector<StackSymbol>& rVector)
const {
2242 FD_DG(
"PushdownGenerator(" <<
this <<
")::EmptyVector(rVector)");
2243 if(rVector.empty()) {
2244 std::stringstream errstr;
2245 errstr <<
"empty vector not allowed in pop or push" <<std::endl;
2246 throw Exception(
"PushdownGenerator::EmptyVector", errstr.str(), 1001);
2248 FD_DG(
"PushdownGenerator(" <<
this <<
")::EmptyVectorPopPush(rVector): ok");
2252 TEMP void THIS::EmptyVectorPopPush(
const std::vector<Idx>& rVector)
const {
2253 FD_DG(
"PushdownGenerator(" <<
this <<
")::EmptyVector(rVector)");
2254 if(rVector.empty()) {
2255 std::stringstream errstr;
2256 errstr <<
"empty vector not allowed in pop or push" <<std::endl;
2257 throw Exception(
"PushdownGenerator::EmptyVector", errstr.str(), 1001);
2259 FD_DG(
"PushdownGenerator(" <<
this <<
")::EmptyVectorPopPush(rVector): ok");
2265 Idx index=BASE::StateIndex(stateName);
2266 BASE::pStates->Attributep(index)->SetMerge(&rMerge);
2271 BASE::pStates->Attributep(index)->SetMerge(&rMerge);
2276 return BASE::pStates->Attributep(index)->Merge();
2280 TEMP void THIS::SetDfaState(
const std::string& stateName,
Idx dfaIndex){
2281 Idx index=BASE::StateIndex(stateName);
2282 BASE::pStates->Attributep(index)->DfaState(dfaIndex);
2287 BASE::pStates->Attributep(index)->DfaState(dfaIndex);
2292 return BASE::pStates->Attributep(index)->DfaState();
2296 TEMP bool THIS::Valid(
void)
const {
2297 FD_DV(
"PushdownGenerator(" <<
this <<
")::Valid()");
2299 if(!BASE::Valid())
return false;
2311 TEMP void THIS::DotWrite(
const std::string& rFileName,
bool printInfo,
bool lr)
const {
2312 FD_DG(
"TpdGenerator(" <<
this <<
")::DotWrite(" << rFileName <<
","<< printInfo <<
"," << lr <<
")");
2314 BASE::SetMinStateIndexMap();
2315 StateSet::Iterator sit;
2316 EventSet::Iterator eit;
2318 StackSymbolSet::Iterator ssit;
2320 PopPushSet::iterator ppit;
2321 std::vector<Idx>::iterator pit;
2324 std::ofstream stream;
2325 stream.exceptions(std::ios::badbit|std::ios::failbit);
2326 stream.open(rFileName.c_str());
2327 stream <<
"// dot output generated by libFAUDES TpdGenerator" << std::endl;
2328 stream <<
"digraph \"" << BASE::Name() <<
"\" {";
2329 stream << std::endl;
2331 if(lr) stream <<
" rankdir=LR" << std::endl;
2334 std::string initState =
"_", stackbottom =
"_";
2335 if(BASE::ExistsState(BASE::InitState()))
2336 initState = StateStr(BASE::InitState());
2337 if(ExistsStackSymbol(StackBottom()))
2338 stackbottom = StackSymbolStr(StackBottom());
2346 stream <<
" \"\" [shape = none " << std::endl;
2347 stream <<
" label = < " << std::endl;
2350 stream <<
" M=(Q, Σ , Γ , "<< initState <<
" , "<< stackbottom <<
", δ, Qm) <br align=\"LEFT\"/>" << std::endl;
2352 stream <<
" Σ = Σc ∪ Σuc <br align=\"LEFT\"/>" << std::endl;
2353 std::stringstream ConEv,UnConEv;
2354 for(eit = BASE::AlphabetBegin(); eit != BASE::AlphabetEnd(); ++eit)
2355 if(BASE::Controllable(*eit)){
2356 if(!IsEventLambda(*eit))
2357 ConEv <<
" "<< EventStr(*eit) <<
",";
2361 if(!IsEventLambda(*eit))
2362 UnConEv <<
" "<< EventStr(*eit) <<
",";
2365 std::string evstr = ConEv.str();
2366 if(!evstr.empty()) evstr.resize(evstr.size()-1);
2367 stream <<
" Σc = {"<< evstr <<
" } <br align=\"LEFT\"/>" << std::endl;
2369 evstr = UnConEv.str();
2370 if(!evstr.empty()) evstr.resize(evstr.size()-1);
2371 stream <<
" Σuc = {"<< evstr <<
" } <br align=\"LEFT\"/>" << std::endl;
2373 stream <<
" Γ = { ";
2374 int countSymbols = 0;
2375 for(ssit = StackSymbolsBegin(); ssit != StackSymbolsEnd(); ++ssit){
2376 if(!IsStackSymbolLambda(*ssit)){
2377 if(countSymbols % 10 == 9){
2378 stream <<
"<br align=\"LEFT\"/>" << std::endl;
2381 if(countSymbols != 0)
2383 stream << StackSymbolStr(*ssit);
2387 stream <<
" } <br align=\"LEFT\"/>" << std::endl;
2388 stream <<
" >];" << std::endl;
2389 stream << std::endl;
2391 stream <<
" node [shape=circle];" << std::endl;
2392 stream << std::endl;
2395 stream <<
" // initial states" << std::endl;
2397 for (sit = BASE::InitStatesBegin(); sit != BASE::InitStatesEnd(); ++sit) {
2398 stream <<
" dot_dummyinit_" << i <<
" [shape=none, label=\"\", width=\"0.0\", height=\"0.0\" ];" << std::endl;
2399 stream <<
" dot_dummyinit_" << i <<
" -> \"" << StateStr(*sit) <<
"\";" << std::endl;
2402 stream << std::endl;
2405 stream <<
" // stackbottom" << std::endl;
2406 if(stackbottom !=
"")
2407 stream <<
" \"" << stackbottom <<
"\" [style=\"invis\", attr=\"stackbottom\"];" <<std::endl;
2409 stream << std::endl;
2412 stream <<
" // mstates" << std::endl;
2413 for (sit = BASE::MarkedStatesBegin(); sit != BASE::MarkedStatesEnd(); ++sit)
2414 stream <<
" \"" << StateStr(*sit) <<
"\" [shape=doublecircle];" << std::endl;
2416 stream << std::endl;
2419 stream <<
" // rest of stateset" << std::endl;
2420 for (sit = BASE::StatesBegin(); sit != BASE::StatesEnd(); ++sit) {
2421 if (! (BASE::ExistsInitState(*sit) || BASE::ExistsMarkedState(*sit)) )
2422 stream <<
" \"" << StateStr(*sit) <<
"\";" << std::endl;
2424 stream << std::endl;
2427 stream <<
" // events" << std::endl;
2429 if(!THIS::IsEventLambda(*eit)){
2431 stream <<
" \"" <<EventStr(*eit) <<
"\" [style=\"invis\", attr=\"";
2432 stream << (BASE::Controllable(*eit)?
"C" :
"c");
2433 stream << (BASE::Observable(*eit)?
"O" :
"o");
2434 stream << (BASE::Forcible(*eit)?
"F" :
"f");
2435 stream << (BASE::Highlevel(*eit)?
"A" :
"a");
2436 stream <<
"\"];" << std::endl;
2439 stream << std::endl;
2442 stream <<
" // transition relation" << std::endl;
2443 for(tit = BASE::TransRelBegin(); tit != BASE::TransRelEnd(); ++tit) {
2444 std::string eventname;
2446 eventname =
"λ";
2448 eventname = EventStr(tit->Ev);
2450 if(PopPush(*tit).empty()){
2451 stream <<
" \"" << StateStr(tit->X1) <<
"\" -> \"" << StateStr(tit->X2)
2452 <<
"\" [label=\"" << eventname <<
" \"];" << std::endl;
2454 for (ppit = PopPushBegin(*tit); ppit != PopPushEnd(*tit); ++ppit) {
2455 std::vector<Idx> vPop = (*ppit).first;
2456 std::vector<Idx> vPush = (*ppit).second;
2459 stream <<
" \"" << StateStr(tit->X1) <<
"\" -> \"" << StateStr(tit->X2)
2460 <<
"\" [label=\"" << eventname <<
",";
2464 for (pit = vPop.begin(); pit != vPop.end(); ++pit) {
2465 if(!IsStackSymbolLambda(*pit))
2466 stream << StackSymbolName(*pit);
2468 stream <<
"λ";
2470 if(pit+1 != vPop.end()) stream <<
",";
2476 for (pit = vPush.begin(); pit != vPush.end(); ++pit) {
2477 if(!IsStackSymbolLambda(*pit))
2478 stream << StackSymbolName(*pit);
2480 stream <<
"λ";
2482 if(pit+1 != vPush.end()) stream <<
",";
2488 if(!BASE::Controllable(tit->Ev))
2489 stream <<
" style=\"dashed\"";
2491 stream <<
" ];" << std::endl;
2494 stream <<
"}" << std::endl;
2497 catch (std::ios::failure&) {
2499 "Exception opening/writing dotfile \""+rFileName+
"\"", 2);
2504 TEMP void THIS::DotWrite(
const std::string& rFileName)
const {
2505 FD_DG(
"TpdGenerator(" <<
this <<
")::DotWrite(" << rFileName <<
")");
2506 DotWrite(rFileName,
true,
true);
2513 TEMP void THIS::DotRead(
const std::string& rFileName) {
2514 FD_DG(
"TpdGenerator(" <<
this <<
")::DotRead(" << rFileName <<
")");
2519 std::stringstream errstr;
2520 errstr <<
"Unable to parse "<< rFileName << std::endl;
2521 throw Exception(
"TpdGenerator::DotRead", errstr.str(), 200);