-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.l
More file actions
118 lines (85 loc) · 1.35 KB
/
scan.l
File metadata and controls
118 lines (85 loc) · 1.35 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
%{
#include "y.tab.h"
int yylineno;
extern YYSTYPE yylval;
#define EQ 0
#define LE 1
#define GE 2
#define NE 3
#define LT 4
#define GT 5
#define TRUE 1
#define FALSE 0
%}
id [_a-zA-Z][_0-9a-zA-Z]*
num [0-9]+
mathop [-+\*\/\%]
logop AND|OR|NOT
other [\(\)\[\]\{\}\;,=]
%%
[ \t] ;
[\n] yylineno++;
main return MAIN;
if return IF;
then return THEN;
else return ELSE;
endif return ENDIF;
decl return DECL;
enddecl return ENDDECL;
begin return BEG;
end return END;
while return WHILE;
do return DO;
endwhile return ENDWHILE;
return return RETURN;
read return READ;
write return WRITE;
integer return INTEGER;
boolean return BOOLEAN;
(true|TRUE) {yylval.ival=TRUE;
return BNUM;
}
(false|FALSE) {
yylval.ival=FALSE;
return BNUM;
}
{num} {
yylval.ival=atoi(yytext);
return NUM;
}
{mathop} {return *yytext;}
== {
yylval.relop=EQ;
return RELOP;
}
\>= {
yylval.relop=GE;
return RELOP;
}
\<= {
yylval.relop=LE;
return RELOP;
}
!= {
yylval.relop=NE;
return RELOP;
}
\< {
yylval.relop=LT;
return RELOP;
}
\> {
yylval.relop=GT;
return RELOP;
}
{logop} {return *yytext;}
{other} {return *yytext;}
{id} {
char *temp=malloc(yyleng*sizeof(char)+1);
strcpy(temp, yytext);
temp[yyleng]='\0';
yylval.str=temp;
return ID;
}
. yyerror("invalid character\n");
%%