File: scanner.l

package info (click to toggle)
mona 1.4-7-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,996 kB
  • ctags: 3,939
  • sloc: ansic: 14,363; cpp: 12,610; sh: 1,076; yacc: 493; lex: 358; makefile: 150; lisp: 53
file content (378 lines) | stat: -rw-r--r-- 10,910 bytes parent folder | download | duplicates (2)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * MONA
 * Copyright (C) 1997-2004 BRICS.
 *
 * This program 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.
 *
 * This program 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, write to the  Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 * USA.
 */

%a 40235 /* size of transitions accepted */
%o 6000  /* size of output slots accepted */

%option nounput
%option never-interactive

%{
#include <stdio.h>
#include <string.h>
#include "untyped.h"
#include "parser.h"
#include "deque.h"
#include "lib.h"
#include "printline.h"      
#include "env.h"
#include "../BDD/bdd.h"

using std::cout;

extern SymbolTable symbolTable;
extern AutLib lib;
extern Options options;
extern YYLTYPE yylloc;    

void copyString(char*);
int get_next_char(char*);  
void loadFile(char *s);
void yyerror(const char *s);

#define MYBUFFER_SIZE 1024  

class FileInfo {
public:
  unsigned lineno; /* linenumber */
  unsigned pos; /* position in line number lineno */
  int prevnl; /* previous char was '\n' */
  char *filename; /* name of the file */
  unsigned number; /* number of the file in the include-chain */
  unsigned next; /* pointer into inputbuffer - where to read next char */
  unsigned bufferused; /* number of chars read into inputbuffer */
  char inputbuffer[MYBUFFER_SIZE]; /* buffer containing inputfile blocks */
  FILE *handle; /* file handle */
};

Deque<FileInfo> loadStack; /* stack of file info */

Deque<Deque<char *> *> dependencies; /* list of dependencies */
Deque<FileSource *> source; /* list of source-file text */ 
Deque<char *> fileNames; /* list of file names */
Deque<char> currentLine; /* contains the current line being parsed */

Deque<char *> currentDir;
char *file = "";
unsigned pos = 0;
int prevnl = 0;
int filenumber = 0;

char mybuffer[MYBUFFER_SIZE]; /* input buffer used in YY_INPUT */
int next = 0;
int bufferused = 0;

#define SETPOS(p) \
 yylloc.first_line = yylineno; yylloc.first_column = pos-yyleng-p+1

#define YY_INPUT(buf,result,max_size) result = get_next_char(buf)
%}

/* Definitions */

name		[a-zA-Z0-9_'@\$]+
whitespace	[ \t\n\f]+
comment		#.*
numeral		[0-9]+

/* Rules */

%x CCOMMENT

%%

"/*"		BEGIN(CCOMMENT);
<CCOMMENT>{
 [^*\n]*	/* ignore */
 "*"+[^*/\n]*	/* ignore */
 \n		/* ignore */
 <<EOF>>	yyerror("end-of-file in comment\n");
 "*"+"/"	BEGIN(INITIAL);
}

"%"             SETPOS(0); return tokMODULO;
"&"             SETPOS(0); return tokAND;
"("             SETPOS(0); return tokLPAREN;
")"             SETPOS(0); return tokRPAREN;
"*"             SETPOS(0); return tokSTAR;
"+"             SETPOS(0); return tokPLUS;
","             SETPOS(0); return tokCOMMA;
"-"             SETPOS(1); return tokMINUS;
"->"            SETPOS(0); return tokARROW;
"."             SETPOS(1); return tokDOT;
"..."           SETPOS(0); return tokINTERVAL;
"/"             SETPOS(1); return tokSLASH;
":"             SETPOS(0); return tokCOLON;
";"             SETPOS(0); return tokSEMICOLON;
"<"             SETPOS(1); return tokLESS;
"<="            SETPOS(1); return tokLESSEQ;
"<=>"           SETPOS(0); return tokBIIMPL;
"="             SETPOS(1); return tokEQUAL;
"=>"            SETPOS(0); return tokIMPL;
">"             SETPOS(1); return tokGREATER;
">="            SETPOS(0); return tokGREATEREQ;
"["             SETPOS(0); return tokLBRACKET;
"\\"            SETPOS(0); return tokSETMINUS;
"]"             SETPOS(0); return tokRBRACKET;
"^"             SETPOS(0); return tokUP;
"{"             SETPOS(0); return tokLBRACE;
"|"             SETPOS(0); return tokOR;
"}"             SETPOS(0); return tokRBRACE;
"~"             SETPOS(1); return tokNOT;
"~="            SETPOS(0); return tokNOTEQUAL;
"m2l-str"       SETPOS(0); return tokM2LSTR;
"m2l-tree"      SETPOS(0); return tokM2LTREE;
"empty"         SETPOS(1); return tokEMPTY;
"universe"      SETPOS(1); return tokUNIVERSE;
"union"         SETPOS(1); return tokUNION;
"inter"         SETPOS(1); return tokINTER;
"true"          SETPOS(1); return tokTRUE;
"false"         SETPOS(1); return tokFALSE;
"in"            SETPOS(1); return tokIN;
"notin"         SETPOS(1); return tokNOTIN;
"sub"           SETPOS(1); return tokSUB;
"all0"          SETPOS(1); return tokALL0;
"ex0"           SETPOS(1); return tokEX0;
"all1"          SETPOS(1); return tokALL1;
"ex1"           SETPOS(1); return tokEX1;
"all2"          SETPOS(1); return tokALL2;
"ex2"           SETPOS(1); return tokEX2;
"var0"          SETPOS(1); return tokVAR0;
"var1"          SETPOS(1); return tokVAR1;
"var2"          SETPOS(1); return tokVAR2;
"let0"          SETPOS(1); return tokLET0;
"let1"          SETPOS(1); return tokLET1;
"let2"          SETPOS(1); return tokLET2;
"guide"         SETPOS(1); return tokGUIDE;
"const"         SETPOS(1); return tokCONST;
"pred"          SETPOS(1); return tokPRED;
"macro"         SETPOS(1); return tokMACRO;
"assert"        SETPOS(1); return tokASSERT;
"root"          SETPOS(1); return tokUNIVROOT;
"tree"          SETPOS(1); return tokTREE;
"ws1s"          SETPOS(1); return tokWS1S;
"ws2s"          SETPOS(1); return tokWS2S;
"min"           SETPOS(1); return tokMIN;
"max"           SETPOS(1); return tokMAX;
"where"         SETPOS(1); return tokWHERE;
"defaultwhere1" SETPOS(1); return tokDEFAULT1;
"defaultwhere2" SETPOS(1); return tokDEFAULT2;
"include"       SETPOS(1); return tokINCLUDE;
"import"        SETPOS(1); return tokIMPORT;
"export"        SETPOS(1); return tokEXPORT;
"prefix"        SETPOS(1); return tokPREFIX;
"in_state_space" SETPOS(1); return tokINSTATESPACE;
"execute"       SETPOS(1); return tokEXECUTE;
"lastpos"       SETPOS(1); return tokLASTPOS;
"allpos"        SETPOS(1); return tokALLPOS;
"type"          SETPOS(1); return tokTYPE;
"sometype"      SETPOS(1); return tokSOMETYPE;
"variant"       SETPOS(1); return tokVARIANT;
"succ"          SETPOS(1); return tokSUCC;
"const_tree"    SETPOS(1); return tokCONSTTREE;
"tree_root"     SETPOS(1); return tokTREEROOT;
"restrict"      SETPOS(1); return tokRESTRICT;
"verify"        SETPOS(1); return tokVERIFY;
\"[^\"\n]*\"    {SETPOS(1); yytext[strlen(yytext)-1] = 0;  
		 copyString(yytext); return tokSTRING;}
{numeral}       SETPOS(1); copyString(yytext); return tokINT;
{name}          SETPOS(1); copyString(yytext); return tokNAME;
{whitespace}    /* ignore */
{comment}       /* ignore */
.               yyerror("illegal character");

%%     

/* Subroutines */

void copyString(char *s)
{
  char *str = new char[strlen(s)+1];
  strcpy(str, s);
  str = symbolTable.insertString(str);
  yylval.string = str;
}

void yyerror(const char *s)
{ 
  cout << "Error in file '" << file
       << "' near line " << yylineno << ": " << s << "\n"
       << "Execution aborted\n";
  exit(-1);
}

int yywrap() {
  unsigned i;

  /* pop current directory */
  delete[] currentDir.pop_back();

  /* dependency info */
  if (options.separateCompilation) {
    Deque<char *> *d = dependencies.pop_back();
    lib.openDir(file, d);
    delete d; 
  }

  /* close file */
  fclose(yyin);

  /* done? */
  if (loadStack.size() == 0)
    return -1;

  /* return to previous file */
  FileInfo f = loadStack.pop_back();
  yylineno = f.lineno;
  pos = f.pos;
  prevnl = f.prevnl;
  file = f.filename;
  filenumber = f.number;
  next = f.next;
  bufferused = f.bufferused;
  strncpy(mybuffer, f.inputbuffer, MYBUFFER_SIZE);
  yyin = f.handle;
  
  /* get currentline contents */
  currentLine.reset(); 
  for (i = 0; i < strlen(source.get(filenumber-1)->top()); i++)
    currentLine.push_back((source.get(filenumber-1)->top())[i]);
  delete[] source.get(filenumber-1)->pop_back();

  return 0;
}

void loadFile(char *s) {
  unsigned i,n;
  char *line;
  char *ss;

  /* find current directory */
  if (!currentDir.empty() && s[0] != '/') {
    ss = new char[strlen(currentDir.top())+strlen(s)+1];
    strcpy(ss, currentDir.top()); 
  }
  else {
    ss = new char[strlen(s)+1];
    ss[0] = 0;
  }
  strcat(ss, s);
  unsigned t = strlen(ss);
  while (t > 0 && ss[t-1] != '/')
    t--;
  char *cd = new char[t+1];
  strncpy(cd, ss, t);
  cd[t] = 0;
  currentDir.push_back(cd);

  if (filenumber > 0) {
    /* save status for current file on stack */
    FileInfo f;
    f.lineno = yylineno;
    f.pos = pos;
    f.prevnl = prevnl;
    f.filename = file;
    f.number = filenumber;
    f.next = next;
    f.bufferused = bufferused;
    strncpy(f.inputbuffer, mybuffer, MYBUFFER_SIZE);
    f.handle = yyin;
    loadStack.push_back(f);
    
    /* store current line source */
    line = new char[sizeof(char)*currentLine.size()+1]; 
    line[currentLine.size()] = '\0';
    for (n = 0; n < currentLine.size(); n++) 
      line[n] = currentLine.get(n);
    source.get(filenumber-1)->push_back(line);      
  }

  /* store info on new file in list */
  file = new char[strlen(ss)+1];
  strcpy(file, ss);
  delete[] ss;
  file = symbolTable.insertString(file);
  fileNames.push_back(file);
  source.push_back(new FileSource);

  /* open file */
  for (i = 0; i+1 < loadStack.size(); i++)
    if (strcmp(loadStack.get(i).filename, file) == 0)
      yyerror("cyclic include");
  if (!(yyin = fopen(file, "r"))) {
    cout << "Unable to open file '" << file << "'\n"
	 << "Execution aborted\n";
    exit(-1);
  }
  currentLine.reset(); 
  filenumber++;
  pos = 0;
  prevnl = 0;
  yylineno = 1;
  next = bufferused = 0;

  /* find dependencies */
  if (options.separateCompilation) {
    dependencies.push_back(new Deque<char *>);
    for (Deque<Deque<char *> *>::iterator d = dependencies.begin();
	 d != dependencies.end(); d++)
      (*d)->push_back(file);
  }
}

int get_next_char(char *buf) {
  char *line; 
  unsigned n;
  int c;
  
  if (next == bufferused) {
    /* get a new chunk from the file */
    bufferused = fread(mybuffer, 1, MYBUFFER_SIZE, yyin);
    next = 0;
  }
  if (next == bufferused || mybuffer[next] == '\n') {
    /* move to next line */
    line = new char[currentLine.size()+1]; 
    line[currentLine.size()] = '\0'; 
    for (n = 0; n < currentLine.size(); n++) 
      line[n] = currentLine.get(n); 
    source.get(filenumber-1)->push_back(line);      
    currentLine.reset(); 
  }
  if (next < bufferused) {
    /* end-of-file not reached yet */
    c = mybuffer[next++]; 
    if (prevnl) {
      prevnl = 0;
      pos = 0;
    }
    pos++; 
    if (c == '\n') 
      prevnl = 1; 
    else
      currentLine.push_back(c); 
    buf[0] = c;
    return 1;
  }
  else {
    pos++; 
    return YY_NULL; 
  }
}