eltlparse.yy 6.78 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* Copyright (C) 2008 Laboratoire d'Informatique de
** Paris 6 (LIP6), dpartement Systmes Rpartis Coopratifs (SRC),
** Universit Pierre et Marie Curie.
**
** 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.
*/
%language "C++"
%locations
%defines
%name-prefix "eltlyy"
%debug
%error-verbose

%code requires
{
#include <string>
#include <sstream>
#include <limits>
#include <cerrno>
#include "public.hh"
#include "ltlast/allnodes.hh"
#include "ltlvisit/destroy.hh"

// Implementation details for error handling.
namespace spot
{
  namespace eltl
  {
    struct parse_error_list_t
    {
      parse_error_list list_;
      std::string file_;
    };
  }
}

#define PARSE_ERROR(Loc, Msg) \
  pe.list_.push_back \
    (parse_error(Loc, spair(pe.file_, Msg)))
}

%parse-param {spot::eltl::nfamap& nmap}
%parse-param {spot::eltl::parse_error_list_t &pe}
%parse-param {spot::ltl::environment &parse_environment}
%parse-param {spot::ltl::formula* &result}
%lex-param {spot::eltl::parse_error_list_t &pe}
%expect 0
%pure-parser
%union
{
  int ival;
  std::string* sval;
  spot::ltl::nfa* nval;
  spot::ltl::automatop::vec* aval;
  spot::ltl::formula* fval;
}

%code {
/* ltlparse.hh and parsedecl.hh include each other recursively.
   We mut ensure that YYSTYPE is declared (by the above %union)
   before parsedecl.hh uses it. */
#include "parsedecl.hh"
using namespace spot::eltl;
using namespace spot::ltl;
}

/* All tokens.  */

%token <sval> ATOMIC_PROP "atomic proposition"
	      IDENT "identifier"

%token <ival> ARG "argument"
	      STATE "state"
	      OP_OR "or operator"
	      OP_XOR "xor operator"
	      OP_AND "and operator"
	      OP_IMPLIES "implication operator"
	      OP_EQUIV "equivalent operator"
	      OP_NOT "not operator"

%token ACC "accept"
       EQ "="
       LPAREN "("
       RPAREN ")"
       COMMA ","
       END_OF_FILE "end of file"
       CONST_TRUE "constant true"
       CONST_FALSE "constant false"

/* Priorities.  */

/* Logical operators.  */
%left OP_OR
%left OP_XOR
%left OP_AND
%left OP_IMPLIES OP_EQUIV

%nonassoc OP_NOT

%type <nval> nfa_def
%type <fval> subformula
%type <aval> arg_list

%destructor { delete $$; } "atomic proposition"
%destructor { spot::ltl::destroy($$); } subformula

%printer { debug_stream() << *$$; } "atomic proposition"

%%

result: nfa_list subformula
	{
	  result = $2;
	  YYACCEPT;
	}
;

/* NFA definitions. */

nfa_list: /* empty. */
        | nfa_list nfa
;

nfa: IDENT "=" "(" nfa_def ")"
	{
	  $4->set_name(*$1);
          nmap[*$1] = nfa::ptr($4);
	  delete $1;
        }
;

nfa_def: /* empty. */
        {
	  $$ = new nfa();
        }
        | nfa_def STATE STATE ARG
        {
	  if ($4 == -1 || $3 == -1 || $2 == -1)
	  {
	    std::string s = "out of range integer";
	    PARSE_ERROR(@1, s);
	    YYERROR;
	  }
	  $1->add_transition($2, $3, $4);
	  $$ = $1;
        }
        | nfa_def STATE STATE CONST_TRUE
        {
	  $1->add_transition($2, $3, -1);
	  $$ = $1;
	}
        | nfa_def ACC STATE
        {
 	  $1->set_final($3);
	  $$ = $1;
        }
;

/* Formulae. */

subformula: ATOMIC_PROP
	{
	   $$ = parse_environment.require(*$1);
	   if (!$$)
	   {
	     std::string s = "unknown atomic proposition `";
	     s += *$1;
	     s += "' in environment `";
	     s += parse_environment.name();
	     s += "'";
	     PARSE_ERROR(@1, s);
	     delete $1;
	     YYERROR;
	   }
	   else
	     delete $1;
	}
	  | ATOMIC_PROP "(" arg_list ")"
	{
	  nfamap::iterator i = nmap.find(*$1);
	  if (i == nmap.end())
	  {
	    std::string s = "unknown automaton operator `";
	    s += *$1;
	    s += "'";
	    PARSE_ERROR(@1, s);
	    delete $1;
	    YYERROR;
	  }

	  automatop* res = automatop::instance(i->second, $3);
	  if (res->size() != i->second->arity())
	  {
	    std::ostringstream oss1;
	    oss1 << res->size();
	    std::ostringstream oss2;
	    oss2 << i->second->arity();

	    std::string s(*$1);
	    s += " is used with ";
	    s += oss1.str();
	    s += " arguments, but has an arity of ";
	    s += oss2.str();
	    PARSE_ERROR(@1, s);
	    delete $1;
	    delete $3;
	    YYERROR;
	  }
	  delete $1;
	  $$ = res;
	}
	  | CONST_TRUE
	{ $$ = constant::true_instance(); }
	  | CONST_FALSE
	{ $$ = constant::false_instance(); }
          | "(" subformula ")"
	{ $$ = $2; }
          | subformula OP_AND subformula
	{ $$ = multop::instance(multop::And, $1, $3); }
          | subformula OP_OR subformula
	{ $$ = multop::instance(multop::Or, $1, $3); }
	  | subformula OP_XOR subformula
	{ $$ = binop::instance(binop::Xor, $1, $3); }
	  | subformula OP_IMPLIES subformula
        { $$ = binop::instance(binop::Implies, $1, $3); }
	  | subformula OP_EQUIV subformula
	{ $$ = binop::instance(binop::Equiv, $1, $3); }
          | OP_NOT subformula
	{ $$ = unop::instance(unop::Not, $2); }
;

arg_list: subformula
	{
	  // TODO: delete it whenever a parse error occurs on a subformula.
	  $$ = new automatop::vec;
	  $$->push_back($1);
	}
	| arg_list "," subformula
	{
	  $1->push_back($3);
	  $$ = $1;
	}
;

%%

void
eltlyy::parser::error(const location_type& loc, const std::string& s)
{
  PARSE_ERROR(loc, s);
}

namespace spot
{
  namespace eltl
  {
    formula*
    parse_file(const std::string& name,
	       parse_error_list& error_list,
	       environment& env,
	       bool debug)
    {
      if (flex_open(name))
      {
	error_list.push_back
	  (parse_error(eltlyy::location(),
		       spair("-", std::string("Cannot open file ") + name)));
	return 0;
      }
      formula* result = 0;
      nfamap nmap;
      parse_error_list_t pe;
      pe.file_ = name;
      eltlyy::parser parser(nmap, pe, env, result);
      parser.set_debug_level(debug);
      parser.parse();
      error_list = pe.list_;
      flex_close();
      return result;
    }

    formula*
    parse_string(const std::string& eltl_string,
		 parse_error_list& error_list,
		 environment& env,
		 bool debug)
    {
      flex_scan_string(eltl_string.c_str());
      formula* result = 0;
      nfamap nmap;
      parse_error_list_t pe;
      eltlyy::parser parser(nmap, pe, env, result);
      parser.set_debug_level(debug);
      parser.parse();
      error_list = pe.list_;
      return result;
    }
  }
}

// Local Variables:
// mode: c++
// End: