编译原理上机题目

7-1 表达式语法分析——递归子程序法 (20 分)

#include 
using namespace std;
string a;
int p;
int flag=1;
int f=0;
void E();
void G();
void T();
void S();
void F();
void S()
{
if(!flag) return;
if(a[p]=='*')
{ printf("%d S-->*FS\n",f++);
p++;
F();
S();
}
else
{
printf("%d S-->&\n",f++);
}
}
void F()
{ if(!flag) return;
if(a[p]=='(')
{
printf("%d F-->(E)\n",f++);
p++;
E();
if(a[p]!=')') flag=0;
p++;
}
else if(a[p]=='i')
{
printf("%d F-->i\n",f++);
p++;
}
else flag=0;
}
void T()
{ if(!flag) return;
if(a[p]=='('||a[p]=='i')
printf("%d T-->FS\n",f++);
F();
S();
}
void G()
{ if(!flag) return;
if(a[p]=='+')
{ printf("%d G-->+TG\n",f++);
p++;
T();
G();
}
else
printf("%d G-->&\n",f++);
}
void E()
{ if(!flag) return;
if(a[p]=='('||a[p]=='i')
printf("%d E-->TG\n",f++);
T();
G();
}
int main()
{ p=0;
cin>>a;
if(a.length()==0||(a.length()==1&&a[0]=='#')) {printf("error\n");return 0;}
E();
if(!flag)
printf("error\n");
else if(p!=a.length()-1)
printf("error\n");
else printf("accept\n");
return 0;
}

7-2 小C语言–词法分析程序 (30 分)

#include
using namespace std;
int main()
{
    string k;
    while(cin>>k)
    {
        int l=k.size();
        for(int i=0;i<l;i++)
        {
            if(k[i]=='{'||k[i]=='}'||k[i]=='('||k[i]==')'||k[i]==','||k[i]==';')
            {
                cout<<"(boundary,"<<k[i]<<")"<<endl;
            }
            else if(k[i]=='+'||k[i]=='='||k[i]=='-'||k[i]=='*'||k[i]=='/'||k[i]=='<'||k[i]=='>'||k[i]=='!')
            {
                if(k[i+1]=='=')
                {
                    cout<<"(operator,"<<k[i]<<k[i+1]<<")"<<endl;
                    i++;
                }
                else
                {
                    cout<<"(operator,"<<k[i]<<")"<<endl;
                }
            }
            else if(k[i]>='0'&&k[i]<='9')
            {
                cout<<"(integer,";
                for(;i<l&&(k[i]>='0'&&k[i]<='9');i++)
                {
                    cout<<k[i];
                }
                cout<<")"<<endl;
                i--;
            }
            else if((k[i]>='a'&&k[i]<='z')||(k[i]>='A'&&k[i]<='Z'))
            {
                int j=0;
                string c="";
                for(;i<l&&((k[i]>='a'&&k[i]<='z')||(k[i]>='A'&&k[i]<='Z')||(k[i]>='0'&&k[i]<='9'));i++)
                {
                    c+=k[i];
                    j++;
                }
                i--;
                if(j==2)
                {
                    if(c=="if")
                    {
                        cout<<"(keyword,"<<c<<")"<<endl;
                    }
                    else
                    {
                        cout<<"(identifier,"<<c<<")"<<endl;
                    }
                }
                else if(j==3)
                {
                    if(c=="for"||c=="int")
                    {
                        cout<<"(keyword,"<<c<<")"<<endl;
                    }
                    else
                    {
                        cout<<"(identifier,"<<c<<")"<<endl;
                    }
                }
                else if(j==4)
                {
                    if(c=="main"||c=="else")
                    {
                        cout<<"(keyword,"<<c<<")"<<endl;
                    }
                    else
                    {
                        cout<<"(identifier,"<<c<<")"<<endl;
                    }
                }
                else if(j==5)
                {
                    if(c=="while")
                    {
                        cout<<"(keyword,"<<c<<")"<<endl;
                    }
                    else
                    {
                        cout<<"(identifier,"<<c<<")"<<endl;
                    }
                }
                else
                {
                     cout<<"(identifier,"<<c<<")"<<endl;
                }
            }
        }
    }
    return 0;
}

7-3 DAG图优化 (15 分)

#include 
using namespace std;
int n;
int cnt;
struct Node
{
    char id;
    int left = -1, right = -1;
    vector<char> var;
} node[110];
 
bool find_var(int i,char c)
{
    for(char j : node[i].var)if(j == c)return 1;
    return 0;
}
 
int add_node(char c)
{
    for(int i = cnt-1; i >= 0; --i)
        if(node[i].id == c || find_var(i,c))return i;
    node[cnt].id = c;
    return cnt++;
}
 
void add_operator(char c,char op,int l,int r)
{
    for(int i = cnt-1; i >= 0; --i)
        if(node[i].left == l && node[i].right == r && node[i].id == op)
        {
            node[i].var.push_back(c);
            return ;
        }
    node[cnt].id = op;
    node[cnt].var.push_back(c);
    node[cnt].left = l;
    node[cnt].right = r;
    cnt++;
}
char s[10];
char ans[110][10];
bool flag[110];
void dfs(int x)
{
    if(node[x].left != -1)
    {
        flag[x] = 1;
        dfs(node[x].left);
        dfs(node[x].right);
    }
}
 
int main()
{
    cnt = 0;
    scanf("%d",&n);
    for(int i = 0; i < n; ++i)
    {
        scanf("%s",s);
        int l = add_node(s[2]);
        int r = add_node(s[4]);
        add_operator(s[0],s[3],l,r);
    }
    for(int i = 0; i < cnt; ++i)
    {
        if(node[i].left != -1)
        {
            ans[i][0] = node[i].var[0];
            ans[i][1] = '=';
            Node ll = node[node[i].left],rr = node[node[i].right];
            ans[i][2] = ll.var.size() > 0 ? ll.var[0] : ll.id;
            ans[i][3] = node[i].id;
            ans[i][4] = rr.var.size() > 0 ? rr.var[0] : rr.id;
            ans[i][5] = 0;
        }
    }
    for(int i = cnt-1; i >= 0; --i)
    {
        if(ans[i][0] == 'A')
        {
            dfs(i);
            break;
        }
    }
    for(int i = cnt-1; i >= 0; --i)
    {
        if(ans[i][0] == 'B')
        {
            dfs(i);
            break;
        }
    }
    for(int i = 0; i < cnt; ++i)if(flag[i])puts(ans[i]);
    return 0;
}

7-4 翻译布尔表达式 (25 分)

#include 
 
using namespace std;
string a,x;
 
vector<string>d;
int main()
{
int t;
int id,yes,no;
getline(cin,a);
a+=" ed";
id=100;
yes=1;
no=100;
stringstream b(a);
while(b>>x)
{
if(x=="or"||x=="ed")
{
if(x=="or")no+=2;
else no=0;
int n=d.size();
for(int i=0;i<n-3;i+=3)
{
printf("%d(j%s,%s,%s,%d)\n",id,d[i+1].c_str(),d[i].c_str(),d[i+2].c_str(),id+2);
id++;
printf("%d(j,_,_,%d)\n", id, no);
no=id++;
}
printf("%d(j%s,%s,%s,%d)\n",id,d[n-2].c_str(),d[n-3].c_str(),d[n-1].c_str(),yes);
yes=id++;
printf("%d(j,_,_,%d)\n", id, no);
id++;
d.clear();
if(x=="ed")break;
}
else if(x=="and") no+=2;
else d.push_back(x);
}
return 0;
}

7-5 简单的代码生成程序 (10 分)

#include 

using namespace std;
const int MAXN = 10;
int n,m;
char s[110][MAXN];
char R[MAXN];
int cntm;
int is_inR(char c)
{
for(int i = 0; i < m; ++i)if(R[i] == c)return i;
return -1;
}

int get_uselast(int pos,char c)
{
for(int i = pos; i < n; ++i)
{
if(s[i][3] == c || s[i][5] == c)return i;
}
return n;
}

int get_pos(int pos)
{
if(cntm < m)return cntm++;
int ans = -1,mm = -1;
for(int i = 0; i < m; ++i)
{
int ne = get_uselast(pos,R[i]);
if(ne > mm)mm = ne,ans = i;
}
return ans;
}

void print_operator(char c)
{
if(c == '+')printf("ADD");
else if(c == '-')printf("SUB");
else if(c == '*')printf("MUL");
else if(c == '\\')printf("DIV");
}

void print_right(char c)
{
int pos = is_inR(c);
if(pos != -1)printf("R%d\n",pos);
else printf("%c\n",c);
}

int main()
{
scanf("%d%d",&n,&m);
cntm = 0;
for(int i = 0; i < n; ++i)scanf("%s",s[i]);
for(int i = 0; i < n; ++i)
{
int pos = is_inR(s[i][3]);
if(pos == -1)
{
pos = get_pos(i);
if(R[pos] && get_uselast(i,R[pos]) < n)printf("ST R%d, %c\n",pos,R[pos]);
printf("LD R%d, %c\n",pos,s[i][3]);
R[pos]=s[i][3];
}
print_operator(s[i][4]);
printf(" R%d, ",pos);
print_right(s[i][5]);
R[pos] = s[i][0];
}
return 0;
}

你可能感兴趣的:(编译原理上机题目)