回 帖 发 新 帖 刷新版面

主题:请大虾救急

哪位大虾能帮我用C语言解决这个词法分析器问题1.  给出一简单语言单词符号的种别编码单词符号    种别编码
begin         1
if            2
then          3
while         4
do           5
end          6
l ( l | d ) *     10
dd *          11
+            13
–           14
*            15
/             16
:             17
:=            18
<             20
<>            21
<=           22
>             23
>=            24
=             25
;              26
(              27
)              28
#               0
要求:输入是源程序字符串,以‘#’ 结束。输出是单词符号的二元组
( 单词种别码, 单词自身字符串 )
即      ( syn , token 或 sum )
已给出伪代码如下:
# include  <stdio.h> 定义 I / O库所用的某些宏和变量
# include  <string.h> 定义 字符串库函数
char   prog[80], token[8] ;
char   ch ;
int  syn, p, m, n ,sum ;
char   *rwtab[6]={ “begin”, “if ”, “then”, “while”,  “do” ,“end” };
main( )
    { p=0 ;
       printf( “\n please input  string : \n” ) ;
       do  { ch=getchar( );
                prog[p++]=ch ;
              } while ( ch!=‘#’);
P=0;
do
  {  scaner();
      switch(syn)
          { case 11:  printf (“(%2d,%8d)\n”,syn,sum); break;
             case –1:  printf(“input error\n”);  break;     
             default:   printf (“(%2d,%8s)\n”,syn,token);
   } while (syn!=0);
}
scaner( )
    {  for ( n=0; n<8; n++ ) token[n]=NULL;
        ch=prog[p++] ;
        while (ch==‘ ‘)  ch=prog[p++] ;
if  (  ch 是字母字符 )
      { while ch 为字母字符或数字字符
             { ch => token ;
                读下一个字符 ;   
              }
         token[m++]=‘\0’ ;   p-- ;   syn=10 ;
         for (n=0 ;   n<6 ;  n++ )
            if  ( strcmp( token, rwtab[n])= =0)
               {  给出 syn 值;
                   break ;
                }
       }
   else   
if  (  ch 是数字字符 )
{  while ch 为数字字符
      { sum=sum*10+ch-’0’;
         读下一个字符 ;   
      }
      p-- ;   syn=11;
}
else
   switch(ch)
      {
case '<':
    m=0; token[m++]=ch;
    ch=prog[p++];
    if (ch=='>')
    {  syn=21;
        token[m++]=ch;
    }
    else if (ch=='=')
           {  syn=22;
               token[m++]=ch;
                 }
    else { syn=20;
              p--;
          }
         break;
case ‘>':
    ch => token ;
    读下一个字符 ;
    if (ch=='=')
    {将>=的种别码=>syn;
            ch => token ;
    }
    else
    { 将>的种别码=>syn ;
           p--;
     }
        break;
case ':':
    ch => token ;
    读下一个字符 ;
    if (ch=='=')
    { 将:=的种别码=>syn;
              ch => token ;
    }
    else
    { 将:的种别码=>syn ;
            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=0;  token[0]=ch; break;
default:
    syn= -1;  

回复列表 (共1个回复)

沙发

#include "stdio.h"
#include "ctype.h"
#include "math.h"
#include  "iostream.h"
#include"string.h"
#define LETTER  0
#define DIGIT   1
#define POINT   2
#define OTHER    3
#define POWER   4
#define PLUS    5
#define MINUS    6
#define ClassNo   100
#define ClassOther   200
#define EndState   -1
char num[100];
int w,n,p,e,d;
int Class,i=0;
int ICON;
float FCON;
static int CurrentState;

int GetChar(void);
int EXCUTE(int,int);
int LEX(void);

int HandleOtherWord(void)
{
    return ClassOther;
}

int HandleError(void)
{
    printf("ERROR!\n");
    return 0;
}

int GetChar(void)
{
    int c;

    c=num[i++];

    if(isdigit(c))
    {
        d=c-'0';return DIGIT;
    }
    if(c=='.')return POINT;
    if(c=='E'||c=='e')return POWER;
    if(c=='+') return PLUS;
    if(c=='-')return MINUS;
    return OTHER;
}
//============================================

int EXCUTE(int state,int symbol)
{
    switch (state)
    {
    case 0:switch(symbol)
           {
            case DIGIT:n=0;p=0;e=1;w=d;CurrentState=1;Class=ClassNo;break;
            case POINT:w=0;n=0;p=0;e=1;CurrentState=3;Class=ClassNo;break;
            default:HandleOtherWord();Class=ClassOther;
                CurrentState=EndState;
           }
        break;
    case 1:switch(symbol)
           {
        case DIGIT:w=w*10+d;break;
        case POINT:CurrentState=2;break;
        case POWER:CurrentState=4;break;
        default:FCON=w;CurrentState=EndState;
           }
        break;
    case 2:switch(symbol)
           {
            case DIGIT:n++;w=w*10+d;break;
            case POWER:CurrentState=4;break;
            default:FCON=w*pow(10,e*p-n);CurrentState=EndState;
           }
        break;
    case 3:switch(symbol)
           {
            case DIGIT:n++;w=w*10+d;CurrentState=2;break;
            default:HandleError();CurrentState=EndState;
           }
        break;
    case 4:switch(symbol)
           {
            case DIGIT:p=p*10+d;CurrentState=6;break;
            case MINUS:e=-1;CurrentState=5;break;
            case PLUS:CurrentState=5;break;
            default:HandleError();CurrentState=EndState;
           }
        break;
    case 5:switch(symbol)
           {
               case DIGIT:p=p*10+d;CurrentState=6;break;
               default:HandleError();CurrentState=EndState;

           }
        break;
    case 6:switch(symbol)
           {
            case DIGIT:p=p*10+d;break;
            default:FCON=w*pow(10,e*p-n);CurrentState=EndState;

           }
        break;

    }
return CurrentState;
}


int LEX(void)
{
    int ch;
    CurrentState=0;
        while(CurrentState!=EndState)        
        {
            ch=GetChar();
            EXCUTE(CurrentState,ch);
        }
        return Class;
}
void main()
{ printf("please input\n");
scanf("%s",num);

LEX();
// cout<<ICON<<endl;
cout<<FCON<<endl;

}

我来回复

您尚未登录,请登录后再回复。点此登录或注册