码迷,mamicode.com
首页 > 其他好文 > 详细

简单语法分析器实现

时间:2015-11-09 15:35:07      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:

#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();

简单语法分析器实现

标签:

原文地址:http://www.cnblogs.com/dongkun/p/4949912.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!