回 帖 发 新 帖 刷新版面

主题:词法分析器程序源代码

词法分析器程序源代码


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("追查ZhuiCha.com网络技术 版权所有\n");
 printf("cannout open outfile\n");
    exit(1);
}
//去掉开头空格
currentchar=fgetc(in);
while(currentchar==" ")

 currentchar=fgetc(in);
}
int ch;


while(currentchar!="@")
{
/*常数部分判断*/
if((currentchar>="a"&¤tchar<="z")||(currentchar>="A"&¤tchar<="Z")||(currentchar>="0"&¤tchar<="9")||currentchar=="("||currentchar==")"||currentchar=="{"||currentchar=="}"||currentchar==">"||currentchar=="<"||currentchar==";"||currentchar==":"||currentchar=="+"||currentchar=="-"||currentchar=="*"||currentchar=="/"||currentchar=="="||currentchar=="!"||currentchar=="&"||(ch=currentchar)=="\n"||currentchar==" "||currentchar=="["||currentchar=="]")
{
i=0;//清空arr字符数组
if(currentchar>="0"&¤tchar<="9")
{
 arr[i++]=currentchar;
    currentchar=fgetc(in);//再读入下一个字符
    while(currentchar>="0"&¤tchar<="9")
 {
    arr[i++]=currentchar;
    currentchar=fgetc(in);
 }
    if(currentchar!=".")//如果当前读入的符号不为小数点,则常数读完毕,输出此常数
 {printf("追查ZhuiCha.com网络技术 版权所有\n");

    arr[i++]="\0";
    fprintf(out,"%s%d,%c%s%c%s\n","(",15,""",arr,""",")");//格式化输出到目标文件,15代表种别码, arr为内码值,规定用数字本身代表
 }
    else//当前字符为小数点则继续向下读入
 {
    arr[i++]=currentchar;
    currentchar=fgetc(in);
    while(currentchar>="0"&¤tchar<="9")
 {
    arr[i++]=currentchar;
    currentchar=fgetc(in);
 }
    arr[i++]="\0";
    fprintf(out,"%s%d,%c%s%c%s\n","(",15,""",arr,""",")");
 }
    while(currentchar==" ")
 { 
 currentchar=fgetc(in);
 }
}
/*判断标识符和关键字部分*/
if((currentchar>="a"&¤tchar<="z")||(currentchar>="A"&¤tchar<="Z"))
{
i=0;//清空arr字符数组
    while((currentchar>="a"&¤tchar<="z")||(currentchar>="0"&¤tchar<="9")||(currentchar>="A"&¤tchar<="Z"))
 {
    arr[i++]=currentchar;
    currentchar=fgetc(in);
 }
//把字符数组arr和关键字表比较,判断单词串是关键字还是标识符
    arr[i++]="\0";
    if(strcmp(arr,"if")==0)
    fprintf(out,"%s%d,%c%s%c%s\n","(",0,""",arr,""",")");
    else if(strcmp(arr,"else")==0)
    fprintf(out,"%s%d,%c%s%c%s\n","(",1,""",arr,""",")");
    else if(strcmp(arr,"while")==0)
    fprintf(out,"%s%d,%c%s%c%s\n","(",2,""",arr,""",")");
    else if(strcmp(arr,"do")==0)
    fprintf(out,"%s%d,%c%s%c%s\n","(",3,""",arr,""",")");
 else
 fprintf(out,"%s%d,%c%s%c%s\n","(",14,""",arr,""",")");
}
while(currentchar==" ")

 currentchar=fgetc(in);
}
//判断标识符包括:+, -, *, /, (, ), {, }, <, >, ;, :, !=, >=, <=, ==, =.
if(currentchar=="+")
{
    fprintf(out,"%s%d,%c%c%c%s\n","(",9,""",currentchar,""",")");
    currentchar=fgetc(in);
}
while(currentchar==" ")

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

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

 currentchar=fgetc(in);
}
if(currentchar=="/")
{printf("追查ZhuiCha.com网络技术 版权所有\n");

回复列表 (共4个回复)

沙发

fprintf(out,"%s%d,%c%c%c%s\n","(",12,""",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","(",13,""",arr,""",")");
     currentchar=fgetc(in);
 }
 else
 { arr[i++]="\0";
  fprintf(out,"%s%d,%c%s%c%s\n","(",17,""",arr,""",")");
    }
}
while(currentchar==" ")

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

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

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

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

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

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

 currentchar=fgetc(in);
}
if(currentchar==")")
{
 fprintf(out,"%s%d,%c%c%c%s\n","(",8,""",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";
printf("追查ZhuiCha.com网络技术 版权所有\n");
  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","(",16,""",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","(",13,""",arr,""",")");
     currentchar=fgetc(in);
 }
 else
 { arr[i++]="\0";
  fprintf(out,"%s%d,%c%s%c%s\n","(",13,""",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","(",13,""",arr,""",")");
     currentchar=fgetc(in);
 }
 else
 { arr[i++]="\0";
  fprintf(out,"%s%d,%c%s%c%s\n","(",13,""",arr,""",")");
    }
}
while(currentchar==" ")

 currentchar=fgetc(in);
}
if(currentchar=="!")
{
 i=0;
 arr[i++]=currentchar;
 currentchar=fgetc(in);
 if(currentchar=="=")
 {
  arr[i++]=currentchar;
  arr[i++]="\0";
printf("追查ZhuiCha.com网络技术 版权所有\n");
  fprintf(out,"%s%d,%c%s%c%s\n","(",13,""",arr,""",")");
     currentchar=fgetc(in);
 }
}
while(currentchar==" ")

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

 currentchar=fgetc(in);
}
while((ch=currentchar)=="\n")
{
 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);
}

注:在程序当前目录里自己建立一个文本文档,取名为infile.txt,所有需要分析的程序都写在此文本文档里,程序的结尾必须以“@”标志符结束。程序结果输出在同目录,文件名为outfile.txt,此文件为自动生成。种别码就不给出了.


 

板凳

顶,好样的.

3 楼

请楼主帮忙啊,为什么我不能运行呢?&¤tchar是什么意思啊?

4 楼


  楼主的程序有很多错误的,例如char c = ' ',而不是c = " ",字符不可以赋值字符串,还有好多乱码!但是总体构思还是很不错!呵呵~

我来回复

您尚未登录,请登录后再回复。点此登录或注册