-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.l
53 lines (51 loc) · 1007 Bytes
/
test.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
%{
#include<iostream>
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
bool declarationFlag=0;
vector<string> storeVariables;
%}
variable [a-zA-Z][A-Za-z0-9]*
dataType "int"|"float"|"char"
colon ";"
integer [0-9]*
openSquareBracket "["
closeSquareBracket "]"
space " "
%%
{dataType}{space} {
cout<<yytext;
declarationFlag=1;
};
{variable}{openSquareBracket}{integer}{closeSquareBracket} {
cout<<yytext;
if(declarationFlag){
storeVariables.push_back(string(yytext));
}
};
{colon} {
cout<<yytext<<"\n";
if(declarationFlag){
for(auto i:storeVariables){
int a=i.find("["),b=i.find("]");
string s=i.substr((int64_t)0,a);
cout<<"int newVar;\nnewVar="<<s<<"[0];\n"<<s<<"[0]=newVar;";
}
storeVariables.clear();
declarationFlag=0;
}
};
{space}* {
cout<<" ";
};
. {
cout<<yytext;
}
%%
int yywrap(){
}
int main(int argc,char **argv){
yylex();
}