UVA12421 (Jiandan) Mua (I) - Lexical Analyzer題解

cruisexsy2011發表於2024-08-30

沒什麼廢話、超級珂愛的大模擬。

本蒟蒻寫了 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;
}

相關文章