📄 syntax.h
字号:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <process.h>
#include <fstream.h>
#include <iostream.h>
//#include "my_getsym.h"
void program();//程序
void proghead();//程序首部,保留字PROGRAM,变量名(标识符),保留字;
void block();//程序块
void consexpl();//常量说明部分,保留字CONST
void consdefi();//常量定义,变量名(标识符),保留字= ,常量
void conssuff();//常量定义后缀
void conssuff();//常量定义后缀,保留字;
void varexpl();//变量说明部分,保留字VAR
void vardefi();//变量定义部分
void idsuff();//标识符后缀
void typeil();//类型
void varsuff();//变量定义后缀部分
void procdefi();//过程说明部分
void argument();//参数部分
void procedh();//过程首部
void conditio();//条件
void procsuff();//过程后缀
void suffix();//赋值后缀,分直接赋值和调用过程赋值两种
void term();//项
void termsuff();//项后缀
void factor();//因子
void factsuff();//因子后缀,或者保留字(
void ifsent(); //保留字IF
void respoper();//6种比较运算符
void whilsent(); //保留字WHILE
void read();//保留字READ ,保留字( ,变量名(标识符)
void write(); //保留字WRITE , 保留字(
void express();//表达式
void exprsuff();//表达式后缀,保留字)
void compsent(); //或复合语句
void sentsuff();//语句后缀
void error(int n)
{
cout<<"错误!"<<endl;
exit(0);
}
///////////////////////////////////////////////////////////
/////////////////////////////////////
//程序构成:
// 程序首部+程序块+.
//
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+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+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+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+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+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+NUM_OF_V)
//此范围为变量名(标识符)的内码范围
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
argument();
if(Code==SEM)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
}
else
error(16);
}
else error(15);
}
}
/////////////////////////////////////
void argument()
{
if(Code==LBR)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
if(Code==ID)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
if(Code==COL)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
typeil();
if(Code==RBR)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
}
else
error(19);
}
else
error(18);
}
else
error(17);
}
}
////////////////////////////////////////////////////////
void procsuff()
{
if(Code==PROCEDURE)
{
procedh();
block();
if(Code==SEM)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
procsuff();
}
else
error(20);
}
}
////////////////////////////////////////////////////////
void assipro()
{
if(Code>=BEGIN_OF_V+1&&Code<=BEGIN_OF_V+NUM_OF_V)
//此范围为变量名(标识符)的内码范围
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
suffix();
}
else
error(21);
}
/////////////////////////////////////////////////////////
void sentence()
{
if(Code>=BEGIN_OF_V+1&&Code<=BEGIN_OF_V+NUM_OF_V)
//此范围为变量名(标识符)的内码范围
assipro();
else if(Code==IF)
ifsent();
else if(Code==WHILE)
whilsent();
else if(Code==READ)
read();
else if(Code==WRITE)
write();
else if(Code==BEGIN)
compsent();
}
/////////////////////////////////////////////////////
void suffix()
{
if(Code==ASS)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
express();
}
else if(Code==LBR)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
express();
if(Code==RBR)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
}
else error(22);
}
}
////////////////////////////////////////////////////////
void ifsent()
{
if(Code==IF)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
conditio();
if (Code==THEN)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
sentence();
}
else error(24);
}
else error(23);
}
/////////////////////////////////////////////////////////
void whilsent()
{
if(Code==WHILE)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
conditio();
if(Code==DO)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
sentence();
}
else error(26);
}
else error(25);
}
////////////////////////////////////////////////////////////
void read()
{
if(Code==READ)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
if(Code==LBR)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
if(Code>=BEGIN_OF_V+1&&Code<=BEGIN_OF_V+NUM_OF_V)
//此范围为变量名(标识符)的内码范围
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
idsuff();
if(Code==RBR)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
}
else error(27);
}
else error(30);
}
else error(29);
}
else error(28);
}
/////////////////////////////////////////////////////////////
void idsuff()
{
if(Code==COM)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
if(Code>=BEGIN_OF_V+1&&Code<=BEGIN_OF_V+NUM_OF_V)
//此范围为变量名(标识符)的内码范围
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
idsuff();
}
else error(31);
}
}
///////////////////////////////////////////////////////////////
void write()
{
if(Code==WRITE)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
if(Code==LBR)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
express();
exprsuff();
if(Code==RBR)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
}
else error(34);
}
else error(33);
}
else error(32);
}
////////////////////////////////////////////////////////////////
void compsent()
{
if(Code==BEGIN)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
sentence();
sentsuff();
if(Code==END)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
}
else error(36);
}
else error(35);
}
/////////////////////////////////////////////////////////////////
void exprsuff()
{
if(Code==COM)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
express();
exprsuff();
}
}
//////////////////////////////////////////////////////////////////
void sentsuff()
{
if(Code==SEM)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
sentence();
sentsuff();
}
}
///////////////////////////////////////////////////////////////////
void conditio()
{
if(Code==LBR)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
//////
if(Code==ODD)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
express();
}
else
{
express();
respoper();
express();
}
//////
if(Code==RBR)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
}
else error(42);
}
else error(43);
}
///////////////////////////////////////////////////////////////////
void express()
{
if(Code==ADD||Code==SUB)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
}
//可以有正负号,也可以没有
term();
termsuff();
}
////////////////////////////////////////////////////////////////////
void term()
{
factor();
factsuff();
}
////////////////////////////////////////////////////////////////////
void termsuff()
{
if(Code==ADD||Code==SUB)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
factor();
factsuff();
}
}
////////////////////////////////////////////////////////////////////
void factsuff()
{
if(Code==MUL||Code==DIV)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
factor();
factsuff();
}
}
//////////////////////////////////////////////////////////////////////
void factor()
{
if(Code>=BEGIN_OF_V+1&&Code<=BEGIN_OF_V+NUM_OF_V)
//此范围为变量名(标识符)的内码范围
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
factsuff();
}
else
if(Code>=BEGIN_OF_C+1&&Code<=BEGIN_OF_C+NUM_OF_C)
//此范围为常量的内码范围
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
factsuff();
}
else
if(Code==LBR)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
express();
if(Code==RBR)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
}
else error(38);
}
else error(37);
}
///////////////////////////////////////////////////////////////
void addoper()
{
if(Code==ADD||Code==SUB)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
}
else error(39);
}
///////////////////////////////////////////////////////////////
void muloper()
{
if(Code==MUL||Code==DIV)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
}
else error(40);
}
///////////////////////////////////////////////////////////////
void respoper()
{
if(Code>=EQU&&Code<=GEQ)//6种比较运算符的内码连在一起的
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
}
else error(41);
}
///////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -