编译原理相关代码备考

表达式语法分析——递归子程序法

#include<bits/stdc++.h>

using namespace std;

char s[100];
int num = 0, cnt = 0;

void E();
void G();
void T();
void S();
void F();

void E()
{
    if(s[cnt] == 'i' || s[cnt] == '(')
    {
        cout<<num++<<" E-->TG"<<endl;
        T();
        G();
    }
    else
    {
        cout<<"error"<<endl;
        exit(0);
    }
}
void G()
{
    if(s[cnt] == '+')
    {
        cout<<num++<<" G-->+TG"<<endl;
        cnt++;
        T();
        G();
    }
    else
        cout<<num++<<" G-->&"<<endl;
}
void T()
{
    if(s[cnt] == 'i' || s[cnt] == '(')
    {
        cout<<num++<<" T-->FS"<<endl;
        F();
        S();
    }
    else
    {
        cout<<"error"<<endl;
        exit(0);
    }
}
void S()
{
    if(s[cnt] == '*')
    {
        cout<<num++<<" S-->*FS"<<endl;
        cnt++;
        F();
        S();
    }
    else
        cout<<num++<<" S-->&"<<endl;
}
void F()
{
    if(s[cnt] == 'i')
    {
        cout<<num++<<" F-->i"<<endl;
        cnt++;
    }
    else if(s[cnt] == '(')
    {
        cout<<num++<<" F-->(E)"<<endl;
        cnt++;
        E();
        if(s[cnt] == ')')
            cnt++;
        else
        {
            cout<<"error"<<endl;
            exit(0);
        }
    }
    else
    {
        cout<<"error"<<endl;
        exit(0);
    }
}
int main()
{
    cin>>s;
    E();
    if(s[cnt] != '#')
        cout<<"error"<<endl;
    else
        cout<<"accept"<<endl;
}


小C语言–词法分析程序

#include <bits/stdc++.h>
using namespace std;
string T[6] = {"main", "if", "else", "for", "while", "int"};
void judge(string s)
{
    if(!s.size())
        return ;
    int f = 1;
    if(s[0]>='0'&&s[0]<='9')
    {
        cout<<"(integer,"<<s<<")"<<endl;
    }
    else
    {
        for(int i = 0; i < 6; i++)
        {
            if(s==T[i])
            {
                cout<<"(keyword,"<<s<<")"<<endl;
                f=0;
                break;
            }
        }
        if(f)
        {
            cout<<"(identifier,"<<s<<")"<<endl;
        }
    }
}
int main()
{
    string s;
    while(cin>>s)
    {
        int len = s.size();
        string tmp;
        for(int i = 0; i < len; i++)
        {
            if(s[i]=='='||s[i]=='+'||s[i]=='-'||s[i]=='*'||
                    s[i]=='/'||s[i]=='<'||s[i]=='>'||s[i]=='!')
            {
                judge(tmp);
                tmp="";
                if(i+1<len&&s[i+1]=='=')
                {
                    cout<<"(operator,"<<s[i]<<s[i+1]<<")"<<endl;
                    i++;
                }
                else
                    cout<<"(operator,"<<s[i]<<")"<<endl;
            }
            else if(s[i]=='{'||s[i]=='}'||s[i]=='('||s[i]==')'||
                    s[i]==','||s[i]==';')
            {
                judge(tmp);
                tmp="";
                cout<<"(boundary,"<<s[i]<<")"<<endl;
            }
            else
                tmp+=s[i];
        }
        judge(tmp);
    }
    return 0;
}

DAG图优化

#include <bits/stdc++.h>
using namespace std;
int cnt;
struct Node
{
    int id;
    int left = -1, right = -1;
    vector<char> var;
} node[110];
char ans[110][10];
bool flag[110];
int n;
bool find_var(int i, char c)
{
    int ii;
    char j;
    for(ii = 0; ii < node[i].var.size(); ii++)
    {
        j = node[i].var[ii];
        if(j==c)
            return 1;
    }
    return 0;
}
int addnode(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 addop(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++;
}
void dfs(int x)
{
    if(node[x].left != -1)
    {
        flag[x] = 1;
        dfs(node[x].left);
        dfs(node[x].right);
    }
}
int main()
{
    cnt=0;
    cin>>n;
    for(int i = 0; i < n; i++)
    {
        string s;
        cin>>s;
        int l = addnode(s[2]);
        int r = addnode(s[4]);
        addop(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;
        }
    }
    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;
}

翻译布尔表达式

#include <bits/stdc++.h>
using namespace std;
string a[100],x;
vector<string>d;
int main() {
    //freopen("data.txt","r",stdin);
    int id,yes,no,tot=0;
    while(cin>>a[tot++]){
        ;
    }
    a[tot-1]="ed";
    id=100;
    yes=1;
    no=100;
    for(int i=0;i<tot;i++){
        x=a[i];
        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){
                cout<<id<<"(j"<<d[i+1]<<","<<d[i]<<","<<d[i+2]<<","<<id+2<<")"<<endl;
                id++;
                cout<<id<<"(j,_,_,"<<no<<")"<<endl;
                no=id++;
            }
            cout<<id<<"(j"<<d[n-2]<<","<<d[n-3]<<","<<d[n-1]<<","<<yes<<")"<<endl;
            yes=id++;
            cout<<id<<"(j,_,_,"<<no<<")"<<endl;
            id++;
            d.clear();
            if(x=="ed")
                break;
        }
        else if(x=="and")
            no+=2;
        else d.push_back(x);
    }
    return 0;
}

简单的代码生成程序

#include <bits/stdc++.h>
using namespace std;
int n,m;
int cntm=0;
string s[110];
char r[10];
int is_inr(char c) //获取c在寄存器中的位置,没有返回-1
{
    for(int i=0; i<m; i++)
    {
        if(r[i]==c)
            return i;
    }
    return -1;
}

int get_uselast(int pos,char c) //c在pos后第一次使用的代码编号
{
    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_op(char c)
{
    if(c=='+')
        cout<<"ADD";
    else if(c=='-')
        cout<<"SUB";
    else if(c=='*')
        cout<<"MUL";
    else if(c=='\\')
        cout<<"DIV";
}

void print_r(char c)
{
    int pos = is_inr(c);
    if(pos!=-1)
        cout<<"R"<<pos<<endl;
    else cout<<c<<endl;
}

int main()
{
    cin>>n>>m;
    cntm=0;
    for(int i=0; i<n; i++)
    {
        cin>>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)
                cout<<"ST R"<<pos<<", "<<r[pos]<<endl;
            cout<<"LD R"<<pos<<", "<<s[i][3]<<endl;
            r[pos]=s[i][3];
        }
        print_op(s[i][4]);
        cout<<" R"<<pos<<", ";
        print_r(s[i][5]);
        r[pos]=s[i][0];
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值