Vcsn  2.3
Be Rational
fado.cc
Go to the documentation of this file.
1 #include <fstream>
2 #include <set>
3 #include <string>
4 
5 #include <lib/vcsn/algos/fwd.hh>
7 #include <vcsn/dyn/algos.hh>
8 #include <vcsn/dyn/automaton.hh>
9 #include <vcsn/misc/symbol.hh>
10 
11 namespace vcsn
12 {
13  namespace dyn
14  {
15 
16  /*-----------------.
17  | read_fado(aut). |
18  `-----------------*/
19 
20  automaton
21  read_fado(std::istream& is)
22  {
23  std::string file = "file.fado";
24  using string_t = symbol;
25 
26  // The header (the first line) looks like:
27  //
28  // @DFA 3 4 5 * 0 1 2
29  //
30  // where 0 1 2 are initial states, and 3 4 5 are final states.
31  //
32  // The star is optional, in which case the initial state
33  // is the src state of the first transition.
34  {
35  std::string kind;
36  is >> kind;
37  if (kind != "@DFA" && kind != "@NFA")
38  raise(file, ": bad automaton kind: ", kind);
39  }
40 
42  edit.open(true);
43 
44  char c;
45  // Whether we process initial states.
46  bool init = false;
47  std::string state;
48  while ((c = is.get()) != '\n' && !is.eof())
49  if (c == ' ' || c == '\t')
50  {
51  // Eat blanks.
52  if (state.empty())
53  continue;
54  string_t s1{state};
55  if (init)
56  edit.add_initial(s1);
57  else
58  edit.add_final(s1);
59  state = "";
60  }
61  else if (c == '*')
62  init = true;
63  else
64  // Grow the name of the state.
65  state.append(1, c);
66 
67  // Make sure we don't skip last state, which happens
68  // when there are no spaces before '\n'.
69  if (!state.empty())
70  {
71  string_t s1{state};
72  if (init)
73  edit.add_initial(s1);
74  else
75  edit.add_final(s1);
76  }
77 
78  {
79  // Line: Source Label Destination.
80  string_t s1, l, s2;
81  is >> s1 >> l >> s2;
82 
83  // First state is our initial state if not declared before by "*".
84  if (!init && !is.eof())
85  edit.add_initial(s1);
86 
87  while (!is.eof())
88  {
89  if (l == "@epsilon")
90  l = "\\e";
91  edit.add_transition(s1, s2, l);
92  is >> s1 >> l >> s2;
93  }
94  }
95  return edit.result();
96  }
97  }// dyn::
98 }// vcsn::
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
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.
bool open(bool o)
Whether unknown letters should be added, or rejected.
Definition: a-star.hh:8
symbol string_t
Definition: parse.hh:66
automaton read_fado(std::istream &is)
Definition: fado.cc:21
dyn::automaton result(const std::string &ctx={})
Return the built automaton.
Build an automaton with unknown context.