| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 398 人关注过本帖
标题:编译原理
只看楼主 加入收藏
huangjun422
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2011-11-3
结帖率:100%
收藏
 问题点数:0 回复次数:0 
编译原理
#include<stdio.h>
#include<stdlib.h>

#include"cifa.h"
#define MAX 20



char index_char[10]={'+','-','*','/','(',')','=','#','S','E'};
int get_index_char(char i)
{
    int j;
    for(j=0;j<10;j++)
    {
        if(index_char[j] == i)
            return j;
    }
    return -1;
}
//规约规则
struct rule{
char x;
int y;
}r[8]={{' ',0},{'S',3},{'E',3},{'E',3},{'E',2},{'E',3},{'E',1},{'E',3}};
/*0-16 表示状态节点,21-27 表示规约标号,-1 表示出错,20 表示成功*/
int table[17][11]={
{2,-1,3,-1,-1,4,-1,-1,-1,13,1},{-1,5,-1,6,14,-1,-1,7,-1,-1,-1},
{26,26,26,26,26,26,26,26,26,-1,-1},{2,-1,3,-1,-1,4,-1,-1,-1,-1,8},
{2,-1,3,-1,-1,4,-1,-1,-1,-1,11},{2,-1,3,-1,-1,4,-1,-1,-1,-1,9},
{2,-1,3,-1,-1,4,-1,-1,-1,-1,10},{2,-1,3,-1,-1,4,-1,-1,-1,-1,16},
{24,5,24,6,16,24,24,24,24,-1,-1},{22,5,22,6,16,22,22,22,22,-1,-1},
{23,5,23,6,16,23,23,23,23,-1,-1},{-1,5,-1,6,16,-1,12,-1,-1,-1,-1},
{25,25,25,25,25,25,25,25,25,-1,-1},{-1,-1,-1,-1,-1,-1,-1,-1,20,-1,-1},
{2,-1,3,-1,-1,4,-1,-1,-1,-1,15},{27,5,27,6,16,27,27,27,27,-1,-1},
{21,5,21,6,16,21,21,21,21,-1,-1}};
 struct status{
    int  stack[MAX];
    int top;
    };
//初始化栈
void init_stack1(struct status *p)
{
    if(!p)
        printf("\n初始化状态栈出错!\n");
    p->top = -1;
}
//压栈
void push1(struct status *p,int x)
{
    if(p->top < MAX-1)
    {
        p->top++;
        p->stack[p->top] = x;
    }
    else printf("\n状态栈溢出!\n");
}
//弹栈
int pop1(struct status *p)
{
    int x;
    if(p->top != 0)
    {
        x = p->stack[p->top];
        p->top--;
        return x;
    }
    else
    {
        printf("\n状态栈1空!\n");
        return 0;
    }
}
//取栈顶元素
int get_top1(struct status *p)
{
    int x;
    if(p->top != -1)
    {
        x = p->stack[p->top];
        return x;
    }
    else
    {
        printf("\n状态栈2空!\n");
        return 0;
    }
}
//遍历栈元素
void out_stack1(struct status *p)
{
    int i;
    if(p->top <0)
        printf("\n状态栈3空!\n");
    for(i=0; i<=p->top;i++)
    {
        printf("%d",p->stack[i]);
    }
}
struct symbol_instr{
    char stack[MAX];
    int top;
};
//初始化栈
void init_stack2(struct symbol_instr *p)
{
    if( !p)
        printf("\n初始化符号栈出错!\n");
    p->top = -1;
}
//压栈
void push2(struct symbol_instr *p,char x)
{
    if(p->top < MAX-1)
    {
        p->top++;
        p->stack[p->top] = x;
    }
    else printf("\n符号栈溢出!\n");
}
//弹栈
char pop2(struct symbol_instr *p)
{
    char x;
    if(p->top != -1)
    {
        x = p->stack[p->top];
        p->top--;
        return x;
    }
    else
    {
        printf("\n符号栈1空!\n");
        return 0;
    }
}
//取栈顶元素
char get_top2(struct symbol_instr *p)
{
    char x;
    if(p->top != -1)
    {
        x = p->stack[p->top];
        return x;
    }
    else
    {
        printf("\n符号栈2空!\n");
        return 0;
    }
}
//自栈底到栈顶遍历栈元素
void out_stack21(struct symbol_instr *p)
{
    int i;
    if(p->top <0)
        printf("\n符号栈3空!\n");
    for(i=0; i<=p->top;i++)
    {
        printf("%c",p->stack[i]);
    }
}
//自栈顶到栈底遍历栈元素
void out_stack22(struct symbol_instr *p)
{
    int i;
    if(p->top <0)
        printf("\n符号栈4空!\n");
    for( i=p->top;i>=0;i--)
    {
        printf("%c",p->stack[i]);
    }
}
int lookup(int a)
{
    switch(a)
    {
    case 2:
        return 2;
    case 3:
        return 3;
    case 4:
        return 4;
    case 5:
        return 5;
    case 6:
        return 6;
    case 7:
        return 7;
    case 12:
        return 12;
    case 14:
        return 14;
    case 16:
        return 16;
    }
}
//移进
void shift(int a,char ch,struct status *status_p,struct symbol_instr *symbol_p)
{
    push1(status_p,a);
    push2(symbol_p,ch);
}
int go(int i,int n)
{
    return table[i][n];
}

//规约
void statute(int j,struct status *status_p,struct symbol_instr *symbol_p)
{
    int i,k,n,m;
    char ch;
    k=r[j].x;
    while(k--)
    {
        pop1(status_p);
        pop2(symbol_p);
    }
    push2(symbol_p,r[j].y);
    i=get_top1(status_p);
    ch=get_top2(symbol_p);
    n=get_index_char(ch);
    m=go(i,n);
    push1(status_p,m);
}

void main()
{
    FILE *fp,*out;
    char name[20];
    char ch;
    int i,j,index;

    printf("                     ***********************************     \n");
    printf("                                词法分析器                   \n");
    printf("                     ***********************************     \n");
    printf("Please input the name  of source:");
    scanf("%s",name);
    fp=fopen(name,"r");
    out=fopen("result.txt","w");
    analysis();
    fclose(fp);
    fclose(out);
    printf("                     ***********************************     \n");
    printf("                                语法分析器                   \n");
    printf("                     ***********************************     \n");
    fp=fopen("result.txt","r");

    struct status * status_p;
    struct symbol_instr * symbol_p;
    status_p=(struct status *)malloc(sizeof(status));  //状态栈
    symbol_p=(struct symbol_instr *)malloc(sizeof(symbol_instr)); //符号栈(字符类型)
    init_stack1(status_p);
    init_stack2(symbol_p);
    //压进栈初始元素
    push1(status_p,0);
    push2(symbol_p,'#');
    while(!feof(fp))
    {
        ch=getc(fp);
        if(ch=='+')
        {
             i=get_top1(status_p);
            j=lookup(table[i][1]);
            if(j<=16)
                shift(j,ch,status_p,symbol_p);
            else if(20<j&&j<28)
            {
                printf("%c\n",'+');
                statute(j,status_p,symbol_p);
            }
        }
        else if(ch=='-')
        {
            i=get_top1(status_p);
            j=lookup(table[i][2]);
            if(j<=16)
                shift(j,ch,status_p,symbol_p);
            else if(20<j&&j<28)
            {
                printf("%c\n",'-');
                statute(j,status_p,symbol_p);
            }
        }
        else if(ch=='*')
        {
            i=get_top1(status_p);
            j=lookup(table[i][3]);
            if(j<=16)
                shift(j,ch,status_p,symbol_p);
            else if(20<j&&j<28)
            {
                printf("%c\n",'*');
                statute(j,status_p,symbol_p);
            }
        }
        else if(ch=='/')
        {
            i=get_top1(status_p);
            j=lookup(table[i][4]);
            if(j<=16)
                shift(j,ch,status_p,symbol_p);
            else if(20<j&&j<28)
            {
                printf("%c\n",'/');
                statute(j,status_p,symbol_p);
            }
        }
        else if(ch=='(')
        {
            i=get_top1(status_p);
            j=lookup(table[i][5]);
            if(j<=16)
                shift(j,ch,status_p,symbol_p);
            else if(20<j&&j<28)
            {
                printf("%c\n",'(');
                statute(j,status_p,symbol_p);
            }
        }
        else if(ch==')')
        
        {
            i=get_top1(status_p);
            j=lookup(table[i][6]);
            if(j<=16)
                shift(j,ch,status_p,symbol_p);
            else if(20<j&&j<28)
            {
                printf("%c\n",')');
                statute(j,status_p,symbol_p);
            }
        }
        else if(ch=='=')
        {
            i=get_top1(status_p);
            j=lookup(table[i][7]);
            if(j<=16)
                shift(j,ch,status_p,symbol_p);
            else if(20<j&&j<28)
            {
                printf("%c\n",'=');
                statute(j,status_p,symbol_p);
            }
        }
        else if(ch=='#')
        {
            i=get_top1(status_p);
            j=lookup(table[i][8]);
            if(j<=16)
                shift(j,ch,status_p,symbol_p);
            else if(20<j&&j<28)
            {
                statute(j,status_p,symbol_p);
            }
            else if(j==20)
                printf("分析成功!");
        }
        else
        {
            i=get_top1(status_p);
            j=lookup(table[i][0]);
            if(j<=16)
                shift(j,ch,status_p,symbol_p);
            else if(20<j&&j<28)
            {
                printf("%c\n",ch);
                statute(j,status_p,symbol_p);}
        }
        
    }
    fclose(fp);
}
这是编译原理的赋值语句的翻译程序,不知道是哪里有问题,求高手指点……
搜索更多相关主题的帖子: 成功 include return 
2011-11-13 20:19
快速回复:编译原理
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037218 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved