没什么废话、超级珂爱的大模拟。
本蒟蒻写了 2h 才过。
其实就是按题意模拟即可,不需要什么高深的算法。
本人就是错在了符号中的 “=” ,因为如果是连续的两个等于号,只能输出 “==”,
而不能输出“=” “==”,然后本人就卡在这个地方卡了 1.5h 。
代码量也不大,主要是毒瘤细节模拟题。
code:
#include <bits/stdc++.h>
using namespace std;
#define For(i,x,y,...) for(int i=(x),##__VA_ARGS__;i<=(y);++i)
#define foR(i,x,y,...) for(int i=(x),##__VA_ARGS__;i>=(y);--i)
#define Rep(i,x,y,...) for(int i=(x),##__VA_ARGS__;i<(y);++i)
#define endl '\n'
#define debug(...)
#define debug1(a,i,...) cout<<i<<" "<<a[i]<<endl;
typedef long long ll;
#define fi first
#define se second
#define PII pair<int,int>
#define me(s,x) memset(s,x,sizeof s)
#define pb emplace_back
template<typename T=int>T read(){T x;cin>>x;return x;}
const int mod=1e9+7;
struct mint{
ll x;mint(int x=0):x(x<0?x+mod:x<mod?x:x-mod){}
mint(ll y){y%=mod,x=y<0?y+mod:y;}
mint& operator += (const mint &y){x=x+y.x<mod?x+y.x:x+y.x-mod;return *this;}
mint& operator -= (const mint &y){x=x<y.x?x-y.x+mod:x-y.x;return *this;}
mint& operator *= (const mint &y){x=1ll*x*y.x%mod;return *this;}
friend mint operator + (mint x,const mint &y){return x+y;}
friend mint operator - (mint x,const mint &y){return x-y;}
friend mint operator * (mint x,const mint &y){return x*y;}
};mint Pow(mint x,ll y=mod-2){mint z(1);for(;y;y>>=1,x*=x)if(y&1)z*=x;return z;}
string s;
string bl[]={"and","break","do","else","elseif","end","false","for","function","if","in","local","nil","not","or","repeat","return","then","true","until","while"};
char fh[]={'+','-','*','/','%','^','#','=','=','>','=','<','=','>','<','~','=','(',')','{','}','[',']',';',':',',','.','=',};
bool chknm(char c){
return (c>='0'&&c<='9')||(c>='a'&&c<='z')||(c>='A'&&c<='Z')||c=='_';
}
bool chkfuhao(char c){
for(auto x:fh){
if(x==c) return 1;
}
return 0;
}
bool fl=0;
bool chknum(char c){
if((c>='0'&&c<='9')||(c>='a'&&c<='f')||(c>='A'&&c<='F')) return 1;
if(c=='.'&&!fl){
fl=1;
return 1;
}
return 0;
}
void MAIN(){
int n=s.size();
Rep(i,0,n){
if(s[i]==' ')continue;
if(s[i]=='-'&&s[i+1]=='-') break;//一整行都是注释
if(s[i]>='0'&&s[i]<='9'||s[i]=='.'&&(s[i+1]>='0'&&s[i+1]<='9')){
fl=0;
printf("[NUMBER] ");
string ans="";
if(s[i]=='.') fl=1,ans+=s[i],i++;
if(s[i]=='0'&&(s[i+1]=='x'||s[i+1]=='X'))ans+=s[i],ans+=s[i+1],i+=2,fl=1;
while(chknum(s[i])){
ans+=s[i];
if(s[i]=='e'&&(s[i+1]=='-'||s[i+1]=='+')){
i++;
ans+=s[i];
}
i++;
}
i--;
cout<<ans;
puts("");
}
else if(s[i]=='"'){
if(s[i-1]=='\\') continue;
printf("[STRING] ");
int k=i;
for(;i<n;i++){
putchar(s[i]);
if(i!=k&&s[i]=='"'&&s[i-1]!='\\'){
break;
}
}
puts("");
}
else if(chkfuhao(s[i])){
printf("[SYMBOL] ");
if(s[i]=='.'){
if(s[i+1]=='.'){
if(s[i+2]=='.') i+=2,cout<<"...";
else i++,cout<<"..";
}
else cout<<".";
}
else if(s[i]=='='){
if(s[i+1]=='=') cout<<"==",i++;
else if(s[i-1]=='~'||s[i-1]=='>'||s[i-1]=='<')cout<<s[i-1]<<"=",i++;
else cout<<'=';
}
else putchar(s[i]);
puts("");
}
else if(s[i]>='a'&&s[i]<='z'||s[i]>='A'&&s[i]<='Z'){
string ans="";
while(chknm(s[i]))ans+=s[i],i++;
i--;
for(auto s:bl){
if(ans==s){
cout<<"[RESERVED] "<<ans<<endl;
goto togo;
}
}
cout<<"[NAME] "<<ans<<endl;
togo:;
}
}
puts("[EOL]");
}signed main(){
while(getline(cin,s)){
s+='\n';
MAIN();
}
return 0;
}
标签:__,return,int,题解,Lexical,Analyzer,&&,define
From: https://www.cnblogs.com/2011xsy0226xsy/p/18388497