gen2hoa.cpp
Go to the documentation of this file.
1 /** gen2hoa.cpp Utility to convert gen files to HOA files */
2 
3 /* FAU Discrete Event Systems Library (libfaudes)
4 
5  Copyright (C) 2025 Thomas Moor
6  Exclusive copyright is granted to Klaus Schmidt
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
21 
22 #include "libfaudes.h"
23 
24 using namespace faudes;
25 
26 // mini help
27 void usage(std::string msg="") {
28  std::cerr << "gen2hao --- convert generators to HOA format (" << faudes::VersionString() << ")" << std::endl;
29  if (msg != "") {
30  std::cerr << "error: " << msg << std::endl;
31  std::cerr << std::endl;
32  exit(1);
33  }
34  std::cerr << std::endl;
35  std::cerr << "usage:" << std::endl;
36  std::cerr << " gen2hoa [-s <sym-out>] [<gen-in> [<hoa-out>]] " << std::endl;
37  std::cerr << std::endl;
38  std::cerr << "with:" << std::endl;
39  std::cerr << " <gen-in> main input file (defaults to std in)" << std::endl;
40  std::cerr << " <hoa-out> main output file (defaults to std out)" << std::endl;
41  std::cerr << " <sym-out> symbol table file (defaults to no such)" << std::endl;
42  std::cerr << std::endl;
43  exit(0);
44 }
45 
46 
47 // streams to operate on
49 std::ostream* shoaout=nullptr;
50 std::ostream* ssymout=nullptr;
51 
52 // entry point
53 int main(int argc, char *argv[]) {
54 
55  // config
56  std::string hoaout;
57  std::string symout;
58  std::string genin;
59 
60  // primitive command line parser
61  for(int i=1; i<argc; i++) {
62  std::string option(argv[i]);
63  // option: s
64  if(option=="-s") {
65  i++;
66  if(i>=argc) usage("output file fro symbol table not specified");
67  symout=argv[i];
68  continue;
69  }
70  // option: help
71  if((option=="-?") || (option=="--help")) {
72  usage();
73  continue;
74  }
75  // option: unknown
76  if(option.c_str()[0]=='-') {
77  usage("unknown option "+ option);
78  continue;
79  }
80  // argument #1 input file
81  if(genin.empty()) {
82  genin=argv[i];
83  continue;
84  }
85  // argument #2 output file
86  if(hoaout.empty()) {
87  hoaout=argv[i];
88  continue;
89  }
90  // fail
91  usage("no more than two arguments must be specified" );
92  }
93 
94  // set input stream
95  if(!genin.empty()) {
96  tgenin=new TokenReader(genin);
97  } else {
98  tgenin=new TokenReader(std::cin);
99  }
100 
101  // sense generator type
102  bool israbin=false;
103  bool isbuechi=true;
104  Token btag;
105  tgenin->SeekBegin("Generator");
106  tgenin->Peek(btag);
107  if(btag.ExistsAttributeString("ftype")) {
108  israbin=false;
109  isbuechi=false;
110  std::string ftype= btag.AttributeStringValue("ftype");
111  if(ftype == "RabinAutomaton") israbin=true;
112  if(ftype == "Generator") isbuechi=true;
113  }
114 
115  // have generator
116  Generator* pgen=nullptr;
117  if(israbin) pgen= new RabinAutomaton;
118  if(isbuechi) pgen= new Generator;
119  if(pgen==nullptr){
120  throw Exception("gen2hoa", "only buechi and rabin automata for the time being -- sorry", 80);
121  }
122 
123  // read generator
124  pgen->Read(*tgenin);
125  pgen->MinStateIndex();
126 
127  // have a symbol table
128  SymbolTable syms;
129  syms.Name("HOA bits vs faudes event names");
130 
131  // output in HOA format
132  if(hoaout.empty())
133  ExportHoa(std::cout,*pgen,&syms);
134  else
135  ExportHoa(hoaout,*pgen,&syms);
136 
137  // export symbol table
138  if(!symout.empty()) {
139  syms.XWrite(symout);
140  }
141 
142 
143  // tidy up
144  delete tgenin;
145  delete pgen;
146 
147  // done
148  return 0;
149 }
150 
const std::string & Name(void) const
void SeekBegin(const std::string &rLabel)
bool Peek(Token &token)
bool ExistsAttributeString(const std::string &name)
Definition: cfl_token.cpp:356
const std::string & AttributeStringValue(const std::string &name)
Definition: cfl_token.cpp:386
void Read(const std::string &rFileName, const std::string &rLabel="", const Type *pContext=0)
Definition: cfl_types.cpp:267
virtual void XWrite(const std::string &pFileName, const std::string &rLabel="", const Type *pContext=0) const
Definition: cfl_types.cpp:206
Idx MinStateIndex(Idx index) const
int main(int argc, char *argv[])
Definition: gen2hoa.cpp:53
std::ostream * ssymout
Definition: gen2hoa.cpp:50
void usage(std::string msg="")
Definition: gen2hoa.cpp:27
std::ostream * shoaout
Definition: gen2hoa.cpp:49
TokenReader * tgenin
Definition: gen2hoa.cpp:48
vGenerator Generator
void ExportHoa(std::ostream &rOutStream, const Generator &rAut, SymbolTable *pSymTab)
Definition: omg_hoa.cpp:187
std::string VersionString()
Definition: cfl_utils.cpp:141
TrGenerator< RabinAcceptance, AttributeVoid, AttributeCFlags, AttributeVoid > RabinAutomaton
Definition: omg_rabinaut.h:226

libFAUDES 2.33l --- 2025.09.16 --- c++ api documentaion by doxygen