递归下降分析法
来源:百度知道 编辑:UC知道 时间:2024/05/03 07:41:32
程序输入/输出示例:
对下列文法,用递归下降分析法对任意输入的符号串进行分析:
(1)E->TG
(2)G->+TG|—TG
(3)G->ε
(4)T->FS
(5)S->*FS|/FS
(6)S->ε
(7)F->(E)
(8)F->i
输出的格式如下:
(1)递归下降分析程序,编制人:姓名,学号,班级
(2)输入一以#结束的符号串(包括+—*/()i#):在此位置输入符号串例如:i+i*i#
(3)输出结果:i+i*i#为合法符号串
备注:输入一符号串如i+i*#,要求输出为“非法的符号串”。
引用也要改变)。
注意:1.表达式中允许使用运算符(+-*/)、分割符(括号)、字符I,结束符#;
2.如果遇到错误的表达式,应输出错误提示信息(该信息越详细越好)
3.高手可以详细的输出推导的过程,即详细列出每一步使用的产生式。
最好能对一些关键的步骤进行一下解释
# include <iostream>
# include <cstdlib>
# include <cctype>
# include <csetjmp>
// Error jmp_buf buffer
static std::jmp_buf errjb;
// Function prototypes.
int addsubt();
int multdiv();
int number();
void error();
// Global expression buffer.
static char expr[81];
static int pos;
////////////////////////////
// The main() function. //
///////////////////////////
int main()
{
int ans;
do
{
// Mark the top of the parsing descent.
if(setjmp(errjb) == 0)
{
// Initialize the string subscript.
pos = 0
// Read an expression.
std::cout << "Enter expression(0 to quit):" << std::endl;
std::cin >>expr;
// Evaluate the expression.