编译原理作业1,2
12.14修改: 实验一换一种更靠谱的dfa并且修改了空格问题
先贴实验一二,这两天写的吐血
实验一:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sz(X) ((int)X.size())
//32-126
map<string, string> mp;
char sep[] = "+-*=/&|!><:;,(){} ";
char ans[][10] = {"","str","ch","num"};
int seple = strlen(sep);
struct Node{
int to,nx; char di; int ty;
}E[1000];
int head[100], tot;
void add(int u, int v, char w, int type) {
E[tot].to = v; E[tot].nx = head[u]; E[tot].di = w; E[tot].ty = type; head[u] = tot++;
}
void make1(int x, int ty) {
for(int i = 0; i < seple-1; ++i) {
add(x, 8, sep[i], ty);
}
add(x, 0, ' ', ty);
}
char s[105];
void solve() {
int len = strlen(s); s[len] = ' ';
int x = 0;
string tt; tt.clear();
for(int i = 0; i <= len; ++i) {
int fl = 0;
// printf("%d %c ",x,s[i]); cout << tt << endl;
for(int j = head[x]; ~j; j = E[j].nx) {
int y = E[j].to; char z = E[j].di; int type = E[j].ty;
if(z == s[i]) {
// printf("%d %d %c\n",x,y,z);
x = y;
fl = 1;
if(!type && s[i] != ' ') tt += s[i];
else {
if(!tt.empty()) {
if(type == 1) {
cout << tt << " str" << endl;
}else if(type == 2) {
cout << tt << " ch" << endl;
}else if(type == 3) {
cout << tt << " num" << endl;
}else if(type == 4) {
i--;
for(int k = 0; k < tt.length(); ++k) {
if(k+1 < tt.length()) {
string tmp; tmp.clear(); tmp += tt[k]; tmp += tt[k+1];
string tmp2; tmp2 += tt[k];
if(mp.count(tmp)) {
cout << tmp << ' ' << mp[tmp] << endl; k++;
}else if(mp.count(tmp2)) {
cout << tmp2 << ' ' << mp[tmp2] << endl;
}else {
fl = 0; goto li;
}
}else {
string tmp2; tmp2 += tt[k];
if(mp.count(tmp2)) {
cout << tmp2 << ' ' << mp[tmp2] << endl;
}else {
fl = 0; goto li;
}
}
}
}
else {
if(mp.count(tt)) {
cout << tt << ' ' << mp[tt] << endl;
}else cout << tt << ' ' << "id" << endl;
}
}
tt.clear();
if(s[i] != ' ' && type != 4) tt += s[i];
}
break;
}
}
li: if(!fl) {
printf("There is a error\n"); break;
}
}
}
int main() {
mp["int"] = "kw_int"; mp["char"] = "kw_char"; mp["void"] = "kw_void"; mp["if"] = "kw_if"; mp["else"] = "kw_else";
mp["switch"] = "kw_switch"; mp["case"] = "kw_case"; mp["default"] = "kw_default"; mp["while"] = "kw_while"; mp["do"] = "kw_do";
mp["for"] = "kw_for"; mp["break"] = "kw_break"; mp["continue"] = "kw_continue"; mp["return"] = "kw_return";
mp["+"] = "add"; mp["-"] = "sub"; mp["*"] = "mul"; mp["/"] = "div"; mp["%"] = "mod"; mp["++"] = "inc"; mp["--"] = "dec"; mp["!"] = "not";
mp["&&"] = "and"; mp["||"] = "or"; mp["="] = "assign"; mp[">"] = "gt"; mp[">="] = "ge"; mp["<"] = "lt"; mp["<="] = "le"; mp["=="] = "equ"; mp["!="] = "nequ";
mp[","] = "comma"; mp[":"] = "colon"; mp[";"] = "simcon"; mp["("] = "lparen"; mp[")"] = "rparen"; mp["{"] ="lbrac"; mp["}"] = "rbrac";
tot = 0;
memset(head,-1,sizeof(head));
add(0, 0, ' ', 0);
add(0, 1, '\"', 0); add(1,10,'\"',0);
for(int i = 32; i <= 126; ++i) {
if(i != '\"') add(1,1,i,0);
}
make1(10,1);
add(0, 2, '\'', 0);
for(int i = 32; i <= 126; ++i) {
if(i != '\\') add(2, 3, i, 0);
}
add(2, 11, '\\', 0); add(11, 3, '\'', 0); add(11, 3, '\"', 0);
add(3, 9, '\'', 0);
make1(9,2);
for(int i = 1; i <= 9; ++i) add(0, 5, i+'0', 0);
for(int i = 0; i <= 9; ++i) add(5, 5, i+'0', 0);
make1(5, 3);
add(0, 6, '0', 0); make1(6, 3);
for(int i = 0; i <= 9; ++i) add(6, 12, i+'0', 0);
for(int i = 0; i <= 9; ++i) add(12,12, i+'0', 0);
make1(12, 3);
add(6,7, 'X',0);
for(int i = 0; i <= 9; ++i) add(7, 7, i+'0', 0);
for(int i = 'A'; i <= 'Z'; ++i) add(7,7, i, 0);
make1(7, 3);
for(int i = 0; i < seple-1; ++i) add(0,8,sep[i],0);
for(int i = 0; i < seple-1; ++i) add(8,8,sep[i],0);
for(int i = 32; i <= 126; ++i) {
int fl = 1;
for(int j = 0; j < seple-1; ++j) {
if(i == sep[j]) {
fl = 0; break;
}
}
if(fl) add(8, 0, i, 4);
}
for(int i = 'a', j = 'A'; i <= 'z'; ++i, ++j) {
add(0,13,i,0); add(0,13,j,0);
} add(0, 13, '_', 0);
for(int i = 'a', j = 'A'; i <= 'z'; ++i, ++j) {
add(13,13,i,0); add(13,13,j,0);
} add(13, 13, '_', 0);
for(int i = 0; i <= 9; ++i) add(13,13,'0'+i,0);
make1(13, 5);
while(gets(s)) {
solve();
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
map<string, string> mp;
char s[1005];
void doo(string S) {
string ans;
for(int i = 0; i < S.length(); ++i) {
if(s[i] >= '0' && s[i] <= '9') ans = "num";
else if(s[i] == '\'') ans = "ch";
else if(s[i] == '\"') ans = "str";
else if(mp.count(S)) ans = mp[S];
else ans = "id";
}
cout << S <<' '<< ans << endl;
}
int main() {
// freopen("in.txt","r",stdin);
mp["int"] = "kw_int"; mp["char"] = "kw_char"; mp["void"] = "kw_void"; mp["if"] = "kw_if"; mp["else"] = "kw_else";
mp["switch"] = "kw_switch"; mp["case"] = "kw_case"; mp["default"] = "kw_default"; mp["kw_while"] = "while"; mp["do"] = "kw_do";
mp["for"] = "kw_for"; mp["break"] = "kw_break"; mp["continue"] = "kw_continue"; mp["return"] = "kw_return";
mp["+"] = "add"; mp["-"] = "sub"; mp["*"] = "mul"; mp["/"] = "div"; mp["%"] = "mod"; mp["++"] = "inc"; mp["--"] = "dec"; mp["!"] = "not";
mp["&&"] = "and"; mp["||"] = "or"; mp["="] = "assign"; mp[">"] = "gt"; mp[">="] = "ge"; mp["<"] = "lt"; mp["<="] = "le"; mp["=="] = "equ"; mp["!="] = "nequ";
mp[","] = "comma"; mp[":"] = "colon"; mp[";"] = "simcon"; mp["("] = "lparen"; mp[")"] = "rparen"; mp["{"] ="lbrac"; mp["}"] = "rbrac";
while(gets(s)) {
int n = strlen(s);
s[n] = ' ';
string tt;
for(int i = 0; i <= n; ++i) {
if(s[i] == ' ') {
if(!tt.empty()) {
doo(tt);
tt.clear();
}
}else tt += s[i];
}
}
return 0;
}
实验二
#include<bits/stdc++.h>
using namespace std;
#define sz(X) ((int)X.size())
const int INF = 0x3f3f3f3f;
const int N = 30;
int n, m;
struct Node{
int to, nx;
char di;
}E[N*N];
int head[N], tot;
void add(int u, int v, char w) {
E[tot].to = v; E[tot].nx = head[u]; E[tot].di = w; head[u] = tot++;
}
int tmp;
void Find(int x) {
for(int i = head[x]; ~i; i = E[i].nx) {
int y = E[i].to; char z = E[i].di;
if(z == '~' && !(tmp>>y&1) ) {
tmp |= 1<<y; Find(y);
}
}
}
char stchar[N];
map<char, int> charst; int cc;
map<int, int> vis; int vc_cn;
vector<pair<int,int> > vc[N];
int deep[N]; int mp_cn; int type[N];
struct Tode{
int fr,to,di;
Tode(int a=0, int b=0, int c=0):fr(a), to(b), di(c){};
bool operator < (const Tode &T) const {
if(fr != T.fr) return fr < T.fr;
else if(to != T.to) return to < T.to;
else return di < T.di;
}
};
vector<Tode> F;
void show(int x) {
for(int i = 0; i < vc_cn; ++i) {
if(x>>i&1) printf("%d ",i);
}
printf("\n");
}
int f[N];
int find(int x) { return x==f[x]? x : f[x] = find(f[x]); }
void dfs(int x) {
// show(x);
int ch[N][N]; int ch_cn = 0; memset(ch,-1,sizeof(ch));
for(int i = 0; i < vc_cn; ++i) f[i] = i;
for(int i = 0; i < vc_cn; ++i) {
if(x>>i&1) {
ch[++ch_cn][0] = i;
for(int j = 0; j < sz(vc[i]); ++j) {
int y = vc[i][j].first; int z = vc[i][j].second;
ch[ch_cn][z] = type[y];
}
for(int j = 1; j < ch_cn; ++j) {
int fl = 1;
for(int k = 1; k <= cc; ++k) {
if(ch[j][k] != ch[ch_cn][k]) {
fl = 0; break;
}
}
if(fl) {
int t1 = find(ch[ch_cn][0]); int t2 = find(ch[j][0]);
if(t1 != t2) f[t1] = t2;
}
}
}
}
map<int, int> num;
map<int, int> ::iterator it;
for(int i = 1; i <= ch_cn; ++i) {
int t1 = find(ch[i][0]);
if(num.find(t1) == num.end()) num[t1] = 0;
num[t1] |= 1<<ch[i][0];
}
if(num.size() == 1) return;
for(it = num.begin(); it != num.end(); ++it) {
int t1 = it->first; int t2 = it->second;
if(it != num.begin()) {
for(int i = 0; i < vc_cn; ++i) {
if(t2>>i&1) {
type[i] = mp_cn;
}
}
mp_cn++;
}
}
for(it = num.begin(); it != num.end(); ++it) {
int t1 = it->first; int t2 = it->second;
if(1<<t1 != t2) {
dfs(t2);
}
}
}
int main(){
int ca = 0;
freopen("in.txt","r",stdin);
while(~scanf("%d %d",&n,&m)) {
tot = 0; cc = 0; vc_cn = 0; mp_cn = 0; F.clear();
vis.clear(); charst.clear();
for(int i = 0; i < n; ++i) scanf("%d",&deep[i]);
memset(head,-1,sizeof(head));
memset(type,0,sizeof(type));
for(int i = 0; i < n; ++i) vc[i].clear();
int edpoint = 0;
for(int i = 1; i <= m; ++i) {
int l,r; char d[10]; scanf("%d %d %s",&l,&r,d);
add(l, r, d[0]);
if( d[0] != '~' && !charst.count(d[0]) ) {
charst[d[0]] = ++cc; stchar[cc] = d[0];
}
}
for(int i = 0; i < n; ++i) if(deep[i]) edpoint |= 1<<i;
printf("%d\n",edpoint);
/****************NFA->DFA*************************/
tmp = 1; Find(0);
// show(tmp);
queue<int> Q;
Q.push(tmp);
vis[tmp] = vc_cn++;
if(tmp & edpoint) type[vc_cn-1] = 1;
while(!Q.empty()) {
int top = Q.front(); Q.pop();
// printf("::"); show(top);
int tt[N]; memset(tt,0,sizeof(tt));
for(int i = 0; i < n; ++i) {
if(top>>i&1) {
for(int j = head[i]; ~j; j = E[j].nx) {
int y = E[j].to; char z = E[j].di;
if(z == '~') {
for(int k = 1; k <= cc; ++k) tt[k] |= 1<<y;
}else {
int bel = charst[z];
tt[bel] |= 1<<y;
}
}
}
}
for(int i = 1; i <= cc; ++i) {
if(tt[i]) {
tmp = 0;
for(int j = 0; j < n; ++j) {
if(tt[i]>>j&1 && !(tmp>>j&1) ) {
tmp |= 1<<j; Find(j);
}
}
// show(tmp);
if(vis.find(tmp) == vis.end()) {
vis[tmp] = vc_cn++; Q.push(tmp);
if(tmp & edpoint ) type[vc_cn-1] = 1;
}
vc[vis[top]].push_back({vis[tmp],i});
F.push_back(Tode(vis[top],vis[tmp],i));
deep[vis[top]] ++;
}
}
}
/***************DFA->minDFA 0开始点1结束点 **************/
int t1 = 0; int t2 = 0;
for(int i = 0; i < vc_cn; ++i) {
if(!type[i]) {
t1 += 1<<i;
}else {
t2 += 1<<i;
}
}
mp_cn = 2;
dfs(t1); dfs(t2);
// for(int i = 0; i < vc_cn; ++i) printf("%d ",type[i]); printf("\n");
set<Tode> has; printf("Case: %d\n",++ca);
for(int i = 0; i < sz(F); ++i) {
int x = F[i].fr; int y = F[i].to; int z = F[i].di;
x = type[x]; y = type[y]; if(x == y && F[i].fr != F[i].to) continue;
Tode tt = Tode(x,y,z);
// printf(":%d %d %c\n",x,y,stchar[z]);
if(has.find(tt) == has.end()) printf("%d %d %c\n",x,y,stchar[z]);
has.insert(tt);
}
}
return 0;
}
/*
11 13
0 0 0 0 0 0 0 0 0 0 1
0 1 ~
0 7 ~
1 2 ~
1 4 ~
2 3 a
4 5 b
3 6 ~
5 6 ~
6 1 ~
6 7 ~
7 8 a
8 9 b
9 10 b
7 14
0 0 0 1 1 1 1
0 1 a
0 2 b
1 2 b
1 3 a
2 1 a
2 5 b
3 3 a
3 4 b
4 5 b
4 6 a
5 5 b
5 6 a
6 4 b
6 3 a
*/