-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.l
90 lines (76 loc) · 2.22 KB
/
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
%option yylineno
%{
#include "string.h"
int ErrorCharNum=0;
int yycolumn=1;
#define YY_USER_ACTION \
yylloc.first_line=yylloc.last_line=yylineno; \
yylloc.first_column=yycolumn;\
yylloc.last_column=yycolumn+yyleng-1;\
yycolumn+=yyleng;
typedef struct {
int type_int;
int type_float;
char type_id[32];
} YYLVAL;
#define YYSTYPE YYLVAL
#include "parser.tab.hpp"
%}
id [A-Za-z][A-Za-z0-9]*
intconst 0|([1-9][0-9]*)
floatconst [0-9]*\.?[0-9]?([eE][-+]?[0-9]+)?
Oneline_comment "//"[^\n]*
Multiline_comment "/*"([^\*]|(\*)*[^\*/])*(\*)*"*/"
%%
"int" {strcpy(yylval.type_id, yytext);return TYPE;}
"float" {strcpy(yylval.type_id, yytext);return TYPE;}
"void" {strcpy(yylval.type_id, yytext);return TYPE;}
"return" {return RETURN;}
"if" {return IF;}
"else" {return ELSE;}
"while" {return WHILE;}
"for" {return FOR;}
"break" {return BREAK;}
"continue" {return CONTINUE;}
"switch" {return SWITCH;}
"case" {return CASE;}
"default" {return DEFAULT;}
{id} {strcpy(yylval.type_id,yytext); return ID;}
":" {return COLON;}
";" {return SEMI;}
"," {return COMMA;}
">=" {strcpy(yylval.type_id, yytext);return GE;}
">" {strcpy(yylval.type_id, yytext);return GT;}
"<=" {strcpy(yylval.type_id, yytext);return LE;}
"<" {strcpy(yylval.type_id, yytext);return LT;}
"!=" {strcpy(yylval.type_id, yytext);return NE;}
"==" {strcpy(yylval.type_id, yytext);return EQ;}
"=" {return ASSIGN;}
"++" {return DPLUS;}
"--" {return DMINUS;}
"+" {return PLUS;}
"-" {return MINUS;}
"*" {return STAR;}
"/" {return DIV;}
"%" {return MOD;}
"&&" {return AND;}
"||" {return OR;}
"!" {return NOT;}
"(" {return LP;}
")" {return RP;}
"{" {return LC;}
"}" {return RC;}
"[" {return LB;}
"]" {return RB;}
{intconst} { yylval.type_int=atoi(yytext); return INT;}
{floatconst} { yylval.type_float=atof(yytext); return FLOAT;}
[ \r\t] {}
{Oneline_comment} {} //单行注释
{Multiline_comment} {} //多行注释
[\n] {yycolumn=1;}
. { printf("在第 %d 行出现不可识别的符号 \'%s\' \n",yylineno,yytext); ErrorCharNum++;}
%%
int yywrap()
{
return 1;
}