forked from XIEXIEA97/PYC_COMPILER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl_C_compiler_lex.l
106 lines (91 loc) · 3 KB
/
l_C_compiler_lex.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
%{
#include "util.h"
#include "y.tab.h"
void count();
char linebuffer[500];
int linebuffer_pos=0;
int pos = 0;
%}
D [0-9]
L [a-zA-Z_]
O [0-7]
H [a-fA-F0-9]
E [Ee][+-]?{D}+
%%
"/*"([^*/]+[\*\/]?)*"*/" { count(); }
"//"[^\n]*\n { count(); }
"break" { count(); return BREAK; }
"case" { count(); return CASE; }
"char" { count(); return CHAR; }
"continue" { count(); return CONTINUE; }
"default" { count(); return DEFAULT; }
"do" { count(); return DO; }
"else" { count(); return ELSE; }
"float" { count(); return FLOAT; }
"for" { count(); return FOR; }
"if" { count(); return IF; }
"int" { count(); return INT; }
"return" { count(); return RETURN; }
"struct" { count(); return STRUCT; }
"switch" { count(); return SWITCH; }
"void" { count(); return VOID; }
"while" { count(); return WHILE; }
{L}({L}|{D})* { count(); yylval.id = strdup(yytext); return IDENTIFIER; }
0[xX]{H}+ { count(); int a; sscanf(yytext, "%x", &a); yylval.constant = L_Num(a); return CONSTANT; }
0{O}+ { count(); int a; sscanf(yytext, "%o", &a); yylval.constant = L_Num(a); return CONSTANT; }
{D}+ { count(); int a; sscanf(yytext, "%d", &a); yylval.constant = L_Num(a); return CONSTANT; }
{D}+"."{D}* { count(); double a; sscanf(yytext, "%lf", &a); yylval.constant = L_Real(a); return CONSTANT; }
'(\\.|[^\\'])+' { count(); char a = yytext[strlen(yytext)-2]; yylval.constant = L_Char(a); return CONSTANT; }
\"(\\.|[^\\"])*\" { count(); yytext[strlen(yytext)-1] = 0; yylval.id = strdup(yytext+1); return STRING_LITERAL; }
">>" { count(); return RIGHT_OP; }
"<<" { count(); return LEFT_OP; }
"->" { count(); return PTR_OP; }
"&&" { count(); return AND_OP; }
"||" { count(); return OR_OP; }
"<=" { count(); return LE_OP; }
">=" { count(); return GE_OP; }
"==" { count(); return EQ_OP; }
"!=" { count(); return NE_OP; }
";" { count(); return ';'; }
"{" { count(); return '{'; }
"}" { count(); return '}'; }
"," { count(); return ','; }
":" { count(); return ':'; }
"=" { count(); return '='; }
"(" { count(); return '('; }
")" { count(); return ')'; }
"[" { count(); return '['; }
"]" { count(); return ']'; }
"." { count(); return '.'; }
"&" { count(); return '&'; }
"!" { count(); return '!'; }
"~" { count(); return '~'; }
"-" { count(); return '-'; }
"+" { count(); return '+'; }
"*" { count(); return '*'; }
"/" { count(); return '/'; }
"%" { count(); return '%'; }
"<" { count(); return '<'; }
">" { count(); return '>'; }
"^" { count(); return '^'; }
"|" { count(); return '|'; }
"?" { count(); return '?'; }
"\r\n" { count(); linebuffer_pos=0; error_newline();}
"\n" { count(); linebuffer_pos=0; error_newline();}
[ \t\v\f] { count(); /* skip blank character */ }
"#" { return 0; }
. { count(); parse_error("unknown character");/* bad characters */ }
%%
int yywrap() //multiple text
{
return 1;
}
void count()
{
int i;
strcpy(linebuffer+linebuffer_pos, yytext);
for (i=0; yytext[i]!='\0'; i++){
pos++;
linebuffer_pos++;
}
}