-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler_hw3.l
159 lines (131 loc) · 4.92 KB
/
compiler_hw3.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* Definition section */
%{
#include "y.tab.h" /* header file generated by bison */
extern YYSTYPE yylval; //YYSTYPE
extern void dump_symbol(int);
extern int dump_flag;
extern void semantic_error(char [], int);
extern int error_flag;
extern char sem_error_msg[100];
#define BUF_SIZE 256
char buf[BUF_SIZE];
/* This macro is used to record code line */
#define CONCAT { strncat(buf, yytext, BUF_SIZE - strlen(buf) - 1); }
int while_label = 0;
%}
/* Define regular expression label */
digit [0-9]
letter [a-zA-Z]
integer {digit}+
float {digit}+"."{digit}+
id {letter}+({letter}|{digit})*
%option yylineno
%x COMMENT STRING_STATE
/* Rules section */
%%
/* Arithmetic */
"+" { yylval.atom.string_val = "+"; CONCAT; return ADD; }
"-" { yylval.atom.string_val = "-"; CONCAT; return SUB; }
"*" { yylval.atom.string_val = "*"; CONCAT; return MUL; }
"/" { yylval.atom.string_val = "/"; CONCAT; return DIV; }
"%" { yylval.atom.string_val = "%"; CONCAT; return MOD; }
"++" { yylval.atom.string_val = "++"; CONCAT; return INC; }
"--" { yylval.atom.string_val = "--"; CONCAT; return DEC; }
/* Relational */
">" { yylval.atom.string_val = ">"; CONCAT; return MT; }
"<" { yylval.atom.string_val = "<"; CONCAT; return LT; }
">=" { yylval.atom.string_val = ">="; CONCAT; return MTE; }
"<=" { yylval.atom.string_val = "<="; CONCAT; return LTE; }
"==" { yylval.atom.string_val = "=="; CONCAT; return EQ; }
"!=" { yylval.atom.string_val = "!="; CONCAT; return NE; }
/* Assignment */
"=" { CONCAT; return ASGN; }
"+=" { yylval.atom.string_val = "+="; CONCAT; return ADDASGN; }
"-=" { yylval.atom.string_val = "-="; CONCAT; return SUBASGN; }
"*=" { yylval.atom.string_val = "*="; CONCAT; return MULASGN; }
"/=" { yylval.atom.string_val = "/="; CONCAT; return DIVASGN; }
"%=" { yylval.atom.string_val = "%="; CONCAT; return MODASGN; }
/* Logical */
"&&" { yylval.atom.string_val = "&&"; CONCAT; return AND; }
"||" { yylval.atom.string_val = "||"; CONCAT; return OR; }
"!" { yylval.atom.string_val = "!"; CONCAT; return NOT; }
/* Delimiters */
"(" { yylval.atom.string_val = "("; CONCAT; return LB; }
")" { yylval.atom.string_val = ")"; CONCAT; return RB; }
"{" { yylval.atom.string_val = "{"; CONCAT; return LCB; }
"}" { yylval.atom.string_val = "}"; CONCAT; return RCB; }
"[" { yylval.atom.string_val = "["; CONCAT; return LSB; }
"]" { yylval.atom.string_val = "]"; CONCAT; return RSB; }
"," { yylval.atom.string_val = ","; CONCAT; return COMMA; }
/* Print Keywords */
"print" { yylval.atom.string_val = yytext; CONCAT; return PRINT; }
/* Condition and Loop Keywords */
"if" { yylval.atom.string_val = yytext; CONCAT; return IF; }
"else" { yylval.atom.string_val = yytext; CONCAT; return ELSE; }
"for" { yylval.atom.string_val = yytext; CONCAT; return FOR; }
"while" { yylval.atom.string_val = yytext; yylval.atom.i_val = while_label++; CONCAT; return WHILE; }
/* Declaration Keywords */
"void" { CONCAT; yylval.atom.string_val = "void"; return VOID; }
"int" { CONCAT; yylval.atom.string_val = "int"; return INT; }
"float" { CONCAT; yylval.atom.string_val = "float"; return FLOAT; }
"string" { CONCAT; yylval.atom.string_val = "string"; return STRING; }
"bool" { CONCAT; yylval.atom.string_val = "bool"; return BOOL; }
/* boolean Keywords */
"true" { yylval.atom.string_val = "true"; CONCAT; return TRUE; }
"false" { yylval.atom.string_val = "false"; CONCAT; return FALSE; }
"return" { yylval.atom.string_val = yytext; CONCAT; return RET; }
/* String Constant */
\" { BEGIN STRING_STATE; CONCAT; }
<STRING_STATE>\" { BEGIN INITIAL; CONCAT; }
<STRING_STATE>[^"]* { yylval.atom.string_val = yytext; CONCAT; return S_CONST; }
/* Number Constant */
{integer} { yylval.atom.i_val = atoi(yytext); CONCAT; return I_CONST; }
{float} { yylval.atom.f_val = atof(yytext); CONCAT; return F_CONST; }
/* C type Comment */
"/*" { BEGIN COMMENT; CONCAT; return START_COMMENT; }
<COMMENT>[^*\n]+ { CONCAT; }
<COMMENT>\n {
CONCAT;
printf("%d: %s", yylineno-1, buf);
bzero(buf, BUF_SIZE);
}
<COMMENT>"*" { CONCAT; }
<COMMENT>"*/" {
BEGIN INITIAL;
CONCAT;
return END_COMMENT;
}
/* C++ type Comment */
\/\/.* { CONCAT; return CPLUS_COMMENT; }
/* Variable ID */
{id} {
yylval.atom.string_val = strdup(yytext);
//printf("lex:%s\n", yylval.string_val);
CONCAT;
return ID;
}
/* others */
[;] { yylval.atom.string_val = yytext; CONCAT; return SEMICOLON; }
[\n] {
//CONCAT;
if(buf[0] == '\0') printf("%d:\n", yylineno-1);
else printf("%d: %s\n", yylineno-1, buf);
if(error_flag == 1) {
semantic_error(sem_error_msg, yylineno-1);
error_flag = 0;
}
bzero(buf, BUF_SIZE);
if(dump_flag == 1) {
dump_symbol(1);
dump_flag = 0;
}
}
[\t] { CONCAT; } /* Ignore */
[ ] { CONCAT; }
[^ \t\n] { } /* Ignore other charactor sets */
<<EOF>> { yyterminate(); }
%%
int yywrap()
{
return 1;
}