-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenize.l
147 lines (115 loc) · 2.3 KB
/
tokenize.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
%{
#include<iostream>
#include<vector>
#include<stdlib.h>
#include<string.h>
#include<string>
#include "symboltable.cpp"
#include "y.tab.h"
using namespace std;
extern YYSTYPE yylval;
extern SymbolTable *table;
void yyerror(char *);
int line_count = 1;
int error = 0;
int scope = 0;
extern FILE *logout;
%}
digit [0-9]
letter [A-Za-z]
delim [ \t\r\f\v]
newline \n
ws [delim]+
id (_|{letter})(_|{letter}|{digit})*
integer {digit}+
number {digit}*(\.{digit}+)?(E[+-]?{digit}+)?
ADDOP [+-]
MULOP [*/%]
%%
{delim}+ {}
{newline} {line_count++;}
"if" {return IF;}
"else" {return ELSE;}
"for" {return FOR;}
"while" {return WHILE;}
"int" {return INT;}
"float" {return FLOAT;}
"return" {return RETURN;}
"void" {return VOID;}
"println" {return PRINTLN;}
{ADDOP} {
SymbolInfo *s= new SymbolInfo(yytext, (char *)"ADDOP");
s->description = s->name;
yylval = (YYSTYPE)s;
return ADDOP;
}
{MULOP} {
SymbolInfo *s= new SymbolInfo(yytext, (char *)"MULOP");
s->description = s->name;
if(s->name != "*") s->description = s->description + s->name;
yylval = (YYSTYPE)s;
return MULOP;
}
"=" {return ASSIGNOP;}
">" |
"<" |
">=" |
"<=" |
"==" |
"!=" {
SymbolInfo *s= new SymbolInfo(yytext, (char *)"RELOP");
s->description = s->name;
yylval = (YYSTYPE)s;
return RELOP;
}
"&&" |
"||" {
SymbolInfo *s= new SymbolInfo(yytext, (char *)"LOGICOP");
s->description = s->name;
yylval = (YYSTYPE)s;
return LOGICOP;
}
"!" { return NOT; }
";" { return SEMICOLON;}
"," {return COMMA;}
"(" {return LPAREN;}
")" {return RPAREN;}
"{" {
scope++;
return LCURL;
}
"}" {
if(scope>=1)
{
scope--;
return RCURL;
}
fprintf(logout, "Error at line %d : Extra RCURL\n", line_count);
}
"[" {return LTHIRD;}
"]" {return RTHIRD;}
"++" {return INCOP;}
"--" {return DECOP;}
{integer} {
SymbolInfo *s= new SymbolInfo(yytext, (char *)"CONST_INT");
s->description = s->name;
yylval = (YYSTYPE)s;
return CONST_INT;
}
{number} {
SymbolInfo *s= new SymbolInfo(yytext, (char *)"CONST_FLOAT");
s->description = s->name;
yylval = (YYSTYPE)s;
return CONST_FLOAT;
}
{id} {
SymbolInfo *s= new SymbolInfo(yytext, (char *)"");
s->description = "";
yylval = (YYSTYPE)s;
return ID;
}
%%
int yywrap(void)
{
return 1;
}