-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.l
247 lines (206 loc) · 5.35 KB
/
source.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
%{
#define MAX_LINE_LENGTH 256
#define LIST strcat(buf,yytext)
// #define token(s) {LIST; printf("<%s>\n", s);}
// #define tokenIdent(t) {LIST; printf("<identifier:%s>\n", t);}
// #define tokenNum(t) {LIST; printf("<num:%s>\n", t);}
#define token(s) {LIST; }
#define tokenIdent(t) {LIST; }
#define tokenNum(t) {LIST; }
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdbool.h>
#include "y.tab.h"
// char* uppercase(char* s);
int linenum = 1;
char buf[MAX_LINE_LENGTH];
char finalBuf[MAX_LINE_LENGTH];
char strBuf[MAX_LINE_LENGTH];
void create();
int lookup(char* s);
int insert(char* s);
void dump();
void printSymbols();
void printLine(int lineNum, char* lineText);
%}
DIGITS [0-9]+
LETTER [a-zA-Z]
ID {LETTER}({LETTER}|{DIGITS})*
%Start com multiCom multiEnd string stringOne stringTwo stringThree
%%
\% {
// LIST;
BEGIN com;}
<com>.* {
// LIST;
BEGIN 0;}
<multiCom>\n {
// LIST;
// printf("%d: %s", linenum, buf);
linenum++;
yylineno++;
buf[0] = '\0';
BEGIN multiCom; }
<multiCom>(\*+)\} {
// LIST;
BEGIN 0; }
<multiCom>[^%}\n]+ {
// LIST;
BEGIN multiCom; }
\{\% {
// LIST;
BEGIN multiCom; }
<stringOne>\"\" {
LIST;
printf("\"");
strcat(strBuf, "\"");
BEGIN string; }
<stringOne>\" {
LIST;
printf(">\n");
BEGIN 0;
yylval.sval = strBuf;
return STRING; }
\" {
LIST;
printf("<string:");
strBuf[0] = '\0';
BEGIN string; }
<string>[^\"]*/\" {
LIST;
printf("%s", yytext);
strcat(strBuf, yytext);
BEGIN stringOne; }
"." { token("."); return DOT; }
"," { token(","); return COMMA; }
":" { token(":"); return COLON; }
";" { token(";"); return SEMICOLON; }
"(" { token("("); return POPEN; }
")" { token(")"); return PCLOSE; }
"[" { token("["); return SOPEN; }
"]" { token("]"); return SCLOSE; }
"{" { token("{"); return BOPEN; }
"}" { token("}"); return BCLOSE; }
"+" { token("+"); return ADD; }
"-" { token("-"); return SUB; }
"*" { token("*"); return MUL; }
"/" { token("/"); return DIV; }
"mod"/.* { token("mod"); return MOD; }
":=" { token(":="); return ASSIGN; }
"<" { token("<"); return RELOPLT; }
"<=" { token("<="); return RELOPLE; }
">" { token(">"); return RELOPGT; }
">=" { token(">="); return RELOPGE; }
"=" { token("="); return RELOPEQ; }
"not=" { token("not="); return RELOPNE; }
"and" { token("and"); return AND; }
"or" { token("or"); return OR; }
"not" { token("not"); return NOT; }
"array" { token("ARRAY"); return ARRAY; }
"begin" { token("BEGIN"); return BEGINT; }
"bool" { token("BOOL"); return BOOL; }
"char" { token("CHAR"); return CHAR; }
"const" { token("CONST"); return CONST; }
"decreasing" { token("DECREASING"); return DECREASING; }
"default" { token("DEFAULT"); return DEFAULT; }
"do" { token("DO"); return DO; }
"else" { token("ELSE"); return ELSE; }
"end" { token("END"); return END; }
"exit" { token("EXIT"); return EXIT; }
"false" { token("FALSE"); return FALSE; }
"for" { token("FOR"); return FOR; }
"function" { token("FUNCTION"); return FUNCTION;}
"get" { token("GET"); return GET; }
"if" { token("IF"); return IF;}
"int" { token("INT"); return INT; }
"loop" { token("LOOP"); return LOOP; }
"of" { token("OF"); return OF;}
"put" { token("PUT"); return PUT; }
"procedure" { token("PROCEDURE"); return PROCEDURE; }
"real" { token("REAL"); return REAL;}
"result" { token("RESULT"); return RESULT;}
"return" { token("RETURN"); return RETURN;}
"skip" { token("SKIP"); return SKIP; }
"string" { token("STRING"); return STRING;}
"then" { token("THEN"); return THEN; }
"true" { token("TRUE"); return TRUE; }
"var" { token("VAR"); return VAR; }
"when" { token("WHEN"); return WHEN; }
{DIGITS}\.{DIGITS} {
// printf("digit\n");
tokenNum(yytext);
yylval.dval = atof(yytext);
return REAL; }
{DIGITS} {
// printf("digit\n");
tokenNum(yytext);
yylval.ival = atoi(yytext);
return INT; }
{ID} {
// printf("id\n");
tokenIdent(yytext);
int idIndex = lookup(yytext);
if(idIndex == -1) {
idIndex = insert(yytext);
if(idIndex == -1) {
printf("Fail to insert new identifier.");
}
}
yylval.ival = idIndex;
// yylval.sval = strdup(yytext);
return ID;
// printf("index: %d\n", idIndex);
}
\n {
// LIST;
// printf("%d: %s", linenum, buf);
printLine(linenum, buf);
// if(buf[0] != '\0') {
// strcpy(finalBuf, buf);
// }
yylineno++;
linenum++;
buf[0] = '\0';
/* Only for debug */
// return NEWLINE;
}
[ \t]* {
LIST;
}
%%
char* getLineText() {
return finalBuf;
}
// char* append(char* s, char c) {
// size_t size = strlen(s);
// char* newStr = malloc(size + 2);
// strcpy(newStr, s);
// newStr[size] = c;
// newStr[size + 1] = '\0';
// return newStr;
// }
// char* uppercase(char* s) {
// size_t size = strlen(s);
// char* newStr = malloc(size+1);
// for(int i = 0; i < (int) size; i++) {
// newStr[i] = toupper(s[i]);
// }
// newStr[size] = '\0';
// return newStr;
// }
// int yywrap(void){
// return 1;
// }
// int main(int argc, char** argv)
// {
// if (argc > 0) {
// yyin = fopen(argv[1], "r");
// } else {
// yyin = stdin;
// }
// create();
// yylex();
// dump();
// return 0;
// }