-
Notifications
You must be signed in to change notification settings - Fork 5
/
compiler.l
62 lines (55 loc) · 1.01 KB
/
compiler.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
%{
#include <stdio.h>
#include "y.tab.h"
int lvl = 0;
int line_num = -1;
%}
alpha [a-zA-Z]
digit [0-9]
%%
[\t] {return TAB;}
[\n] {
line_num++;
FILE *fptr = fopen("line_num.txt", "w");
fprintf(fptr,"%d", line_num);
fclose(fptr);
return NL;}
" " ;
"+" {return *yytext;}
"-" {return *yytext;}
"*" {return *yytext;}
"/" {return *yytext;}
"print" {return PRINT;}
"int" {return INT;}
"float" {return FLOAT;}
"else" {return ELSE;}
"endif" {
lvl--;
FILE *fptr = fopen("if_lvl.txt", "w");
fprintf(fptr,"%d", lvl);
fclose(fptr);
return ENDIF;}
"if" {
lvl++;
FILE *fptr = fopen("if_lvl.txt", "w");
fprintf(fptr,"%d", lvl);
fclose(fptr);
return IF;}
"endfor" {return ENDFOR;}
"end" {return END;}
"for" {return FOR;}
"in" {return IN;}
"range" {return RANGE;}
"start" {return START;}
{digit}+"."{digit}+ {return FLOAT_DIGIT;}
{digit}+ {return DIGIT;}
{alpha}({alpha}|{digit})* {yylval.str=strdup(yytext);
return ID;}
\/\/.* ;
\/\*(.*\n)*.*\*\/ ;
. {return *yytext;}
%%
yywrap()
{
return 1;
}