-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstencc.l
72 lines (66 loc) · 2.08 KB
/
stencc.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
%{
#include <stdlib.h>
#include <string.h>
#include "y.tab.h"
void debug (char* s);
%}
identifier [a-zA-Z_][0-9a-zA-Z_]*
number [0-9]+
comment (\/\/.*)|("/*"([^*]|\*+[^*/])*\*+"/")
string \"(\\.|[^"\\])*\"
%option case-insensitive
%option yylineno
%%
"int" { return INT; }
"stencil" { return STENCIL; }
"main" { return MAIN; }
"return" { return RETURN; }
"void" { return VOID; }
"if" { return IF; }
"while" { return WHILE; }
"else" { return ELSE; }
"for" { return FOR; }
"true" { return TRUE; }
"false" { return FALSE; }
"const" { return CONST; }
"printi" { return PRINTI; }
"printf" { return PRINTF; }
"#define" { return DEFINE_STRING;}
{identifier} { yylval.string = strdup(yytext);
//yylval.string = yytext;
return ID; }
{number} { yylval.value = atoi(yytext);return NUM; }
{comment} { }
{string} { yylval.string = strdup(yytext); return STRING;}
"+" { return OP_PLUS; }
"++" { return OP_INC; }
"-" { return OP_MINUS; }
"--" { return OP_DEC; }
"*" { return OP_MULTI; }
"/" { return OP_DIV; }
"$" { return OP_STEN; }
"==" { return OP_EQUAL; }
"=" { return OP_ASSIGN; }
"!=" { return OP_DIFF;}
"&&" { return OP_AND; }
"||" { return OP_OR; }
"!" { return OP_NOT; }
">" { return OP_SUP; }
"<" { return OP_INF; }
">="|"=>" { return OP_SUP_EQUAL; }
"=<"|"<=" { return OP_INF_EQUAL; }
[()] { return yytext[0]; }
[{}] { return yytext[0]; }
"[" { return yytext[0]; }
"]" { return yytext[0]; }
"," { return yytext[0]; }
";" { return yytext[0]; }
\n { }
[\t ] { }
. { printf("[Lex] Unknown character: %s\n", yytext); exit(1);}
%%
// Free the memory allocated for Lex when we are done.
void lex_free() {
yy_delete_buffer(YY_CURRENT_BUFFER);
free(yy_buffer_stack);
}