-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbs.l
66 lines (60 loc) · 1.11 KB
/
bs.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
%{
#include <stdlib.h>
#include <stdio.h>
#include "ast.h"
#include "y.tab.h"
void yyerror(char *);
int cnt, stk[100], top;
%}
digit [0-9]
number {digit}+
float {number}\.{number}
comment #.*\n
%x INDENT
%%
"\n" { BEGIN INDENT; cnt = 0;return LINE; }
<INDENT>" " { cnt += 1; }
<INDENT>"\t" { cnt += 8; }
<INDENT>"\n" { cnt = 0; }
<INDENT>. {
unput(*yytext);
if(cnt > stk[top-1]) {
stk[top++] = cnt;
return '{';
} else if(cnt < stk[top-1]) {
top--;
return '}';
} else {
BEGIN 0;
}
}
{comment} ;
[a-z] {
yylval.sIndex = *yytext - 'a';
return VARIABLE;
}
{float} {
yylval.iValue = atof(yytext);
printf("float: %s\n", yytext);
return FLOAT;
}
{number} {
yylval.iValue = atoi(yytext);
printf("int: %s\n", yytext);
return INTEGER;
}
[-+()<>=*\/:] {return *yytext;}
">=" { return GE; }
"<=" { return LE; }
"==" { return EQ; }
"!=" { return NE; }
"while" { return WHILE; }
"if" { return IF; }
"else" { return ELSE; }
"print" { return PRINT; }
[ \t]+ ;
. yyerror("invalid character");
%%
int yywrap(void) {
return 1;
}