| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1759 人关注过本帖
标题:语法分析器错误代码求教!
取消只看楼主 加入收藏
wk7622317
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-11-12
收藏
 问题点数:0 回复次数:1 
语法分析器错误代码求教!
编了一个语法分析器,但是运行时说错误了。找不出在哪里,请高手们帮帮忙。

#include<stdio.h>
#include<iostream.h>
#include<string.h>
#define NULL 0    
FILE *p;
char b=' ';
void zhuprogram();
void son_function();//子函数
//void error();//报错
void function();//函数
int DataType();
void Biaozhifu();//标识符
void xingcan();//形参
void function_body();//函数体
void definate_val();//定义变量
void operate_centence();//执行语句
void centence();//语句
void fuzhi();//赋值语句
void function_diaoyong();
void biaodashi();
void item();//项
void itemrec();
void factrec();
void factor();
void shican();
void condition();
int guanxi();
void condition_centence();
void while_circle();
void function_centence();
void fuhecentence();
void empty();
void read_centence();
void write_centence();

int row=1;
char *sym;
char str1[]={"void"};//字符串
char str2[]={"int"};
void Getsymbol(char s[])
{
    int i=0;
    char a;
    a=b;
    if (b!=' '&&b!=9&&b!='\n')
        s[i++]=b;
    while(!feof(p))
    {
       a=getc(p);
      while(a=' '||a==9)//制表符为9
            a=getc(p);
        if ((a>='a'&&a<='z')||(a>='A'&&a<='Z'))
        {
           s[i++]=a;
           a=getc(p);
           while((a>='a'&&a<='z')||(a>='A'&&a<='Z')||(a>='0'&&a<='9')||a=='-')
           {
              a=getc(p);
              s[i++]=a;
           }
           s[i]='\0';
           b=a;
           break;
        }
       else if (a>='0'&&a<='9')
       {
          s[i++]=a;
          a=getc(p);
          while (a>='0'&&a<='9')
          {
               s[i++]=a;
               a=getc(p);
          }
         s[i]='\0';
            b=a;
         break;
       }
       else if(a=='>'||a=='<'||a=='='||a=='!')
       {
          s[i++]=a;
          a=getc(p);
          while(a=='=')
          {
             s[i++]=a;
             a=getc(p);
          }
          s[i]='\0';
             b=a;
          break;
       }
       else if (a=='+'||a=='-'||a=='*'||a=='/'||a=='('||a==')'||a=='{'||a=='}'||a==';'||a==','||a=='"')
       {
          s[i++]=a;
          a=getc(p);
          s[i]='\0';
          b=a;
          break;
       }
       else if (a=='%')
       {
           s[i++]=a;
           a=getc(p);
           while(a=='d')
           {
               s[i++]=a;
               a=getc(p);
           }
           s[i]=a;
           b=a;
           break;
       }
       else if (a=='\n')
       {
          row++;
          while(a=='\n')
          {
             row++;
             a=getc(p);
          }
           b=a;
         break;
      }
      else break;
    }
}

void main()
{
       p=fopen("a.txt","r");
    if(p==NULL)
        printf("cannot open file\n");
        sym=new char[100];
        zhuprogram();
       Getsymbol(sym);
       delete sym;
       fclose (p);
}
void zhuprogram()
{
    son_function();//可选
    zhuprogram();
}
void son_function()//子函数
{
    function();
}
void function()//函数
{
    DataType();
    Biaozhifu();
    if(!strcmp(sym,"("))
    {
        Getsymbol(sym);
        xingcan();//可选
        if(!strcmp(sym,")"))
        {
            Getsymbol(sym);
            if(!strcmp(sym,"{"))
            {
                Getsymbol(sym);
                function_body();
                if(!strcmp(sym,"}"))
                {
                    Getsymbol(sym);
                }
                else cout<<" } "<<endl;
            }
            else cout<<"  { ";
        }
        else cout<<"  ) ";
    }
    else cout<<"  ( ";
}
int DataType()
{
    if(!strcmp(sym,str1)||strcmp(sym,str2))//字符串表示
        return 1;
    else
    {
        cout<<"数据类型错误"<<endl;
        return 0;
    }
}
void Biaozhifu()//标识符
{
    char zimu[52][2]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p",
        "q","r","s","r","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I",
        "J","K","L","M","N","O","P","Q","R","S","R","U","V","W","X","Y","Z"};
//    char shuzi[10]={"0","1","2","3","4","5","6","7","8","9"};

    for(int i=0;i<52;i++)
    {
    if(!strcmp(sym,zimu[i]))
    {
        Getsymbol(sym);
        Biaozhifu();
    }
    else cout<<" 定义错误"<<endl;
    }
}

void xingcan()//形参
{
    if(!strcmp(sym,str2))
    {
        Getsymbol(sym);
        Biaozhifu();
    }
    else cout<<" 定义错误"<<endl;
}
void function_body()//函数体
{
    definate_val();//定义变量
    operate_centence();//执行语句
}
void definate_val()//定义变量
{
    if(!strcmp(sym,str2))
    {
        Biaozhifu();
    }
    else cout<<" 定义错误"<<endl;
}

void operate_centence()//执行语句
{
    centence();
}

void centence()//语句
{
    if ((!strcmp(sym,str1))&&(!strcmp(sym,str2)))
          fuzhi();
    else if (!(!strcmp(sym,"if")))
           condition_centence();
         else if(!(!strcmp(sym,"while")))
             while_circle();
              else if (!(!strcmp(sym,"{")))
              {
                  Getsymbol(sym);
                  if (!(!strcmp(sym,"}")))
                       empty();
                  else fuhecentence();
              }
                    else if (!(!strcmp(sym,"scanf")))
                        read_centence();
                         else if(!(!strcmp(sym,"printf")));
                              else
                                   function_diaoyong();
                              Getsymbol(sym);
}
void fuzhi()//赋值语句
{
//    Biaozhifu()=biaodashi;
}
void biaodashi()
{
    item();
    itemrec();
}
void item()//项
{
    factor();
    factrec();

}
void itemrec()
{
    if(!strcmp(sym,"+")||strcmp(sym,"-"))
        Getsymbol(sym);
    item();
    itemrec();

}
void factrec()
{
   if(!strcmp(sym,"*")||strcmp(sym,"/"))
        Getsymbol(sym);
   factor();
   factrec();
}
void factor()
{
    if(!(*sym>='A'&&*sym<='Z') && !(*sym>='a'&&*sym<='z')
        && !(*sym>='0'&&*sym<='9') && strcmp(sym,"("))
        cout<<"非法"<<endl;
    else
        if(!strcmp(sym,"("))
        {
            Getsymbol(sym);
            biaodashi();
            if(!strcmp(sym,")"))
                cout<<"   )  "<<endl;
        }
    Getsymbol(sym);
}
void condition_centence()//条件语句
{
    if(!strcmp(sym,"if"))
    {
        Getsymbol(sym);
        if(!strcmp(sym,"("))
        {
            Getsymbol(sym);
            condition();
            if(!strcmp(sym,")"))
            {
                Getsymbol(sym);
                centence();
                if(!strcmp(sym,";"))
                {
                    Getsymbol(sym);
                    if(!strcmp(sym,"else"))
                    {
                        Getsymbol(sym);
                        centence();
                    }
                }
                else cout<<"  ;  "<<endl;
            }
            else cout<<"  )  "<<endl;
        }
        else cout<<"  (  "<<endl;
    }
}

void condition()//条件
{
    biaodashi();
    guanxi();
    biaodashi();
}

int guanxi()
{
    if(sym==">")
        return 1;
    else if(sym=="<")
            return 1;
         else if(sym==">=")
                return 1;
              else if(sym=="<=")
                       return 1;
                    else if(sym=="==")
                            return 1;
                          else if(sym=="!=")
                                   return 1;
                                else
                                    return 0;

}
void while_circle()//当循环语句
{
    if(sym=="while")
    {
        Getsymbol(sym);
        if(sym=="(")
        {
            Getsymbol(sym);
            condition();
            if(sym==")")
            {
                Getsymbol(sym);
                centence();
            }
            else cout<<"  )  "<<endl;
        }
        else cout<<"  (  "<<endl;
    }
}
void function_diaoyong()//函数调用语句
{
    Biaozhifu();
    if(sym=="(")
    {
        Getsymbol(sym);
        shican();
        if(sym==")")
            Getsymbol(sym);
        else cout<<"  )  "<<endl;

    }
    else cout<<"  (  "<<endl;
}
void shican() //实参
{
    biaodashi();
}

void fuhecentence() //复合语句,没有最外边的else表示可选
{
    if(sym=="{")
    {
        Getsymbol(sym);
        centence();
        if(sym=="}")
            Getsymbol(sym);
        else cout<<"  }  "<<endl;
    }
}
void empty()//空语句
{
}
void read_centence()//读语句
{
    if(strcmp(sym,"scanf")==0)
    {
        Getsymbol(sym);
        if(strcmp(sym,"(")==0)
        {
            Getsymbol(sym);
            if(strcmp(sym,"\" ")==0)
            {
                Getsymbol(sym);
                if(strcmp(sym,"%d")==0)
                {
                    Getsymbol(sym);
                    if(strcmp(sym," \" ")==0)
                    {
                        Getsymbol(sym);
                        if(strcmp(sym,",")==0)
                        {
                            Getsymbol(sym);
                            if(strcmp(sym,"&")==0)
                            {
                                Getsymbol(sym);
                                Biaozhifu();
                                if(strcmp(sym,")")==0)
                                    Getsymbol(sym);
                                else cout<<"缺少) "<<endl;
                            }
                            else cout<<"错误 "<<endl;
                        }
                        else cout<<"格式错误,缺少\",\" "<<endl;
                    }
                    else cout<<"格式错误 "<<endl;
                }
                else cout<<"缺少数据类型"<<endl;
            }
            else cout<<"格式错误 "<<endl;
        }
        else cout<<"缺少( "<<endl;
    }
}
void write_centence()//写语句
{
    if(strcmp(sym,"printf")==0)
    {
        Getsymbol(sym);
        if(strcmp(sym,"(")==0)
        {
            Getsymbol(sym);
            if(strcmp(sym,"\" ")==0)
            {
                Getsymbol(sym);
                if(strcmp(sym,"%d")==0)
                {
                    Getsymbol(sym);
                    if(strcmp(sym," \" ")==0)
                    {
                        Getsymbol(sym);
                        if(strcmp(sym,",")==0)
                        {
                            Getsymbol(sym);
                            biaodashi();
                            if(strcmp(sym,")")==0)
                                Getsymbol(sym);
                            else cout<<"缺少) "<<endl;
                        }
                        else cout<<"格式错误,缺少\",\" "<<endl;
                    }
                    else cout<<"格式错误 "<<endl;
                }
                else cout<<"缺少数据类型"<<endl;
            }
            else cout<<"格式错误 "<<endl;
        }
        else cout<<"缺少) "<<endl;
    }
}
搜索更多相关主题的帖子: 分析器 语法 代码 
2008-11-12 15:37
wk7622317
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-11-12
收藏
得分:0 
谢谢,我去试试……
2008-11-12 20:43
快速回复:语法分析器错误代码求教!
数据加载中...
 
   



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

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