词法分析器C语言版词法分析器C语言版
#include
#include #define NULL 0
typedef struct {
char a[40];
} SignSave;
typedef struct {
char b[80];
} StringSave;
typedef struct {
int syn;
int add;
}TypeAdd;
typedef struct { /*关键字结构体*/
char keyword[15];
int num;
} Key;
typedef s...
词法分析器C语言版
#include
#include #define NULL 0
typedef struct {
char a[40];
} SignSave;
typedef struct {
char b[80];
} StringSave;
typedef struct {
int syn;
int add;
}TypeAdd;
typedef struct { /*关键字结构体*/
char keyword[15];
int num;
} Key;
typedef struct { /*运算符结构体*/
char ope[5];
int num;
} OpeSymbol;
typedef struct { /*界符符结构体*/
char loc[3];
int num;
} LocSymbol;
void preanalysis(){
FILE* fromfile;
FILE* tofile;
char fromfilename[50];
char ch1;
char a,b;
printf("请输入要处理的文件: ");
scanf("%s",fromfilename);
fromfile = fopen(fromfilename,"rt");
tofile = fopen("d:\\preanalysis.txt","wt");
if(fromfile == NULL || tofile == NULL){
printf("Error in operating files\n");
exit(0);
}
/*相关ASCII 水平制表符9 换行符10 空格符32*/
ch1 = fgetc(fromfile);
while(ch1!=EOF){
if((ch1 == 9)||(ch1 == 32)){ /*ch1为水平制表符*/
fputc(' ',tofile);
ch1 = fgetc(fromfile);
while((ch1 == 9)||(ch1 == 32)){ /*一直读到ch1不是空格符*/
ch1 = fgetc(fromfile);
}
}
else if(ch1 == 10){ /*ch1为换行符*/
ch1 = fgetc(fromfile);
}
else if(ch1 == '/'){ /*注释符的情况*/
ch1 = fgetc(fromfile);
switch (ch1){
case '/': ch1 = fgetc(fromfile);
while(ch1 != 10){
ch1 = fgetc(fromfile);
}
ch1 = fgetc(fromfile);
break;
case '*': a = fgetc(fromfile);
b = fgetc(fromfile);
while((a != '*')&&(b != '/')){
a = b;
b = fgetc(fromfile);
}
ch1 = fgetc(fromfile);
break;
default: fputc('/',tofile);
break;
}
}
else if(ch1 =='"'){
fputc(ch1,tofile);
ch1 = fgetc(fromfile);
while(ch1 != '"'){
fputc(ch1,tofile);
ch1 = fgetc(fromfile);
}
fputc(ch1,tofile);
ch1 = fgetc(fromfile);
}
else{
fputc(ch1,tofile);
ch1 = fgetc(fromfile);
}
}
fclose(fromfile);
fclose(tofile);
}
int isLetter(char a){ /*单词的第一个符号为字母*/
if((a>=65)&&(a<=90))
return 1;
else if((a>=97)&&(a<=122))
return 1;
else
return 0;
}
int isNum(char a){
if((a>=48)&&(a<=57))
return 1;
else
return 0;
}
void display(int groupcount,TypeAdd type[],int intsave[],SignSave sign[],
StringSave stringsave[],Key key[],OpeSymbol opesymbol[],LocSymbol
locsymbol[]){
int s,t;
int intcount = 0;
int signcount = 0;
int stringcount = 0;
int i,j;
char ch[15];
int p;
printf("符号类别 种别码 单词符号\n");
for(i=0;i",32},{"<",33},{"<>",34},
{"<=",35},{">",36},{">=",37},{"=",38},{"++",48},{"+=",49},{"--",50},{"-=",51},
{"*=",52},{"/=",53},{"%=",54},{"%",55},{"!=",56},{"!",57},{".",58},{"&&",59},
{"&",60},{"||",61},{"|",62},{"==",63}};
/*界符*/
LocSymbol locsymbol[8] =
{{":",31},{";",41},{"(",42},{")",43},{",",44},{"{",45},{"}",46},{"#",0}};
preanalysis(); /*预处理文件*/
fp = fopen("d:\\preanalysis.txt","rt");
if(fp== NULL){
printf("Error in operating files\n");
exit(0);
}
ch1 = fgetc(fp);
while(ch1 != EOF){
if(ch1 == ' '){
ch1 = fgetc(fp);
continue;
}
else if(isLetter(ch1)){ /*所读取首字符为字母*/
for(i=0;((isLetter(ch1))||(isNum(ch1)));i++){
temporary[i] = ch1;
ch1 = fgetc(fp); /*已取得下一字符*/
}
temporary[i] = 0;
for(j=0;j<26;j++){
if(strcmp(temporary,(key[j]).keyword) == 0){
iskey = 1;
break;
}
}
if(iskey == 1){ /*字符串是关键字*/
type[groupcount].syn = key[j].num;
iskey = 0; /*恢复iskey的值,下次循环使用*/
groupcount++;
continue;
}
if(iskey == 0){ /*字符串不是关键字,而是标识符*/
strcpy(sign[signcount].a,temporary);
type[groupcount].syn = 25; /*25为标识符的种别码*/
type[groupcount].add = sign[signcount].a;
groupcount++;signcount++;
continue;
}
}
else if(isNum(ch1) == 1){ /*所读取首字符为数字*/
for(i=0;(isNum(ch1));i++){
temporary[i] = ch1;
ch1 = fgetc(fp); /*已取得下一字符*/
}
temporary[i] = 0;
intsave[intcount] = atoi(temporary); /*
函数
excel方差函数excelsd函数已知函数 2 f x m x mx m 2 1 4 2拉格朗日函数pdf函数公式下载
atoi(参数)将字
符串转为整数*/
type[groupcount].syn = 26; /*26为整数的种别码*/
type[groupcount].add = &intsave[intcount];
groupcount++;intcount++;
continue;
}
else if(ch1 == '#'){
type[groupcount].syn = 0;printf("%c\n",ch1);
groupcount++;
ch1 = fgetc(fp);
continue;
}
else if(ch1 == '('){
type[groupcount].syn = 42;
ch1 = fgetc(fp);
groupcount++;
continue;
}
else if(ch1 == ')'){
type[groupcount].syn = 43;
ch1 = fgetc(fp);
groupcount++;
continue;
}
else if(ch1 == ';'){
type[groupcount].syn = 41;
ch1 = fgetc(fp);
groupcount++;
continue;
}
else if(ch1 == ','){
type[groupcount].syn = 44; /*','自定义种别码为44*/
ch1 = fgetc(fp);
groupcount++;
continue;
}
else if(ch1 == ':'){
type[groupcount].syn = 31; /*:种别码为31*/
ch1 = fgetc(fp);
groupcount++;
continue;
}
else if(ch1 == '{'){ /*'{'自定义种别码为45*/
type[groupcount].syn = 45;
ch1 = fgetc(fp);
groupcount++;
continue;
}
else if(ch1 == '}'){ /*'}'自定义种别码为46*/
type[groupcount].syn = 46;
ch1 = fgetc(fp);
groupcount++;
continue;
}
else if(ch1 == '"'){
ch1 = fgetc(fp);
for(i=0;ch1 != '"';i++){
temporary[i] = ch1;
ch1 = fgetc(fp);
}
temporary[i] = 0;
strcpy(stringsave[stringcount].b,temporary);
type[groupcount].syn = 47; /*自定义47为字符串的种别码*/
type[groupcount].add = stringsave[stringcount].b;
ch1 = fgetc(fp);
groupcount++;stringcount++;
continue;
}
else if(ch1 == '+'){
ch1 = fgetc(fp);
switch(ch1){
case '+': type[groupcount].syn = 48;ch1 = fgetc(fp);groupcount++;break; /*自定义48为++的种别码*/
case '=': type[groupcount].syn = 49;ch1 = fgetc(fp);groupcount++;break; /*自定义49为+=的种别码*/
default : type[groupcount].syn = 27;groupcount++;break;
/*27为+的种别码*/
}
continue;
}
else if(ch1 == '-'){
ch1 = fgetc(fp);
switch(ch1){
case '>': type[groupcount].syn = 32;ch1 = fgetc(fp);groupcount++;break; /*32为->的种别码*/
case '-': type[groupcount].syn = 50;ch1 =
fgetc(fp);groupcount++;break; /*自定义50为--的种别码*/
case '=': type[groupcount].syn = 51;ch1 = fgetc(fp);groupcount++;break; /*自定义51为-=的种别码*/
default : type[groupcount].syn = 28;groupcount++;break;
/*28为-的种别码*/
}
continue;
}
else if(ch1 == '*'){
ch1 = fgetc(fp);
switch(ch1){
case '=': type[groupcount].syn = 52;ch1 = fgetc(fp);groupcount++;break; /*自定义52为*=的种别码*/
default : type[groupcount].syn = 29;groupcount++;break;
/*29为*的种别码*/
}
continue;
}
else if(ch1 == '/'){
ch1 = fgetc(fp);
switch(ch1){
case '=': type[groupcount].syn = 53;ch1 = fgetc(fp);groupcount++;break; /*自定义53为/=的种别码*/
default : type[groupcount].syn = 30;groupcount++;break;
/*30为/的种别码*/
}
continue;
}
else if(ch1 == '%'){
ch1 = fgetc(fp);
switch(ch1){
case '=': type[groupcount].syn = 54;ch1 = fgetc(fp);groupcount++;break; /*自定义54为%=的种别码*/
default : type[groupcount].syn = 55;groupcount++;break;
/*自定义55为%的种别码*/
}
continue;
}
else if(ch1 == '>'){
ch1 = fgetc(fp);
switch(ch1){
case '=': type[groupcount].syn = 37;ch1 = fgetc(fp);groupcount++;break; /*37为>=的种别码*/
default : type[groupcount].syn = 36;groupcount++;break;
/*36为>的种别码*/
}
continue;
}
else if(ch1 == '<'){
ch1 = fgetc(fp);
switch(ch1){
case '=': type[groupcount].syn = 35;ch1 = fgetc(fp);groupcount++;break; /*35为<=的种别码*/
case '>': type[groupcount].syn = 34;ch1 = fgetc(fp);groupcount++;break; /*34为<>的种别码*/
default : type[groupcount].syn = 33;groupcount++;break;
/*33为<的种别码*/
}
continue;
}
else if(ch1 == '!'){
ch1 = fgetc(fp);
switch(ch1){
case '=': type[groupcount].syn = 56;ch1 = fgetc(fp);groupcount++;break; /*自定义56为!=的种别码*/
default : type[groupcount].syn = 57;groupcount++;break;
/*自定义57为!的种别码*/
}
continue;
}
else if(ch1 == '.'){
type[groupcount].syn = 58;
/*自定义58为.的种别码*/
ch1 = fgetc(fp);
groupcount++;
continue;
}
else if(ch1 == '&'){
ch1 = fgetc(fp);
switch(ch1){
case '&': type[groupcount].syn = 59;ch1 = fgetc(fp);groupcount++;break; /*自定义59为&&的种别码*/
default : type[groupcount].syn = 60;groupcount++;break;
/*自定义60为&的种别码*/
}
continue;
}
else if(ch1 == '|'){
ch1 = fgetc(fp);
switch(ch1){
case '|': type[groupcount].syn = 61;ch1 = fgetc(fp);groupcount++;break; /*自定义61为||的种别码*/
default : type[groupcount].syn = 62;groupcount++;break;
/*自定义62为|的种别码*/
}
continue;
}
else if(ch1 == '='){
ch1 = fgetc(fp);
switch(ch1){
case '=': type[groupcount].syn = 63;ch1 = fgetc(fp);groupcount++;break; /*自定义63为==的种别码*/
default : type[groupcount].syn = 38;groupcount++;break;
/*38为=的种别码*/
}
continue;
}
else {
ch1 = fgetc(fp);
continue;
}
}
display(groupcount,type,intsave,sign,stringsave,key,opesymbol,locsymbol);
return 0;
}
本文档为【词法分析器C语言版】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。