资源简介

三地址代码是编译原理语法分析后的中间语言的一种,这是我刚完成的三地址代码生成器,符合的语法规则及其语义规则如下(S→if C then S1 else S2,这条规则没有加,其余都已完成,也许还有bug,欢迎大家给予指正):产生式 语义规则S → id = E S.code = E.code || gen(id.place’:=’E.place)S → if C then S1 C.true = newlabel; C.false = S.next;S1.next = S.next;S.code = C.code || gen(E.true’:’) || S1.codeS → if C then S1 else S2 C.true = newlabel; C.false = newlabel;S1.next = S2.next =S.next;S.code = C.code || gen(E.true’:’) || S1.code ||gen(‘goto’,S.next)|| gen(E.false’:’) || S2.codeS → while C do S1 S.begin = newlabel; C.true = newlabel;C.false = S.next; S1.next = S.begin;S.code = gen(S.begin’:’) || C.code ||gen(E.true’:’) || S1.code || gen(‘goto’S.begin);C → E1 > E2 C.code = E1.code || E2.code ||gen(‘if’E1.place’>’E2.place’goto’C.true) ||gen(‘goto’C.false)C → E1 < E2 C.code = E1.code || E2.code ||gen(‘if’E1.place’<’E2.place’goto’C.true) ||gen(‘goto’C.false)C → E1 = E2 C.code = E1.code || E2.code ||gen(‘if’E1.place’=’E2.place’goto’C.true) ||gen(‘goto’C.false)E → E1 + T E.place = newtemp;E.code = E1.code||T.code||gen(E.place’:=’E1.place’+’T.place)E → E1 - T E.place = newtemp; E.code = E1.code || T.code ||gen(E.place’:=’E1.place’-’T.place)E → T E.place = T.place; E.code = T.codeT → F T.place = F.place; T.code = F.codeT → T1 * F T.place = newtemp;T.code = T1.code || F.code ||gen(T.place’:=’T1.place’*’F.place)T → T1 / F T.place = newtemp; T.code = T1.code || F.code ||gen(T.place’:=’T1.place’/’F.place)F → ( E ) F.place = E

资源截图

代码片段和文件信息

#include
#include
#include
#include
#include
char input[200]mech[10];        /*input存放最先输入的字符串,在输入的过程中,就进行了预处理*/
char ech[100][10];
char echattr[100][10];
char nch[10];
char Fplace[10]Eplace[10]Tplace[10]TTplace[10]EEplace[10]E1place[10]E2place[10]idplace[10]Splace[10]Snext[10]Ctrue[10]Cfalse[10]Sbegin[10];
char S1next[10]S2next[10];
char Fcode[50]Ecode[50]Tcode[50]TTcode[50]EEcode[50]E1code[50]E2code[50]idcode[50]Scode[50]Ccode[50];
int temp=1;
int label=0;
int stack;
int nuattr;
int flag=0;
char attr[10];
int number=0;
int now;
void init(char *ch)
{
    char newc[10];                                   /*xinlingsoft http://hi.csdn.net/bjutlife  sxlbjut@gmail.com*/
    newc[0]=NULL;
    strcpy(chnewc);
}
int S();
int SS();
int E();
int C();
int CC();
int T();
int TT();
int EE();
int F();
int scan();
void newtemp(char *ch)
{
    sprintf(attr“%d“temp);
    strcpy(ch“t“);
    strcat(chattr);
    temp++;
}
void newlabel(char *ch)
{
    sprintf(attr“%d“label);
    strcpy(ch“L“);
    strcat(chattr);
    label++;
}
main()
{
    int i=0j=0t;
    char c;
    char c1[2];
    c1[1]=‘\0‘;
    printf(“Please input the String end with #:\n“);    /*以#代表结束*/
    do
    {
        c=getchar();
        if(c==‘ ‘)
        {
            if(i==0)
            {
                while(c==‘ ‘)
                {
                    c=getchar();
                }
                input[i]=c;
                i++;
            }
            else
            {
                while(c==‘ ‘)
                {
                    c=getchar();
                }
                input[i]=‘ ‘;
                i++;
                input[i]=c;
                i++;
            }
        }
        else
        {
            input[i]=c;
            i++;
        }
    }while(c!=‘#‘);                                   /*输入字符串,输入的过程中进行预处理,去掉多余的空格*/
    i=0;
    while(input[i]!=‘#‘)
    {
        c1[0]=input[i];
        if(c1[0]==‘+‘||c1[0]==‘-‘||c1[0]==‘*‘||c1[0]==‘/‘||c1[0]==‘>‘||c1[0]==‘<‘||c1[0]==‘=‘||c1[0]==‘(‘||c1[0]==‘)‘)
        {
            if(strlen(mech)!=0)
            {
                t=scan(mech);
                if(t==15)
                {
                    sprintf(attr“%d“nuattr);
                    strcpy(echattr[j]attr);                 /*xinlingsoft http://hi.csdn.net/bjutlife  sxlbjut@gmail.com*/
                    strcpy(mech“int10“);
                }
                if(t==16)
                {
                    sprintf(attr“%d“nuattr);
                    strcpy(echattr[j]attr);
                    strcpy(mech“int16“);
                }
                if(t==17)
                {
                    sprintf(attr“%d“nuattr);
                    strcpy(echattr[j]attr);
                    strcpy(mech“int8“);
                }
                if(t==18)
        

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      23283  2008-05-30 10:18  sandizhifinal.c

----------- ---------  ---------- -----  ----

                23283                    1


评论

共有 条评论