求一份C语言编写的极其简化的C词法分析器,高手应该不超过20分钟就能写出来
几个月都没编程了,过几天就要交了,很是头疼要求很简单:1.识别C常见关键字(10个就够了,随便定义10个常见的关键字),常量(只识别0-100的整数)和运算符(+-/*)...
几个月都没编程了,过几天就要交了,很是头疼
要求很简单:1.识别C常见关键字(10个就够了,随便定义10个常见的关键字),常量(只识别0-100的整数)和运算符(+ - / *)
2. 运行时手动输入一段C语言源程序,结果将识别出来的关键字,常量(整数0-100),和运算符分别显示出来,不需要编号和统计数目。运行格式如下显示:
关键字:if then and array bool等
常量:5 36 15 48等
运算符:+ - * 等 展开
要求很简单:1.识别C常见关键字(10个就够了,随便定义10个常见的关键字),常量(只识别0-100的整数)和运算符(+ - / *)
2. 运行时手动输入一段C语言源程序,结果将识别出来的关键字,常量(整数0-100),和运算符分别显示出来,不需要编号和统计数目。运行格式如下显示:
关键字:if then and array bool等
常量:5 36 15 48等
运算符:+ - * 等 展开
3个回答
展开全部
#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);
}
#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);
}
展开全部
呵呵 我们刚刚做了这个交了 差别还是有点大的。不知道你要吗,要的话追问我。
追问
可不可以把源代码发出来?
追答
#include
#include
#include
#define MAX 256
char baoliuzi[10][10] =
{
"main","if","else","switch","int","do","while","char","write","read"//保留字
};
/*打开对应的代码文件,2元素文件,标识符文件,常熟文件*/
FILE *fp,*wfp;
int id_count = 0;
int num_count = 0;
char word[MAX],ch;
char a[20],b[20];//原文件路径和保存路径
int n_word = 0;
/*对应保留字的编码*/
int c;
/*finished read the file*/
int over = 0;
/*扫描指针回退一个字符*/
void retract()
{
fseek(fp,-1,SEEK_CUR);
}
int init()
{
fp = fopen(a,"a");
if (fp==NULL)
{
printf("打开失败。\n");
return 0;
}
fprintf(fp,"%c",'\n');//将文件末尾添加结束符
fclose(fp);
}
/*将当前读入的字符送入word数组中*/
void concatenation()
{
word[n_word] = ch;
n_word++;
word[n_word] = '\0';
}
/*判断是否是字母*/
int letter()
{
if ((ch >='a'&&ch='A'&&ch='a'&&ch='A'&&ch='0'&&ch ='0'&&ch <='9')
return 1;
else
return 0;
}
/*判断是否是保留字。是则返回保留字对应的编码不是则返回标识符编码6*/
int reserve()
{
if (strcmp(word,"main") == 0)
return 1;
if (strcmp(word,"if") == 0)
return 2;
if (strcmp(word,"else") == 0)
return 3;
if (strcmp(word,"switch") == 0)
return 4;
if (strcmp(word,"int") == 0)
return 5;
if (strcmp(word,"do") == 0)
return 6;
if (strcmp(word,"while") == 0)
return 7;
if (strcmp(word,"char") == 0)
return 8;
if (strcmp(word,"write") == 0)
return 9;
if (strcmp(word,"read") == 0)
return 10;
return 0;
}
void error()
{
fprintf(wfp,"%s\t%c\n","Error",ch);
}
主函数在下面
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我不是高手
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询