-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_project.l
More file actions
93 lines (76 loc) · 1.55 KB
/
final_project.l
File metadata and controls
93 lines (76 loc) · 1.55 KB
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
%{
#include "y.tab.h"
int column=0;
%}
%%/*yyleng是你傳入的token的長度*/
"mod" {
printf("Lex mod\n");
return mod;
}
"print-bool" {
printf("Lex print-bool\n");
return printbool;
}
"print-num" {
printf("Lex print-num\n");
return printnum;
}
"if" {
printf("Lex if\n");
return ifop;
}
"define" {
printf("Lex define\n");
return define;
}
"lambda" {
printf("Lex lambda\n");
return lambda;
}
"and" {
printf("Lex and-op\n");
return andop;
}
"or" {
printf("Lex or-op\n");
return orop;
}
"not" {
printf("Lex not-op\n");
return notop;
}
0|[1-9][0-9]*|-[1-9][0-9]* {
column=column+yyleng;
yylval.ival=atoi(yytext);
yylval.var.value=atoi(yytext);
yylval.var.Datatype=2;
printf("Lex number\n");
return number;
}
[a-z]([a-z]|[0-9]|"-")* {
column=column+yyleng;
printf("Lex ID: %s\n", yytext);
yylval.var.Name=yytext;
return id;
}
"#t"|"#f" {
column=column+yyleng;
printf("Lex boolval\n");
yylval.var.Datatype=1;
if(yytext[1]=='t')
yylval.var.value=1;
else if(yytext[1]=='f')
yylval.var.value=0;
printf("Lex bool is %d\n", yylval.var.value);
return boolval;
}
"("|")"|"+"|"-"|"*"|"/"|"<"|">"|"=" {
column=column+yyleng;
printf("Lex %c\n", yytext[0]);
return yytext[0];
}
\n {printf("Lex linefeed\n");/*Not EOF*/}
[\t]+ {/* Do nothing */}
[ ]+ {printf("Lex blank\n");/*遇到空格, 也要加一個column, 因為空格也算一行*/column=column+yyleng;}
. {printf("Lex . %c\n", yytext[0]);/*printf("current line: %d\n", column);printf("Got %c\n", yytext[0]);*/return(yytext[0]);}
%%