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

#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
38
39
40
#include <spot/tl/formula.hh>
#include <spot/tl/print.hh>
#include <spot/twaalgos/translate.hh>
#include <spot/misc/optionmap.hh>
#include <spot/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
57
58
59
60
    { "%<", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
      "the part of the line before the formula if it "
      "comes from a column extracted from a CSV file", 4 },
    { "%>", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
      "the part of the line after the formula if it "
      "comes from a column extracted from a CSV file", 4 },
61
    /**************************************************/
62
    { "unambiguous", 'U', nullptr, 0, "output unambiguous automata", 2 },
63
    { nullptr, 0, nullptr, 0, "Miscellaneous options:", -1 },
64
    { "extra-options", 'x', "OPTS", 0,
65
      "fine-tuning options (see spot-x (7))", 0 },
66
    { nullptr, 0, nullptr, 0, nullptr, 0 }
67
68
  };

69
const struct argp_child children[] =
70
  {
71
72
73
74
75
76
    { &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 }
77
  };
78

79
static spot::option_map extra_options;
80
static spot::postprocessor::output_pref unambig = 0;
81
82
83
84
85
86
87

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

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}


namespace
{
112
  class trans_processor: public job_processor
113
114
  {
  public:
115
    spot::translator& trans;
116
    automaton_printer printer;
117

118
    trans_processor(spot::translator& trans)
119
      : trans(trans), printer(ltl_input)
120
121
122
123
    {
    }

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

139
140
141
142
143
      spot::stopwatch sw;
      sw.start();
      auto aut = trans.run(&f);
      const double translation_time = sw.stop();

144
145
      printer.print(aut, f, filename, linenum, translation_time, nullptr,
		    prefix, suffix);
146
147
148
149
150
151
152
153
      return 0;
    }
  };
}

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

157
  setup(argv);
158
159

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

162
163
  simplification_level = 3;

164
  if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
165
166
167
168
169
170
    exit(err);

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

171
  spot::translator trans(&extra_options);
172
  trans.set_pref(pref | comp | sbacc | unambig);
173
174
  trans.set_type(type);
  trans.set_level(level);
175

176
177
  try
    {
178
      trans_processor processor(trans);
179
180
181
182
183
184
185
      if (processor.run())
	return 2;
    }
  catch (const std::runtime_error& e)
    {
      error(2, 0, "%s", e.what());
    }
186
  return 0;
187
}