File: main.cpp

package info (click to toggle)
libclaw 1.7.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,080 kB
  • sloc: cpp: 13,287; sh: 227; makefile: 8
file content (246 lines) | stat: -rw-r--r-- 5,570 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
 * \file main.cpp
 * \brief This is an example program to show a basic use of the claw::automaton
 *        class.
 * \author Julien Jorge
 */
#include <claw/automaton.hpp>
#include <fstream>
#include <iostream>
#include <vector>
#include <list>
#include <sstream>

/*----------------------------------------------------------------------------*/
/** 
 * \brief Print an automation.
 * \param os The stream to write in.
 * \param a The automaton to print.
 * \return os.
 */
template<typename Automaton>
std::ostream& print_automaton( std::ostream& os, const Automaton& a )
{
  typedef typename Automaton::state_type state_type;
  typedef typename Automaton::edge_type edge_type;

  std::vector<state_type> states;
  std::vector<edge_type> alphabet;

  typename std::vector<state_type>::const_iterator it_s;
  typename std::vector<edge_type>::const_iterator  it_a;

  /* Print the alphabet. */
  a.alphabet( alphabet );
  if ( alphabet.empty() )
    os << "A = {}" << std::endl;
  else
    {
      it_a = alphabet.begin();

      os << "A = { " << *it_a;
      ++it_a;

      for (; it_a != alphabet.end(); ++it_a)
        os << ", " << *it_a;

      os << " }" << std::endl;
    }

  /* Print initial states. */
  a.initial_states( states );
  if ( states.empty() )
    os << "I = {}" << std::endl;
  else
    {
      it_s = states.begin();

      os << "I = { " << *it_s;
      ++it_s;

      for (; it_s != states.end(); ++it_s)
        os << ", " << *it_s;

      os << " }" << std::endl;
    }

  /* Print final states. */
  a.final_states( states );
  if ( states.empty() )
    os << "F = {}" << std::endl;
  else
    {
      it_s = states.begin();

      os << "F = { " << *it_s;
      ++it_s;

      for (; it_s != states.end(); ++it_s)
        os << ", " << *it_s;

      os << " }" << std::endl;
    }

  /* Print states and edges. */
  a.states( states );
  if ( states.empty() )
    {
      os << "E = {}" << std::endl;
      os << "T = {}" << std::endl;
    }
  else
    {
      std::vector<edge_type> transitions;

      typename std::vector<state_type>::const_iterator it_v;
          
      /* states */
      it_s = states.begin();

      os << "E = { " << *it_s;
      ++it_s;

      for(; it_s != states.end(); ++it_s)
        os << ", " << *it_s;

      os << " }" << std::endl;

      /* edges */
      os << "T = {" << std::endl;

      for(it_s = states.begin(); it_s != states.end(); ++it_s)
        {
          os << *it_s << " -> { ";

          for (it_v = states.begin(); it_v != states.end(); ++it_v)
            {
              a.edges( *it_s, *it_v, transitions );
              for (it_a = transitions.begin(); it_a!=transitions.end(); ++it_a)
                os << "(" << *it_v << ", " << *it_a << ") ";
            }
                         
          os << "}" << std::endl;
        }               
      os << "}" << std::endl;
    }

  return os;
} // print_automaton()

/*----------------------------------------------------------------------------*/
/**
 * \brief Read a line describing a part of an automaton.
 * \param is The stream in which we read the attributes.
 * \param a The automaton in which we add the result of the line.
 *
 * The following actions can be done by this function:
 * add an edge:
 *  E <source state> <target state> <symbol>
 *
 * add a state:
 *  S <state>
 *
 * add an initial state:
 *  I <state>
 *
 * add a final state:
 *  F <state>
 *
 * Other lines are ignored.
 */
template<typename Automaton>
void read_line( std::istream& is, Automaton& a )
{
  typename Automaton::state_type src, target;
  typename Automaton::edge_type edge;
  char line_type; 

  if ( is >> line_type )
    switch ( line_type )
      {
      case 'E':
	{
	  is >> src >> target >> edge;
	  a.add_edge( src, target, edge );
	  break;
	}
      case 'S':
	{
	  is >> src;
	  a.add_state( src );
	  break;
	}
      case 'I':
	{
	  is >> src;
	  a.add_initial_state( src );
	  break;
	}
      case 'F':
	{
	  is >> src;
	  a.add_final_state( src );
	  break;
	}
      }
} // read_line()

/*----------------------------------------------------------------------------*/
/**
 * \brief Load an automation from a file.
 * \param is The stream describing the automaton.
 * \param a (out) The read automaton.
 */
template<typename Automaton>
void load_automaton( std::istream& is, Automaton& a )
{
  std::string line;

  while ( std::getline(is, line) )
    {
      std::istringstream iss(line);
      read_line( iss, a );
    }
} // load_automaton()

/*----------------------------------------------------------------------------*/
/**
 * \brief Tell if a pattern is recognized by an automaton.
 * \param pattern The pattern to check.
 * \param a The automaton.
 */
bool valid_pattern
( const std::string& pattern, const claw::automaton<int, char>& a )
{
  return a.match( pattern.begin(), pattern.end() );
} // valid_pattern()

/*----------------------------------------------------------------------------*/
int main( int argc, char* argv[] )
{
  if (argc < 3)
    std::cerr << "usage:\n" << argv[0] << " automaton_file pattern..."
	      << std::endl;
  else
    {
      std::ifstream f( argv[1] );

      if ( !f )
	std::cerr << "Can't open file '" << argv[1] << "'" << std::endl;
      else
	{
	  claw::automaton<int, char> a;

	  load_automaton( f, a );
	  print_automaton(std::cout, a) << std::endl;

	  for (int i=2; i!=argc; ++i)
	    if ( valid_pattern( argv[i], a ) )
	      std::cout << argv[i] << ": valid" << std::endl;
	    else
	      std::cout << argv[i] << ": not valid" << std::endl;
	}
    }

  return 0;
}