无符号数词法分析程序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include

#include

#include

#define DIGIT 1

#define POINT 2

#define OTHER 3

#define POWER 4

#define PLUS 5

#define MINUS 6

#define UCON 7 //Suppose the class number of unsigned constant is 7

#define ClassOther 200

#define EndState -1

int w,n,p,e,d;

int Class; //Used to indicate class of the word

int ICON;

float FCON;

static int CurrentState; //Used to present current state, the initial value:0

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=getchar( );

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=UCON;break;

case POINT: w=0;n=0;p=0;e=1;CurrentState=3;Class=UCON;break;

default: HandleOtherWord( );Class=ClassOther;

CurrentState=EndState;

}

break;

case 1:switch (symbol)

{

case DIGIT: w=w*10+d;break; //CurrentState=1

case POINT: CurrentState=2;break;

case POWER: CurrentState=4;break;

default: ICON=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 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;

}

int main()

{

int c =LEX();

printf("%d\n",c);

printf("%lf\n",FCON);

}

相关文档
最新文档