#include<iostream.h>
#include<string.h>
char *KeyWord[32]={"auto","break","case","char","const","continue","default","do","double",
"else","enum","extern","float","for","goto","if","int","long","register","return","short",
"signed","sizeof","static","struct","switch","typedef","union","unsigned","void",
"volatile","while"};//c语言中的32个关键字
int i=0,j=0,k=0,t=0;
char ch;
char strToken[20];//存放构成单词符号的字符串
char *chr_form[100];//字符表
char *int_form[100];//常数表
char form[1000];
int q=0;
void GetChar(){
ch=form[k];
k++;
}
void GetBC(){
while(ch==' '){
GetChar();
}
}
void Concat(){//将ch中的字符连接到strToken之后
strToken[i]=ch;
i++;
}
bool isLetter(){
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
return true;
else
return false;
}
bool isDigit(){
if(ch>='0'&&ch<='9')
return true;
else
return false;
}
int SearchKeyWord(){
for(int q=0;q<32;q++){
if(strcmp(strToken,KeyWord[q]))
return q;//是关键字
if(q==32) return -1;//是字符串
}
}
void Reset(){
k--;
ch=NULL;
}
char *InsertChar(){//将strToken中的标识符插入符号表,返回符号表的指针
chr_form[j]=strToken;
j++;
return chr_form[0];
}
char *InsertInt(){//将strToken中的常数插入常数表,返回常数表指针
int_form[t]=strToken;
t++;
return int_form[0];
}
int code;
void analyze()
{
GetChar();
GetBC();
if(isLetter())
{//如果是字符
while(isLetter()||isDigit())
{
Concat();
GetChar();
}
Reset();
code=SearchKeyWord();
switch(code){
case -1 :cout<<"<字符串,"<<strToken<<">"<<endl;break;
default :cout<<"<关键字"<<","<<strToken<<">"<<endl;break;}
}
else
{
if(isDigit())//是数字
{
while(isDigit()||ch=='.'){
Concat();
GetChar();
}
Reset();
cout<<"<数字,"<<strToken<<">"<<endl;
}
else
{
switch(ch){
case '=':
case '*':
case '%':cout<<"<运算符,"<<ch<<">"<<endl;break;
case '-':GetChar();
if(ch=='-') cout<<"<运算符,-->"<<endl;
else {Reset();cout<<"<运算符,->"<<endl;}
break;
case '+':GetChar();
if(ch=='+') cout<<"<运算符,++>"<<endl;
else {Reset();cout<<"<运算符,+>"<<endl;}
break;
case '|':GetChar();
if(ch=='|') cout<<"<运算符,||>"<<endl;
else {Reset();cout<<"非法符号"<<endl;}
break;
case '&':GetChar();
if(ch=='&') cout<<"<运算符,&&>"<<endl;
break;
case '/':GetChar();
if(ch=='/') {
do{GetChar();}
while(ch!='\n');
cout<<"<注释,//>"<<endl;}
else {Reset();cout<<"<注释,/>"<<endl;}
break;
case ';':
case '(':
case ')':
case '{':
case '}':
case '[':
case ']':
case ':':
case ',':
case '"':cout<<"<界符,"<<ch<<">"<<endl;break;
case '<':GetChar();
if(ch=='<') cout<<"<运算符,<< >"<<endl;
else {Reset();cout<<"<界符,<>"<<endl;}
break;
case '>':GetChar();
if(ch=='+') cout<<"<运算符,>> >"<<endl;
else {Reset();cout<<"<界符,>>"<<endl;}
break;
}
}
}
while(k<q)
{
for(int p=0;p<20;p++)
strToken[p]='\0';
i=0;
analyze();
}
}
void main(){
cout<<"请输入一段C语言程序,以符号@结尾:"<<endl;
form[0]=cin.get();
for(q=1;form[q-1]!='@';q++){
form[q]=cin.get();
if(form[q]=='@'){
cout<<"你输入的程序段为:"<<endl;
cout.write(form,q);
cout<<endl;
break;
}
}
analyze();
}
没有合适的资源?快使用搜索试试~ 我知道了~
编译原理的词法分析器
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共13个文件
pdb:2个
dsp:1个
dsw:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
需积分: 0 15 下载量 166 浏览量
2008-11-05
20:00:14
上传
评论
收藏 244KB RAR 举报
温馨提示
这是一个学习软件开发的人员必须会的程序,也是编译原理中最基础的程序,它是编译原理的基础。这个程序对编译原理的初学者很重要。
资源推荐
资源详情
资源评论
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![h](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/EXE.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 13 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/a889ee3ee4484d2d8e55ad36aaa61192_fanlinsun.jpg!1)
铿锵玫瑰
- 粉丝: 12
- 资源: 9
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)