-
Notifications
You must be signed in to change notification settings - Fork 2
/
lexicalAnalyzer.l
69 lines (53 loc) · 1.66 KB
/
lexicalAnalyzer.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
%{
#include <stdlib.h>
#include "Doda.h"
#include "y.tab.h"
#include <string>
void yyerror(char *);
%}
%option yylineno
%%
"const" {return Constant;}
"int" { yylval.intVal = 0; return INT;}
"float" {yylval.intVal = 1; return FLOAT;}
"bool" {yylval.intVal = 2; return BOOL;}
"char" {yylval.intVal = 3; return STRINGG;}
"void" {return VOID;}
[\+\*\-\%\/\^\}\{\(\)\,\;\:\=] { return yytext[0];}
"==" {return EQUAL;}
"!=" {return NOT_EQUAL;}
">=" {return GREATER_EQUAL;}
"<=" {return LESS_EQUAL;}
"<" {return LESS_THAN;}
">" {return GREATER_THAN;}
\| {return OR;}
\& {return AND;}
\! {return NOT;}
"else" {return ELSE;}
"for" {return FOR;}
"do" {return DO;}
"break" {return BREAK;}
"case" {return CASE;}
"func" {return FUNC;}
"default" {return DEFAULT;}
"if" {return IF;}
"while" {return WHILE;}
"switch" {return SWITCH;}
"return" {return RETURN;}
.*(\.{2,}).* {yyerror("invalid sequence\n");} //invalid float number
([0-9]+\.[0-9]+)|(\.[0-9]+)|([0-9]+\.) {yylval.floatVal = std::stof(yytext); return floatType;}
[0-9]+ {yylval.intVal = atoi(yytext); return intType;}
(\".*\".*\")|(\".*\\\")|(\'.*\') {yyerror("invalid sequence\n");}
(\"\")|(\"("\n"|"\t"|"\r"|.*)\")|(\"\'("\n"|"\t"|"\r"|.*)\'\") {yylval.stringVal = yytext[0]; return stringType;}
"false" {yylval.boolVal = false; return boolType;}
"true" {yylval.boolVal = true; return boolType;}
[a-zA-Z\_][0-9a-zA-Z\_]* {yylval.realStringVal = strdup(yytext); return Identifiers;} // Identifiers
\n { }
[\#\?\@\~\.\[\]\\\`\"\'] {yyerror("invalid character\n");} //The invalid characters
%%
/*
To be called at the EOF
*/
int yywrap(void) {
return 1;
}