컴파일링 원리

80907 단어
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
"
,f++); p++; F(); S(); } else { printf("%d S-->&
"
,f++); } } void F() { if(!flag) return; if(a[p]=='(') { printf("%d F-->(E)
"
,f++); p++; E(); if(a[p]!=')') flag=0; p++; } else if(a[p]=='i') { printf("%d F-->i
"
,f++); p++; } else flag=0; } void T() { if(!flag) return; if(a[p]=='('||a[p]=='i') printf("%d T-->FS
"
,f++); F(); S(); } void G() { if(!flag) return; if(a[p]=='+') { printf("%d G-->+TG
"
,f++); p++; T(); G(); } else printf("%d G-->&
"
,f++); } void E() { if(!flag) return; if(a[p]=='('||a[p]=='i') printf("%d E-->TG
"
,f++); T(); G(); } int main() { p=0; cin>>a; if(a.length()==0||(a.length()==1&&a[0]=='#')) {printf("error
"
);return 0;} E(); if(!flag) printf("error
"
); else if(p!=a.length()-1) printf("error
"
); else printf("accept
"
); 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)
"
,id,d[i+1].c_str(),d[i].c_str(),d[i+2].c_str(),id+2); id++; printf("%d(j,_,_,%d)
"
, id, no); no=id++; } printf("%d(j%s,%s,%s,%d)
"
,id,d[n-2].c_str(),d[n-3].c_str(),d[n-1].c_str(),yes); yes=id++; printf("%d(j,_,_,%d)
"
, 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
"
,pos); else printf("%c
"
,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
"
,pos,R[pos]); printf("LD R%d, %c
"
,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; }

좋은 웹페이지 즐겨찾기