-
Notifications
You must be signed in to change notification settings - Fork 2
/
gramtree.l
156 lines (133 loc) · 4.56 KB
/
gramtree.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
/**Name:gramtree.l
*Function:词法分析模块,对每个终结符建立一个叶子结点,返回记号,供bison语法分析使用
*/
%x COMMENT
%{
#include "stdio.h"
#include "stdlib.h"
#include "Ast.h"
#include "gramtree.tab.h"
#include "math.h"
int yyparse(void);
%}
/*
*会自动在gramtree.tab.h中分配编号 line:41
*/
%option yylineno
line_commet (\/\/.*\n)
TYPE int|float|void
CONST const
STRUCT struct
RETURN return
IF if
ELSE else
WHILE while
BREAK break
CONTINUE continue
PLUS \+
MINUS \-
INTEGER [1-9]+[0-9]*|0
FLOAT [0-9]+\.[0-9]*
OCT_INT 0[0-7]+
HEX_INT 0[xX][0-9a-fA-F]+
SCI_INT [0-9]+[eE][+-]?[0-9]+
SCI_FLOAT [0-9]*\.[0-9]+([eE][+-]?[0-9]+)?
ID [a-z_A-Z][a-zA-Z_0-9]*
SPACE [ \t\r]*
EOL \n
SEMI ;
COMMA ,
ASSIGNOP =
GREAT >
GREATEQUAL >=
LESS <
LESSEQUAL <=
NOTEQUAL !=
EQUAL ==
STAR \*
DIV \/
MOD \%
AND &&
OR \|\|
DOT \.
NOT !
LP \(
RP \)
LB \[
RB \]
LC \{
RC \}
AERROR .
%%
{line_commet}
"/*" {BEGIN COMMENT;}
<COMMENT>"*/" {BEGIN INITIAL;}
<COMMENT>. {}
"int" {yylval.a=newast("TYPE",0,yylineno);return TYPE;}
"float" {yylval.a=newast("TYPE",0,yylineno);return TYPE;}
"void" {yylval.a=newast("TYPE",0,yylineno);return TYPE;}
"struct" {yylval.a=newast("STRUCT",0,yylineno);return STRUCT;}
"const" {yylval.a=newast("CONST",0,yylineno);return CONST;}
{RETURN} {yylval.a=newast("RETURN",0,yylineno); return RETURN;}
{IF} {yylval.a=newast("IF",0,yylineno);return IF;}
{ELSE} {yylval.a=newast("ELSE",0,yylineno); return ELSE;}
{WHILE} {yylval.a=newast("WHILE",0,yylineno); return WHILE;}
{BREAK} {yylval.a=newast("BREAK",0,yylineno); return BREAK;}
{CONTINUE} {yylval.a=newast("CONTINUE",0,yylineno); return CONTINUE;}
{PLUS} {yylval.a=newast("PLUS",0,yylineno); return PLUS;}
{MINUS} {yylval.a=newast("MINUS",0,yylineno); return MINUS;}
{INTEGER} {yylval.a=newast("INTEGER",0,yylineno); return INTEGER;}
{FLOAT} {yylval.a=newast("FLOAT",0,yylineno); return FLOAT;}
{OCT_INT} {yylval.a=newast("OCT_INT",0,yylineno); return INTEGER;}
{HEX_INT} {yylval.a=newast("HEX_INT",0,yylineno); return INTEGER;}
{SCI_INT} {yylval.a=newast("SCI_INT",0,yylineno); return INTEGER;}
{SCI_FLOAT} {yylval.a=newast("SCI_FLOAT",0,yylineno); return FLOAT;}
{ID} {yylval.a=newast("ID",0,yylineno); return ID;}
{SPACE} {}
{EOL} {}
{SEMI} {yylval.a=newast("SEMI",0,yylineno); return SEMI;}
{COMMA} {yylval.a=newast("COMMA",0,yylineno); return COMMA;}
{ASSIGNOP} {yylval.a=newast("ASSIGNOP",0,yylineno); return ASSIGNOP;}
{GREAT} {yylval.a=newast("GREAT",0,yylineno); return GREAT;}
{GREATEQUAL} {yylval.a=newast("GREATEQUAL",0,yylineno); return GREATEQUAL;}
{LESS} {yylval.a=newast("LESS",0,yylineno); return LESS;}
{LESSEQUAL} {yylval.a=newast("LESSEQUAL",0,yylineno); return LESSEQUAL;}
{NOTEQUAL} {yylval.a=newast("NOTEQUAL",0,yylineno); return NOTEQUAL;}
{EQUAL} {yylval.a=newast("EQUAL",0,yylineno); return EQUAL;}
{STAR} {yylval.a=newast("STAR",0,yylineno); return STAR;}
{DIV} {yylval.a=newast("DIV",0,yylineno); return DIV;}
{MOD} {yylval.a=newast("MOD",0,yylineno); return MOD;}
{AND} {yylval.a=newast("AND",0,yylineno); return AND;}
{OR} {yylval.a=newast("OR",0,yylineno); return OR;}
{DOT} {yylval.a=newast("DOT",0,yylineno); return DOT;}
{NOT} {yylval.a=newast("NOT",0,yylineno); return NOT;}
{LP} {yylval.a=newast("LP",0,yylineno); return LP;}
{RP} {yylval.a=newast("RP",0,yylineno); return RP;}
{LB} {yylval.a=newast("LB",0,yylineno); return LB;}
{RB} {yylval.a=newast("RB",0,yylineno); return RB;}
{LC} {yylval.a=newast("LC",0,yylineno); return LC;}
{RC} {yylval.a=newast("RC",0,yylineno); return RC;}
{AERROR} { printf("Error type A at line %d: Mystirious charachter '%s'\n",yylineno,yytext);}
%%
int yywrap()
{
return 1;
}
int parser(char* input)
{
//创建缓存,并将字符串扫描进缓存中
YY_BUFFER_STATE bp = yy_scan_string(input);
//分配失败
if (bp == NULL) {
printf("error on creating YY_BUFFER_STATE");
return -1;
}
//将输入源转为指定内存
yy_switch_to_buffer(bp);
//词法语法分析
yyparse();
//清理内存
yy_delete_buffer(bp);
yylex_destroy();
return 0;
}