-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.l
90 lines (62 loc) · 1.91 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
/***********************************************************************
* 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
*/
%%
"(" return('(');
")" return(')');
"<" return('<');
">" return('>');
"=" return('=');
-?[0-9]+ {yylval.actualChars = strdup(yytext);
return(Int);
}
-?[0-9]+\.[0-9]* {yylval.actualChars = strdup(yytext);
return(Float);
}
"OR" return(OR);
"AND" return(AND);
\'[^'\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);
}
}
[A-Za-z][A-Za-z0-9_-]* {yylval.actualChars = strdup(yytext);
return(Name);
}
\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;
}