使用递归前缀转换前缀转换

Infix to Prefix Conversion using Recursion

本文关键字:转换 前缀 递归      更新时间:2023-10-16

我正在努力寻找前缀翻译方案的摘要。

我已经想到了infix to Postfix translation sceme:

expr -> Term, Rest
Rest -> +Term, { print('+') } , Rest | -Term, { print('-') }, Rest | epsilon
Term -> Factor, Rest_
Rest_ -> *Factor, { print('*') }, Rest_ | /Factor, { print('/') }, Rest_ | epsilon
Factor -> Digit | (expr)
Digit -> 0,1,2,3,4,5,6,7,8,9

和我的命中符号根据上述翻译方案:

#include<iostream>
using namespace std;
const char input[] = "9-5*2";
int index = 0;
char LookAhead = input[index];
void Match(char newChar);
void Factor();
void Rest_();
void Rest();
void Term();
void Expression();

int main(){
    Expression();
    return 0;
}
void Match(char newChar){
    if(newChar == LookAhead){
        index++;
        LookAhead = input[index];
    }
}
void Expression(){
    Term();
    Rest();
}
void Term(){
    Factor();
    Rest_();
}
void Rest(){
    if(LookAhead == '+'){
        Match('+');
        Term();
        cout << '+';
        Rest();
    }else if(LookAhead == '-'){
        Match('-');
        Term();
        cout << '-';
        Rest();
    }else{
    }
}
void Rest_(){
    if(LookAhead == '*'){
        Match('*');
        Factor();
        cout << '*';
        Rest_();
    }else if(LookAhead == '/'){
        Match('/');
        Factor();
        cout << '/';
        Rest_();
    }else{
    }
}
void Factor(){
    if(isdigit(LookAhead)){
        cout << LookAhead;
        Match(LookAhead);
    }
}

所以现在有任何专家可以帮助我了解前缀转换翻译方案的插图,这将不胜感激。

我们可以通过解析树测试。如果我们可以生成> -9 52 示例字符串 9-5 2 的前缀字符串。

告诉我,我是否需要对我的插图进行更多有关后缀转换翻译方案和代码的信息,以获得更好的理解。

预先感谢!

编辑:简而言之,我遇到了一个问题,发现前缀表达转换翻译方案的插图。举个例子,我的输入:

9-5+2

预期输出:

-9+52

,我想通过上面显示的与后缀转换相同的结构来实现这一目标。就是全部!

最简单的解决方案是在解析时构造一个抽象语法树(AST),然后使用预订的遍历递归地行走树。

您也可以在使用时构造字符串(例如,@chrisdodd在此处建议),但是:

  • 这不是递归解决方案:)和
  • 它涉及大量的字符串复制,因此它可能具有二次运行时间。

将后缀转换成某种临时数据结构(例如,堆栈)似乎也很诱人,然后通过打印前缀表达式来递归地行走后缀表示"评估"。这肯定会起作用,但是您会遇到一个以自然方式穿越后缀表示的问题左手参数。这意味着您需要向后构造字符串,这涉及另一个临时数据架构(例如,另一个堆栈)。

总体上,AST解决方案更干净,并为将来添加更多功能提供了良好的基础。

因此,我使用了一个时间变量来存储我的数字,并以维护订单的优先级进行递归检查和递归打印。

我的解决方案:

#include<iostream>
using namespace std;
const char input[] = "9-5+2";
int index = 0;
char LookAhead = input[index];
char TempLookAhead = 'x';
void Match(char newChar);
void Factor();
void Rest_();
void Rest();
void Term();
void Expression();

int main(){
    Expression();
    return 0;
}
void Match(char newChar){
    if(newChar == LookAhead){
        index++;
        LookAhead = input[index];
    }
}
void Expression(){
    Term();
    Rest();
    cout << TempLookAhead;
}
void Term(){
    Factor();
    Rest_();
}
void Rest(){
    if(LookAhead == '+'){
        cout << '+';
        cout << TempLookAhead;
        Match(LookAhead);
        Term();
        Rest();
    }else if(LookAhead == '-'){
        cout << '-';
        cout << TempLookAhead;
        Match(LookAhead);
        Term();
        Rest();
    }else{
    }
}
void Rest_(){
    if(LookAhead == '*'){
        cout << '*';
        cout << TempLookAhead;
        Match(LookAhead);
        Factor();
        Rest_();
    }else if(LookAhead == '/'){
        cout << '/';
        cout << TempLookAhead;
        Match(LookAhead);
        Factor();
        Rest_();
    }else{
    }
}
void Factor(){
    if(isdigit(LookAhead)){
        TempLookAhead = LookAhead;
        Match(LookAhead);
    }
}