PL0语言词法分析

#include "head.h"
#define ness 13
#define nmax 14
#define al 10
#define cxmax 200
//int cc;//用来记录一共读入了多少个单词
char word[ness][al];
char ch;
char id[al+1];//记录标识符ident
int num;
int cc,ll;
int cx;
char line[81];
char a[al+1];//用来暂时存储
char tem;
int err;
FILE *fin;
FILE *fout;
int errline;
#define getchdo   if(-1==getch()) return -1;
void init()
{
 strcpy(&(word[0][0]), "begin");
 strcpy(&(word[1][0]), "call");
 strcpy(&(word[2][0]), "const");
 strcpy(&(word[3][0]), "do");
 strcpy(&(word[4][0]), "end");
 strcpy(&(word[5][0]), "if");
 strcpy(&(word[6][0]), "odd");
 strcpy(&(word[7][0]), "procedure");
 strcpy(&(word[8][0]), "read");
 strcpy(&(word[9][0]), "then");
 strcpy(&(word[10][0]), "var");
 strcpy(&(word[11][0]), "while");
 strcpy(&(word[12][0]), "write");
}
int getch()
{
 
 if(cc==ll)
 {
  //ch=getc(fin);  
  if(feof(fin))
  {
   cout<<"文件已读完"<<endl;
   return -1;
  }
  ll=0;
  cc=0;
  ch=' ';
  while(ch !=10)
  {
   //cout<<ch;
   //ch=getc(fin);
   if(EOF==fscanf(fin,"%c",&ch))
   {
    line[ll]=0;
    break;
   }
   //cout<<ch;
   line[ll]=ch;
   ll++;
  }
  //cout<<endl;
  //if(line[ll-1]){}
  errline++;
 }
 ch=line[cc];//从这里开始读
 cc++;
 return 0;
}
void error(int n)
{
 char space[81];
 memset(space,32,81);
 space[cc-1]=0;
 cout<<"出错位置是"<<errline<<endl;
 //printf("****%s!%d\n",space,n);
 err++;
}
int getsym()
{
 int i,j,k;
 while(ch==' '||ch==10||ch==9)
 {
  if(getch()==-1)
  {
   return -1;
  }
 }
 if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
 {
  k=0;
  do
  {
   if(k<al)
   {
    a[k]=ch;
    k++;
   }
   if(getch()==-1)
   {
    return -1;
   }
  }while(ch >= 'a' && ch <= 'z' || ch >= '0' && ch <= '9'||ch>='A'&&ch<='Z');
  a[k]=0;
  strcpy(id,a);
  i=0;
  j=ness-1;
  int t=0;
  for(t=0;t<ness;t++)
  {
   if(strcmp(id,word[t])==0)
   {
    break;
   }
  }
  if(t<ness)
  {
   cout<<"关键字"<<"(1,"<<id<<")"<<endl;
  }
  else
  {
   cout<<"标识符"<<"(5,"<<id<<")"<<endl;
  }
 }
 else{
  if(ch>='0'&&ch<='9')
  {
   k=0;
   num=0;
   do{
    num=num*10+ch-'0';
    k++;
    if(getch()==-1)
    {
     return -1;
    }
   }while(ch>='0'&&ch<='9');
   k--;
   if(k>nmax)
   {
    error(30);
   }
   if(ch<='z'&&ch>='a'||ch>='A'&&ch<='Z'){
    cout<<"不能有数字开头的标识符"<<endl;
    cout<<"出错的行数为"<<errline<<endl;
    cout<<"出错行数的内容为"<<endl;
    for(int ttem=0;ttem<ll;ttem++)
    {
     cout<<line[ttem];
    }
    return -1;
   }
  }
  else
  {
   if(ch==':')
   {
    if(getch()==-1)
    {
     return -1;
    }
    while(ch==' ')
    {
     if(getch()==-1)
     {
      return -1;
     }
    }
    if(ch=='=')
    {
     cout<<"赋值  "<<"(4,:=)"<<endl; 
     if(getch()==-1)
     {
      return -1;
     }
    }
    
    else
    {
     cout<<":不能单独出现"<<endl;
     cout<<"出错的行数为"<<errline<<endl;
     cout<<"出错行数的内容为"<<endl;
     for(int ttem=0;ttem<ll;ttem++)
     {
      cout<<line[ttem];
     }
     return -1;
    }
   }
   else{
    if(ch=='<')
    {
     if(getch()==-1)
     {
      return -1;
     }
     while(ch==' ')
     {
      if(getch()==-1)
      {
       return -1;
      }
     }
     if(ch=='=')
     {
      cout<<"小于等于"<<"(4,<=)"<<endl;
      if(getch()==-1)
      {
       return -1;
      }
     }
     else
     {
      cout<<"小于  "<<"(4,<)"<<endl;
     }
    }
    if(ch=='>')
    {
     if(getch()==-1)
     {
      return -1;
     }
     while(ch==' ')
     {
      if(getch()==-1)
      {
       return -1;
      }
     }
     if(ch=='=')
     {
      cout<<"大于等于"<<"(4,>=)"<<endl;
      if(getch()==-1)
      {
       return -1;
      }
      
     }
     else{
       cout<<"大于  "<<"(4,>)"<<endl;
     }
    }
    else{
     if(ch=='+'){
      cout<<"加号  "<<"(4,+)"<<endl;
      if(getch()==-1)
      {
       return -1;
      }
     }
     else if(ch=='-'){
      cout<<"减号  "<<"(4,-)"<<endl;
      if(getch()==-1)
      {
       return -1;
      }
     }
     else if(ch=='*'){
      cout<<"乘号  "<<"(4,*)"<<endl;
      if(getch()==-1)
      {
       return -1;
      }
     }
     else if(ch=='/'){
      cout<<"斜杠  "<<"(4,/)"<<endl;
      if(getch()==-1)
      {
       return -1;
      }
     }
     else if(ch=='='){
      cout<<"等号  "<<"(4,=)"<<endl;
      if(getch()==-1)
      {
       return -1;
      }
     }
     else if(ch=='#'){
      cout<<"井号  "<<"(4,#)"<<endl;
      if(getch()==-1)
      {
       return -1;
      }
     }
     else if(ch=='('){
      cout<<"左括号 "<<"(4,()"<<endl;
      if(getch()==-1)
      {
       return -1;
      }
     }
     else if(ch==')'){
      cout<<"右括号 "<<"(4,))"<<endl;
      if(getch()==-1)
      {
       return -1;
      }
     }
     else if(ch==','){
      cout<<"逗号  "<<"(4,,)"<<endl;
      if(getch()==-1)
      {
       return -1;
      }
     }
     else if(ch==';'){
      cout<<"分号  "<<"(4,;)"<<endl;
      if(getch()==-1)
      {
       return -1;
      }
     }
     else if(ch=='.'){
      cout<<"句号  "<<"(4,.)"<<endl;
     }
     else{
      cout<<"无法识别的符号"<<endl;
      cout<<"出错的行数为"<<errline<<endl;
      cout<<"出错行数的内容为"<<endl;
      for(int ttem=0;ttem<ll;ttem++)
      {
       cout<<line[ttem];
      }
      return -1;
     }
    }
   }
  }
 }
 return 0;
}
int main(){
 //读文件
 //FILE *fin;
 errline=0;
 char *in="E:\\c++\\xhcifa\\1.txt";
 fin=fopen(in,"r");
 if(!fin)
 {
  cout<<"文件不能打开"<<endl;
 }
 init();
 cc=0;
 ll=0;
 cx=0;
 ch=' '; 
 for(;;){
  if(getsym()==-1){
   break;
  }
  ch=' ';
 }
 getchar();
 return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值