-
Notifications
You must be signed in to change notification settings - Fork 0
/
compyler.l
119 lines (94 loc) · 3.47 KB
/
compyler.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
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
%{
#include <stdlib.h>
#include <string.h>
#include "AST.h"
#include "y.tab.h"
%}
NoZeroDecimal [1-9][0-9]*
Decimal [0-9]
Alpha [a-zA-Z_]
Hexa [a-fA-F0-9]
Octal [0-7]
%%
"bool" { return(BOOL); }
"break" { return(BREAK); }
"char" { return(CHAR); }
"const" { return(CONST); }
"continue" { return(CONTINUE); }
"else" { return(ELSE); }
"extern" { return(EXTERN); }
"float4" { return(FLOAT4); }
"float8" { return(FLOAT8); }
"float" { return(FLOAT4); }
"for" { return(FOR); }
"if" { return(IF); }
"in" { return(IN); }
"int2" { return(INT2); }
"int4" { return(INT4); }
"int8" { return(INT8); }
"int" { return(INT4); }
"printf" { return(PRINT); }
"return" { return(RETURN); }
"scanf" { return(SCAN); }
"sizeof" { return(SIZEOF); }
"static" { return(STATIC); }
"void" { return(VOID); }
"volatile" { return(VOLATILE); }
"while" { return(WHILE); }
"include" { return(INCLUDE); }
"define" { return(DEFINE); }
"ifdef" { return(IFDEF); }
"ifndef" { return(IFNDEF); }
"endif" { return(ENDIF); }
\"(\\.|[^\\\"])*\" { yylval.sval = strdup(yytext); return(STRING_LITERAL);
/* either \. comes in sequence
or any characters besides \ and ". */ }
{Alpha}({Alpha}|{Decimal})* { yylval.sval = yytext; return(IDENTIFIER);
/* if check_type() not needed, just return(IDENTIFIER) */ }
{NoZeroDecimal} { yylval.ival = atoi(yytext); return(CONSTANT_INT);
/* decimal expression starting with none-zero */ }
{Decimal} { yylval.ival = atoi(yytext); return(CONSTANT_INT); }
{NoZeroDecimal}"."{Decimal}* { yylval.fval = atof(yytext); return(CONSTANT_FLOAT);
/* floating point ( there is a number before . ) */}
"."{Decimal}+ { yylval.fval = atof(yytext); return(CONSTANT_FLOAT);
/* floating point ( with no number before . ) */}
\'[a-zA-Z0-9]\' { yylval.sval = yytext; return(CONSTANT_CHAR);
/* includes multi character literal */ }
"=" { return('='); }
"+=" { return(ADD_ASSIGN); }
"-=" { return(SUB_ASSIGN); }
"*=" { return(MUL_ASSIGN); }
"/=" { return(DIV_ASSIGN); }
"%=" { return(MOD_ASSIGN); }
"++" { return(INC_OP); }
"--" { return(DEC_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('#'); }
[ \t\v\n\f] { /* skip */ }
. { /* ignore unexpected characters */ }
%%
int yywrap() {
return 1;
}