[編譯原理]PTA複習筆記

編譯原理複習筆記

遞歸下降

#include<bits/stdc++.h>
using namespace std;
char a[100];
int cur=0;
int N=0;
void E();
void T();
void G();
void S();
void F();

/**
E-->
**/
int main(){
    scanf("%s",&a);
    if(a[cur]!='i'&& a[cur]!='('){
         cout<<"error"<<endl;
         exit(0);
    }
    E();
    if(a[cur]!='#'){
        cout<<"error"<<endl;
    }
    else{
        cout<<"accept"<<endl;
    }
}
void E(){
    cout<<N<<" E-->TG"<<endl;
    N++;
    T();
    G();
}
void T(){
    cout<<N<<" T-->FS"<<endl;
    N++;
    F();
    S();
}
void G(){

    if(a[cur]=='+'){
         cout<<N<<" G-->+TG"<<endl;
         N++;
         cur++;
         T();
         G();
    }
    else{
        cout<<N<<" G-->&"<<endl;
        N++;
    }
}
void F(){
    if(a[cur]=='('){
        cout<<N<<" F-->(E)"<<endl;
        cur++;
        N++;
        E();
        if(a[cur]==')'){
            cur++;
        }
        else{
            cout<<"error"<<endl;
            exit(0);
        }
       }
    else if(a[cur]=='i'){
        cout<<N<<" F-->i"<<endl;
        cur++;
        N++;
    }
    else{
        cout<<"error"<<endl;
        exit(0);
    }

}
void S(){
    if(a[cur]=='*'){
        cout<<N<<" S-->*FS"<<endl;
        N++;
        cur++;
        F();
        S();
    }
    else{
        cout<<N<<" S-->&"<<endl;
        N++;
    }
}

翻譯布爾表達式

#include<bits/stdc++.h>
using namespace std;
vector <string> ans;
string s;
int main(){

        getline(cin,s);
        s+=" end";
        string x;
        int t =1,f=100;
        stringstream ss(s);
        int id =100;
        while(ss>>x){
            if(x == "or"||x=="end"){
                if(x=="or") f+=2;
                else f=0;
                int i;
                int n = ans.size();
                for(i=0;i<n-3;i+=3){
                printf("%d(j%s,%s,%s,%d)\n",id,ans[i+1].c_str(),ans[i+0].c_str(),ans[i+2].c_str(),id+2);
                id++;
                printf("%d(j,_,_,%d)\n",id,f);
                f =id++;
            }
                printf("%d(j%s,%s,%s,%d)\n",id,ans[i+1].c_str(),ans[i].c_str(),ans[i+2].c_str(),t);
                t = id -(n-3)/3*2;
                id++;
                printf("%d(j,_,_,%d)\n",id,f);
                id++;
                ans.clear();
                if(x=="end")break;
            }

            else if(x == "and") f+=2;
            else
                ans.push_back(x);
            }
    }


小C語言

//********************
//小C語言--詞法分析程序**
//********************

/*解法1*/
#include <iostream>
#include <string>
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
string s[5]={"keyword","identifier","integer","boundary","operator"};
string t[6]={"main","if","else","for","while","int"};
void pre(string ch)
{
    if(ch[0]>='0'&&ch[0]<='9') {cout<<"("<<s[2]<<","<<ch<<")"<<endl;}
    else
    {
        int f=1;
        for (int i=0;i<6;i++)
            if(ch==t[i])
            {
                cout<<"("<<s[0]<<","<<ch<<")"<<endl;
                f=0;break;
            }
        if(f)
        cout<<"("<<s[1]<<","<<ch<<")"<<endl;
    }
}
int main()
{
    string p;
    while(cin>>p)
    {
        string temp="";
        int len=p.length();
        for(int i=0;i<len;i++)
        {
            if(p[i]=='='||p[i]=='+'||p[i]=='-'||p[i]=='*'||p[i]=='/'||p[i]=='!'||p[i]=='<'||p[i]=='>')
            {
                if(temp.length()) pre(temp);
                temp="";
                if(i+1<len&&p[i+1]=='=')
                {
                   cout<<"("<<s[4]<<","<<p[i]<<p[i+1]<<")"<<endl;i++;
                }
                else cout<<"("<<s[4]<<","<<p[i]<<")"<<endl;
            }
            else if(p[i]=='{'||p[i]=='}'||p[i]=='('||p[i]==')'||p[i]==','||p[i]==';')
            {
                if(temp.length()) pre(temp);
                temp="";
                cout<<"("<<s[3]<<","<<p[i]<<")"<<endl;
            }
            else temp+=p[i];
        }
        if(temp.length()) pre(temp);
    }
    return 0;

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章