neverclaimparse.yy 4.07 KB
Newer Older
1
/* Copyright (C) 2010 Laboratoire de Recherche et Dveloppement
Felix Abecassis's avatar
Felix Abecassis committed
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
** 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.
*/
%language "C++"
%locations
%defines
%expect 0 // No shift/reduce
%expect-rr 0 // No reduce/reduce
%name-prefix "neverclaimyy"
%debug
%error-verbose

%code requires
{
#include <string>
#include "public.hh"
typedef std::pair<std::string*, std::string*> pair;
}

%parse-param {spot::neverclaim_parse_error_list& error_list}
%parse-param {spot::ltl::environment& parse_environment}
%parse-param {spot::tgba_explicit_string*& result}
%union
{
  std::string* str;
  pair* p;
  std::list<pair>* list;
}

%code
{
#include "ltlast/constant.hh"
  /* Unfortunately Bison 2.3 uses the same guards in all parsers :( */
#undef BISON_POSITION_HH
#undef BISON_LOCATION_HH
#include "ltlparse/public.hh"

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

%token NEVER "never"
%token SKIP "skip"
%token IF "if"
%token FI "fi"
%token ARROW "->"
%token GOTO "goto"
%token <str> FORMULA
%token <str> IDENT
%type <p> transition
%type <list> transitions

%destructor { delete $$; } <str>
%destructor { delete $$->first; delete $$->second; delete $$; } <p>
%destructor {
  for (std::list<pair>::iterator i = $$->begin();
       i != $$->end(); ++i)
  {
    delete i->first;
    delete i->second;
  }
  delete $$;
  } <list>
%printer { debug_stream() << *$$; } <str>

%%
neverclaim:
  "never" '{' states '}'
;

states:
  /* empty */
  | state states
;

state:
  IDENT ':' "skip"
    {
      result->create_transition(*$1, *$1);
      delete $1;
    }
  | IDENT ':' { delete $1; }
  | IDENT ':' "if" transitions "fi"
    {
      std::list<pair>::iterator it;
      for (it = $4->begin(); it != $4->end(); ++it)
      {
	spot::tgba_explicit::transition* t =
	  result->create_transition(*$1,*it->second);
	spot::ltl::parse_error_list pel;
	spot::ltl::formula* f = spot::ltl::parse(*(it->first), pel);
	result->add_condition(t, f);
      }
      // Free the list
      delete $1;
      for (std::list<pair>::iterator it = $4->begin();
	   it != $4->end(); ++it)
      {
	delete it->first;
	delete it->second;
      }
      delete $4;
    }
;

transitions:
  /* empty */ { $$ = new std::list<pair>; }
  | transition transitions
    {
      $2->push_back(*$1);
      delete $1;
      $$ = $2;
    }
;

transition:
  ':' ':' FORMULA  "->" "goto" IDENT
    {
      $$ = new pair($3, $6);
    }
%%

void
neverclaimyy::parser::error(const location_type& location,
			    const std::string& message)
{
  error_list.push_back(spot::neverclaim_parse_error(location, message));
}

namespace spot
{
  tgba_explicit_string*
  neverclaim_parse(const std::string& name,
		   neverclaim_parse_error_list& error_list,
		   bdd_dict* dict,
		   environment& env,
		   bool debug)
  {
    if (neverclaimyyopen(name))
      {
	error_list.push_back
	  (neverclaim_parse_error(neverclaimyy::location(),
				  std::string("Cannot open file ") + name));
	return 0;
      }
    tgba_explicit_string* result = new tgba_explicit_string(dict);
    neverclaimyy::parser parser(error_list, env, result);
    parser.set_debug_level(debug);
    parser.parse();
    neverclaimyyclose();
    result->merge_transitions();
    return result;
  }
}

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