毕业论文论文范文课程设计实践报告法律论文英语论文教学论文医学论文农学论文艺术论文行政论文管理论文计算机安全
您现在的位置: 毕业论文 >> 报告 >> 正文

编译原理词法分析器

更新时间:2008-6-16:  来源:毕业论文

词法分析器
//此程序读取infile.txt文件的信息进行词法分析,相应分析结果写入到自动生成的outfile.txt文件中
#include <stdio.h>
#include <string.h>
#include <process.h>
#define MAX 50
void main()
{
FILE *in,*out;/*存放输入字符串和输出单词串的文件*/
char arr[MAX];/*arr数组存放单词符号*/
char currentchar;/*currentchar存放当前输入字符*/
int i=0;/*arr数组的一个指针*/
/*以读方式打开输入文件*/
if((in=fopen("infile.txt","r"))==NULL)
{
 printf("can not open file\n");
 exit(0);
}
/*以写方式打开输出文件*/
if((out=fopen("outfile.txt","w"))==NULL)
{
 printf("cannout open outfile\n");
    exit(1);
}
//去掉开头空格
currentchar=fgetc(in);
while(currentchar==' ')
{
 currentchar=fgetc(in);
}
int ch;


while(currentchar!='@')
{
/*常数部分判断*/
if((currentchar>='a'&&currentchar<='z')||(currentchar>='A'&&currentchar<='Z')||(currentchar>='0'&&currentchar<='9')||currentchar=='>'||currentchar=='<'||currentchar==';'||currentchar==':'||currentchar=='+'||currentchar=='-'||currentchar=='*'||currentchar=='/'||currentchar=='='||(ch=currentchar)=='\n'||currentchar==' ')
{
i=0;//清空arr字符数组
if(currentchar>='0'&&currentchar<='9')
{
 arr[i++]=currentchar;
    currentchar=fgetc(in);//再读入下一个字符
    while(currentchar>='0'&&currentchar<='9')
 {
    arr[i++]=currentchar;
    currentchar=fgetc(in);
 }
    if(currentchar!='.')//如果当前读入的符号不为小数点,则常数读完毕,输出此常数
 {
    arr[i++]='\0';
    fprintf(out,"%s%d,%c%s%c%s\n","(",11,'"',arr,'"',")");//格式化输出到目标文件,11代表种别码, arr为内码值,规定用数字本身代表
 }
    else//当前字符为小数点则继续向下读入
 {
    arr[i++]=currentchar;
    currentchar=fgetc(in);
    while(currentchar>='0'&&currentchar<='9')
 {
    arr[i++]=currentchar;
    currentchar=fgetc(in);
 }
    arr[i++]='\0';
    fprintf(out,"%s%d,%c%s%c%s\n","(",11,'"',arr,'"',")");
 }
    while(currentchar==' ')
 {
 currentchar=fgetc(in);
 }
}
/*判断标识符和关键字部分*/
if((currentchar>='a'&&currentchar<='z')||(currentchar>='A'&&currentchar<='Z'))
{
i=0;//清空arr字符数组
    while((currentchar>='a'&&currentchar<='z')||(currentchar>='0'&&currentchar<='9')||(currentchar>='A'&&currentchar<='Z'))
 {
    arr[i++]=currentchar;
    currentchar=fgetc(in);
 }
//把字符数组arr和关键字表比较,判断单词串是关键字还是标识符
    arr[i++]='\0';
    if(strcmp(arr,"begin")==0)
    fprintf(out,"%s%d,%c%s%c%s\n","(",1,'"',arr,'"',")");
    else if(strcmp(arr,"end")==0)
    fprintf(out,"%s%d,%c%s%c%s\n","(",2,'"',arr,'"',")");
 
  
    else if(strcmp(arr,"if")==0)
    fprintf(out,"%s%d,%c%s%c%s\n","(",3,'"',arr,'"',")");

 
    else if(strcmp(arr,"then")==0)
    fprintf(out,"%s%d,%c%s%c%s\n","(",4,'"',arr,'"',")");
   else if(strcmp(arr,"else")==0)
    fprintf(out,"%s%d,%c%s%c%s\n","(",5,'"',arr,'"',")");
    else if(strcmp(arr,"while")==0)
    fprintf(out,"%s%d,%c%s%c%s\n","(",6,'"',arr,'"',")");
    else if(strcmp(arr,"do")==0)
    fprintf(out,"%s%d,%c%s%c%s\n","(",7,'"',arr,'"',")");
 else
 fprintf(out,"%s%d,%c%s%c%s\n","(",10,'"',arr,'"',")");
}
while(currentchar==' ')
{
 currentchar=fgetc(in);
}
//判断标识符包括:+, -, *, /, (, ), {, }, <, >, ;, :, !=, >=, <=, ==, =.
if(currentchar=='+')
{
    fprintf(out,"%s%d,%c%c%c%s\n","(",13,'"',currentchar,'"',")");
    currentchar=fgetc(in);
}
while(currentchar==' ')
{
 currentchar=fgetc(in);
}
if(currentchar=='-')
{
 fprintf(out,"%s%d,%c%c%c%s\n","(",14,'"',currentchar,'"',")");
    currentchar=fgetc(in);
}
while(currentchar==' ')
{
 currentchar=fgetc(in);
}
if(currentchar=='*')
{
 fprintf(out,"%s%d,%c%c%c%s\n","(",15,'"',currentchar,'"',")");
    currentchar=fgetc(in);
}
while(currentchar==' ')
{
 currentchar=fgetc(in);
}
if(currentchar=='/')
{
 fprintf(out,"%s%d,%c%c%c%s\n","(",16,'"',currentchar,'"',")");
    currentchar=fgetc(in);
}


while(currentchar==' ')
{
 currentchar=fgetc(in);
}
if(currentchar=='<')
{
 i=0;
 arr[i++]=currentchar;
 currentchar=fgetc(in);
 if(currentchar=='=')
 {
  arr[i++]=currentchar;
  arr[i++]='\0';
  fprintf(out,"%s%d,%c%s%c%s\n","(",17,'"',arr,'"',")");
     currentchar=fgetc(in);
 }
 else if(currentchar=='>')
 {
  arr[i++]=currentchar;
  arr[i++]='\0';
  fprintf(out,"%s%d,%c%s%c%s\n","(",18,'"',arr,'"',")");
     currentchar=fgetc(in);
 }
 else
 { arr[i++]='\0';
  fprintf(out,"%s%d,%c%s%c%s\n","(",19,'"',arr,'"',")");
    }
}

while(currentchar==' ')
{
 currentchar=fgetc(in);
}
if(currentchar==':')
{
 i=0;
 arr[i++]=currentchar;
 currentchar=fgetc(in);
 if(currentchar=='=')
 {
  arr[i++]=currentchar;
  arr[i++]='\0';
  fprintf(out,"%s%d,%c%s%c%s\n","(",22,'"',arr,'"',")");
     currentchar=fgetc(in);
 }
 else
 { arr[i++]='\0';
  fprintf(out,"%s%d,%c%s%c%s\n","(",21,'"',arr,'"',")");
    }
}

 

while(currentchar==' ')
{
 currentchar=fgetc(in);
}
if(currentchar==';')
{
 fprintf(out,"%s%d,%c%c%c%s\n","(",23,'"',currentchar,'"',")");
    currentchar=fgetc(in);
}


while(currentchar==' ')
{
{需要全部代码请联系QQ752018766
 currentchar=fgetc(in);
}
 while(currentchar==' ')
{
 currentchar=fgetc(in);
}
}
else
{
 fprintf(out,"%s,%c%c%c\n","error",'"',currentchar,'"');
    currentchar=fgetc(in);
}

}//结束开头while语句
/*关闭文件退出*/
fclose(in);
fclose(out);
printf("相应的词法分析结果在自动生成的outfile.txt文件中\n");
}
输出结果:
infile:avgfgvf+b;
<=
@

outfile:(10,"avgfgvf")
(13,"+")
(10,"b")
(23,";")
(17,"<=")

编译原理词法分析器下载如图片无法显示或论文不完整,请联系qq752018766
设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©751com.cn 辣文论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。