lbt.cc 4.03 KB
Newer Older
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
// -*- coding: utf-8 -*-
// Copyright (C) 2012 Laboratoire de Recherche et
// Développement de l'Epita (LRDE).
//
// 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
// the Free Software Foundation; either version 2 of the License, or
// (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
// along with Spot; see the file COPYING.  If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.

#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
      {
      public:
	lbt_visitor(std::ostream& os)
	  : os_(os)
	{
	}

	virtual
	~lbt_visitor()
	{
	}

	void
	visit(const atomic_prop* ap)
	{
	  std::string str = ap->name();
	  if (!is_pnum(str.c_str()))
	    os_ << '"' << str << "\" ";
	  else
	    os_ << str << ' ';
	}

	void
	visit(const constant* c)
	{
	  switch (c->val())
	    {
	    case constant::False:
	      os_ << "f ";
	      break;
	    case constant::True:
	      os_ << "t ";
	      break;
	    case constant::EmptyWord:
	      assert(!"unsupported constant");
	      break;
	    }
	}

	void
	visit(const binop* bo)
	{
	  switch (bo->op())
	    {
	    case binop::Xor:

	      break;
	    case binop::Implies:
	      os_ << "i ";
	      break;
	    case binop::Equiv:
	      os_ << "e ";
	      break;
	    case binop::U:
	      os_ << "U ";
	      break;
	    case binop::R:
	      os_ << "V ";
	      break;
	    case binop::W:
	      os_ << "W ";
	      break;
	    case binop::M:
	      os_ << "M ";
	      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)
	{
	  switch (uo->op())
	    {
	    case unop::Not:
	      os_ << "! ";
	      break;
	    case unop::X:
	      os_ << "X ";
	      break;
	    case unop::F:
	      os_ << "F ";
	      break;
	    case unop::G:
	      os_ << "G ";
	      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)
	    os_ << o << ' ';

	  for (unsigned i = 0; i < n; ++i)
	    {
	      mo->nth(i)->accept(*this);
	    }
	}
      protected:
	std::ostream& os_;
      };

    } // 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;
    }
208
209
210
211
212
213
214
215

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