-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.l
184 lines (160 loc) · 5.96 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
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
/* BEGIN_BANNER */
/*******************************************************************************************************
sclp : A C Language Processor
--------------------------------
About:
Originally implemented by Tanu Kanvar and Uday Khedker (http://www.cse.iitb.ac.in/~uday)
for the courses cs302+cs316 Language Processors (theory and lab) at IIT Bombay.
Initial release date Jan 15, 2013.
Updated by N. Venkatesh in Jan 2019. The main updates included
- Changing the input language from a basic-block-based GCC GIMPLE dump to a subset of C.
- Replacing the flexc++ and bisonc++ scripts by flex and bison.
- Supporting print operation for variables.
Updated by Uday Khedker with the help of Nisha Biju and Saari Rajan in Jan 2020. The main
updates include
- Introduction of three address code (TAC) phase between abstract syntax tree (AST)
and Register Transfer Language (RTL) which was originally called icode.
- Introduction of string data type with constant strings.
- Support for read operation for integer and floating point variables, and print
operation for all variables (including string variables) and string constants.
- Significant changes in command line switches for printing each representation (tokens,
AST, TAC, RTL, and assembly) as well as stopping after each phase (scanning, parsing,
AST construction (i.e. semantic analysis), TAC generation and RTL generation).
- Substantial clean up of the code including removal of AST to icode translation,
and interpretation (which was buggy and had not been tested thorougly after the front
end and the input langauge changed in 2019).
Copyrights reserved by Uday Khedker. This implemenation has been made available purely
for academic purposes without any warranty of any kind.
*******************************************************************************************************/
/* END_BANNER */
%{
#include "common-classes.hh"
#include "user-options.hh"
#include "error-display.hh"
#include "symbol-table.hh"
#include "tac.hh"
#include "ast.hh"
#include "procedure.hh"
#include "program.hh"
#include "store-tokens.hh"
#include "parser.tab.h"
#include<iostream>
%}
%option noyywrap
digit [0-9]
%%
class {store_token_name("CLASS",yytext,yylineno);
return CLASS; }
public {store_token_name("PUBLIC",yytext,yylineno);
return PUBLIC; }
private {store_token_name("PRIVATE",yytext,yylineno);
return PRIVATE; }
do { store_token_name("DO",yytext,yylineno); return DO;}
while { store_token_name("WHILE",yytext,yylineno); return WHILE;}
if { store_token_name("IF",yytext,yylineno); return IF; }
else { store_token_name("ELSE",yytext,yylineno); return ELSE;}
int {
store_token_name("INTEGER",yytext,yylineno);
return INTEGER;
}
float {
store_token_name("FLOAT",yytext,yylineno);
return FLOAT;
}
bool {
store_token_name("BOOL",yytext,yylineno);
return BOOL;
}
string {
store_token_name("STRING",yytext,yylineno);
return STRING;
}
void {
store_token_name("VOID",yytext,yylineno);
return VOID;
}
print {
store_token_name("WRITE",yytext,yylineno);
return WRITE;
}
read {
store_token_name("READ",yytext,yylineno);
return READ;
}
main {
store_token_name("NAME",yytext,yylineno);
yylval.string_value = new std::string(yytext);
return NAME;
}
return {
store_token_name("RETURN",yytext,yylineno);
return RETURN;
}
"=" {
store_token_name("ASSIGN_OP",yytext,yylineno);
return ASSIGN;
}
">" { store_token_name("GREATER_THAN",yytext,yylineno); return GT;}
"<" { store_token_name("LESS_THAN",yytext,yylineno); return LT;}
">=" { store_token_name("GREATER_THAN_EQUAL",yytext,yylineno); return GE;}
"<=" { store_token_name("LESS_THAN_EQUAL",yytext,yylineno); return LE;}
"!=" { store_token_name("NOT_EQUAL",yytext,yylineno); return NE;}
"==" { store_token_name("EQUAL",yytext,yylineno); return EQ;}
"+"|"-"|"*"|"/" {
store_token_name("ARITHOP",yytext,yylineno);
return *yytext;
}
"&&" { store_token_name("AND",yytext,yylineno); return AND; }
"||" { store_token_name("OR",yytext,yylineno); return OR;}
"!" { store_token_name("NOT",yytext,yylineno); return NOT; }
[{}();,?:.] {
store_token_name("META CHAR",yytext,yylineno);
return *yytext;
}
{digit}+[.]{digit}*|{digit}*[.]{digit}+ {
store_token_name("FNUM",yytext,yylineno);
yylval.double_value = strtod(yytext,NULL);
return DOUBLE_NUMBER;
}
[[:digit:]]+ {
store_token_name("NUM",yytext,yylineno);
yylval.integer_value = atoi(yytext);
return INTEGER_NUMBER;
}
["][^"\n]*["] {
store_token_name("STR_CONST",yytext,yylineno);
int i;
for(i=0;yytext[i]!='\0';i++)
{
if (yytext[i+1] != '"')
yytext[i]=yytext[i+1];
else
yytext[i]='\0';
}
yylval.string_value = new std::string(yytext);
return STRING_CONSTANT;
}
[[:alpha:]_][[:alpha:][:digit:]_]* {
store_token_name("NAME",yytext,yylineno);
int i;
for(i=0;yytext[i]!='\0';i++);
yytext[i]='_';
yytext[i+1]='\0';
yylval.string_value = new std::string(yytext);
return NAME;
}
[@] {
store_token_name("AT",yytext,yylineno);
yylval.integer_value = atoi(yytext);
return AT;
}
[ \t]* {}
\n {}
\/\/.* {}
. {
string error_message;
error_message = "Illegal character `" + *yytext;
error_message += "' on line " + yylineno;
CHECK_INPUT(CONTROL_SHOULD_NOT_REACH, error_message, yylineno);
}
%%