Project

General

Profile

Statistics
| Branch: | Revision:

cool / src / lib / GMLMIP-0.1 / parser / mlf-scanner.ll @ 7c4d2eb4

History | View | Annotate | Download (3.5 KB)

1
%{
2
# include <cstdlib>
3
# include <errno.h>
4
# include <limits.h>
5
# include <string>
6
# include "mlf-driver.h"
7
# include "mlf-parser.tab.h"
8

    
9
/* Added since it fixes compile errors */
10
#define YY_NEW_FILE yyrestart(yyin  )
11

    
12
#define YY_DO_BEFORE_ACTION \
13
	(yytext_ptr) = yy_bp; \
14
/* %% [2.0] code to fiddle yytext and yyleng for yymore() goes here \ */\
15
	yyleng = (size_t) (yy_cp - yy_bp); \
16
	(yy_hold_char) = *yy_cp; \
17
	*yy_cp = '\0'; \
18
/* %% [3.0] code to copy yytext_ptr to yytext[] goes here, if %array \ */\
19
	(yy_c_buf_p) = yy_cp;
20
/* ----------------------- */
21

    
22
/* Work around an incompatibility in flex (at least versions
23
   2.5.31 through 2.5.33): it generates code that does
24
   not conform to C89.  See Debian bug 333231
25
   <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>.  */
26
# undef yywrap
27
# define yywrap() 1
28

    
29
/* By default yylex returns int, we use token_type.
30
   Unfortunately yyterminate by default returns 0, which is
31
   not of token_type.  */
32
#define yyterminate() return token::END
33
%}
34

    
35
%option noyywrap nounput batch debug
36

    
37

    
38

    
39
%{
40
# define YY_USER_ACTION  yylloc->columns (yyleng);
41
%}
42

    
43
%{
44
  typedef yy::mlf_parser::token token;
45
%}
46

    
47

    
48
%%
49
[ \t\n]+ /*eat white space*/
50
"Logic:GML"				{ return token::GML; }
51
"Logic:PML"				{ return token::PML; }
52
"Logic: GML"			{ return token::GML; }
53
"Logic: PML"			{ return token::PML; }
54
"&" 					{ return token::AND; }
55
"->"   					{ return token::IMP; }
56
"<->"					{ return token::IFF; }
57
"<"[0-9]+">"			{	if(driver.get_logic()!='g')
58
     							driver.error(*yylloc, "Invalid operator type for specified logic!");
59
     						else {
60
     							yylval->number = atoi(yytext + 1); 
61
     							return token::GMDIA; 
62
     						}
63
     					}
64
"["[0-9]+"]"			{	if(driver.get_logic()!='g')
65
     							driver.error(*yylloc, "Invalid operator type for specified logic!");
66
     						else {
67
     							yylval->number = atoi(yytext + 1); 
68
     							return token::GMBOX; 
69
     						}
70
     					}
71
"<"[0-9]+"/"[0-9]+">"	{ 	if(driver.get_logic()!='p')
72
     							driver.error(*yylloc, "Invalid operator type for specified logic!");
73
     						else {
74
       							(yylval->fraction).numerator = atoi(yytext + 1);
75
     							/*TODO: find more elegant way to perform this - skipping through until "/" */
76
								int a = 1;
77
								int b = atoi(yytext + 1);
78
								while(b > 0){
79
									b = b/10;
80
									a++;
81
								}
82
     							(yylval->fraction).denominator = atoi(yytext + a + 1);
83
     							return token::PMDIA;
84
     						}
85
     					}
86
"["[0-9]+"/"[0-9]+"]"	{ 	if(driver.get_logic()!='p')
87
     							driver.error(*yylloc, "Invalid operator type for specified logic!");
88
     						else {
89
     							(yylval->fraction).numerator = atoi(yytext + 1);
90
								int a = 1;
91
								int b = atoi(yytext + 1);
92
								while(b > 0){
93
									b = b/10;
94
									a++;
95
								}
96
     							(yylval->fraction).denominator = atoi(yytext + a + 1);
97
     							return token::PMBOX;
98
     						}
99
     					}
100
"v"						{ return token::OR; }
101
"~"						{ return token::NOT; }
102
p[0-9]+					{ yylval->binarydd = driver.variable(atoi(yytext + 1)); return token::PVAR; }
103
"("						{ return token::LPAREN; }
104
")"						{ return token::RPAREN; }
105
"true"					{ return token::TRUE; }
106
"false"					{ return token::FALSE; }
107
.          driver.error (*yylloc, "invalid character!");
108
%%
109

    
110
void mlf_driver::scan_begin (){
111
  yy_flex_debug = trace_scanning;
112
  if (file == "-")
113
    yyin = stdin;
114
  else if (!(yyin = fopen (file.c_str (), "r")))
115
    {
116
      error (std::string ("cannot open ") + file);
117
      exit (1);
118
    }
119
}
120

    
121
void mlf_driver::scan_end (){
122
  fclose (yyin);
123
}
124