?? main.cpp
字號(hào):
//規(guī)定一行可以書(shū)寫(xiě)多條語(yǔ)句,一個(gè)語(yǔ)句也可以占領(lǐng)多行書(shū)寫(xiě)
#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"
//////////////////////////////////////////////////////////////////////////////
//下表為語(yǔ)法分析遞歸程序的層次結(jié)構(gòu):
void program();//程序
void proghead();//程序首部
//保留字PROGRAM
//變量名(標(biāo)識(shí)符)
//保留字;
void block();//程序塊
void consexpl();//常量說(shuō)明部分
//保留字CONST
void consdefi();//常量定義
//變量名(標(biāo)識(shí)符)
//保留字=
//常量
void conssuff();//常量定義后綴
//保留字,
void consdefi();//常量定義
void conssuff();//常量定義后綴
//保留字;
void varexpl();//變量說(shuō)明部分
//保留字VAR
void vardefi();//變量定義部分
//變量名(標(biāo)識(shí)符)
void idsuff();//標(biāo)識(shí)符后綴
//保留字:
void typeil();//類(lèi)型
//保留字;
void varsuff();//變量定義后綴部分(由"變量定義部分"和"變量定義后綴部分"構(gòu)成,或?yàn)榭?
void vardefi();//變量定義部分
void varsuff();//變量定義后綴部分
void procdefi();//過(guò)程說(shuō)明部分
void procedh();//過(guò)程首部
//保留字PROCEDURE
//變量名(標(biāo)識(shí)符)
void argument();//參數(shù)部分
//保留字;
void block();//程序塊
//保留字;
void procsuff();//過(guò)程后綴
void procedh();//過(guò)程首部
void block();//程序塊
//保留字;
void procsuff();//過(guò)程后綴
void compsent();
//保留字BEGIN
void sentence();//語(yǔ)句
//或賦值結(jié)構(gòu)
void assipro();
//變量名(標(biāo)識(shí)符)
void suffix();//賦值后綴,分直接賦值和調(diào)用過(guò)程賦值兩種
//或者保留字:=
void express();//表達(dá)式
//保留字ADD或SUB(正負(fù)號(hào))
void term();//項(xiàng)
void factor();//因子
void factsuff();//因子后綴
void termsuff();//項(xiàng)后綴
//保留字ADD或SUB(加型運(yùn)算符)
void factor();//因子
void factsuff();//因子后綴
//或者保留字(
void express();//表達(dá)式
//保留字)
//或if結(jié)構(gòu)
void ifsent();
//保留字IF
void conditio();//條件分兩種情況:ODD作用在表達(dá)式上,或者兩個(gè)表達(dá)式進(jìn)行比較
//或者
//保留字
void express();//表達(dá)式
//或者
void express();//表達(dá)式
void respoper();//6種比較運(yùn)算符
//保留字THEN
void sentence();//語(yǔ)句
//或while結(jié)構(gòu)
void whilsent();
//保留字WHILE
void conditio();//條件
//保留字DO
void sentence();//語(yǔ)句
//或Read()
void read();
//保留字READ
//保留字(
//變量名(標(biāo)識(shí)符)
void idsuff();//標(biāo)識(shí)符后綴
//保留字)
//或Write()
void write();
//保留字WRITE
//保留字(
void express();//表達(dá)式
void exprsuff();//表達(dá)式后綴
//保留字,
void express();//表達(dá)式
void exprsuff();//表達(dá)式后綴
//保留字)
//或復(fù)合語(yǔ)句
void compsent();
void sentsuff();//語(yǔ)句后綴
//保留字;
void sentence();//語(yǔ)句
void sentsuff();//語(yǔ)句后綴
//保留字END
//標(biāo)識(shí)符.
/////////////////////////////////////////////////////////////////////////////
void main()
{
//詞法分析部分:
//////////////////////////////////////////////////////////////////////
cout<<"詞法分析開(kāi)始!!"<<endl;
if((f1=fopen("input.txt","r"))==NULL)
{
cout<<"不能打開(kāi)輸入文件!"<<endl;
exit(0);
}
cout<<"輸入文件為"<<"input.txt"<<endl;
if((f2=fopen("output.txt","w"))==NULL)
{
cout<<"不能打開(kāi)輸出文件!"<<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;
//////////////////////////////////////////////////////////////////////
//
//語(yǔ)法分析部分:
//////////////////////////////////////////////////////////////////////
cout<<endl;
cout<<"語(yǔ)法分析開(kāi)始!!"<<endl;
// ifstream infile("output.txt",ios::in|ios::nocreate);
if (!infile)
{
cout<<"不能打開(kāi)輸入文件:\n";
exit(1);
}
cout<<"輸入文件為"<<"output.txt"<<endl;
if((f2=fopen("output2.txt","w"))==NULL)
{
cout<<"不能打開(kāi)輸出文件!"<<endl;
exit(0);
}
cout<<"輸出文件為"<<"output2.txt"<<endl;
onfile.open("output2.txt");
/**/infile>>Code;
/**/infile>>buff;cout<<buff<<endl;
/**/program();
/**/cout<<"該程序語(yǔ)法正確!"<<endl;
infile.close();
fclose(f2);
onfile.close();
}
///////////////////////////////////////////////////////////
/////////////////////////////////////
//程序構(gòu)成:
// 程序首部+程序塊+.
//
void program()//程序
{
proghead();
block();
if(Code==DOT)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
}
else { error(2); }
};
//////
/////////////////////////////////////
//程序塊構(gòu)成:
// 常量說(shuō)明部分
// 變量說(shuō)明部分
// 過(guò)程說(shuō)明部分
// 語(yǔ)句部分
//
void block()//程序塊
{
consexpl();//常量說(shuō)明部分
varexpl();//變量說(shuō)明部分
procdefi();//過(guò)程說(shuō)明部分
compsent();//語(yǔ)句部分
};
//////
/////////////////////////////////////
//程序首部舉例:
// 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)
//此范圍為變量名(標(biāo)識(shí)符)的內(nèi)碼范圍
{
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);
};
//////
/////////////////////////////////////
//常量說(shuō)明部分構(gòu)成:
// 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); }
}
};
//////
/////////////////////////////////////
// <常量定義> 格式:
// <標(biāo)識(shí)符> = 數(shù)
void consdefi()
{
if(Code>=BEGIN_OF_V+1&&Code<=BEGIN_OF_V+MAX_NUM_OF_V)
//此范圍為變量名(標(biāo)識(shí)符)的內(nèi)碼范圍
{
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)
//此范圍為常量的內(nèi)碼范圍
{
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();
}
//當(dāng)Code!=COM時(shí), void conssuff()不執(zhí)行,
//此時(shí)只定義了一個(gè)常量
}
/////////////////////////////////////
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)
//此范圍為變量名(標(biāo)識(shí)符)的內(nèi)碼范圍
{
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)
//此范圍為變量名(標(biāo)識(shí)符)的內(nèi)碼范圍
{
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)
//此范圍為變量名(標(biāo)識(shí)符)的內(nèi)碼范圍
{
infile>>Code;
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -