⽤C语⾔写的Pascal语⾔词法分析器
虽然只是个词法分析器,但是重拾C语⾔好难!好久没⽤过C了。放在这⼉勉励⾃⼰做事⼀定要有始有终
/*Trans.c -main,Trans */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
//char *name[]={"begin","1","end","2","integer","3","if","4","then","5","else","6","function","7","read","8","write","9"};
//char *sign[]={"=","<>","<=","<",">=",">","-","*",":=","(",")",";"};
int match_code=0;
char buffer1[256];
char buffer2[256];
char *infile;
char* outfile;
char* errfile;
char token[15];
int token_index=0;
char ch;
int buffer1Empty=1;
int buffer2Empty=1;
int EndofFile=0;
//int reserve_(int* code)  //本来⾃⼰写了⼀个⽐较保留字段的函数,但是不知道什么原因⼯作不是很良好,鉴于时间很紧就没有⽤ //{
//    int match=-1;
//    int i=0;
//    while(i!=18)
//    {
//                match=strcmp(name[i],token);
//    i++;
//                if(match==0)
//                {
//                            *code=i;
//                            break;
//                }
/
/    }
//    return match;
//}
//void write_dyd(int code,FILE* fp)  //这个是针对上⼀个函数的写⼊⽂件流的函数,同样也放弃了
//{
//    int blank=16-strlen(name[code]);
//    while(blank!=0)
//    {
//      fputc(' ',fp);
//    blank=blank-1;
//    }
/
/ fputs(name[code],fp);
// fputc(' ',fp);
// fputs(name[code+1],fp);
// fputc('/n',fp);
//}
void ReadIntoBuffer(char buffer[256],FILE* fp)
{
int i;
char c;
char tmp;
for(i=0;i<255;i++)
{
c=fgetc(fp);
switch(c)
{
case ' ':
tmp=c;
while(c==' ')
c=fgetc(fp);
if(c!=EOF)
{
if(c!='/t'&&c!=' ')
fseek(fp,-1L,1);
if(buffer[i-1]!=' ')
buffer[i]=tmp;
else
i=i-1;
}
else
buffer[i]=EOF;
break;
case '/t':
while(c=='/t')
c=fgetc(fp);
if(c!=EOF)
{
if(c!='/t'&&c!=' ')    //如果⾮这些字符,就要把⽂件当前读取位置往前移
fseek(fp,-1L,1);
if(buffer[i-1]!=' ')                //如果缓冲区的前⼀个字符不为空格,就要输⼊⼀个空格来分开token
buffer[i]=' ';
else
i=i-1;            //因为这轮for循环缓冲区什么也没有修改,⽽缓冲区的游标⼜要被加1,所以这⾥要把i减1
}
else
buffer[i]=EOF;        //其他情况就把⽂件结束符写到缓冲区⾥⾯去
break;
case EOF:
buffer[i]=EOF;
break;
default:
buffer[i]=c;
break;
}
}
}
int read_buffer(FILE* fp,int* index) //这个函数从两个缓冲区中的⼀个读出⼀个字符赋给全局变量ch,缓冲区空则调⽤ReadInotBuffer 函数写⼊缓冲区
{
char* readfrom;                    //readfrom是我设的缓冲区数组的指针
if(buffer1Empty&&!buffer2Empty)      //缓冲区空与否由两个变量buffer1Empty和buffer2Empty来测定
readfrom=buffer2;
else
{
if(buffer2Empty&&!buffer1Empty)
readfrom=buffer1;
else
{
if(buffer1Empty&&buffer2Empty)
{
readfrom=buffer1;
ReadIntoBuffer(readfrom,fp);    //第⼀次读⽂件时默认填充缓冲区1
}
}
}
if(readfrom==buffer1)
buffer1Empty=0;
else
{
if(readfrom==buffer2)
buffer2Empty=0;
}
if(readfrom[*index]!=EOF&&*index<256)
{
ch=readfrom[*index];                  //将ch赋值
*index=*index+1;
}
else
{
if(readfrom[*index]==EOF)
{
return 1;                      //若读到⽂件尾,则返回1,1在函数translate中被赋给了全局变量EndofFile,这个全局变量控制了整个translate的while循环
}
else
if(*index>255)
{
*index=0;
readfrom=buffer1Empty?buffer1:buffer2;
ReadIntoBuffer(readfrom,fp);
if(readfrom==buffer1)
{
buffer1Empty=0;
buffer2Empty=1;
}
else
{
if(readfrom==buffer2)
{
buffer1Empty=1;
buffer2Empty=0;
}
}
ch=readfrom[*index];
*index=*index+1;
}
}
return 0;
}
void translate(const char *infile,const char *outfile,const char *errfile)        //词法分析器主要功能函数
{
FILE *ifp = fopen(infile,"r");
FILE *ofp = fopen(outfile,"a");
FILE *efp = fopen(errfile,"a");
int index=0;                            //index为读缓冲区的下标变量
int line_number=1;
while(ifp!=NULL&&!EndofFile)
{
printf("token now is %s|/n",token);
EndofFile=read_buffer(ifp,&index);
if(isdigit((int)ch)&&!EndofFile)
{
while(isdigit((int)ch)&&!EndofFile)
{
token[token_index]=ch;
token_index=token_index+1;
EndofFile=read_buffer(ifp,&index);
}
token[token_index]='/0';
token_index=0;
index=((index-1)==-1)?255:index-1;    //每个缓冲区为256个字符,若回退下标的时候处在缓冲区头,则切换到上⼀次读的缓冲区尾
int blank=16-strlen(token);
while(blank!=0)
{
fputc(' ',ofp);
blank=blank-1;
}
fputs(token,ofp);
fputc(' ',ofp);
fputs("11/n",ofp);
continue;
}
if(isalpha((int)ch)&&!EndofFile)
{
while((isalpha((int)ch)||isdigit((int)ch))&&!EndofFile)
{
token[token_index]=ch;
token_index=token_index+1;
EndofFile=read_buffer(ifp,&index);
}
token[token_index]='/0';
token_index=0;
index=((index-1)==-1)?255:index-1;
if(!strcmp(token,"begin"))
{
fputs("          begin 1/n",ofp);
continue;
}
pascal语言还有人用吗
if(!strcmp(token,"end"))
{
fputs("            end 2/n",ofp);
continue;
}
if(!strcmp(token,"integer"))
{
fputs("        integer 3/n",ofp);
continue;
}
if(!strcmp(token,"if"))
{
fputs("              if 4/n",ofp);
continue;
}
if(!strcmp(token,"then"))
{
fputs("            then 5/n",ofp);
continue;
}
if(!strcmp(token,"else"))
{
fputs("            else 6/n",ofp);
continue;
}
if(!strcmp(token,"function"))
{
fputs("        function 7/n",ofp);                                  continue;
}
if(!strcmp(token,"read"))
{
fputs("            read 8/n",ofp);
continue;
}
if(!strcmp(token,"write"))
{
fputs("          write 9/n",ofp);
continue;
}                                                                                  else
{
int blank=16-strlen(token);
while(blank!=0)
{
fputc(' ',ofp);
blank=blank-1;
}
fputs(token,ofp);
fputc(' ',ofp);
fputs("10/n",ofp);
}
continue;
}
if((ch=='=')&&!EndofFile)
{
fputs("              = 12/n",ofp);
continue;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。