-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlessie.l
151 lines (124 loc) · 3.05 KB
/
lessie.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
%{
#include "common.h"
#include <string.h>
#include <stdlib.h>
#include "lessie.tab.h"
int createCnumber();
double truncCreal();
char* createId();
char* createStringLiteral();
const int MAX_LENGTH = 25; // maximum length of id in characters
%}
%option noyywrap
/* %option c++ */
DIGIT [0-9]
REAL {DIGIT}+\.{DIGIT}+
WHITESPACE [ \t]
LETTER [a-zA-Z_]
%%
"if" { return IF; }
"else" { return ELSE; }
"return" { return RETURN; }
"for" { return FOR; }
"class" {return CLASS_; }
"new" {return NEW_; }
"def" {return DEF_;}
"var" {return VAR_;}
{WHITESPACE}+
"\n" {yylineno++;}
"::" { return COLON_;}
"}" { return CURLY_CLOSE_; }
"{" { return CURLY_OPEN_; }
"]" { return SQUARE_CLOSE_; }
"[" { return SQUARE_OPEN_; }
")" { return PAR_CLOSE_; }
"(" { return PAR_OPEN_; }
";" { return SEMICOLON_; }
"+" { return PLUS; }
"*" { return MULT; }
"-" { return MINUS; }
"/" { return DIV_; }
"=" { return ASSIGN; }
"==" { return EQUAL; }
"!=" { return NOT_EQUAL; }
">=" { return GREATER_EQUAL; }
"<=" { return LESS_EQUAL; }
"<" { return LESS; }
">" { return GREATER; }
"++" { return INC_; }
"--" { return DEC_; }
"." { return POINT_; }
"," { return COMMA; }
L?\"(\\.|[^\\"])*\" { yylval.string_literal = createStringLiteral(); return STRING_LITERAL_; }
{REAL} {yylval.real = truncCreal(); return REAL_;}
{DIGIT}+ { yylval.integer =createCnumber(); return CTI_; }
{LETTER}({LETTER}|{DIGIT})* { yylval.id = createId(); return ID_; }
"//"[^\n]*
. {
char buffer[300];
sprintf(buffer, "Unknown character '%s'\n", yytext, yylineno);
yyerror(buffer);
}
%%
//escapes the first and the last quote and returns the string
char* createStringLiteral()
{
int newLen = yyleng - 2;
char *buffer = (char*)malloc(newLen + 1);
for(int i = 0; i < newLen; i ++)
{
buffer[i] = yytext[i + 1];
}
//strncpy(buffer, yytext + 1, newLen);
buffer[newLen] = 0;
return buffer;
}
/*returns new allocated char* copied from yytext */
char *createId()
{
if(yyleng >MAX_LENGTH) {
printf("Warning: Identifier '%s' at line %i will be reduced to %i letters\n", yytext, yylineno, MAX_LENGTH);
yyleng = MAX_LENGTH;
}
char *buffer = (char*)malloc(yyleng + 1);
strncpy(buffer, yytext, yyleng);
buffer[yyleng] = '\0';
return buffer;
}
double truncCreal()
{
return atof(yytext);
}
int createCnumber()
{
return atoi(yytext);
}
/*MAIN*/
int main(int argc, char **argv) {
try{
char *fileName;
int i, n = 0;
for(i = 0; i<argc; ++i) {
if(strcmp(argv[i], "-v") == 0) { verbose = TRUE; n++; }
else if(strcmp(argv[i], "-t") == 0) { showTDS = TRUE; n++; }
}
--argc;
n++;
if (argc == n) {
if((yyin = fopen (argv[argc], "r")) == NULL)
fprintf(stderr, "Invalid file %s\n", argv[argc]);
else {
if (verbose == TRUE) fprintf(stdout, "$3d.- ", yylineno);
fileName = argv[argc];
yyparse();
if (numErrores == 0) ;
else fprintf(stdout, "\nNumber of errors: %d\n", numErrores);
}
}
else fprintf(stderr, "Usage: lessie [-v] [-t] file\n");
} catch(const char * ex)
{
printf("%s", ex);
}
return 0;
}