/*
C++手写.....
就当写了个大模拟
老师的数据真是强...
*/
//#pragma comment(linker,"/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <ctime>
#include <cmath>
#include <cassert>
#include <stack>
#include <cctype>
#include <fstream>
#include <cassert>
#include <sstream>
#define MP make_pair
#define PB push_back
#define SZ(x) (int)x.size()
#define INF 1<<29
#define pii pair<int,int>
#define pll pair<LL,LL>
#define pdd pair<double,double>
#define vi vector<int>
#define L(x) x<<1
#define R(x) x<<1|1
#if(_WIN32||__WIN32__)
#define LL __int64
#define ll I64
#else
#define LL long long
#endif
//#define Local
using namespace std;
class lexicalanalysis{
private :
map<string,int>table;
public :
void init_table()
{
table["function"]=1;
table["if"]=2;
table["then"]=3;
table["while"]=4;
table["do"]=5;
table["endfunc"]=6;
table["Letter_"]=10;
table["digit_"]=11;
table["+"]=13;
table["-"]=14;
table["*"]=15;
table["/"]=16;
table["="]=18;
table["<"]=20;
table["<="]=21;
table["!="]=22;
table[">"]=23;
table[">="]=24;
table["=="]=25;
table[";"]=26;
table["("]=27;
table[")"]=28;
table["#"]=-1;
}
bool is_digit(string s)
{
for(int i=0;i<SZ(s);i++)
{
if(!isdigit(s[i]))
return false;
}
return true;
}
bool is_letter(string s)
{
if(!isalpha(s[0]))
return false;
for(int i=1;i<SZ(s);i++)
{
if(!isalpha(s[i])&&!isdigit(s[i]))
return false;
}
return true;
}
int check(string s)
{
if(!table.count(s))
{
if(is_digit(s))
s="digit_";
if(is_letter(s))
s="Letter_";
}
if(!table.count(s))
return 0;
return table[s]<0?0:table[s];
}
void process_annotation_1(string& s) //块注释
{
int star_id=0,end_id=4;
while(star_id<end_id-2)
{
star_id=SZ(s),end_id=0;
for(int i=0;i<SZ(s)-1;i++)
if(s[i]=='/'&&s[i+1]=='*')
{
star_id=i;
break;
}
for(int i=0;i<SZ(s)-1;i++)
{
if(s[i]=='*'&&s[i+1]=='/')
{
end_id=i+1;
break;
}
}
if(star_id>end_id-2)
break;
s.erase(s.begin()+star_id,s.begin()+end_id+1);
}
}
void process_annotation_2(string& s) //行注释
{
while(true)
{
bool flag=false;
for(int i=0;i<SZ(s)-1;i++)
{
if(s[i]=='/'&&s[i+1]=='/')
{
flag=true;
for(int j=i;j<SZ(s);j++)
{
if(s[j]=='\n')
{
s.erase(s.begin()+i,s.begin()+j+1);
break;
}
}
}
if(flag)
break;
}
if(!flag)
break;
}
}
string jump_real_num(string now,string s,int& id) //实数判断,貌似有问题?
{
if(now!="+"&&now!="-"&&!isdigit(now[0]))
return "";
bool flag1=false;
string t;
if(id>2)
t+=s[id-2];
if(id<=2||s[id-2]=='-'||s[id-2]=='+'||s[id-2]==' '||s[id-2]=='\n'||s[id-2]
=='\t'||isdigit(now[0])||s[id-2]=='='||isdigit(s[id-2])||table[t])
{
flag1=true;
}
if(!flag1)
return "";
bool flag=false;
int statu=0;
int i;
for(i=id;i<SZ(s);i++)
{
if(isdigit(s[i]))
now+=s[i];
else if(s[i]=='.')
{
if(statu==0)
now+=s[i],statu=1;
else
break;
}
else if(s[i]=='e')
{
if(!flag&&s[i-1]!='.')
statu=0,flag=true,now+='e';
else
break;
}
else if(s[i]=='+'||s[i]=='-')
{
if(s[i-1]=='e')
now+=s[i];
else
break;
}
else
break;
}
i--;
while(s[i]=='e'||s[i]=='.')
i--,now.erase(now.end()-1);
if(i<id)
return "";
id=i+1;
return "(real_num , "+now+")";
}
string process(string user_input)
{
stringstream ss;
string now,next;
process_annotation_2(user_input);
process_annotation_1(user_input);
now+=user_input[0];
for(int i=1;i<SZ(user_input);i++)
{
string temp=jump_real_num(now,user_input,i);
if(SZ(temp))
{
ss<<temp<<endl;
now.clear();
while(user_input[i]==' '||user_input[i]=='\n'||
user_input[i]=='\t')
i++;
now+=user_input[i++];
if(i>=SZ(user_input))
break;
}
if(now==" "||now=="\n"||now=="\t")
{
now.clear();
while(user_input[i]==' '||user_input[i]=='\n'||
user_input[i]=='\t')
i++;
if(i==SZ(user_input))
break;
now+=user_input[i];
}
else{
while(!check(now))
{
ss<<"error"<<endl;
now.clear();
if(now==" "||now=="\n"||now=="\t")
{
now.clear();
while(user_input[i]==' '||
user_input[i]=='\n'||user_input[i]=='\t')
i++;
now+=user_input[i];
}
if(i==SZ(user_input))
break;
}
if(i==SZ(user_input))
break;
next=now+user_input[i];
if(check(next))
now=next;
else{
if(!check(now))
continue;
if(now.size()==0)
continue;
ss<<"("<<check(now)<<","<<now<<")"<<endl;
now.clear();
now+=user_input[i];
}
}
}
if(now.size()!=0)
{
ss<<"("<<check(now)<<","<<now<<")"<<endl;
}
string ret;
char s[1100];
while(ss.getline(s,200))
{
ret+=s;
if(s[0]!='#')
ret+='\n';
}
return ret;
}
string input() //读入
{
string str,user_input;
puts("******************************");
puts("presses 1,input from console");
puts("presses 2,input from flie");
puts("******************************");
cin>>str;
if(str=="1")
{
getchar();
getline(cin,user_input,'#');
user_input+='#';
}
else{
char name[100];
puts("input file name");
scanf("%s",name);
fstream file_input(name,ios::in);
char s[1100];
while(file_input.getline(s,200))
{
user_input+=s;
if(s[0]!='#')
user_input+='\n';
}
}
return user_input;
}
void Display(string s)
{
puts("******************************");
puts("presses 1,output to console");
puts("presses 2,output to flie");
puts("******************************");
string str;
cin>>str;
if(str=="1")
cout<<s<<endl;
else{
char name[100];
puts("output file name");
scanf("%s",name);
fstream file_output(name,ios::out);
file_output<<s;
}
}
void run()
{
init_table();
string user_input=input();
string str=process(user_input);
Display(str);
}
};
int main()
{
lexicalanalysis test;
test.run();
return 0;
}
/*
//function
x= 9;
if x> 0 then
x=2*x+1/3;
y= 1.1e-2.8999.-
endfunc
#
1.1e-1.0001
-1.1e10000
1e.
1.e
#
function
y= -1++1+1--1-1;
if (( x<=-1.2e1.2++1.2e-100.2+-1.2e-12--1.2e+12))
// x= x+1;
y= +1++1+1--1-1;
if(+1e8 <=+1)
x=1e18;
endfunc
#
*/
第一章 词法分析
最新推荐文章于 2021-03-16 09:52:53 发布