-
Notifications
You must be signed in to change notification settings - Fork 1
/
lex.l
93 lines (79 loc) · 2.76 KB
/
lex.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
/* Bortoli Gianluca 159993 */
%{
#include <stdlib.h>
#include <string.h>
#include "myCalc.h"
#include "y.tab.h"
%}
%%
"main" return MAIN; //main statement at the beginning of input
"while" return WHILE; // all passible statements
"for" return FOR;
"to" return TO;
"if" return IF;
"else" return ELSE;
"print" return PRINT;
"printInt" return PRINTINT;
"printReal" return PRINTREAL;
"int" return INT; // types
"real" return REAL;
"bool" return BOOL;
"true" {yylval.bValue = 1; return BOOLEAN;}
"false" {yylval.bValue = 0; return BOOLEAN;}
"<=" return LRE; //bool operations
">=" return GRE;
"==" return DBE;
"=" return EQ;
"!=" return NE;
"or" return OR;
"not" return NOT;
"and" return AND;
"+" return PLUS; // numpers operations
"-" return MIN;
"*" return MULT;
"/" return DIV;
"<" return LT;
">" return GT;
0 {
yylval.iValue = 0;
return INTEGER;
}
[1-9][0-9]* {
yylval.iValue = atoi(yytext);
return INTEGER;
}
[a-zA-Z][a-zA-Z0-9]* { // +1 for the \0 at the end of the string
yylval.varName = (char *)malloc(strlen(yytext)+1);
// copy assigned name to varName
strcpy(yylval.varName, yytext);
return VARIABLE;
}
(0|[1-9][0-9]*),[0-9]+ { // takes the hole string yytext and replces DOT with COMMA
// REQ: write real numbers with COMMA, not with DOTS
// +1 for the \0 at the end of the string
char * tmp = (char *)malloc(strlen(yytext)+1);
strcpy(tmp, yytext);
// find the comma inside the string
for (int i = 0 ; ; i++){
if(tmp[i] == ','){
// comma is in there for sure when I match this regexp
// so when I find it, I replace it with the dot to use internal
// c rappresentation for float numpers
tmp[i] = '.';
break;
}
}
yylval.rValue = atof(tmp);
free(tmp);
// return number with replacement
return REALVALUE;
}
[ \t\n] ; // symbols
\; return SEMICOLON;
\, return COMMA;
\( return LP;
\) return RP;
\{ return LCURLY;
\} return RCURLY;
. {ECHO; yyerror("Previous char is unknown to lexer");}
%%