?? main.cpp
字號:
//規(guī)定一行可以書寫多條語句,一個語句也可以占領多行書寫
#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"
//////////////////////////////////////////////////////////////////////////////
//下表為語法分析遞歸程序的層次結(jié)構(gòu):
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();//變量定義后綴部分(由"變量定義部分"和"變量定義后綴部分"構(gòu)成,或為空)
void vardefi();//變量定義部分
void varsuff();//變量定義后綴部分
void procdefi();//過程說明部分
void procedh();//過程首部
//保留字PROCEDURE
//變量名(標識符)
void argument();//參數(shù)部分
//保留字;
void block();//程序塊
//保留字;
void procsuff();//過程后綴
void procedh();//過程首部
void block();//程序塊
//保留字;
void procsuff();//過程后綴
void compsent();
//保留字BEGIN
void sentence();//語句
//或賦值結(jié)構(gòu)
void assipro();
//變量名(標識符)
void suffix();//賦值后綴,分直接賦值和調(diào)用過程賦值兩種
//或者保留字:=
void express();//表達式
//保留字ADD或SUB(正負號)
void term();//項
void factor();//因子
void factsuff();//因子后綴
void termsuff();//項后綴
//保留字ADD或SUB(加型運算符)
void factor();//因子
void factsuff();//因子后綴
//或者保留字(
void express();//表達式
//保留字)
//或if結(jié)構(gòu)
void ifsent();
//保留字IF
void conditio();//條件分兩種情況:ODD作用在表達式上,或者兩個表達式進行比較
//或者
//保留字
void express();//表達式
//或者
void express();//表達式
void respoper();//6種比較運算符
//保留字THEN
void sentence();//語句
//或while結(jié)構(gòu)
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();
}
///////////////////////////////////////////////////////////
/////////////////////////////////////
//程序構(gòu)成:
// 程序首部+程序塊+.
//
void program()//程序
{
proghead();
block();
if(Code==DOT)
{
infile>>Code;
infile>>buff;cout<<buff<<endl;
}
else { error(2); }
};
//////
/////////////////////////////////////
//程序塊構(gòu)成:
// 常量說明部分
// 變量說明部分
// 過程說明部分
// 語句部分
//
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)
//此范圍為變量名(標識符)的內(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);
};
//////
/////////////////////////////////////
//常量說明部分構(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); }
}
};
//////
/////////////////////////////////////
// <常量定義> 格式:
// <標識符> = 數(shù)
void consdefi()
{
if(Code>=BEGIN_OF_V+1&&Code<=BEGIN_OF_V+MAX_NUM_OF_V)
//此范圍為變量名(標識符)的內(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();
}
//當Code!=COM時, void conssuff()不執(zhí)行,
//此時只定義了一個常量
}
/////////////////////////////////////
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)
//此范圍為變量名(標識符)的內(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)
//此范圍為變量名(標識符)的內(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)
//此范圍為變量名(標識符)的內(nèi)碼范圍
{
infile>>Code;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -