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

📄 main.cpp

📁 词法、语法分析程序。 绝对不是目前网络上流行的任何一个版本
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//规定一行可以书写多条语句,一个语句也可以占领多行书写

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <process.h>

#include <fstream.h>
#include <iostream.h>

#include "my_define.h"
#include "my_globals.h"
#include "my_judge.h"
#include "my_subfunc.h"
#include "my_subfunc2.h"
//////////////////////////////////////////////////////////////////////////////

//下表为语法分析递归程序的层次结构:

void program();//程序
   void proghead();//程序首部
      //保留字PROGRAM
      //变量名(标识符)
      //保留字;
   void block();//程序块
      void consexpl();//常量说明部分
	     //保留字CONST
         void consdefi();//常量定义
		    //变量名(标识符)
		    //保留字=
            //常量
         void conssuff();//常量定义后缀
            //保留字,
		    void consdefi();//常量定义
            void conssuff();//常量定义后缀
			//保留字;
      void varexpl();//变量说明部分
	     //保留字VAR
         void vardefi();//变量定义部分
		    //变量名(标识符) 
		    void idsuff();//标识符后缀
            //保留字:
            void typeil();//类型
            //保留字;
         void varsuff();//变量定义后缀部分(由"变量定义部分"和"变量定义后缀部分"构成,或为空)
		    void vardefi();//变量定义部分
            void varsuff();//变量定义后缀部分
      void procdefi();//过程说明部分
         void procedh();//过程首部
		    //保留字PROCEDURE
		    //变量名(标识符) 
		    void argument();//参数部分
            //保留字;
         void block();//程序块
         //保留字;
         void procsuff();//过程后缀
		    void procedh();//过程首部
            void block();//程序块
            //保留字;
            void procsuff();//过程后缀
      void compsent();
	     //保留字BEGIN
         void sentence();//语句
		    //或赋值结构
		    void assipro();
		       //变量名(标识符) 
			   void suffix();//赋值后缀,分直接赋值和调用过程赋值两种
			      //或者保留字:=
			           void express();//表达式 
					      //保留字ADD或SUB(正负号)
                          void term();//项
			                 void factor();//因子
                             void factsuff();//因子后缀
                          void termsuff();//项后缀
			                 //保留字ADD或SUB(加型运算符)
			                 void factor();//因子
                             void factsuff();//因子后缀
                  //或者保留字(
                       void express();//表达式 
                       //保留字)
		    //或if结构
            void ifsent();
		       //保留字IF
		       void conditio();//条件分两种情况:ODD作用在表达式上,或者两个表达式进行比较
			      //或者
			      //保留字
			      void express();//表达式 
				  //或者
                  void express();//表达式 
				  void respoper();//6种比较运算符
			   //保留字THEN
               void sentence();//语句
		    //或while结构
            void whilsent();
		       //保留字WHILE
			   void conditio();//条件
               //保留字DO
               void sentence();//语句
		    //或Read()
            void read();
		       //保留字READ
			   //保留字(
               //变量名(标识符) 
               void idsuff();//标识符后缀
               //保留字)
		    //或Write()
            void write();
		       //保留字WRITE
			   //保留字(
               void express();//表达式
               void exprsuff();//表达式后缀
			      //保留字,
			      void express();//表达式
                  void exprsuff();//表达式后缀
               //保留字)
		    //或复合语句
			void compsent();
         void sentsuff();//语句后缀
		    //保留字;
		    void sentence();//语句
            void sentsuff();//语句后缀
         //保留字END
   //标识符.
 
/////////////////////////////////////////////////////////////////////////////



void main()
{
//词法分析部分:
//////////////////////////////////////////////////////////////////////
    cout<<"词法分析开始!!"<<endl;
	if((f1=fopen("input.txt","r"))==NULL)
	{
		cout<<"不能打开输入文件!"<<endl;
	    exit(0);
	}
	cout<<"输入文件为"<<"input.txt"<<endl;
	if((f2=fopen("output.txt","w"))==NULL)
	{
		cout<<"不能打开输出文件!"<<endl;
	    exit(0);
	}
    cout<<"输出文件为"<<"output.txt"<<endl;
	ofstream onfile;
    onfile.open("output.txt");
    while(fgets(buff,80,f1)!=NULL)
    {
      I=0;
      while(I<=strlen(buff)-2)
	  {
        Get_A_Word(onfile);
	  }
	}
    fclose(f1); fclose(f2);
    onfile.close();
    cout<<"词法分析完毕!!"<<endl;
//////////////////////////////////////////////////////////////////////
//
//语法分析部分:
//////////////////////////////////////////////////////////////////////
	cout<<endl;
    cout<<"语法分析开始!!"<<endl;
//	ifstream infile("output.txt",ios::in|ios::nocreate);
	if (!infile)
	{
	  cout<<"不能打开输入文件:\n";
	  exit(1);
	}
    cout<<"输入文件为"<<"output.txt"<<endl;
	if((f2=fopen("output2.txt","w"))==NULL)
	{
		cout<<"不能打开输出文件!"<<endl;
	    exit(0);
	}
    cout<<"输出文件为"<<"output2.txt"<<endl;
    onfile.open("output2.txt");
/**/infile>>Code;
/**/infile>>buff;cout<<buff<<endl;
/**/program();
/**/cout<<"该程序语法正确!"<<endl;
    infile.close();
	fclose(f2);
    onfile.close();
}

///////////////////////////////////////////////////////////

/////////////////////////////////////
//程序构成:
//   程序首部+程序块+.
//  
void program()//程序
{
  proghead();
  block();
  if(Code==DOT)
  { 
	infile>>Code;
    infile>>buff;cout<<buff<<endl; 
  }
  else { error(2); }
};
//////

/////////////////////////////////////
//程序块构成:
//   常量说明部分
//   变量说明部分
//   过程说明部分
//   语句部分
//
void block()//程序块
{ 
  consexpl();//常量说明部分
  varexpl();//变量说明部分
  procdefi();//过程说明部分
  compsent();//语句部分
};
//////

/////////////////////////////////////
//程序首部举例:
//   program testexample;
//
void proghead()//程序首部
{
 if(Code==PROGRAM)
 {
   infile>>Code;
   infile>>buff;cout<<buff<<endl; 
   if(Code>=BEGIN_OF_V+1&&Code<=BEGIN_OF_V+MAX_NUM_OF_V)
   //此范围为变量名(标识符)的内码范围
   {
     infile>>Code;
     infile>>buff;cout<<buff<<endl; 
	 if(Code==SEM)
	 {
	   infile>>Code;
       infile>>buff;cout<<buff<<endl; 
	 }
	 else
	   error(5);
   }
   else
     error(4);
 }
 else
   error(3);
};
//////

/////////////////////////////////////
//常量说明部分构成:
//   CONST + <常量定义> + <常量定义后缀> + ;
//   格式举例:const  num=20, times=10, hd=0;
void consexpl()
{
  if(Code==CONST)
  {
    infile>>Code;
    infile>>buff;cout<<buff<<endl;
	consdefi();
	conssuff();
	if(Code==SEM)
	{
	  infile>>Code;
      infile>>buff;cout<<buff<<endl;
	}
	else
	{ error(6); }
  }
};
//////

/////////////////////////////////////
// <常量定义> 格式:
//  <标识符> = 数
void consdefi()
{
  if(Code>=BEGIN_OF_V+1&&Code<=BEGIN_OF_V+MAX_NUM_OF_V)
  //此范围为变量名(标识符)的内码范围
  {
    infile>>Code;
    infile>>buff;cout<<buff<<endl;
	if(Code==EQU)
	{
	  infile>>Code;
      infile>>buff;cout<<buff<<endl;
	  if(Code>=BEGIN_OF_C+1&&Code<=BEGIN_OF_C+MAX_NUM_OF_C)
      //此范围为常量的内码范围
	  {
	    infile>>Code;
        infile>>buff;cout<<buff<<endl;
	  }
	  else
	   error(9);
	}
	else
	 error(8);
  }
  else
   error(7);
}
/////////////////////////////////////
void conssuff()
{
  if(Code==COM)
  {
    infile>>Code;
    infile>>buff;cout<<buff<<endl;
	consdefi();
	conssuff();
  }
  //当Code!=COM时, void conssuff()不执行, 
  //此时只定义了一个常量
}
/////////////////////////////////////
void varexpl()
{
  if(Code==VAR)
  {
    infile>>Code;
    infile>>buff;cout<<buff<<endl;
	vardefi();
	varsuff();
  }
}
/////////////////////////////////////
void vardefi()
{
 if(Code>=BEGIN_OF_V+1&&Code<=BEGIN_OF_V+MAX_NUM_OF_V)
 //此范围为变量名(标识符)的内码范围
 {
   infile>>Code;
   infile>>buff;cout<<buff<<endl;
   idsuff();
   if(Code==COL)
   {
     infile>>Code;
     infile>>buff;cout<<buff<<endl;
	 typeil();
	 if(Code==SEM)
	 {
	   infile>>Code;
       infile>>buff;cout<<buff<<endl;
	 }
	 else
		 error(12);
   }
   else
	   error(11);
 }
}
/////////////////////////////////////
void varsuff()
{
  if(Code>=BEGIN_OF_V+1&&Code<=BEGIN_OF_V+MAX_NUM_OF_V)
  //此范围为变量名(标识符)的内码范围
  {
    vardefi();
	varsuff();
  }
}
/////////////////////////////////////
void typeil()
{
  if(Code==INTEGER||Code==LONG)
  {
    infile>>Code;
    infile>>buff;cout<<buff<<endl; 
  }
  else
  error(13);
}
/////////////////////////////////////
void procdefi()
{
  if(Code==PROCEDURE)
  {
    procedh();
	block();
	if(Code==SEM)
	{
	  infile>>Code;
      infile>>buff;cout<<buff<<endl;  
	  procsuff();
	}
	else
	  error(14);
  }
}
/////////////////////////////////////
void procedh()
{
  if(Code==PROCEDURE)
  {
	infile>>Code;
    infile>>buff;cout<<buff<<endl;  
	if(Code>=BEGIN_OF_V+1&&Code<=BEGIN_OF_V+MAX_NUM_OF_V)
    //此范围为变量名(标识符)的内码范围
	{
	  infile>>Code;

⌨️ 快捷键说明

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