ltl2tgba.cc 4.7 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
// Copyright (C) 2012, 2013, 2014, 2015 Laboratoire de Recherche et
3
// 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

#include "common_sys.hh"

#include <string>
#include <iostream>

#include <argp.h>
#include "error.h"

28
#include "common_setup.hh"
29
#include "common_r.hh"
30
#include "common_cout.hh"
31
#include "common_finput.hh"
32
#include "common_output.hh"
33
#include "common_aoutput.hh"
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
34
#include "common_post.hh"
35

36
37
#include "tl/formula.hh"
#include "tl/print.hh"
38
#include "twaalgos/translate.hh"
39
#include "misc/optionmap.hh"
40
#include "misc/timer.hh"
41

42
static const char argp_program_doc[] ="\
43
44
45
Translate linear-time formulas (LTL/PSL) into Büchi automata.\n\n\
By default it will apply all available optimizations to output \
the smallest Transition-based Generalized Büchi Automata, \
46
47
48
in GraphViz's format.\n\
If multiple formulas are supplied, several automata will be output.";

49

50
51
52
static const argp_option options[] =
  {
    /**************************************************/
53
    { "%f", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
54
      "the formula, in Spot's syntax", 4 },
55
    /**************************************************/
56
    { "unambiguous", 'U', nullptr, 0, "output unambiguous automata", 2 },
57
    { nullptr, 0, nullptr, 0, "Miscellaneous options:", -1 },
58
    { "extra-options", 'x', "OPTS", 0,
59
      "fine-tuning options (see spot-x (7))", 0 },
60
    { nullptr, 0, nullptr, 0, nullptr, 0 }
61
62
  };

63
const struct argp_child children[] =
64
  {
65
66
67
68
69
70
    { &finput_argp, 0, nullptr, 1 },
    { &aoutput_argp, 0, nullptr, 0 },
    { &aoutput_o_format_argp, 0, nullptr, 0 },
    { &post_argp, 0, nullptr, 0 },
    { &misc_argp, 0, nullptr, -1 },
    { nullptr, 0, nullptr, 0 }
71
  };
72

73
static spot::option_map extra_options;
74
static spot::postprocessor::output_pref unambig = 0;
75
76
77
78
79
80
81

static int
parse_opt(int key, char* arg, struct argp_state*)
{
  // This switch is alphabetically-ordered.
  switch (key)
    {
82
    case 'U':
83
      unambig = spot::postprocessor::Unambiguous;
84
      break;
85
86
87
88
89
90
91
    case 'x':
      {
	const char* opt = extra_options.parse_options(arg);
	if (opt)
	  error(2, 0, "failed to parse --options near '%s'", opt);
      }
      break;
92
93
    case ARGP_KEY_ARG:
      // FIXME: use stat() to distinguish filename from string?
94
      jobs.emplace_back(arg, false);
95
96
97
98
99
100
101
102
103
104
105
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}


namespace
{
106
  class trans_processor: public job_processor
107
108
  {
  public:
109
    spot::translator& trans;
110
    automaton_printer printer;
111

112
    trans_processor(spot::translator& trans)
113
      : trans(trans), printer(ltl_input)
114
115
116
117
    {
    }

    int
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
118
    process_formula(spot::formula f,
119
		    const char* filename = nullptr, int linenum = 0)
120
    {
121
      // This should not happen, because the parser we use can only
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
122
      // read PSL/LTL formula, but since our formula type can
123
124
      // represent more than PSL formula, let's make this
      // future-proof.
125
      if (!f.is_psl_formula())
126
	{
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
127
	  std::string s = spot::str_psl(f);
128
129
130
131
132
	  error_at_line(2, 0, filename, linenum,
			"formula '%s' is not an LTL or PSL formula",
			s.c_str());
	}

133
134
135
136
137
138
      spot::stopwatch sw;
      sw.start();
      auto aut = trans.run(&f);
      const double translation_time = sw.stop();

      printer.print(aut, f, filename, linenum, translation_time, nullptr);
139
140
141
142
143
144
145
146
      return 0;
    }
  };
}

int
main(int argc, char** argv)
{
147
148
149
  // By default we name automata using the formula.
  opt_name = "%f";

150
  setup(argv);
151
152

  const argp ap = { options, parse_opt, "[FORMULA...]",
153
		    argp_program_doc, children, nullptr, nullptr };
154

155
156
  simplification_level = 3;

157
  if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
158
159
160
161
162
163
    exit(err);

  if (jobs.empty())
    error(2, 0, "No formula to translate?  Run '%s --help' for usage.",
	  program_name);

164
  spot::translator trans(&extra_options);
165
  trans.set_pref(pref | comp | sbacc | unambig);
166
167
  trans.set_type(type);
  trans.set_level(level);
168

169
170
  try
    {
171
      trans_processor processor(trans);
172
173
174
175
176
177
178
      if (processor.run())
	return 2;
    }
  catch (const std::runtime_error& e)
    {
      error(2, 0, "%s", e.what());
    }
179
  return 0;
180
}