利用高级语言的递归过程为给定文法的每个非终结符构造对应的递归函数
源代码在线查看: 递归下降分析法.txt
#include "fstream.h"
#include "iostream.h"
#include "stdlib.h"
struct code_val{
char code;char val[20];
} t; //定义临时结构变量,存放单词二元式。
ifstream cinf("lex_r.txt",ios::in); //从文件lex_r.txt输入数据
ofstream coutf("par_r.txt",ios::out); //结果输出至文件par_r.txt
void E(void);
void F(void) //F→(E)|i|x|y
{
if(t.code=='i'||t.code=='x'||t.code=='y')
{
cinf>>t.code>>t.val;coutf }
else
if(t.code=='(')
{
cinf>>t.code>>t.val;coutf E( );
if(t.code==')')
{
cinf>>t.code>>t.val;coutf }
else{
coutf }
}
else{
coutf }
}
void T1(void) //T'→*FT'|ε
{
if(t.code=='*')
{
cinf>>t.code>>t.val;coutf F( );T1( );
}
else
if(!(t.code=='#'||t.code==')'||t.code=='+'))
{
coutf }
}
void T(void) //T→FT'
{
if(t.code=='i'||t.code=='x'||t.code=='y'||t.code=='(')
{
F( );T1( );
}
else{
coutf }
}
void E1(void) //E'→+TE'|ε
{
if(t.code=='+')
{
cinf>>t.code>>t.val;coutf T( );E1( );
}
else
if(!(t.code=='#'||t.code==')'))
coutf }
void E(void) //E→T E'
{
if(t.code=='i'||t.code=='x'||t.code=='y'||t.code=='(')
{
T( );E1( );
}
else
{
coutf }
}
void main(void)
{
cinf>>t.code>>t.val;coutf E( );
if(t.code=='#')
coutf else
coutf }