hoascan.ll 10.9 KB
Newer Older
1
/* -*- coding: utf-8 -*-
2
** Copyright (C) 2014, 2015 Laboratoire de Recherche et Développement
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
** 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 3 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/
%option noyywrap
%option prefix="hoayy"
%option outfile="lex.yy.c"
/* %option debug */

%{
#include <string>
27
#include <sys/stat.h>
28
#include "hoaparse/parsedecl.hh"
29
#include "misc/escape.hh"
30
31
32
33
34

#define YY_USER_ACTION yylloc->columns(yyleng);
#define YY_NEVER_INTERACTIVE 1

typedef hoayy::parser::token token;
35
static unsigned comment_level = 0;
36
37
static unsigned parent_level = 0;
static int orig_cond = 0;
38
39
40
static bool lbtt_s = false;
static bool lbtt_t = false;
static unsigned lbtt_states = 0;
41

42
43
44
45
46
47
%}

eol         \n+|\r+
eol2        (\n\r)+|(\r\n)+
identifier  [[:alpha:]_][[:alnum:]_-]*

48
%x in_COMMENT in_STRING in_NEVER_PAR
49
50
51
%s in_HOA in_NEVER in_LBTT_HEADER
%s in_LBTT_STATE in_LBTT_INIT in_LBTT_TRANS
%s in_LBTT_T_ACC in_LBTT_S_ACC in_LBTT_GUARD
52
53
54
55
56
%%

%{
  std::string s;
  yylloc->step();
57
58
59
60
61
62
63
64
65
66
67
68
69

  auto parse_int = [&](){
    errno = 0;
    char* end;
    unsigned long n = strtoul(yytext, &end, 10);
    yylval->num = n;
    if (errno || yylval->num != n)
      {
        error_list.push_back(spot::hoa_parse_error(*yylloc, "value too large"));
        yylval->num = 0;
      }
    return end;
  };
70
71
72
73
74
75
76
%}


                        /* skip blanks and comments */
{eol}			yylloc->lines(yyleng); yylloc->step();
{eol2}			yylloc->lines(yyleng / 2); yylloc->step();
[ \t\v\f]+		yylloc->step();
77
78
79
80
81
"/""*"+			{
                          orig_cond = YY_START;
			  BEGIN(in_COMMENT);
			  comment_level = 1;
			}
82
<INITIAL>"HOA:"           BEGIN(in_HOA); return token::HOA;
83
<INITIAL,in_HOA>"--ABORT--" BEGIN(INITIAL); throw spot::hoa_abort{*yylloc};
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<INITIAL>"never"	  BEGIN(in_NEVER); return token::NEVER;

<INITIAL>[0-9]+[ \t][0-9]+[ts]?  {
	                  BEGIN(in_LBTT_HEADER);
			  char* end = 0;
			  errno = 0;
			  unsigned long n = strtoul(yytext, &end, 10);
			  yylval->num = n;
			  unsigned s = end - yytext;
			  yylloc->end = yylloc->begin;
 			  yylloc->end.columns(s);
			  yyless(s);
			  if (errno || yylval->num != n)
			    {
                              error_list.push_back(
			        spot::hoa_parse_error(*yylloc,
				  "value too large"));
			      yylval->num = 0;
                            }
                          lbtt_states = yylval->num;
			  return token::LBTT;
			}
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

<in_HOA>{
  "States:"		return token::STATES;
  "Start:"		return token::START;
  "AP:"			return token::AP;
  "Alias:"              return token::ALIAS;
  "Acceptance:"         return token::ACCEPTANCE;
  "acc-name:"           return token::ACCNAME;
  "tool:"               return token::TOOL;
  "name:"               return token::NAME;
  "properties:"         return token::PROPERTIES;
  "--BODY--"		return token::BODY;
  "--END--"		BEGIN(INITIAL); return token::END;
  "State:"		return token::STATE;
  [tf{}()\[\]&|!]	return *yytext;

  {identifier}          {
123
124
125
			   yylval->str = new std::string(yytext, yyleng);
			   return token::IDENTIFIER;
			}
126
  {identifier}":"       {
127
128
129
			   yylval->str = new std::string(yytext, yyleng - 1);
			   return token::HEADERNAME;
			}
130
  "@"[[:alnum:]_-]+     {
131
132
133
			   yylval->str = new std::string(yytext + 1, yyleng - 1);
			   return token::ANAME;
			}
134
  [0-9]+                parse_int(); return token::INT;
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
}

<in_NEVER>{
  "skip"		return token::SKIP;
  "if"			return token::IF;
  "fi"			return token::FI;
  "do"			return token::DO;
  "od"			return token::OD;
  "->"			return token::ARROW;
  "goto"		return token::GOTO;
  "false"|"0"		return token::FALSE;
  "atomic"		return token::ATOMIC;
  "assert"		return token::ASSERT;

  ("!"[ \t]*)?"("	{
			  parent_level = 1;
			  BEGIN(in_NEVER_PAR);
			  yylval->str = new std::string(yytext, yyleng);
			}

  "true"|"1"		{
                          yylval->str = new std::string(yytext, yyleng);
			  return token::FORMULA;
                        }

  [a-zA-Z][a-zA-Z0-9_]* {
			  yylval->str = new std::string(yytext, yyleng);
	                  return token::IDENTIFIER;
		        }
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
}

  /* Note: the LBTT format is scanf friendly, but not Bison-friendly.
     If we only tokenize it as a stream of INTs, the parser will have
     a very hard time recognizing what is a state from what is a
     transitions.  As a consequence we abuse the start conditions to
     maintain a state an return integers with different sementic types
     depending on the purpose of those integers. */
<in_LBTT_HEADER>{
  [0-9]+[st]*           {
			  BEGIN(in_LBTT_STATE);
                          auto end = parse_int();
			  lbtt_s = false;
			  lbtt_t = false;
			  if (end)
			    while (int c = *end++)
			      if (c == 's')
			        lbtt_s = true;
			      else // c == 't'
			        lbtt_t = true;
  		          if (!lbtt_t)
			    lbtt_s = true;
			  if (lbtt_states == 0)
			    {
                              BEGIN(INITIAL);
                              return token::LBTT_EMPTY;
			    }
			  if (lbtt_s && !lbtt_t)
			    return token::INT_S;
			  else
			    return token::INT;
			}
}
197

198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<in_LBTT_STATE>[0-9]+   {
                           parse_int();
			   BEGIN(in_LBTT_INIT);
			   return token::STATE_NUM;
			}
<in_LBTT_INIT>[01]	{
                           yylval->num = *yytext - '0';
			   if (lbtt_s)
			      BEGIN(in_LBTT_S_ACC);
			   else
			      BEGIN(in_LBTT_TRANS);
			   return token::INT;
			}
<in_LBTT_S_ACC>{
  [0-9]+		parse_int(); return token::ACC;
  "-1"                  BEGIN(in_LBTT_TRANS); yylloc->step();
}
<in_LBTT_TRANS>{
216
  [0-9]+                {
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
			  parse_int();
			  if (lbtt_t)
			    BEGIN(in_LBTT_T_ACC);
			  else
			    BEGIN(in_LBTT_GUARD);
			  return token::DEST_NUM;
			}
  "-1"			{
                          if (--lbtt_states)
			    {
			       BEGIN(in_LBTT_STATE);
			       yylloc->step();
			    }
			  else
			    {
			       BEGIN(INITIAL);
			       return token::ENDAUT;
			    }
			}
}
<in_LBTT_T_ACC>{
238
  [0-9]+	        parse_int(); return token::ACC;
239
240
241
242
243
244
245
246
  "-1"			BEGIN(in_LBTT_GUARD); yylloc->step();
}
<in_LBTT_GUARD>{
  [^\n\r]*		{
  			  yylval->str = new std::string(yytext, yyleng);
			  BEGIN(in_LBTT_TRANS);
 			  return token::STRING;
			}
247
248
}

249
250

<in_COMMENT>{
251
  "/""*"+		++comment_level;
252
253
254
  [^*/\n\r]*		continue;
  "/"[^*\n\r]*		continue;
  "*"+[^*/\n\r]*	continue;
255
256
  {eol}			yylloc->lines(yyleng); yylloc->end.column = 1;
  {eol2}		yylloc->lines(yyleng / 2); yylloc->end.column = 1;
257
  "*"+"/"		if (--comment_level == 0) BEGIN(orig_cond);
258
  <<EOF>>		{
259
                           BEGIN(orig_cond);
260
261
262
263
264
265
266
                           error_list.push_back(
			     spot::hoa_parse_error(*yylloc,
			       "unclosed comment"));
			   return 0;
                        }
}

267
268
269
270
271
272
273
  /* matched late, so that the in_LBTT_GUARD pattern has precedence */
"\""                    {
                          orig_cond = YY_START;
			  BEGIN(in_STRING);
			  comment_level = 1;
			}

274
275
<in_STRING>{
  \"	                {
276
                           BEGIN(orig_cond);
277
278
279
			   yylval->str = new std::string(s);
			   return token::STRING;
 			}
280
281
282
283
284
285
286
287
  {eol}			{
  			  s.append(yytext, yyleng);
                          yylloc->lines(yyleng); yylloc->end.column = 1;
			}
  {eol2}		{
  			  s.append(yytext, yyleng);
                          yylloc->lines(yyleng / 2); yylloc->end.column = 1;
			}
288
  \\.			s += yytext[1];
289
  [^\\\"\n\r]+		s.append(yytext, yyleng);
290
291
292
293
  <<EOF>>		{
                           error_list.push_back(
			     spot::hoa_parse_error(*yylloc,
			       "unclosed string"));
294
295
296
                           BEGIN(orig_cond);
			   yylval->str = new std::string(s);
			   return token::STRING;
297
298
299
                        }
}

300
<in_NEVER_PAR>{
301
  "("		        {
302
303
304
			  ++parent_level;
			  yylval->str->append(yytext, yyleng);
			}
305
306
  /* if we match ")&&(" or ")||(", stay in <in_NEVER_PAR> mode */
  ")"[ \t]*("&&"|"||")[ \t!]*"(" {
307
308
	                  yylval->str->append(yytext, yyleng);
			}
309
  ")"                   {
310
311
312
313
314
315
316
317
	                  yylval->str->append(yytext, yyleng);
			  if (!--parent_level)
			    {
                              BEGIN(in_NEVER);
			      spot::trim(*yylval->str);
			      return token::FORMULA;
			    }
			}
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
  {eol}			{
                          yylval->str->append(yytext, yyleng);
			  yylloc->lines(yyleng); yylloc->end.column = 1;
			}
  {eol2}		{
			  yylval->str->append(yytext, yyleng);
  			  yylloc->lines(yyleng / 2); yylloc->end.column = 1;
			}
  [^()\n\r]+		yylval->str->append(yytext, yyleng);
  <<EOF>>		{
                          error_list.push_back(
			    spot::hoa_parse_error(*yylloc,
 			      "missing closing parenthese"));
                          yylval->str->append(parent_level, ')');
                          BEGIN(in_NEVER);
			  spot::trim(*yylval->str);
			  return token::FORMULA;
335
336
337
			}
}

338
339
340
341
342
343
344
345
346
347
348
.			return *yytext;

%{
  /* Dummy use of yyunput to shut up a gcc warning.  */
  (void) &yyunput;
%}

%%

namespace spot
{
349
350
351
352
353
354
355
356
  void
  hoayyreset()
  {
    BEGIN(INITIAL);
    comment_level = 0;
    parent_level = 0;
  }

357
358
359
  int
  hoayyopen(const std::string &name)
  {
360
361
    bool want_interactive = false;

362
363
364
    // yy_flex_debug = 1;
    if (name == "-")
      {
365
366
367
368
369
370
371
372
        // If the input is a pipe, make the scanner
        // interactive so that it does not wait for the input
        // buffer to be full to process automata.
        struct stat s;
        if (fstat(fileno(stdin), &s) < 0)
           throw std::runtime_error("fstat failed");
	if (S_ISFIFO(s.st_mode))
	  want_interactive = true;
373
374
375
376

	// Only set yyin once we know we will use
	// it, so that we do not close it otherwise.
        yyin = stdin;
377
378
379
380
381
382
383
384
385
386
      }
    else
      {
        yyin = fopen(name.c_str(), "r");
        if (!yyin)
	  return 1;
      }
    // Reset the lexer in case a previous parse
    // ended badly.
    YY_NEW_FILE;
387
    hoayyreset();
388
389
    if (want_interactive)
      yy_set_interactive(true);
390
391
392
    return 0;
  }

393
394
395
396
397
398
399
  int
  hoayyopen(int fd)
  {
    bool want_interactive = false;

    yyin = fdopen(fd, "r");

400
401
402
    if (!yyin)
      throw std::runtime_error("fdopen failed");

403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
    // If the input is a pipe, make the scanner
    // interactive so that it does not wait for the input
    // buffer to be full to process automata.
    struct stat s;
    if (fstat(fd, &s) < 0)
      throw std::runtime_error("fstat failed");
    if (S_ISFIFO(s.st_mode))
      want_interactive = true;

    // Reset the lexer in case a previous parse
    // ended badly.
    YY_NEW_FILE;
    hoayyreset();
    if (want_interactive)
      yy_set_interactive(true);
    return 0;
  }

421
422
423
  void
  hoayyclose()
  {
424
425
426
427
428
    if (yyin)
      {
	fclose(yyin);
	yyin = NULL;
      }
429
430
  }
}