-
Notifications
You must be signed in to change notification settings - Fork 6
/
scanner.l
129 lines (127 loc) · 4.63 KB
/
scanner.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
%{
#include "y.tab.h"
#include <bits/stdc++.h>
#include "symtab.h"
//void yyerror(const char *s);
int yylex();
unsigned int lineno = 0;
//int yylineno = 1;
%}
%x ML_COMMENT
%option yylineno
%option noyywrap
SINGLE_COMMENT \/\/(.*)
MULTI_COMMENT \/\*(.*\n)*.*\*\/
DIGIT [0-9]
FL [fFlL]?
UL (u|U|l|L)*
FRAC (\.{DIGIT}+)?
EXP ([Ee][+-]?{DIGIT}+)?
NUMBER ({DIGIT}+{FRAC}{EXP})
LETTER [a-zA-Z]
ID [a-zA-Z_][a-zA-Z_0-9]*
PRINT [ -~]
CCONST (\'{PRINT}\')|(\'\\[nftrbv]\')
STRING \"{PRINT}*\"
ICONST {DIGIT}+
FCONST ({DIGIT}+(\.{DIGIT}+){EXP})
DATATYPES int|float|char|double|long|string|void
%%
{SINGLE_COMMENT} { std::cout<<yytext<<std::endl; }
"/*" { BEGIN(ML_COMMENT); }
<ML_COMMENT>"*/" { BEGIN(INITIAL);}
<ML_COMMENT>[^*\n]+ { }
<ML_COMMENT>"*" { }
<ML_COMMENT>\n { }
"main" { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_MAIN;}
"for" { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_FOR;}
"while" { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_WHILE;}
"if" { //if_cond_flag = 0;
char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_IF;}
"else" { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_ELSE;}
"include" { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_INCLUDE;}
{DATATYPES} { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_TYPE;}
"struct" { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_STRUCT;}
"class" { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_CLASS;}
"return" { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_RETURN;}
"private" { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_PRIVATE;}
"public" { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_PUBLIC;}
"protected" { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_PROTECTED;}
"cout" { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_COUT;}
"cin" { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_CIN;}
[\+\-\*\/#\",;<>(){}=\[\]:] { std::cout<<yytext<<" sent"<<std::endl; return *yytext;}
{ID}\.[h] { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_IDH;}
\"{ID}\.h\" { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_IDH;}
{CCONST} { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_CHAR_VAL;}
{STRING} { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_STRING_VAL;}
{ICONST} { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_INTEGER_VAL;}
{FCONST} { char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;return T_FLOAT_VAL;}
\n { lineno++;}
{ID} { /*
cout << yytext << " identifier" << endl;
insert(yytext, strlen(yytext), UNDEF, yylineno);
*/
char *s = (char*)malloc(sizeof(char)*(strlen(yytext)+1));
strcpy(s,yytext);
yylval.sval=s;
return T_IDENTIFIER;}
\+\+ { return T_ADDADD;}
\-\- { return T_MINMIN;}
\+\= { return T_ADDEQ;}
\-\= { return T_MINEQ;}
\*\= { return T_MULEQ;}
\/\= { return T_DIVEQ;}
"<=" { return T_LTEQ;}
">=" { return T_GTEQ;}
"!=" { return T_NEQEQ;}
"==" { return T_EQEQ;}
"||" { return T_OROR;}
"&&" { return T_ANDAND;}
"|" { return T_OR;}
"&" { return T_AND;}
"^" { return T_XOR;}
">>" { return T_LRSHIFT;}
"<<" { return T_LLSHIFT;}
%%