-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.l
150 lines (93 loc) · 2.96 KB
/
Lexer.l
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
/***********************************************************************
* SECTION 1
***********************************************************************/
/* The code in %{ %} is included as it is in lex.yy.c file
* it has C global variables, prototypes, and comments
*/
%{
#include <string.h> // For strdup()
#include <stdlib.h> // For malloc()
#include "ParseTree.h"
#include "y.tab.h"
int lineno = 1;
void yyerror(char*s);
%}
/******************************************************************************
* SECTION 2
******************************************************************************/
/* This is the DEFINITION section which contains substitutions, code, and
* start stats; will be copied into lex.yy.c
*/
/******************************************************************************
* SECTION 3
******************************************************************************/
/* This is the RULES section which defines how to "scan" and what action
* to take for each token
*/
%%
"CREATE" return(CREATE);
"TABLE" return(TABLE);
"INSERT" return(INSERT);
"INTO" return(INTO);
"DROP" return(DROP);
"SET" return(SET);
"OUTPUT" return(OUTPUT);
"SELECT" return(SELECT);
"FROM" return(FROM);
"WHERE" return(WHERE);
"SUM" return(SUM);
"AND" return(AND);
"GROUP" return(GROUP);
"DISTINCT" return(DISTINCT);
"BY" return(BY);
"OR" return(OR);
"AS" return(AS);
"quit" return(QUIT);
"NONE" return(NONE);
"STDOUT" return(STDOUT);
"setupdemo" return(SETUPDEMO);
"ON" return(ON);
"UPDATE" return(UPDATE);
"STATISTICS" return(STATISTICS);
"for" return(FOR);
"(" return('(');
"<" return('<');
">" return('>');
"=" return('=');
")" return(')');
"+" return('+');
"-" return('-');
"/" return('/');
"*" return('*');
"," return(',');
-?[0-9]+ {yylval.actualChars = strdup(yytext);
return(Int);
}
-?[0-9]+\.[0-9]* {yylval.actualChars = strdup(yytext);
return(Float);
}
[A-Za-z][A-Za-z0-9_-]* {yylval.actualChars = strdup(yytext);
return(Name);
}
[A-Za-z][A-Za-z0-9_-]*\.[A-Za-z][A-Za-z0-9_-]* {yylval.actualChars = strdup(yytext);
return(Name);
}
\'[^'\n]*\' {/* take care of ' in a string */
if (yytext[yyleng - 2] == '\\') {
yymore();
} else {
yylval.actualChars = strdup(yytext + 1);
yylval.actualChars[strlen(yylval.actualChars) - 1] = 0;
return(String);
}
}
\n lineno++;
[ \t] ;
. yyerror("LEX_ERROR: invalid character");
%%
void yyerror(char *s) {
printf("%d: %s at %s\n", lineno, s, yytext);
}
int yywrap(void){
return 1;
}