资源简介

编译原理 实验报告 语法分析器 c语言版

资源截图

代码片段和文件信息

#include
#include
#include
int change(char n)
{
    return n-48;
}
int find(char nchar m[])
{
    int i=0;
    while(m[i]!=n)i++;
    return i;
}
int main()
{
    char in[20]sign[30]avt[]={‘+‘‘*‘‘(‘‘)‘‘#‘};
    int f[5][5]={{1-1-111}{11-111}{-1-1-102}{11211}{-1-1-120}};
    int num[30]intop=-1tp=-1bsum;
    sign[++top]=‘#‘;
l3:
system(“cls“);
    printf(“请输入算式:(退出请按q键)\n“);
    for(i=0;i<20;i++)
    in[i]=‘\0‘;
    scanf(“%s“in);
    if(strcmp(in“q“)==0)exit(0);
    strcat(in“#“);
    for(i=0;i    {
        a=in[i];
        if(a>=‘0‘&&a<=‘9‘)
        {
            b=change(a);
            num[++tp]=b;
        }
        else if(a==‘+‘||a==‘*‘||a==‘(‘||a==‘)‘||a==‘#‘)
        {
l1:
            if(f[find(sign[top]vt)][find(avt)]==-1)
            {
                sign[++top]=a;
            }
            else if(f[find(sign[top]vt)][find(avt)]==2)
            {
              printf(“有错误的算式!\n“);
                 system(“pause“);
                 exit(0);
            }
            else
            {
                if(a==‘)‘)
                {
                 if(sign[top]==‘(‘)
{
top--;
continue;
}
                }
                if(sign[top]==‘*‘)
                {
                    sum=num[tp--];
                    sum*=num[tp--];
                    top--;
                    num[++tp]=sum;
                    if(a!=‘#‘&&a!=‘)‘)sign[++top]=a;
                    else goto l1;
                }
                else if(sign[top]==‘+‘)
                {
                    sum=num[tp--];
                    sum+=num[tp--];
                    top--;
                    num[++tp]=sum;
                    if(a!=‘#‘&&a!=‘)‘)sign[++top]=a;
                    else goto l1;
                }
                else if(a==‘#‘)
                {
                    sum=num[tp--];
                }
            }
        }
        else
        {
            printf(“错误的字符或符号!\n位置:%d错误的符号:%c\n“i+1a);
            system(“pause“);
           goto  l1;
        }
}
   printf(“sum=%d\n“sum);
   system(“pause“);
   goto l3;
 }



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

     文件     203264  2012-05-30 17:01  编译原理课程设计_语法分析程序_[1]....doc

     文件       2293  2010-01-19 12:30  语法分析 词法分析.cpp

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

               205557                    2


评论

共有 条评论