编译原理词法分析程序设计

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OK 1
#define ERROR 0
#define Status int
typedef struct{
char key[20];
char num[10];
}ElemType;
typedef struct{
char num[10];
char sign[10];
char mnemonics[10];
char isn[10];
}ElemType2;
typedef struct{
ElemType elem;
int length;
int listsize;
}SqList;
SqList L;
SqList L2;
int n=1;//行数
FILE in,out;// 指向文件的指针
char token[LISTINCREMENT];
char order_number[LIST_INIT_SIZE][LISTINCREMENT]=
{
“0”,“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,
“11”,“12”,“13”,“14”,“15”,“16”,“17”,“18”,“19”,“20”,
“21”,“22”,“23”,“24”,“25”,“26”,“27”,“28”,“29”,“30”,
“31”,“32”,“33”,“34”,“35”,“36”,“37”,“38”,“39”,“40”,
“41”,“42”,“43”,“44”,“45”,“46”,“47”,“48”,“49”,“50”,
};
ElemType key_world[14]=
{
{“int”,“0”},{“char”,“1”},{“float”,“2”},
{“void”,“3”},{“const”,“4”},{“for”,“5”},
{“if”,“6”},{“else”,“7”},{“then”,“8”},
{“while”,“8”},{“switch”,“9”},{“break”,“10”},
{“begin”,“11”},{“end”,“12”}
};
ElemType2 symbol[25]=
{
{“0”,"+","+",""},{“1”,"-","-",""},{“2”,"
","
",""},
{“3”,"/","/","
"},{“4”,"%","%",""},{“5”,"(","(",""},
{“6”,")",")",""},{“7”,"[","[",""},{“8”,"]","]",""},
{“9”,"<",“rlop”,"<"},{“9”,">",“rlop”,">"},{“9”,"<=",“rlop”,"<="},
{“9”,">=",“rlop”,">="},{“9”,"",“rlop”,""},{“9”,"!=",“rlop”,"!="},
{“10”,";",";","
"},{“11”,"/=","/=",""},
{“12”,"+=","+=","
"},{“13”,"-=","-=","_"},{“14”,"
=","*=",""},
{“15”,"%=","%=","
"},{“16”,"||",“or”,""},{“17”,"&&",“and”,""},
{“18”,"!",“not”,""},{“19”,"=","=",""}
};
Status InitList_Sq(SqList *L) { // 算法2.3
// 构造一个空的线性表L。
L->elem = (ElemType )malloc(LIST_INIT_SIZEsizeof(ElemType));
memset(L->elem, 0, sizeof(ElemType *));
if (!L->elem) return OK; // 存储分配失败
L->length = 0; // 空表长度为0
L->listsize = LIST_INIT_SIZE; // 初始存储容量
return OK;
} // InitList_Sq

Status ListInsert_Sq(SqList &L, int i,char a[10],char n[10])
{ // 算法2.4
// 在顺序线性表L的第i个元素之前插入新的元素e,
// i的合法值为1≤i≤ListLength_Sq(L)+1
char p[10];
int b;
if (i < 1 || i > L.length+1)
return ERROR; // i值不合法
if (L.length >= L.listsize)
{ // 当前存储空间已满,增加容量
ElemType *newbase = (ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof (ElemType));
if (!newbase)
return ERROR; // 存储分配失败
L.elem = newbase; // 新基址
L.listsize += LISTINCREMENT; // 增加存储容量
}
for (b=L.length-1; b>=(i-1); --b)
{
strcpy(L.elem[b+1].key,L.elem[b].key);
strcpy(L.elem[b+1].num,L.elem[b].num);
}
// 插入位置及之后的元素右移
strcpy(L.elem[i-1].key,a);
strcpy(L.elem[i-1].num,n);
++L.length; // 表长增1
return OK;
} // ListInsert_Sq
void remove() //扫描指针回退一个字符
{
fseek(in,-1,SEEK_CUR);
}
void Output(char a[10],char m[10])
{
fputs("(",out);
fputs(a,out);
fputs(",",out);
//if(m==-1)
//fputs(Key[n].keyValue,out);
//else
fputs(m,out);//下标
fputs(")",out);
}

int Initscanner()
{
InitList_Sq(&L);
InitList_Sq(&L2);
if((in=fopen(“s.txt”,“r”))==NULL)
{
printf(“cannot open infile!\n”);
return 0;
}
if((out=fopen(“word.txt”,“w+”))==NULL)
{
printf(“cannot open outfile!\n”);
return 0;
}
return 0;
}
int Isalpha()
{
int i;
for(i=0;i<14;i++)//关键字
{
if(strcmp(token,key_world[i].key)==0)
{
Output(key_world[i].key,"_");
return 0;
}
}
//标识符
for(i=0;i<=L.length;i++)
{
if((L.elem[i].key)&&strcmp(token,L.elem[i].key)==0)
{
Output(“id”,L.elem[i].num);
return 0;
}
}
//可识别100
ListInsert_Sq(L,i,token,order_number[i-1]);
Output(“id”,order_number[i-1]);

return 0;

}
int Isnumber()//Isnumber函数:consts[NUM][MAX]
{
int i;
for(i=0;i<=L2.length;i++)
{
if(strcmp(token,L2.elem[i].key)0)
{
Output(“num”,L2.elem[i].num);
return 0;
}
}
//可识别100
ListInsert_Sq(L2,i,token,order_number[i-1]);
Output(“num”,order_number[i-1]);
return 0;
}
int Isanotation()
{
char ch,pre;
ch=getc(in);
if(ch
’=’)// “/=”
Output(symbol[16].mnemonics,symbol[16].isn);
else if(ch==’’)// "/"
{
ch=getc(in);
do{
if(feof(in))
{
//num++;
printf("\n第(%d)行 : 注释错误: /后面的字符全部当做注释,缺少结束注释/\n",n);
break;
}
pre=ch;
ch=getc(in);
}while(pre!=’’||ch!=’/’);
}
else if(ch!=’/’)// “/”
{
if(!feof(in))
{
remove(); //指针回退一个字符
Output(symbol[3].mnemonics,symbol[3].isn);
}
else
{
Output(symbol[3].mnemonics,symbol[3].isn);
}
}
else if(ch==’/’)
{
//remove();
printf("\n第(%d)行 : 非法字符://错误\n",n);
}
}
//Isother函数识别其他特殊字符
int Isother()
{
int i;
int j;
char ch;
for(i=0;i<25;i++)
{
if(strcmp(token,symbol[i].sign)0)
{
if(token[0]
’<’||token[0]’>’||token[0]’!’||
token[0]’+’||token[0]’-’||token[0]==’
’||token[0]’%’||token[0]’=’)
{
ch=getc(in);
if(ch==’=’)
{
token[1]=ch;
for(j=0;j<25;j++)
{
if(strcmp(token,symbol[j].sign)0)
{
Output(symbol[j].mnemonics,symbol[j].isn);
return OK;
}
}
}
else if(!feof(in))
{
remove();
Output(symbol[i].mnemonics,symbol[i].isn);
return OK;
}
else
{
Output(symbol[i].mnemonics,symbol[i].isn);
return OK;
}
}
else
{
Output(symbol[i].mnemonics,symbol[i].isn);
return OK;
}
}
else if(token[0]
’|’||token[0]’&’)
{
ch=getc(in);
token[1]=ch;
for(j=0;j<25;j++)
{
if(strcmp(token,symbol[j].sign)0)
{
Output(symbol[j].mnemonics,symbol[j].isn);
return OK;
}
}
remove();
printf("\n第(%d)行 : 非法字符:%c错误\n",n,token[0]);
return ERROR;
}
}
printf("\n第(%d)行 : 非法字符:%s错误\n",n,token);
return ERROR;
}
void Scanner()
{
char ch;
int i;
ch=getc(in);
while(!feof(in))
{
while(ch
’ '||ch
’\t’||ch==’\n’) //先统计行数
{
if(ch==’\n’)
++n;
ch=getc(in);
}
if(((ch>=‘a’)&&(ch<=‘z’))||(ch==’’)||((ch>=‘A’)&&(ch<=‘Z’)))//标识符 isalpha(ch)单词
{
i=1;
memset(token,0,sizeof(token));
token[0]=ch;
ch=getc(in);
while(((ch>=‘a’)&&(ch<=‘z’))||((ch>=‘0’)&&(ch<=‘9’))||((ch>=‘A’)&&(ch<=‘Z’))||(ch==’
’))//word number
{
token[i++]=ch;
ch=getc(in);
}
token[i]=’\0’;
Isalpha();
}
else if((ch>=‘0’)&&(ch<=‘9’))//整数
{
i=1;
memset(token,0,sizeof(token));
token[0]=ch;
ch=getc(in);
while((ch>=‘0’)&&(ch<=‘9’))//word number
{
token[i++]=ch;
ch=getc(in);
}
token[i]=’\0’;
Isnumber();
}
else if(ch==’/’)//区分//,/*,/=
{
memset(token,0,sizeof(token));
token[0]=ch;
Isanotation();
ch=getc(in);
}
else if(!feof(in)) //特殊字符
{
memset(token,0,sizeof(token));
token[0]=ch;
Isother();
ch=getc(in);
}
}
}
int show()
{
int i;
char ch;
printf("\n二元式:\n");
rewind(out);
while(!feof(out))
{
printf("%c",getc(out));
}
if(L.length!=0)
printf("\n--------------标识符表--------------\n");
for(i=0;i<L.length;i++)
{
printf("%s,%s\n",L.elem[i].num,L.elem[i].key);
}
if(L2.length!=0)
printf("\n--------------常数表---------------\n");
for(i=0;i<L2.length;i++)
{
printf("%s,%s\n",L2.elem[i].num,L2.elem[i].key);
}
return OK;
}

int main()
{
Initscanner();
Scanner();
show();
fclose(in);
fclose(out);
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值