求一个C语言词法分析器源代码

 我来答
回忆动画
推荐于2018-05-06 · TA获得超过275个赞
知道小有建树答主
回答量:40
采纳率:0%
帮助的人:50.4万
展开全部
我有,这是这学期刚做的,
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

bool isLetter(char ch){
if ((ch>='A' && ch<='Z') || (ch>='a' && ch<='z')) return true;
else return false;
}

bool isDigit(char ch){
if (ch>='0' && ch<='9') return true;
else return false;
}

bool isP(char ch){
if(ch=='+'||ch=='*'||ch=='-'||ch=='/') return true;
//ch==':'||ch==','||ch=='='||ch==';'||ch=='('||ch==')'
else return false;
}
bool isJ(char ch){
if(ch==','||ch==';'||ch=='.'||ch=='('||ch==')'||ch=='['||ch==']'||ch=='='||ch==':'||ch=='<'||ch=='>'||ch=='{'||ch=='}'||ch=='#') return true;
//
else return false;
}
bool isBlank(char ch){
if(ch==' '||ch=='\t') return true;
else return false;
}

int main(){
string src,ste,s;
char ch0,ch,ch1[2];
char ktt[48][20]={"and","begin","const","div","do","else","end","function","if","integer",
"not","or","procedure","program","read","real","then","type","var","while","write","标识符","无符号数",
",",";",":",".","(",")","[","]","..","++","--","+","-","*","/","=","<",">","<>","<="
,">=",":=","{","}","#"};
int pos=0;
FILE *fp;
fp=fopen("d:\\in.txt","r");
ch0=fgetc(fp);
while(ch0!=EOF)
{
//if(ch0!='\t'){src+=ch0;}
src+=ch0;
ch0=fgetc(fp);
}
src+='#';
cout<<src<<endl;
ch=src[pos++];
ste=" ";
for(int j=0;j<47;j++){cout<<j<<ktt[j]<<endl;}
cout<<"词法分析:\n";
while(ch!='#')
{
char str[20];
if(ch!='\n')
{
if(isDigit(ch))
{ //判断常数
int i=0;
while(isDigit(ch)||ch=='.')
{
str[i++]=ch;
//i++;
ch=src[pos++];
}
str[i]='\0';
ste=ste+"|"+"22";
cout<<str;
continue;
}
else if(isLetter(ch))
{ //判断字符
int i=0,j;
while(isLetter(ch)||isDigit(ch))
{
str[i++]=ch;
//i++;
ch=src[pos++];
}
str[i]='\0';
for(j=0;j<21;j++){ //判断是否关键字
int t=strcmp(str,ktt[j]);
if(t==0) {
stringstream ss;
ste+="|";
ss<<ste;ss<<j;
ss>>ste;
break;
}
}
if(j==21){ste=ste+"|"+"21";}
// cout<<" ";
cout<<str;
continue;
}
else if(isP(ch)){ ///判断是否运算符
int i=0,j;
str[i++]=ch;
str[i]='\0';
for(j=34;j<38;j++){
int t=strcmp(str,ktt[j]);
if(t==0) {
stringstream ss;
ste+="|";
ss<<ste;ss<<j;
ss>>ste;
break;
}
}
cout<<str;
ch=src[pos++];
continue;
}
else if(isJ(ch)) //判断是否界符
{
int i=0,j;
while(isJ(ch))
{
str[i++]=ch;
ch=src[pos++];
}
str[i]='\0';
for(j=23;j<47;j++){
int t=strcmp(str,ktt[j]);
if(t==0) {
stringstream ss;
ste+="|";
ss<<ste;ss<<j;
ss>>ste;
break;
}
}
cout<<str;
continue;
}
else if(isBlank(ch))
{
cout<<ch;
ch=src[pos++];
continue;
}
}
else{
cout<<ste<<endl;
ste=" ";
}
ch=src[pos++];
}
return 0;
}

还有运行效果图,和实验报告 ,你要的话留下邮箱
zhujinpion
2010-07-17 · 超过12用户采纳过TA的回答
知道答主
回答量:26
采纳率:0%
帮助的人:24万
展开全部
#include <iostream>

#include<string>

using namespace std;

#define MAX 22

char ch =' ';

string key[15]={"begin","end","if","then","else","while","write","read",

"do", "call","const","char","until","procedure","repeat"};

int Iskey(string c){ //关键字判断

int i;

for(i=0;i<MAX;i++) {

if(key[i].compare(c)==0) return 1;

}

return 0;

}

int IsLetter(char c) { //判断是否为字母

if(((c<='z')&&(c>='a'))||((c<='Z')&&(c>='A'))) return 1;

else return 0;

}

int IsDigit(char c){ //判断是否为数字

if(c>='0'&&c<='9') return 1;

else return 0;

}

void analyse(FILE *fpin){

string arr="";

while((ch=fgetc(fpin))!=EOF) {

arr="";

if(ch==' '||ch=='\t'||ch=='\n'){}

else if(IsLetter(ch)){

while(IsLetter(ch)||IsDigit(ch)) {

if((ch<='Z')&&(ch>='A')) ch=ch+32;

arr=arr+ch;

ch=fgetc(fpin);

}

fseek(fpin,-1L,SEEK_CUR);

if (Iskey(arr)){cout<<arr<<"\t$关键字"<<endl;}

else cout<<arr<<"\t$普通标识符"<<endl;

}

else if(IsDigit(ch)){

while(IsDigit(ch)||ch=='.'&&IsDigit(fgetc(fpin))){

arr=arr+ch;

ch=fgetc(fpin);

}

fseek(fpin,-1L,SEEK_CUR);

cout<<arr<<"\t$无符号实数"<<endl;

}

else switch(ch){

case'+':

case'-' :

case'*' :

case'=' :

case'/' :cout<<ch<<"\t$运算符"<<endl;break;

case'(' :

case')' :

case'[' :

case']' :

case';' :

case'.' :

case',' :

case'{' :

case'}' :cout<<ch<<"\t$界符"<<endl;break;

case':' :{ch=fgetc(fpin);

if(ch=='=') cout<<":="<<"\t$运算符"<<endl;

else {cout<<"="<<"\t$运算符"<<endl;;

fseek(fpin,-1L,SEEK_CUR);}

}break;

case'>' :{ch=fgetc(fpin);

if(ch=='=') cout<<">="<<"\t$运算符"<<endl;

if(ch=='>')cout<<">>"<<"\t$输入控制符"<<endl;

else {cout<<">"<<"\t$运算符"<<endl;

fseek(fpin,-1L,SEEK_CUR);}

}break;

case'<' :{ch=fgetc(fpin);

if(ch=='=')cout<<"<="<<"\t$运算符"<<endl;

else if(ch=='<')cout<<"<<"<<"\t$输出控制符"<<endl;

else if(ch=='>') cout<<"<>"<<"\t$运算符"<<endl;

else{cout<<"<"<<"\t$运算符"<<endl;

fseek(fpin,-1L,SEEK_CUR);}

}break;

default : cout<<ch<<"\t$无法识别字符"<<endl;

}

}

}

void main(){

char in_fn[30];

FILE * fpin;

cout<<"请输入源文件名(包括路径和后缀名):";

for(;;){

cin>>in_fn;

if((fpin=fopen(in_fn,"r"))!=NULL) break;

else cout<<"文件路径错误!请输入源文件名(包括路径和后缀名):";

}

cout<<"\n********************分析如下*********************"<<endl;

analyse(fpin);

fclose(fpin);

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
zhang_tao1988
2010-07-06 · 超过15用户采纳过TA的回答
知道答主
回答量:43
采纳率:0%
帮助的人:31.8万
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式