• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
迷失的数组
博客园    首页    新随笔    联系   管理    订阅  订阅

简单语法分析器实现

#include <iostream>

#include <string.h>

#include <ctype.h>

char prog[80],token[8];

char ch;

int syn,p,m,n,kk,sum=0;

char *rwtab[6]={"begin","if","then","while","do","end"};

int lrparser();

int yucu();

int statement();

int expression();

int term();

int factor();

void scanner()

{

     m=0;

     for(n=0;n<8;n++)

         token[n]=NULL;

     ch=prog[p++];

     while(ch==' ')ch=prog[p++];

     if(isalpha(ch))//字母字符

     {

         while(isdigit(ch)||isalpha(ch))//字母或数字字符

         {

              token[m++]=ch;

              ch=prog[p++];

         }

         token[m++]='\0';p--;syn=10;

         for(n=0;n<6;n++)

              if(strcmp(token,rwtab[n])==0)

              {

                   syn=n+1;

                   break;

              }

     }

     else if(isdigit(ch))//数字字符

     {

         sum=0;

         while(isdigit(ch))//shuzi

         {

              sum=sum*10+ch-'0';

              ch=prog[p++];

         }

         p--;

         syn=11;

     }

     else   

         switch(ch)

     {

         case '<': m=0;

              token[m++]=ch;

              ch=prog[p++];

              if(ch=='>')

              {

                   syn=22;

                   token[m++]=ch;

              }

              else if(ch=='=')

              {

                   syn=22;

                   token[m++]=ch;

              }

              else

              {

                   syn=20;

                   p--;

              }

              break;

         case '>':token[m++]=ch;

              ch=prog[p++];

              if(ch=='=')

              {

                   syn=24;

                   token[m++]=ch;

              }

              else

              {

                   syn=23;

                   p--;

              }

              break;

         case ':':token[m++]=ch;

              ch=prog[p++];

              if(ch=='=')

              {

                   syn=18;

                   token[m++]=ch;

              }

              else

              {

                   syn=17;

                   p--;

              }

              break;

         case '+':syn=13;token[0]=ch;break;

         case '-':syn=14;token[0]=ch;break;

         case '*':syn=15;token[0]=ch;break;

         case '/':syn=16;token[0]=ch;break;

         case '=':syn=25;token[0]=ch;break;

         case ';':syn=26;token[0]=ch;break;

         case '(':syn=27;token[0]=ch;break;

         case ')':syn=28;token[0]=ch;break;

         case '#':syn=0;token[0]=ch;break;

         default:syn=-1;

     }

}

int lrparser()//分析语句串开始结束

         {

              //scanner();//读取下一个单词符号

              if(syn==1)

              {

                   scanner();

                   yucu();

                   if(syn==6)

                   {

                       scanner();

                       if(syn==0&&(kk==0))

                            printf("Success!");

                   }

                   else{

                       if(kk!=1)

                            printf("lose end!");

                       kk=1;

                   }

              }

                   else{

                       printf("lose begin!");

                       kk=1;

                   }

                   return 1;

             

         }

int yucu()

{

     statement();

     while(syn==26)//分号,

     {

         scanner();

         statement();

     }

     return 1;

}

int statement()//赋值语句分析

{

     if(syn==10)//标示符

     {

         scanner();

         if(syn==18)//赋值号:=

         {

              scanner();

              expression();

         }

         else{printf(":=错误!");kk=1;}

     }

 

     return 1;

}

int expression()

{

     term();

     while(syn==13||syn==14)

     {

         scanner();

         term();

     }

     return 1;

}

int term()//运算符识别

{

     factor();

     while(syn==15||syn==16)

     {

         scanner();

         factor();

     }

     return 1;

}

int factor()

{

     if(syn==11||syn==10)

         scanner();

     else if(syn==27)

     {

         scanner();

         expression();

         if(syn==28)

              scanner();

         else

         {

              printf("')'ERROR!");

              kk=1;

         }

     }

     else

     {

         printf("biaodashicuowu");

     }

     return 1;

}

void main()

{

     p=0;

     printf("\n please input string:\n");

         do{

              ch=getchar();

              prog[p++]=ch;

         }while(ch!='#');

         p=0;

         scanner();

         lrparser();

posted @ 2015-11-09 14:47  迷失的数组  阅读(534)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3