-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler_hw1.l
97 lines (92 loc) · 3.12 KB
/
compiler_hw1.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
/* Definition section */
%{
#include <stdio.h>
#include <stdlib.h>
int line_number = 0;
int cmt_number = 0;
%}
/* Define regular expression label */
letter [a-zA-Z]
digit [0-9]
id {letter}+({letter}|{digit})*
number {digit}+
float_number {digit}+[\.]+{number}
%x C_Comment
%x Str
/* Rules section */
%%
"/*" {BEGIN(C_Comment);printf("%s", yytext);++cmt_number;}
<C_Comment>\n {++cmt_number;++line_number;printf("\n");}
<C_Comment>"*/" { BEGIN(INITIAL);printf("%s \t C Comment\n", yytext); }
<C_Comment>. {printf("%s", yytext); }
"\"" { BEGIN(Str);printf("%s \t QUOTA\n", yytext); }
<Str>"\"" { BEGIN(INITIAL);printf("%s \t QUOTA\n", yytext); }
<Str>([^\\\"]|\\.)+ {printf("%s \t STR_CONST\n", yytext); }
"//".* { ++cmt_number;printf("%s \t C++ Comment\n", yytext); }
"+" { printf("%s \t ADD\n", yytext); }
"-" { printf("%s \t SUB\n", yytext); }
"*" { printf("%s \t MUL\n", yytext); }
"/" { printf("%s \t DIV\n", yytext); }
"%" { printf("%s \t MOD\n", yytext); }
"++" { printf("%s \t INC\n", yytext); }
"--" { printf("%s \t DEC\n", yytext); }
">" { printf("%s \t MT\n", yytext); }
"<" { printf("%s \t LT\n", yytext); }
">=" { printf("%s \t MTE\n", yytext); }
"<=" { printf("%s \t LTE\n", yytext); }
"==" { printf("%s \t EQ\n", yytext); }
"!=" { printf("%s \t NE\n", yytext); }
"[" { printf("%s \t LSB\n", yytext); }
"]" { printf("%s \t RSB\n", yytext); }
"," { printf("%s \t COMMA\n", yytext); }
";" { printf("%s \t SEMICOLON\n", yytext); }
"print" { printf("%s \t PRINT\n", yytext); }
"if" { printf("%s \t IF\n", yytext); }
"else" { printf("%s \t ELSE\n", yytext); }
"for" { printf("%s \t FOR\n", yytext); }
"while" { printf("%s \t WHILE\n", yytext); }
"string" { printf("%s \t STRING\n", yytext); }
"int" { printf("%s \t INT\n", yytext); }
"float" { printf("%s \t FLOAT\n", yytext); }
"=" { printf("%s \t ASGN\n", yytext); }
"+=" { printf("%s \t ADDASGN\n", yytext); }
"-=" { printf("%s \t SUBASGN\n", yytext); }
"*=" { printf("%s \t MULASGN\n", yytext); }
"/=" { printf("%s \t DIVASGN\n", yytext); }
"%=" { printf("%s \t MODASGN\n", yytext); }
"&&" { printf("%s \t AND\n", yytext); }
"||" { printf("%s \t OR\n", yytext); }
"!" { printf("%s \t NOT\n", yytext); }
"(" { printf("%s \t LB\n", yytext); }
")" { printf("%s \t RB\n", yytext); }
"{" { printf("%s \t LCB\n", yytext); }
"}" { printf("%s \t RCB\n", yytext); }
"void" { printf("%s \t VOID\n", yytext); }
"bool" { printf("%s \t BOOL\n", yytext); }
"true" { printf("%s \t TRUE\n", yytext); }
"false" { printf("%s \t FALSE\n", yytext); }
"return" { printf("%s \t RET\n", yytext); }
"continue" { printf("%s \t CONT\n", yytext); }
"break" { printf("%s \t BREAK\n", yytext); }
{digit} { printf("%s \t I_CONST\n", yytext); }
{number} { printf("%s \t I_CONST\n", yytext); }
{float_number} { printf("%s \t F_CONST\n", yytext); }
{id} { printf("%s \t ID\n", yytext); }
" " { ; }
\t { ; }
\n {++line_number;}
. {;}// undefined characters
%%
/* C Code section */
int yywrap(void)
{
return 1;
}
int main(int argc,char *argv[])
{
yyin = fopen(argv[1],"r");
yylex();
printf("\nParse over, the line number is %d.\n\n", line_number);
printf("comment: %d lines\n\n", cmt_number);
return 0;
}