⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pascal词法分析器.txt

📁 #include "define.h" #include <stdio.h> #include <string.h> #include <ctype.h>
💻 TXT
字号:
#include "define.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
FILE *fp;

/*//////////////////////////////////////////////////////////////////////////////
This is a pretreatment.
/////////////////////////////////////////////////////////////////////////////*/
void readIntoBuffer(char buffer[256])
{
 char ch1;
 char temp;
 int i;

 for(i=0;i<256&&!feof(fp);i++)
 {
  ch1=fgetc(fp);
  if(ch1==' ')   
  {
   temp=ch1;
   while(ch1==' ')
    ch1=fgetc(fp);
   if(ch1!=EOF)
   {
    if(ch1!='\\n'&&ch1!=' '&&ch1!='\\t')
     fseek(fp,-1L,1);
    if(buffer[i-1]!=' ')
     buffer[i]=temp;
    else i=i-1;
   }
   else
    buffer[i]=ch1;
   continue;
  }
  if(ch1=='\\n')
  {
   temp=ch1;
   while(ch1=='\\n')
    ch1=fgetc(fp);
   if(ch1!=EOF)
   {
    if(ch1!='\\n'&&ch1!=' '&&ch1!='\\t')
     fseek(fp,-1L,1);
    if(buffer[i-1]!=' ')
     buffer[i]=' ';
    else i=i-1;
   }
   else
    buffer[i]=ch1;

   continue;
  }
  if(ch1=='\\t')
  {

   while(ch1=='\\t')
    ch1=fgetc(fp);
   if(ch1!=EOF)
   {
    if(ch1!='\\n'&&ch1!=' '&&ch1!='\\t')
     fseek(fp,-1L,1);
    if(buffer[i-1]!=' ')
     buffer[i]=' ';
    else i=i-1;
   }
   else buffer[i]=ch1;

   continue;
  }
  if(ch1=='{')
  {
   ch1=fgetc(fp);
   while(ch1!='}')
    ch1=fgetc(fp);
   fseek(fp,-1L,1);
  }

  else
  {
   buffer[i]=ch1;
   continue;
  }
      }

}
int insertid(char id[])
{
       int i;
      
       pid1=(struct Identifier*)malloc(LENID);
      if(idhead==NULL)
      {
           for(i=0;i<20;i++)
              pid1->str1[i]=id[i];
           pid1->next1=idhead;
           idhead=pid1;
           pid2=pid1;
      } 
      else
      {
           for(i=0;i<20;i++)
              pid1->str1[i]=id[i];
           pid1->next1=pid2->next1;
           pid2->next1=pid1;
           pid2=pid1; 
      }
   return count1++;
}
int insertint(char id[])
{
       int i;
       
       pint1=(struct Integer*)malloc(LENINT);
      if(inthead==NULL)
      {
           for(i=0;i<16;i++)
              pint1->str2[i]=id[i];
           pint1->next2=inthead;
           inthead=pint1;
           pint2=pint1;
      } 
      else
      {
           for(i=0;i<16;i++)
              pint1->str2[i]=id[i];
           pint1->next2=pint2->next2;
           pint2->next2=pint1;
           pint2=pint1; 
      }
      return count2++;
}
int insertreal(char real[])
{
       int i;
       
       preal1=(struct Real*)malloc(LENREAL);
      if(realhead==NULL)
      {
           for(i=0;i<32;i++)
              preal1->str3[i]=real[i];
           preal1->next3=realhead;
           realhead=preal1;
           preal2=preal1;
      } 
      else
      {
           for(i=0;i<32;i++)
              preal1->str3[i]=real[i];
           preal1->next3=preal2->next3;
           preal2->next3=preal1;
           preal2=preal1;
      }
      return count3++;
}
void  getChar()
{

 if(isb1empty)
 {
  readIntoBuffer(buffer1);
  isb1empty=FALSE;
 }
 if(isb2empty)
 {
  readIntoBuffer(buffer2);
  isb2empty=FALSE;
 }
 if(buffer1[k1]==EOF)
 {
  EndFlag=0;
  ch=EOF;
 }
 if(buffer2[k2]==EOF)
 {
  EndFlag=0;
  ch=EOF;
 }
 if(flag==0&&buffer1[k1]!=EOF)
 {
  ch=buffer1[k1];
  k1++;
  if(k1==256)
  {
   flag=1;
   isb1empty=TRUE;
   k1=0;
  }

 }
 if(flag==1&&buffer2[k2]!=EOF)
 {
  ch=buffer2[k2];
  k2++;
  if(k2==256)
  {
   flag=0;
   isb2empty=TRUE;
   k2=0;
  }
 }


}
void retract()
{

 if(flag==0)
  k1=k1-1;
 if(flag==1)
        k2=k2-1;
}
void insert(char ch)
{
 strToken[j]=ch;
 j++;
}
int reserve()
{
 int k;
 for(k=0;k<8;k++)
 {
  if(strcmp(strToken,p[k])==0)
   return k+1;

 }
 return 0;
}
/*////////////////////////////////////////////////////////////////////////////////
词法分析器,从缓冲区中取出字符,识别出单词符号
///////////////////////////////////////////////////////////////////////////////*/

void lex()
{
 int code;
 getChar();
        
 if(isalpha((int)ch)||(int)ch==UNDERLINE)
 {

  while(isalpha((int)ch)||isdigit((int)ch)||(int)ch==UNDERLINE)
  {
   insert(ch);
   getChar();
  }
  retract();
  code=reserve();
  if(code==0)
  {
                        
           
   printf("(6,%d)\\n",insertid(strToken));
   return;
  }
  else
  {
   printf("(%s,%d)\\n",strToken,code);
   return;
  }
 }
 if(isdigit((int)ch))
 {

  while(isdigit((int)ch))
  {
   insert(ch);
   getChar();
  }

  if((int)ch==DOT)
  {
   getChar();
   if(isdigit((int)ch))
   {
    while(isdigit((int)ch))
    {
     insert(ch);
     getChar();
    }

    if((int)ch==EXP||(int)ch==exp)
    {
     getChar();
     if((int)ch==MINUS)
     {
      insert(ch);
      getChar();
     }
     if(isdigit((int)ch))
     {
      while(isdigit((int)ch))
      {
       insert(ch);
       getChar();
      }
      retract();
      printf("exponential\\n");
      return;
     }
     else
     {
      retract();
      retract();
      retract();
      return;
     }
    }
    else
    {
     retract();
                                        
                    
     printf("(7,%d)\\n",insertreal(strToken));
     return;
    }
   }
   
   else
   {
    retract();
    printf("(2,)\\n");
    return;
   }
  }
  if((int)ch==EXP||(int)ch==exp)
  {
   getChar();
   if((int)ch==MINUS)
   {
    insert(ch);
    getChar();
   }
   if(isdigit((int)ch))
   {
    while(isdigit((int)ch))
    {
     insert(ch);
     getChar();
    }
    retract();
    printf("exponential\\n");
    return;
   }
   else
   {
    retract();
    retract();
    retract();
    return;
   }
  }
        else if(isalpha((int)ch)||ch=='$'||ch=='#')
  {       
       insert(ch);
       getChar();
    while(isalpha((int)ch)||ch=='$'||ch=='#'||isdigit((int)ch))
    {
                    insert(ch);
     getChar();
    }
    retract();
    printf("idetifier %s error\\n",strToken);return;
  }
  else
  {
  
   printf("(8,%d)\\n",insertint(strToken));return;
  }

 }
 if((int)ch==PLUS)
 {
  printf("(3,10h)\\n");
  return;
 }
 if((int)ch==MINUS)
 {
  printf("(3,11h)\\n");
  return;
 }
 if((int)ch==MULTIPLY)
 {
  printf("(3,20h)\\n");
  return;
 }
 if((int)ch==DEVIDE)
 {
  printf("(3,21h)\\n");
  return;
 }
 if((int)ch==LBRACKET)
 {
  printf("(2,4)\\n");
  return;
 }
 if((int)ch==RBRACKET)
 {
  printf("(2,5)\\n");
  return;
 }
 if((int)ch==SEMICOLON)
 {
  printf("(2,2)\\n");
  return;
 }
 if((int)ch==COLON)
 {
  getChar();
  if((int)ch==EQUAL)
  {
   printf("(2,3)\\n");
   return;
  }
  else{printf("冒号\\n");retract();return;}
 }
 if((int)ch==LESS)
 {
  getChar();
  if((int)ch==EQUAL)
  {
   printf("(4,01H)\\n");
   return;
  }
  if((int)ch==MORE)
  {
   printf("(4,05H)\\n");
   return;
  }
  else
  {
   retract();
   printf("(3,21h)\\n");
   return;
  }
 }
 if((int)ch==MORE)
 {
  getChar();
  if((int)ch==EQUAL)
  {
   printf("(4,04H)\\n");
   return;
  }

  else
  {
   retract();
   printf("(3,03h)\\n");
   return;
  }
 }
 if((int)ch==EQUAL)
 {
  printf("(4,02H)\\n");
  return;
 }
 if((int)ch==COMMA)
 {
  printf("(2,0)\\n");
  return;
 }

}
void del()
{
 while(idhead!=NULL)
 {
  pid2=idhead->next1;
  free(idhead);
  idhead=pid2;
 }
 while(inthead!=NULL)
 {
  pint2=inthead->next2;
  free(inthead);
  inthead=pint2;
 }
 while(realhead!=NULL)
 {
  preal2=realhead->next3;
  free(realhead);
  realhead=preal2;
 }
}


int main()
{
 char fName[20];
 int i;
 printf("Please intput the file name that you want to compile:    ");
 scanf("%s",fName);
    if((fp=fopen(fName,"r"))==NULL)
 {
  printf("cannot find thie file!");
 }
 while(EndFlag)
 {
  for(i=0;i<40;i++)
   strToken[i]='\\0';
  j=0;
  lex();
 }
 fclose(fp);
 printf("\\nOutput the keywords!\\n");
 while(idhead!=NULL)
 {
  printf("<id,%s>\\n",idhead->str1);
  idhead=idhead->next1;
 }
 printf("\\nOutput the integer!\\n");
 while(inthead!=NULL)
 {
  printf("<int,%s>\\n",inthead->str2);
  inthead=inthead->next2;
 }
 printf("\\nOutput the real!\\n");
 while(realhead!=NULL)
 {
  printf("<real,%s>\\n",realhead->str3);
  realhead=realhead->next3;
 }
 del();
 return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -