-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.l
64 lines (58 loc) · 3.63 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
%option noyywrap
%{
#include "parser.tab.h"
extern YYSTYPE yylval;
%}
%option yylineno
%%
"print" return PRINT;
"+" return ADD;
":" return BLOCK_START;
"-" return SUBTRACT;
"*" return MULTIPLY;
"/" return DIVIDE;
"=" return EQUAL;
"(" return OPENPAREN;
")" return CLOSEPAREN;
"while" return WHILE;
"for" return FOR;
"__index__" return INDEX;
"," return COMMA;
"range" return RANGE;
"in" return IN;
"if" return IF;
"else" return ELSE;
"["|"]" return ARRAY;
";" return SEMICOLON;
"and" return AND;
"or" return OR;
"not" return NOT;
"<" return LESS;
">" return GREATER;
"!=" return NOTEQUAL;
"==" return EQUALS;
">=" return GREATEREQUAL;
"<=" return LESSEQUAL;
"+=" return ADDEQUAL;
"-=" return SUBTRACTEQUAL;
"{" return OPENBRACE;
"}" return CLOSEBRACE;
"True"|"False" { yylval.stringVal = strdup(yytext); return BOOLEAN; }
"def" return FUNCTION;
"return" return RETURN;
"break" return BREAK;
"continue" return CONTINUE;
[0-9]+ { yylval.intVal = atoi(yytext); return NUMERIC; }
[0-9]+\.[0-9]+ { yylval.floatVal = atof(yytext); return DECIMALNUMERIC; }
_?[a-zA-Z][a-zA-Z0-9_]* { yylval.stringVal = strdup(yytext); return IDENTIFIER; }
\n { /* Ignore line break */ }
#.* { /* Ignore comments */ }
\"[a-zA-Z0-9 ]*?\" { yylval.stringVal = strdup(yytext); return STRING; }
"" return EMPTY;
^[" "]* { yylval.intVal = strlen(yytext); return TABS; }
%%
void yyerror(const char *str)
{
fprintf(stderr,"Error | Line: %d\n%s\n", yylineno, str);
exit(1);
}