-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer.flex
148 lines (127 loc) · 4.16 KB
/
lexer.flex
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
%option noyywrap
%{
// Avoid error "error: `fileno' was not declared in this scope"
//extern "C" int fileno(FILE *stream);
#include "parser.tab.hpp"
#include <string>
//lexing hack
std::map<std::string, std::string> type_map;
void update_type_map(std::string identifier, std::string type){
type_map[identifier] = type;
}
auto check_type_map (std::string identifier){
if (type_map.count(identifier)){
if (type_map[identifier] == "void") { return(VOID);}
else if (type_map[identifier] == "int") { return(INT); }
else if (type_map[identifier] == "long") { return(LONG); }
else if (type_map[identifier] == "short") { return(SHORT); }
else if (type_map[identifier] == "double") { return(DOUBLE); }
else if (type_map[identifier] == "float") { return(FLOAT); }
else if (type_map[identifier] == "char") { return(CHAR); }
else {std::cerr<< "Aliasing unknown datatype"<<std::endl; exit(1);} //This shouldn't happen lol
}
return IDENTIFIER;
}
%}
D [0-9]
L [a-zA-Z_]
H [a-fA-F0-9]
E [Ee][+-]?{D}+
FS (f|F|l|L)
IS (u|U|l|L)*
%%
"auto" { return(AUTO); }
"break" { return(BREAK); }
"case" { return(CASE); }
"continue" { return(CONTINUE); }
"default" { return(DEFAULT); }
"do" { return(DO); }
"else" { return(ELSE); }
"enum" { return(ENUM); }
"extern" { return(EXTERN); }
"for" { return(FOR); }
"goto" { return(GOTO); }
"if" { return(IF); }
"register" { return(REGISTER); }
"return" { return(RETURN); }
"signed" { return(SIGNED); }
"sizeof" { return(SIZEOF); }
"static" { return(STATIC); }
"struct" { return(STRUCT); }
"switch" { return(SWITCH); }
"typedef" { return(TYPEDEF); }
"union" { return(UNION); }
"unsigned" { return(UNSIGNED); }
"volatile" { return(VOLATILE); }
"while" { return(WHILE); }
"..." { return(ELLIPSIS); }
">>=" { return(RIGHT_ASSIGN); }
"<<=" { return(LEFT_ASSIGN); }
"+=" { return(ADD_ASSIGN); }
"-=" { return(SUB_ASSIGN); }
"*=" { return(MUL_ASSIGN); }
"/=" { return(DIV_ASSIGN); }
"%=" { return(MOD_ASSIGN); }
"&=" { return(AND_ASSIGN); }
"^=" { return(XOR_ASSIGN); }
"|=" { return(OR_ASSIGN); }
">>" { return(RIGHT_OP); }
"<<" { return(LEFT_OP); }
"++" { return(INC_OP); }
"--" { return(DEC_OP); }
"->" { return(PTR_OP); }
"&&" { return(AND_OP); }
"||" { return(OR_OP); }
"<=" { return(LE_OP); }
">=" { return(GE_OP); }
"==" { return(EQ_OP); }
"!=" { return(NE_OP); }
";" { return(';'); }
("{"|"<%") { return('{'); }
("}"|"%>") { return('}'); }
"," { return(','); }
":" { return(':'); }
"=" { return('='); }
"(" { return('('); }
")" { return(')'); }
("["|"<:") { return('['); }
("]"|":>") { return(']'); }
"." { return('.'); }
"&" { return('&'); }
"!" { return('!'); }
"~" { return('~'); }
"-" { return('-'); }
"+" { return('+'); }
"*" { return('*'); }
"/" { return('/'); }
"%" { return('%'); }
"<" { return('<'); }
">" { return('>'); }
"^" { return('^'); }
"|" { return('|'); }
"?" { return('?'); }
"void" { return(VOID); }
"int" { return(INT); }
"long" { return(LONG); }
"short" { return(SHORT); }
"double" { return(DOUBLE); }
"float" { return(FLOAT); }
"char" { return(CHAR); }
{L}({L}|{D})* { yylval.string=new std::string(yytext); return check_type_map(std::string(yytext));}
0[xX]{H}+{IS}? { yylval.number=std::stold(yytext); return(CONSTANT); } //
0{D}+{IS}? { yylval.number=std::stold(yytext); return(CONSTANT); } //
{D}+{IS}? { yylval.number=std::stold(yytext); return(CONSTANT); } //need to fix data types everywhere
{D}+{E}{FS}? { yylval.number=std::stold(yytext); return(CONSTANT); } //
{D}*"."{D}+({E})?{FS}? { yylval.number=std::stold(yytext); return(CONSTANT); } //
{D}+"."{D}*({E})?{FS}? { yylval.number=std::stold(yytext); return(CONSTANT); } //
L?'(\\.|[^\\'])+' { yylval.string=new std::string(yytext); return CHAR_LITERAL; }
L?\"(\\.|[^\\"])*\" { yylval.string=new std::string(yytext); return STRING_LITERAL; }
"//"(.)* {;} // Single-line comment
"/*"([^*]|\n|\r|("*"+[^*/]))*("*"+"/")? {;} /* Multi-line comment */
[ \t\r\n]+ {;}
. {;}
%%
void yyerror (char const *s){
fprintf (stderr, "Parse error : %s\n", s);
exit(1);
}