求一词法分析器程序啊,希望有注释的,顺便给下面这个程序注释一下啊,也是词法分析器的
这是C语言写的此分析器,希望大家给我注释一下啊,谢谢了,呵呵,这个程序要求用户输入时只能输入一行,谁可以把它改成可以输入多行的啊#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
int scan(char s[]);
int IsKeyword(char s[]);
int LookUp(char s[]);
int to10(char s[],int n);
#define IDN 0
#define IF 1
#define THEN 2
#define ELSE 3
#define WHILE 4
#define DO 5
#define INT10 6
#define INT8 7
#define INT16 8
#define ADD 9
#define SUB 10
#define MUL 11
#define DIV 12
#define EQ 13
#define GT 14
#define LT 15
#define LP 16
#define RP 17
#define SEMI 18
#define ASG 19
char * Keywords[]={"if","then","else","while","do"};
char MyIDN[100][512];
int symbol;
int id;
char token[1024];
char ch;
char str[1024];
int attr;
int sta;
void main(){
int i;
id=0;
sta=0;
attr=0;
printf("\nInput the string:\t");
gets(str);
for(i=0;str[i]!='\0';i++){
symbol=scan(str);
switch(symbol){
case IDN: printf("\nIDN\t%s",&MyIDN[attr]); break;
case IF: printf("\nIF\t_"); break;
case THEN: printf("\nTHEN\t_"); break;
case ELSE: printf("\nELSE\t_"); break;
case WHILE: printf("\nWHILE\t_"); break;
case DO: printf("\nDO\t_"); break;
case INT10: printf("\nINT10\t%d",attr); break;
case INT8: printf("\nINT8\t%d",attr); break;
case INT16: printf("\nINT16\t%d",attr); break;
case ADD: printf("\nADD\t_"); break;
case SUB: printf("\nSUB\t_"); break;
case MUL: printf("\nMUL\t_"); break;
case DIV: printf("\nDIV\t_"); break;
case EQ: printf("\nEQ\t_"); break;
case GT: printf("\nGT\t_"); break;
case LT: printf("\nLT\t_"); break;
case LP: printf("\nLP\t_"); break;
case RP: printf("\nRP\t_"); break;
case SEMI: printf("\nSEMI\t_"); break;
case ASG: printf("\nASG\t_"); break;
case 111: break;
default: printf("\nInput error~!\n"); break;
}
memset(token,0,32);
}
}
int scan(char s[]){
int i=0;
while(s[sta]==' ') sta++;
for(;s[sta]!='\0';sta++){
if(isdigit(s[sta])){
if((s[sta]-'0')==0){/////
if((s[sta+1])=='x'){ //
token[i++]=s[sta++];
token[i++]=s[sta++];
if(!(isdigit(s[sta]))&(s[sta]<'a'||s[sta]>'f')){
sta--;
token[0]='0';
return INT10;
}
else{
while(isdigit(s[sta])||(s[sta]>='a'&&s[sta]<='f'))
token[i++]=s[sta++];
attr=to10(token,16);
return INT16;
}
}
else{
if((s[sta+1]>='0')&(s[sta+1]<='7')){//
token[i++]=s[sta++];
while(isdigit(s[sta])) token[i++]=s[sta++];
attr=to10(token,8);
return INT8;
}
else{
token[0]='0';
sta++;
attr=to10(token,10);
return INT10;
}
}
}
else{/////
token[i++]=s[sta++];
while(isdigit(s[sta])) token[i++]=s[sta++];
attr=to10(token,10);
return INT10;
}
}
if(isalpha(s[sta])){
token[i++]=s[sta++];
while(isalpha(s[sta])||isdigit(s[sta])) token[i++]=s[sta++];
switch(IsKeyword(token)){
case 0: return IF;
case 1: return THEN;
case 2: return ELSE;
case 3: return WHILE;
case 4: return DO;
case -1:
if((attr=LookUp(token))>=0) return IDN;
default:
printf("\nInput Error~!"); break;
}
}
if(s[sta]=='+'){ sta++; return ADD;}
if(s[sta]=='-'){ sta++; return SUB;}
if(s[sta]=='*'){sta++; return MUL;}
if(s[sta]=='/'){sta++; return DIV;}
if(s[sta]=='='){sta++; return EQ; }
if(s[sta]=='>'){sta++; return GT; }
if(s[sta]=='<'){sta++; return LT; }
if(s[sta]=='('){sta++; return LP; }
if(s[sta]==')'){sta++; return RP; }
if(s[sta]==';'){sta++; return SEMI;}
if(s[sta]==':'){
if(s[sta+1]=='='){ sta++;return ASG; }
else printf("\nInput Error~!");
}
}
return 111;
}
int IsKeyword(char s[]){
int i;
for(i=0;i<5;i++)
if(!strcmp(s,Keywords[i])) return i;
return -1;
}
int LookUp(char s[]){
int i=0;
for(i=0;i<=id;i++)
if(!strcmp(s,MyIDN[i])) return i;
strcpy(MyIDN[id++],s);
return id-1;
}
int to10(char s[],int n){
int i,no=0;
switch(n){
case 10: for(i=0;s[i]!='\0';i++) no=no*10+(s[i]-'0');
return no;
case 8: for(i=0;s[i]!='\0';i++) no=no*8+(s[i]-'0');
return no;
case 16: for(i=2;s[i]!='\0';i++){
if(isdigit(s[i])) no=no*16+(s[i]-'0');
else no=no*16+(s[i]-'a'+10);
}
return no;
default: break;
}
return 0;
}