Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
fado.cc
Go to the documentation of this file.
1 #include <fstream>
2 #include <set>
3 #include <string>
4 
5 #include <vcsn/misc/symbol.hh>
6 
8 #include <lib/vcsn/algos/fwd.hh>
10 #include <vcsn/algos/grail.hh>
11 #include <vcsn/dyn/algos.hh>
12 #include <vcsn/dyn/automaton.hh>
13 
14 namespace vcsn
15 {
16  namespace dyn
17  {
18 
19  /*-----------------.
20  | read_fado(aut). |
21  `-----------------*/
22 
23  automaton
24  read_fado(std::istream& is)
25  {
26  std::string file = "file.fado";
27  using string_t =
28  boost::flyweight<std::string, boost::flyweights::no_tracking>;
29 
30  // The header (the first line) looks like:
31  //
32  // @DFA 3 4 5 * 0 1 2
33  //
34  // where 0 1 2 are initial states, and 3 4 5 are final states.
35  //
36  // The star is optional, in which case the initial state
37  // is the src state of the first transition.
38  {
39  std::string kind;
40  is >> kind;
41  if (kind != "@DFA" && kind != "@NFA")
42  raise(file, ": bad automaton kind: ", kind);
43  }
44 
46  edit.open(true);
47 
48  char c;
49  // Whether we process initial states.
50  bool init = false;
51  std::string state;
52  while ((c = is.get()) != '\n' && !is.eof())
53  if (c == ' ' || c == '\t')
54  {
55  // Eat blanks.
56  if (state.empty())
57  continue;
58  string_t s1{state};
59  if (init)
60  edit.add_initial(s1);
61  else
62  edit.add_final(s1);
63  state = "";
64  }
65  else if (c == '*')
66  init = true;
67  else
68  // Grow the name of the state.
69  state.append(1, c);
70 
71  // Make sure we don't skip last state, which happens
72  // when there are no spaces before '\n'.
73  if (!state.empty())
74  {
75  string_t s1{state};
76  if (init)
77  edit.add_initial(s1);
78  else
79  edit.add_final(s1);
80  }
81 
82  {
83  // Line: Source Label Destination.
84  string_t s1, l, s2;
85  is >> s1 >> l >> s2;
86 
87  // First state is our initial state if not declared before by "*".
88  if (!init && !is.eof())
89  edit.add_initial(s1);
90 
91  while (!is.eof())
92  {
93  if (l == "@epsilon")
94  l = "\\e";
95  edit.add_transition(s1, s2, l);
96  is >> s1 >> l >> s2;
97  }
98  }
99  return edit.result();
100  }
101 
102  /*------------.
103  | fado(aut). |
104  `------------*/
105 
106  REGISTER_DEFINE(fado);
107 
108  std::ostream&
109  fado(const automaton& aut, std::ostream& out)
110  {
111  detail::fado_registry().call(aut, out);
112  return out;
113  }
114 
115  }// dyn::
116 }// vcsn::
bool open(bool o)
Whether unknown letters should be added, or rejected.
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:71
automaton read_fado(std::istream &is)
Definition: fado.cc:24
void add_initial(string_t s, string_t w=string_t{})
Add s as an initial state.
void add_transition(string_t src, string_t dst, string_t lbl, string_t w=string_t{})
Add an acceptor transition from src to dst, labeled by lbl.
void add_final(string_t s, string_t w=string_t{})
Add s as a final state.
boost::flyweight< std::string, boost::flyweights::no_tracking > string_t
Definition: parse.hh:68
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:329
std::ostream & fado(const automaton &aut, std::ostream &out)
Output in FAdo format.
Definition: fado.cc:109
dyn::automaton result()
Return the built automaton.
Build an automaton with unknown context.