-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler_hw3.l
More file actions
119 lines (101 loc) · 2.56 KB
/
compiler_hw3.l
File metadata and controls
119 lines (101 loc) · 2.56 KB
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
/* Definition section */
%{
#include "common.h"
#include "y.tab.h" /* header file generated by bison */
extern YYSTYPE yylval;
#define YY_NO_UNPUT
#define YY_NO_INPUT
char var_name[50];
%}
/* Define regular expression label */
letter [a-zA-Z_]
digit [0-9]
id {letter}+({letter}|{digit})*
inumber {digit}+
fnumber ({digit}*\.{digit}+)
newline [\n]
%x CMT
%x STR
%option yylineno
/* Rules section */
%%
"/*" { BEGIN(CMT); }
<CMT>"*/" { BEGIN(INITIAL); }
<CMT>\n {;}
<CMT>. {;}
"//".* {;}
"\"" { BEGIN(STR);
return '"';
}
<STR>"\"" { BEGIN(INITIAL);
return '"';
}
"\"".*"\"" { yytext = yytext + 1;
yytext[strlen(yytext)-1] = '\0';
yylval.s_val = strdup(yytext);
return STRING_LIT;
}
"+" { return ADD; }
"-" { return SUB; }
"*" { return MUL; }
"/" { return QUO; }
"%" { return REM; }
"++" { return INC; }
"--" { return DEC; }
">" { return GTR; }
"<" { return LSS; }
">=" { return GEQ; }
"<=" { return LEQ; }
"==" { return EQL; }
"!=" { return NEQ; }
"=" { return ASSIGN; }
"+=" { return ADD_ASSIGN; }
"-=" { return SUB_ASSIGN; }
"*=" { return MUL_ASSIGN; }
"/=" { return QUO_ASSIGN; }
"%=" { return REM_ASSIGN; }
"&&" { return LAND; }
"||" { return LOR; }
"!" { return NOT; }
"(" { return '('; }
")" { return ')'; }
"[" { return '['; }
"]" { return ']'; }
"{" { return '{'; }
"}" { return '}'; }
";" { return SEMICOLON; }
"," { return ','; }
"if" { return IF; }
"else" { return ELSE; }
"while" { return WHILE; }
"for" { return FOR; }
"print" { return PRINT; }
"int" { return INT; }
"float" { return FLOAT; }
"string" { return STRING; }
"bool" { return BOOL; }
"true" { return TRUE; }
"false" { return FALSE; }
{inumber} { yylval.i_val = atoi(yytext);
return INT_LIT;
}
{fnumber} { yylval.f_val = atof(yytext);
return FLOAT_LIT;
}
{id} { yylval.id = strdup(yytext);
return IDENT;
}
<<EOF>> { static int once = 0;
if (once++) {
yyterminate();
}
}
[\n] {;}
[ \t]+ {;}
. {;}
%%
/* C Code section */
int yywrap(void)
{
return 1;
}