lbt.cc 4.11 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2012, 2013 Laboratoire de Recherche et Développement
// de l'Epita (LRDE).
4
5
6
7
8
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 3 of the License, or
10
11
12
13
14
15
16
17
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
18
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
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

#include <cassert>
#include <sstream>
#include <ctype.h>
#include <ostream>
#include <cstring>
#include "ltlast/allnodes.hh"
#include "ltlast/visitor.hh"
#include "lbt.hh"


namespace spot
{
  namespace ltl
  {
    namespace
    {
      static bool
      is_pnum(const char* str)
      {
	if (str[0] != 'p')
	  return false;
	while (*++str)
	  if (*str < '0' || *str > '9')
	    return false;
	return true;
      }

      class lbt_visitor: public visitor
      {
49
50
51
      protected:
	std::ostream& os_;
	bool first_;
52
      public:
53

54
	lbt_visitor(std::ostream& os)
55
	  : os_(os), first_(true)
56
57
58
	{
	}

59
60
61
62
63
64
65
66
	void blank()
	{
	  if (first_)
	    first_ = false;
	  else
	    os_ << ' ';
	}

67
68
69
70
71
72
73
74
	virtual
	~lbt_visitor()
	{
	}

	void
	visit(const atomic_prop* ap)
	{
75
	  blank();
76
77
	  std::string str = ap->name();
	  if (!is_pnum(str.c_str()))
78
	    os_ << '"' << str << '"';
79
	  else
80
	    os_ << str;
81
82
83
84
85
	}

	void
	visit(const constant* c)
	{
86
	  blank();
87
88
89
	  switch (c->val())
	    {
	    case constant::False:
90
	      os_ << "f";
91
92
	      break;
	    case constant::True:
93
	      os_ << "t";
94
95
96
97
98
99
100
101
102
103
	      break;
	    case constant::EmptyWord:
	      assert(!"unsupported constant");
	      break;
	    }
	}

	void
	visit(const binop* bo)
	{
104
	  blank();
105
106
107
	  switch (bo->op())
	    {
	    case binop::Xor:
108
	      os_ << "^";
109
110
	      break;
	    case binop::Implies:
111
	      os_ << "i";
112
113
	      break;
	    case binop::Equiv:
114
	      os_ << "e";
115
116
	      break;
	    case binop::U:
117
	      os_ << "U";
118
119
	      break;
	    case binop::R:
120
	      os_ << "V";
121
122
	      break;
	    case binop::W:
123
	      os_ << "W";
124
125
	      break;
	    case binop::M:
126
	      os_ << "M";
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
	      break;
	    case binop::UConcat:
	    case binop::EConcat:
	    case binop::EConcatMarked:
	      assert(!"unsupported operator");
	      break;
	    }
	  bo->first()->accept(*this);
	  bo->second()->accept(*this);
	}

	void
	visit(const bunop*)
	{
	  assert(!"unsupported operator");
	}

	void
	visit(const unop* uo)
	{
147
	  blank();
148
149
150
	  switch (uo->op())
	    {
	    case unop::Not:
151
	      os_ << "!";
152
153
	      break;
	    case unop::X:
154
	      os_ << "X";
155
156
	      break;
	    case unop::F:
157
	      os_ << "F";
158
159
	      break;
	    case unop::G:
160
	      os_ << "G";
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
	      break;
	    case unop::Finish:
	    case unop::Closure:
	    case unop::NegClosure:
	    case unop::NegClosureMarked:
	      assert(!"unsupported operator");
	      break;
	    }
	  uo->child()->accept(*this);
	}

	void
	visit(const automatop*)
	{
	  assert(!"unsupported operator");
	}

	void
	visit(const multop* mo)
	{
	  char o = 0;
	  switch (mo->op())
	    {
	    case multop::Or:
	      o = '|';
	      break;
	    case multop::And:
	      o = '&';
	      break;
	    case multop::OrRat:
	    case multop::AndRat:
	    case multop::AndNLM:
	    case multop::Concat:
	    case multop::Fusion:
	      assert(!"unsupported operator");
	      break;
	    }

	  unsigned n = mo->size();
	  for (unsigned i = n - 1; i != 0; --i)
	    {
202
203
	      blank();
	      os_ << o;
204
	    }
205
206
207

	  for (unsigned i = 0; i < n; ++i)
	    mo->nth(i)->accept(*this);
208
209
210
211
212
213
214
215
216
217
218
219
220
	}
      };

    } // anonymous

    std::ostream&
    to_lbt_string(const formula* f, std::ostream& os)
    {
      assert(f->is_ltl_formula());
      lbt_visitor v(os);
      f->accept(v);
      return os;
    }
221
222
223
224
225
226
227
228

    std::string
    to_lbt_string(const formula* f)
    {
      std::ostringstream os;
      to_lbt_string(f, os);
      return os.str();
    }
229
230
  }
}