词法分析程序

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

#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
char Getch(char* &p){
char ch;
ch=*(p++);
return ch;
}
void main(){
char expression[100],*pExpression,text[20];
char ch=' ';
int i=0;
double num=0;
scanf("%c",&expression[0]);
while(expression[i]!='#'){
i=i+1;
scanf("%c",&expression[i]);
}//输入源程序串
pExpression=expression;
ch=Getch(pExpression);
while(ch!='#'){//大循环开始
if(ch==' '||ch=='\t'||ch=='\n') ch=Getch(pExpression);
else{
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){//开头是字母
for(i=0;i<20;i++) text[i]='\0';
i=0;
while((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch>='0'&&ch<='9'))
{
text[i]=ch;
i++;
ch=Getch(pExpression);
}
text[i]='\0';
if(!strcmp(text,"begin")||!strcmp(text,"BEGIN"))cout << "(beginsym,“"<< text<<"”)" <<endl;
else if(!strcmp(te xt,"if")||!strcmp(text,"IF"))cout << "(ifsym,“"<< text <<"”)"<<endl;
else if(!strcmp(text,"then")||!strcmp(text,"THEN"))cout << "(thensym,“"<< text <<"”)" <<endl;
else if(!strcmp(text,"end")||!strcmp(text,"END"))cout << "(endsym,“"<< text << "”)"<<endl;
else if(!strcmp(text,"while")||!strcmp(text,"WHILE"))cout << "(whilesym,“"<< text << "”)"<<endl;
else if(!strcmp(text,"do")||!strcmp(text,"DO"))cout << "(dosym,“"<< text << "”)"<<endl;
else cout << "(Ident,“"<< text <<"”)"<<endl;
}
else{ //是数字
for(i=0;i<20;i++)
text[i]='\0';
i=0;
if(isdigit(ch)){
while(isdigit(ch)){
text[i]=ch;
i++;
ch=Getch(pExpression);
}
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
{
cout << "error"<<endl;
while(ch!=' '&&ch!='\t'&&ch!='\n')ch=Getch(pExpression);
}
else {num = atoi(text);
cout << "(Number,“"<< num <<"”)"<<endl;}
}
else{//是运算符(除了运算符:=)
if(ch=='+'){cout << "(plus,“"<< "+”)"<<endl;ch=Getch(pExpression);}
else if(ch=='-'){cout << "(minus,“"<< "-”)"<<endl;ch=Getch(pExpression);}
else if(ch=='*'){cout << "(times,“"<< "*”)"<<endl;ch=Getch(pExpression);}
else if(ch=='/'){cout << "(slash,“"<< "/”)"<<endl;ch=Getch(pExpression);}
else if(ch=='='){cout << "(eql,“"<< "=”)"<<endl;ch=Get ch(pExpression);}
else if(ch==':'){cout << "(becomes,“"<< ":=”)"<<endl;ch=Getch(pExpression);ch=Getch(pExpression);}
else if(ch=='<'){
ch=Getch(pExpression);
if(ch=='>')
{cout << "(neq,“"<< "<>”)"<<endl;ch=Getch(pExpression);}
else if(ch=='=')
{cout << "(leq,“"<< "<=”)"<<endl;ch=Getch(pExpression);}
else cout << "(lss,“"<< "<”)"<<endl;
}
else if(ch=='>'){
ch=Getch(pExpression);
if(ch=='=')
{cout << "(geq,“"<< ">=”)"<<endl;ch=Ge tch(pExpression);}
else cout << "(gtr,“"<< ">”)"<<endl;
}
else{//是界符
if(ch==';'){cout << "(semicolon,“"<< ";”)"<<endl;ch=Getch(pExpression);}
else if(ch=='('){cout << "(lparen,“"<< "(”)"<<endl;ch=Getch(pExpression);}
else if(ch==')'){cout << "(rparen,“"<< ")”)"<<endl;ch=Getch(pExpression);}
else {printf("Error\n"); ch=Getch(pExpression);}
}//是界符结束
}//是运算符结束
}
}
}//while循环结束
}//mian函数结束。

相关文档
最新文档