-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCompiler.l
189 lines (154 loc) · 3.97 KB
/
Compiler.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
%{
#include <stdio.h>
#include "y.tab.h"
// #define YY_USER_ACTION yylloc.first_line = yylloc.last_line = yylineno;
extern void yyerror(const char *); /* prints grammar violation message */
extern int sym_type(const char *); /* returns type from symbol table */
#define sym_type(identifier) IDENTIFIER /* with no symbol table, fake it */
static void comment(void);
static int check_type(void);
%}
%option yylineno
O [0-7]
D [0-9]
NZ [1-9]
L [a-zA-Z_]
A [a-zA-Z_0-9]
H [a-fA-F0-9]
HP (0[xX])
E ([Ee][+-]?{D}+)
P ([Pp][+-]?{D}+)
FS (f|F|l|L)
IS (((u|U)(l|L|ll|LL)?)|((l|L|ll|LL)(u|U)?))
CP (u|U|L)
SP (u8|u|U|L)
ES (\\(['"\?\\abfnrtv]|[0-7]{1,3}|x[a-fA-F0-9]+))
WS [ \t\v\n\f]
%%
"/*" { comment(); }
"//"[^\n]* { /* Consume Comment */ }
/* Data Types */
int { return(INT); }
signed { return(SIGNED); }
unsigned { return(UNSIGNED); }
short { return(SHORT); }
long { return(LONG); }
float { return(FLOAT); }
double { return(DOUBLE); }
char { return(CHAR); }
void { return(VOID); }
static { return(STATIC); }
typedef { return(TYPEDEF); }
const { return(CONST); }
main { return(MAIN); }
/* User Defined Data Types */
struct { return(STRUCT); }
sizeof { return(SIZEOF); }
/* Program Flow Statements */
break { return(BREAK); }
continue { return(CONTINUE); }
return { return(RETURN); }
/* Headers */
"#" { return HASH; }
include { return INCLUDE; }
define { return DEFINE; }
/* C Libraries */
"math.h" { return MATH; }
"stdio.h" { return STDIO; }
"stdlib.h" { return STDLIB; }
"string.h" { return STRING; }
"time.h" { return TIME; }
/* Control Structures */
if { return(IF); }
else { return(ELSE); }
for { return(FOR); }
/* User Defined Data Types, Identifiers */
{L}{A}* { return check_type(); }
/* Constants, Numbers, Literals */
{HP}{H}+{IS}? { return I_CONSTANT; }
{NZ}{D}*{IS}? { return I_CONSTANT; }
"0"{O}*{IS}? { return I_CONSTANT; }
{CP}?"'"([^'\\\n]|{ES})+"'" { return I_CONSTANT; }
{D}+{E}{FS}? { return F_CONSTANT; }
{D}*"."{D}+{E}?{FS}? { return F_CONSTANT; }
{D}+"."{E}?{FS}? { return F_CONSTANT; }
{HP}{H}+{P}{FS}? { return F_CONSTANT; }
{HP}{H}*"."{H}+{P}{FS}? { return F_CONSTANT; }
{HP}{H}+"."{P}{FS}? { return F_CONSTANT; }
({SP}?\"([^"\\\n]|{ES})*\"{WS}*)+ { return STRING_LITERAL; }
/* Assignment Operators */
"+=" {return(ADD_ASSIGN); }
"-=" {return(SUB_ASSIGN); }
"*=" {return(MUL_ASSIGN); }
"/=" {return(DIV_ASSIGN); }
"%=" {return(MOD_ASSIGN); }
"&=" {return(AND_ASSIGN); }
"^=" {return(XOR_ASSIGN); }
"|=" {return(OR_ASSIGN); }
/* Relational Operators */
"++" {return(INC_OP); }
"--" {return(DEC_OP); }
"->" {return(PTR_OP); }
"&&" {return(AND_OP); }
"||" {return(OR_OP); }
"<=" {return(LE_OP); }
">=" {return(GE_OP); }
"==" {return(EQ_OP); }
"!=" {return(NE_OP); }
/* Basic Syntac */
";" {return(';'); }
("{"|"<%") {return('{'); }
("}"|"%>") {return('}'); }
"," {return(','); }
":" {return(':'); }
"=" {return('='); }
"(" {return('('); }
")" {return(')'); }
("["|"<:") {return('['); }
("]"|":>") {return(']'); }
"." {return('.'); }
"&" {return('&'); }
"!" {return('!'); }
"~" {return('~'); }
"-" {return('-'); }
"+" {return('+'); }
"*" {return('*'); }
"/" {return('/'); }
"%" {return('%'); }
"<" {return('<'); }
">" {return('>'); }
"^" {return('^'); }
"|" {return('|'); }
"?" {return('?'); }
{WS}+ { /* whitespace separates tokens */}
. { printf("No Match, Invalid Expression %s\n", yytext); }
%%
int yywrap(void)
{
return 1;
}
static void comment(void)
{
int c;
while ((c = input()) != 0)
if (c == '*')
{
while ((c = input()) == '*')
;
if (c == '/')
return;
if (c == 0)
break;
}
yyerror("unterminated comment");
}
static int check_type(void)
{
switch (sym_type(yytext))
{
case TYPEDEF_NAME: /* previously defined */
return TYPEDEF_NAME;
default: /* includes undefined */
return IDENTIFIER;
}
}