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

📄 编译pl0词法分析器 .cpp

📁 是我刚完成的
💻 CPP
字号:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream.h>

//*********************  变量定义  **************************************
#define al 10
#define norw 13
char *SourceFile;     
int  SourceFLength;    
int  CurrentPosition;   
char ch;
char a[al+1];
char sym[30];
char word[norw+1][al+1]={"  ","begin     ","call      ","const     ",
                              "do        ","end       ","if        ",
							  "odd       ","procedure ","read      ",
							  "then      ","var       ","while     ",
							  "write     "};

//*********************  成员函数定义  ********************************



void Getche( )
{
   CurrentPosition++;
   if (CurrentPosition>=SourceFLength)
   {   
	   cout<<endl<<"PL/0 词法分析结束!"<<endl;
	   exit(1) ;   
   }
   ch=SourceFile[CurrentPosition];
}

void getsym()
{
//----------------------------------------------------------保留字
 while (ch==' '||ch=='\r')  Getche(); 
 int k;
 int kk=al;
 if ((ch>='a')&&(ch<='z'))
 {  
    k=-1;
	
	do
	{
	  if (k<al)
	  { k++;
	    a[k]=ch;		
	  }
	  Getche();
	}
	while ((ch>='a')&&(ch<='z')||(ch>='0')&&(ch<='9'));
    if (k>=kk-1)
		kk=k;
	else
		do
		{
		  a[kk-1]=' ' ;
		  kk-- ;
		}
		while (kk-1>k);
    a[al]='\0';

	int i,j;
	char id[al+1];
	i=1;j=norw;
	strcpy(id,a);
	do
	{
      k=(i+j)/2;
	  if (strcmp(id,word[k])<=0)
		  j=k-1;
	  if (strcmp(id,word[k])>=0)
		  i=k+1;
	}
	while (i<=j);
    if (i-1>j)
		strcpy(sym,"保留字");
	else
        strcpy(sym,"ident");

    printf("%-10s",sym);
	cout<<a<<endl;
	return;
 }
//-------------------------------------------------------------常数
if ((ch>='0')&&(ch<='9'))
{
		char shu[2];
		k=0;
		int num=0;
		strcpy(sym,"常数");
		
			do
		{
			shu[0]=ch;
			shu[1]='\0';
			num=10*num+atoi(shu);
			k++;
			Getche();

		}
		while ((ch>='0')&&(ch<='9'));
		if(k<13)
		{  printf("%-10s",sym);
		   cout<<num<<endl;  }
		else 
			cout<<"常数太大!!!"<<endl;
	    return;
}
//-------------------------------------------------------------运算符
if (ch==':')
{
  Getche();
  if (ch=='=')
	 {  cout<<"运算符    :="<<endl; 
        Getche();
	 }
  else
	    cout<<"未定义    :"<<endl;
  return;

}

if (ch=='>')
{
  Getche();
  if (ch=='=')
	 {  cout<<"运算符    >="<<endl; 
        Getche();
	 }
  else
	   cout<<"运算符    >"<<endl; 
  return;

}
if (ch=='<')
{
  Getche();
  if (ch=='=')
	 {  cout<<"运算符    <="<<endl; 
        Getche();
	 }
  else
	   cout<<"运算符    <"<<endl; 
  return;

}
//-------------------------------------------------------------注释
if (ch=='/')  
{  Getche();
   if (ch=='/')
   {  while (ch!='\r') Getche();}
   else 
	   if (ch=='*')
	   { 
       while(1)
	   {  Getche();
	      if (ch=='*')
		  { Getche();
	        if (ch=='/')
			break;
		  }
	   }
	   Getche();
   }
   else
     cout<<"运算符    /"<<endl; 
  return;
}
//--------------------------------------------------------运算符+-*/
if ((ch=='=')||(ch=='+')||(ch=='-')||(ch=='*'))
{
     cout<<"运算符    "<<ch<<endl; 
    Getche();
    return;
}
//--------------------------------------------------------界符
if ((ch=='.')||(ch==',')||(ch==';')||(ch=='(')||(ch==')'))
{
     cout<<"界符      "<<ch<<endl; 
     Getche();
     return;
}

//--------------------------------------------------------其他符号
     cout<<"未定义     "<<ch<<endl; 
     Getche();

}


//***********************************************************主函数

void main()
{   char c; 

do  {

	cout<<"请输入被词法分析的程序源代码:(按CTRL+Z结束)"<<endl;
	
	SourceFile=(char *) malloc(sizeof(char)*500);
    int index=0;
	while(1)
	{    ch=getche();
	     if (ch=='\r')     // 回车键
			 puts("");     //换行
		 if (ch==26 )      // Ctrl+z
		 {
			 puts("\n程序源代码输入完毕!!!\n");
			 break;
		 }
		 if (index<500)
		 {  SourceFile[index]=ch;
		    index++;
		 }
		 else
		 { printf(" \nOut of buffer within 500 byte!\n ");
		   break;
         }
    }
	SourceFile[index]='\0';

    SourceFLength=strlen(SourceFile);
	if (SourceFLength<1)
	{
		printf("退出程序!\n");
		exit(1);
	}
	CurrentPosition=-1;	
	ch=' ';
	printf("\n");
    while(1)
       getsym();
    free(SourceFile);
	
	cout<<"是否还要再输新的程序分析???(是的话按Y或y)"<<endl;
    cin>>c;
}while(c=='Y'&&c=='y');
}


//例如程序源代码
    //gets(SourceFile);
    //SourceFile="  const a=35,b=49 ; 
	//var c,d,e; 
	//procedure p; 
	//var g ; 
	//begin
	//g = a*b 
	//end 
	//begin d= a+b; 
	//if d>=a  then c=a  end  ";

⌨️ 快捷键说明

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